'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"; import { pb } from "@/lib/pocketbase"; import { Badge } from "./ui/badge"; import clsx from "clsx"; const ScriptBrowser = () => { const [links, setLinks] = useState([]); const [searchTerm, setSearchTerm] = useState(""); const inputRef = useRef(null); 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 { // you can also fetch all records at once via getFullList const res = await pb.collection("categories").getFullList({ expand: "items", sort: "order", requestKey: "desktop", }); setLinks(res as unknown as Category[]); } catch (error) { console.error("Error fetching links:", error); } }; useEffect(() => { fetchLinks(); }, []); const handleSearch = (value: string) => { setSearchTerm(value); }; const filteredLinks = useMemo(() => { return links.filter((category) => category.expand.items.some((script) => script.title.toLowerCase().includes(searchTerm.toLowerCase()), ), ); }, [links, searchTerm]); return (

Scripts

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

{script.title}{" "} {script.item_type}

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