blob: 252952d78c96306375f1af5f8f0de8cd5a87b239 [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[]) {
giod0026612025-05-08 13:00:36 +000010 return a.indexOf(v) === i;
gio5f2f1002025-03-20 18:38:48 +040011}
12
giofe746dd2025-05-07 09:57:40 +000013const nodeTypeIndex = new Map<NodeType, number>([
giod0026612025-05-08 13:00:36 +000014 ["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],
giofe746dd2025-05-07 09:57:40 +000022]);
23
gio6cf8c272025-05-08 09:01:38 +000024function cmpNodes(x: AppNode, y: AppNode): number {
giod0026612025-05-08 13:00:36 +000025 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);
gio6cf8c272025-05-08 09:01:38 +000035}
36
gio5f2f1002025-03-20 18:38:48 +040037export function Details() {
giod0026612025-05-08 13:00:36 +000038 const nodes = useNodes<AppNode>();
39 const sorted = useMemo(() => nodes.filter((n) => n.type !== "network").sort(cmpNodes), [nodes]);
40 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)}>
45 {sorted.map((n) => (
46 <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 );
gio5f2f1002025-03-20 18:38:48 +040060}