| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { Button } from "@/components/ui/button"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 2 | import { v4 as uuidv4 } from "uuid"; |
| 3 | import { useCallback, useState } from "react"; |
| 4 | import { Accordion, AccordionTrigger } from "./ui/accordion"; |
| 5 | import { AccordionContent, AccordionItem } from "@radix-ui/react-accordion"; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 6 | import { AppNode, AppState, NodeType, useCategories, useStateStore } from "@/lib/state"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 7 | import { CategoryItem } from "@/lib/categories"; |
| 8 | import { Icon } from "./icon"; |
| 9 | |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 10 | function addResource(i: CategoryItem<NodeType>, store: AppState) { |
| 11 | const node = { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 12 | id: uuidv4(), |
| 13 | position: { |
| 14 | x: 0, |
| 15 | y: 0, |
| 16 | }, |
| 17 | type: i.type, |
| 18 | connectable: true, |
| 19 | data: i.init, |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 20 | selected: true, |
| 21 | }; |
| 22 | const nodes = store.nodes.map((n) => ({ |
| 23 | ...n, |
| 24 | selected: false, |
| 25 | })); |
| 26 | nodes.push(node as AppNode); |
| 27 | store.setNodes(nodes); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | export function Resources() { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 31 | const store = useStateStore(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 32 | const categories = useCategories(); |
| 33 | const onResourceAdd = useCallback( |
| 34 | (item: CategoryItem<NodeType>) => { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 35 | return () => addResource(item, store); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 36 | }, |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 37 | [store], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 38 | ); |
| 39 | const [open, setOpen] = useState<string[]>(categories.map((c) => c.title)); |
| 40 | return ( |
| 41 | <> |
| 42 | <Accordion type="multiple" value={open} onValueChange={(v) => setOpen(v)}> |
| 43 | {categories.map((c) => ( |
| 44 | <AccordionItem key={c.title} value={c.title} className={"px-3" + (c.active ? " bg-amber-100" : "")}> |
| 45 | <AccordionTrigger>{c.title}</AccordionTrigger> |
| 46 | <AccordionContent> |
| 47 | <div className="flex flex-col space-y-1"> |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 48 | {c.items.map((item: CategoryItem<NodeType>) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 49 | <Button |
| 50 | key={item.title} |
| 51 | onClick={onResourceAdd(item)} |
| 52 | style={{ justifyContent: "flex-start" }} |
| 53 | > |
| gio | 0b4002c | 2025-05-11 15:48:51 +0000 | [diff] [blame] | 54 | <Icon type={item.type} /> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 55 | {item.title} |
| 56 | </Button> |
| 57 | ))} |
| 58 | </div> |
| 59 | </AccordionContent> |
| 60 | </AccordionItem> |
| 61 | ))} |
| 62 | </Accordion> |
| 63 | </> |
| 64 | ); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 65 | } |