-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstored-recommendations-button.tsx
More file actions
160 lines (152 loc) · 5.04 KB
/
Copy pathstored-recommendations-button.tsx
File metadata and controls
160 lines (152 loc) · 5.04 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use client";
import { useState } from "react";
import { Button } from "../ui/button";
import { Sparkles } from "lucide-react";
import RecommendationsDialog from "./stored-recommendations-dialog";
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 StoredRecommendationsButton({
value,
tableId,
rowId,
contentType,
mappingRecommendations,
}: {
value: string;
tableId: string;
rowId: number;
contentType: string;
scanReportId: string;
fieldId: number;
mappingRecommendations: MappingRecommendation[];
}) {
const [isOpen, setIsOpen] = useState(false);
const [suggestions, setSuggestions] = useState<RecommendationItem[]>([]);
const [domainId, setDomainId] = useState<string>("");
// Handle click to show stored recommendations
const handleClick = () => {
if (!value) {
toast.error("No value provided to search for recommendations");
return;
}
if (!mappingRecommendations || mappingRecommendations.length === 0) {
toast.info("No pre-computed recommendations found for this value");
return;
}
// Transform the recommendations we already have
const transformedRecommendations = transformMappingRecommendations(
mappingRecommendations
);
setSuggestions(transformedRecommendations);
setIsOpen(true);
};
// Transform mapping recommendations to expected format
const transformMappingRecommendations = (
recommendations: MappingRecommendation[]
): RecommendationItem[] => {
return recommendations.map((rec) => ({
accuracy: rec.score ?? null,
conceptId: rec.concept.concept_id,
conceptName: rec.concept.concept_name,
conceptCode: rec.concept.concept_code,
vocabulary: rec.concept.vocabulary_id ?? "Unknown",
domain: rec.concept.domain_id ?? "Unknown",
conceptClass: rec.concept.concept_class_id ?? "Unknown",
explanation: rec.score
? `Pre-computed recommendation from ${rec.tool_name} (score: ${rec.score})`
: `Pre-computed recommendation from ${rec.tool_name} (no score)`,
}));
};
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 ${
!mappingRecommendations || mappingRecommendations.length === 0
? "opacity-50 cursor-not-allowed"
: ""
}`}
disabled={
!mappingRecommendations || mappingRecommendations.length === 0
}
>
<Sparkles className="h-4 w-4 text-purple-500" />
{mappingRecommendations && mappingRecommendations.length > 0
? "Recommendations"
: "No Recommendations"}
</Button>
</div>
</DropdownMenuTrigger>
{mappingRecommendations && mappingRecommendations.length > 0 && (
<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}
</DropdownMenuItem>
);
})}
</DropdownMenuContent>
)}
</DropdownMenu>
<RecommendationsDialog
open={isOpen}
onOpenChange={setIsOpen}
suggestions={suggestions}
onApplySuggestion={handleApplySuggestion}
searchedValue={value}
tableId={tableId}
rowId={rowId}
domainId={domainId}
contentType={contentType}
source="v3"
/>
</>
);
}