-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathai-suggesions-button.tsx
More file actions
146 lines (138 loc) · 4.34 KB
/
Copy pathai-suggesions-button.tsx
File metadata and controls
146 lines (138 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use client";
import { useState } from "react";
import { Button } from "../ui/button";
import { Sparkles, Loader2 } from "lucide-react";
import AISuggestionDialog from "./ai-suggestions-dialog";
import { getRecommendations } from "@/api/recommendations";
import { addConcept } from "@/api/concepts";
import { toast } from "sonner";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { domains } from "@/constants/domains";
import { DropdownMenuItem } from "@radix-ui/react-dropdown-menu";
export function AISuggestionsButton({
value,
tableId,
rowId,
contentType,
}: {
value: string;
tableId: string;
rowId: number;
contentType: string;
}) {
const [isLoading, setIsLoading] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const [suggestions, setSuggestions] = useState<RecommendationItem[]>([]);
const [metadata, setMetadata] = useState<RecommendationMetadata | null>(null);
const [domainId, setDomainId] = useState<string>("");
// Fetches AI suggestions from the API
const handleClick = async (domainId: string) => {
if (!value) {
toast.error("No value provided to search for recommendations");
return;
}
setIsLoading(true);
try {
const recommendations: RecommendationServiceResponse =
await getRecommendations(value, domainId);
// Filter to get only unique concept IDs
const uniqueRecommendations = recommendations.items.filter(
(item, index, array) =>
array.findIndex((i) => i.conceptId === item.conceptId) === index
);
if (recommendations.metadata) {
setMetadata(recommendations.metadata);
}
setSuggestions(uniqueRecommendations);
setIsOpen(true);
} catch (error) {
console.error("Error generating suggestions:", error);
toast.error("Failed to fetch suggestions. Please try again.");
} finally {
setIsLoading(false);
}
};
const handleApplySuggestion = async (data: {
concept: number;
object_id: number;
content_type: string;
creation_type: string;
table_id: string;
}) => {
setIsOpen(false);
const response = await addConcept(data);
if (response) {
toast.error(`Adding concept failed. ${response.errorMessage}`);
} else {
toast.success(`OMOP Concept successfully added.`);
// Reload the page after 500ms to avoid race condition
setTimeout(() => {
window.location.reload();
}, 500);
}
};
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<div className="flex focus:outline-hidden">
<Button
variant="ghost"
size="sm"
className="border-purple-400 hover:bg-purple-100 hover:text-black dark:hover:bg-gray-700 dark:hover:text-white"
disabled={isLoading}
>
{isLoading ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Sparkles className="h-4 w-4 text-purple-500" />
)}
Suggestions
</Button>
</div>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="w-52 overflow-y-auto max-h-96"
>
<DropdownMenuLabel className="text-black dark:text-white font-semibold text-center">
Select Relevant Domain
</DropdownMenuLabel>
<DropdownMenuSeparator />
{domains.map((domain) => {
return (
<DropdownMenuItem
key={domain.id}
className="cursor-pointer hover:bg-blue-100 hover:text-black focus:outline-hidden p-1"
onClick={() => {
setDomainId(domain.id);
handleClick(domain.id);
}}
>
{domain.id}
</DropdownMenuItem>
);
})}
</DropdownMenuContent>
</DropdownMenu>
<AISuggestionDialog
open={isOpen}
onOpenChange={setIsOpen}
suggestions={suggestions}
onApplySuggestion={handleApplySuggestion}
searchedValue={value}
tableId={tableId}
rowId={rowId}
domainId={domainId}
contentType={contentType}
metadata={metadata}
/>
</>
);
}