blob: d498771f1e5ee601ab6565348ba0a82be99a2626 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001import { useNodes } from "@xyflow/react";
giofe746dd2025-05-07 09:57:40 +00002import { AppNode, nodeLabel, NodeType } from "@/lib/state";
gio5f2f1002025-03-20 18:38:48 +04003import { NodeDetails } from "@/components/node-details";
4import { Accordion, AccordionContent, AccordionTrigger } from "./ui/accordion";
5import { AccordionItem } from "@radix-ui/react-accordion";
gio6cf8c272025-05-08 09:01:38 +00006import { useMemo, useState } from "react";
gio5f2f1002025-03-20 18:38:48 +04007import { Icon } from "./icon";
8
9function unique<T>(v: T, i: number, a: T[]) {
10 return a.indexOf(v) === i;
11}
12
giofe746dd2025-05-07 09:57:40 +000013const nodeTypeIndex = new Map<NodeType, number>([
14 ["github", 1],
15 // ["gitlab", 2],
16 ["volume", 3],
17 ["postgresql", 4],
18 ["mongodb", 5],
19 ["app", 6],
20 ["gateway-tcp", 7],
21 ["gateway-https", 8],
22]);
23
gio6cf8c272025-05-08 09:01:38 +000024function cmpNodes(x: AppNode, y: AppNode): number {
25 if (x.type === y.type) {
26 if (nodeLabel(x) < nodeLabel(y)) {
27 return -1;
28 } else if (nodeLabel(x) > nodeLabel(y)) {
29 return 1;
30 }
31 return 0;
32 }
33 // TODO(gio): why !
34 return (nodeTypeIndex.get(x.type!) || 0) - (nodeTypeIndex.get(y.type!) || 0);
35}
36
gio5f2f1002025-03-20 18:38:48 +040037export function Details() {
38 const nodes = useNodes<AppNode>();
gio6cf8c272025-05-08 09:01:38 +000039 const sorted = useMemo(() => nodes.filter((n) => n.type !== "network").sort(cmpNodes), [nodes]);
gio5f2f1002025-03-20 18:38:48 +040040 const [open, setOpen] = useState<string[]>([]);
41 const selected = useMemo(() => nodes.filter((n) => n.selected).map((n) => n.id), [nodes]);
42 const all = useMemo(() => open.concat(selected).filter(unique), [open, selected]);
43 return (
44 <Accordion type="multiple" value={all} onValueChange={(v) => setOpen(v)}>
giofe746dd2025-05-07 09:57:40 +000045 {sorted.map((n) => (
gio5f2f1002025-03-20 18:38:48 +040046 <AccordionItem key={n.id} value={n.id} className="px-3">
47 <AccordionTrigger>
48 <div className="flex flex-row space-x-2">
49 {Icon(n.type)}
50 <span>{nodeLabel(n)}</span>
51 </div>
52 </AccordionTrigger>
53 <AccordionContent>
54 <NodeDetails {...n} />
55 </AccordionContent>
56 </AccordionItem>
57 ))}
58 </Accordion>
59 );
60}