blob: ffac03fc514a623596ff777192e8c089102864d4 [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";
giofe746dd2025-05-07 09:57:40 +00006import { useCallback, 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
gio5f2f1002025-03-20 18:38:48 +040024export function Details() {
25 const nodes = useNodes<AppNode>();
giofe746dd2025-05-07 09:57:40 +000026 const cmpNodes = useCallback(() => {
27 return (x: AppNode, y: AppNode): number => {
28 if (x.type === y.type) {
29 if (nodeLabel(x) < nodeLabel(y)) {
30 return -1;
31 } else if (nodeLabel(x) > nodeLabel(y)) {
32 return 1;
33 }
34 return 0;
35 }
36 // TODO(gio): why !
37 return (nodeTypeIndex.get(x.type!) || 0) - (nodeTypeIndex.get(y.type!) || 0);
38 };
39 }, [nodes]);
40 const sorted = useMemo(() => nodes.filter((n) => n.type !== "network").sort(cmpNodes()), [nodes, cmpNodes]);
gio5f2f1002025-03-20 18:38:48 +040041 const [open, setOpen] = useState<string[]>([]);
42 const selected = useMemo(() => nodes.filter((n) => n.selected).map((n) => n.id), [nodes]);
43 const all = useMemo(() => open.concat(selected).filter(unique), [open, selected]);
44 return (
45 <Accordion type="multiple" value={all} onValueChange={(v) => setOpen(v)}>
giofe746dd2025-05-07 09:57:40 +000046 {sorted.map((n) => (
gio5f2f1002025-03-20 18:38:48 +040047 <AccordionItem key={n.id} value={n.id} className="px-3">
48 <AccordionTrigger>
49 <div className="flex flex-row space-x-2">
50 {Icon(n.type)}
51 <span>{nodeLabel(n)}</span>
52 </div>
53 </AccordionTrigger>
54 <AccordionContent>
55 <NodeDetails {...n} />
56 </AccordionContent>
57 </AccordionItem>
58 ))}
59 </Accordion>
60 );
61}