'use client' import { Accordion, AccordionItem, AccordionTrigger, AccordionContent, } from "@/components/ui/accordion"; import { Input } from "@/components/ui/input"; import Link from "next/link"; import React, { useState, useEffect, useRef, useMemo } from "react"; import { Category } from "@/lib/types"; const ScriptBrowser = () => { const [links, setLinks] = useState([]); const [searchTerm, setSearchTerm] = useState(""); const inputRef = useRef(null); const [categoryList, setCategoryList] = useState([]); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "/") { inputRef.current?.focus(); event.preventDefault(); } }; document.addEventListener("keydown", handleKeyDown); return () => { document.removeEventListener("keydown", handleKeyDown); }; }, []); const fetchLinks = async () => { try { const res = await fetch( `${process.env.NEXT_PUBLIC_POCKETBASE_URL}/api/collections/proxmox_items/records`, ); const data = await res.json(); setLinks(data.items as Category[]); } catch (error) { console.error("Error fetching links:", error); } }; // get a list of all the category ids useEffect(() => { setCategoryList(links.map((category) => category.id)); }, [links]); useEffect(() => { fetchLinks(); }, []); const handleSearch = (value: string) => { setSearchTerm(value); }; const filteredLinks = useMemo(() => { return links.filter((category) => category.Items.some((script) => script.title.toLowerCase().includes(searchTerm.toLowerCase()), ), ); }, [links, searchTerm]); return (

Scripts

handleSearch(e.target.value)} ref={inputRef} /> {filteredLinks.map((category) => ( {category.Catagory_Title} {category.Items.filter((script) => script.title.toLowerCase().includes(searchTerm.toLowerCase()), ).map((script, index) => (

{script.title}

))}
))}
); }; export default ScriptBrowser;