| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { Button } from "@/components/ui/button"; |
| gio | 90a265b | 2025-06-26 09:16:58 +0000 | [diff] [blame^] | 2 | import { Input } from "@/components/ui/input"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 3 | import { v4 as uuidv4 } from "uuid"; |
| gio | 90a265b | 2025-06-26 09:16:58 +0000 | [diff] [blame^] | 4 | import { useCallback, useState, useMemo } from "react"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 5 | import { Accordion, AccordionTrigger } from "./ui/accordion"; |
| 6 | import { AccordionContent, AccordionItem } from "@radix-ui/react-accordion"; |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 7 | import { AppState, useCategories, useMode, useProjectId, useStateStore } from "@/lib/state"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 8 | import { CategoryItem } from "@/lib/categories"; |
| 9 | import { Icon } from "./icon"; |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 10 | import { AppNode, NodeType } from "config"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 11 | |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 12 | function addResource(i: CategoryItem<NodeType>, store: AppState) { |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 13 | const deselected = store.nodes.map((n) => ({ |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 14 | ...n, |
| 15 | selected: false, |
| 16 | })); |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 17 | store.setNodes(deselected); |
| 18 | store.addNode({ |
| 19 | id: uuidv4(), |
| 20 | type: i.type, |
| 21 | data: i.init, |
| 22 | selected: true, |
| 23 | connectable: true, |
| 24 | } as any); // eslint-disable-line @typescript-eslint/no-explicit-any |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | export function Resources() { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 28 | const store = useStateStore(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 29 | const categories = useCategories(); |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 30 | const projectId = useProjectId(); |
| 31 | const mode = useMode(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 32 | const onResourceAdd = useCallback( |
| 33 | (item: CategoryItem<NodeType>) => { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 34 | return () => addResource(item, store); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 35 | }, |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 36 | [store], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 37 | ); |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 38 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 39 | const [open, setOpen] = useState<string[]>(categories.map((c) => c.title)); |
| gio | 90a265b | 2025-06-26 09:16:58 +0000 | [diff] [blame^] | 40 | const [searchValue, setSearchValue] = useState<string>(""); |
| 41 | |
| 42 | // Filter categories based on search value |
| 43 | const filteredCategories = useMemo(() => { |
| 44 | if (!searchValue.trim()) { |
| 45 | return categories; |
| 46 | } |
| 47 | |
| 48 | const searchLower = searchValue.toLowerCase(); |
| 49 | return categories |
| 50 | .map((category) => ({ |
| 51 | ...category, |
| 52 | items: category.items.filter((item) => item.title.toLowerCase().includes(searchLower)), |
| 53 | })) |
| 54 | .filter((category) => category.items.length > 0); // Remove empty categories |
| 55 | }, [categories, searchValue]); |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 56 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 57 | return ( |
| 58 | <> |
| gio | 90a265b | 2025-06-26 09:16:58 +0000 | [diff] [blame^] | 59 | <div className="px-3 pb-3 mt-2"> |
| 60 | <Input |
| 61 | type="text" |
| 62 | placeholder="Search resources..." |
| 63 | value={searchValue} |
| 64 | onChange={(e) => setSearchValue(e.target.value)} |
| 65 | className="w-full" |
| 66 | /> |
| 67 | </div> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 68 | <Accordion type="multiple" value={open} onValueChange={(v) => setOpen(v)}> |
| gio | 90a265b | 2025-06-26 09:16:58 +0000 | [diff] [blame^] | 69 | {filteredCategories.map((c) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 70 | <AccordionItem key={c.title} value={c.title} className={"px-3" + (c.active ? " bg-amber-100" : "")}> |
| 71 | <AccordionTrigger>{c.title}</AccordionTrigger> |
| 72 | <AccordionContent> |
| 73 | <div className="flex flex-col space-y-1"> |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 74 | {c.items.map((item: CategoryItem<NodeType>) => ( |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 75 | <Button |
| 76 | key={item.title} |
| 77 | onClick={onResourceAdd(item)} |
| 78 | style={{ justifyContent: "flex-start" }} |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 79 | disabled={projectId == null || mode !== "edit"} |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 80 | > |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 81 | <Icon node={{ type: item.type, data: item.init } as AppNode} /> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 82 | {item.title} |
| 83 | </Button> |
| 84 | ))} |
| 85 | </div> |
| 86 | </AccordionContent> |
| 87 | </AccordionItem> |
| 88 | ))} |
| 89 | </Accordion> |
| 90 | </> |
| 91 | ); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 92 | } |