| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { useNodes } from "@xyflow/react"; |
| gio | fe746dd | 2025-05-07 09:57:40 +0000 | [diff] [blame] | 2 | import { AppNode, nodeLabel, NodeType } from "@/lib/state"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 3 | import { NodeDetails } from "@/components/node-details"; |
| 4 | import { Accordion, AccordionContent, AccordionTrigger } from "./ui/accordion"; |
| 5 | import { AccordionItem } from "@radix-ui/react-accordion"; |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 6 | import { useMemo, useState } from "react"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 7 | import { Icon } from "./icon"; |
| 8 | |
| 9 | function unique<T>(v: T, i: number, a: T[]) { |
| 10 | return a.indexOf(v) === i; |
| 11 | } |
| 12 | |
| gio | fe746dd | 2025-05-07 09:57:40 +0000 | [diff] [blame] | 13 | const 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 | |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 24 | function 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 | |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 37 | export function Details() { |
| 38 | const nodes = useNodes<AppNode>(); |
| gio | 6cf8c27 | 2025-05-08 09:01:38 +0000 | [diff] [blame] | 39 | const sorted = useMemo(() => nodes.filter((n) => n.type !== "network").sort(cmpNodes), [nodes]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 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)}> |
| gio | fe746dd | 2025-05-07 09:57:40 +0000 | [diff] [blame] | 45 | {sorted.map((n) => ( |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 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 | ); |
| 60 | } |