From e3815c48366f96129b0c22c3c5cdc73c306dbf78 Mon Sep 17 00:00:00 2001 From: Bram Suurd Date: Fri, 17 May 2024 09:32:16 +0200 Subject: [PATCH] chore: Update Navbar component imports and removed input --- components/Navbar.tsx | 76 ++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/components/Navbar.tsx b/components/Navbar.tsx index 9a6b801..388e396 100644 --- a/components/Navbar.tsx +++ b/components/Navbar.tsx @@ -6,11 +6,12 @@ import logo from "../public/logo.png"; import Image from "next/image"; import { FaGithub } from "react-icons/fa"; import { ModeToggle } from "./theme-toggle"; -import { LuGitPullRequestDraft } from "react-icons/lu"; -import { LuBookOpenCheck } from "react-icons/lu"; -import { LuClipboardSignature } from "react-icons/lu"; +import { + LuGitPullRequestDraft, + LuBookOpenCheck, + LuClipboardSignature, +} from "react-icons/lu"; import { Coffee, Menu } from "lucide-react"; -import { FaDiscord } from "react-icons/fa"; import { Sheet, SheetClose, @@ -35,6 +36,7 @@ function Navbar() { const [links, setLinks] = useState([]); const [searchTerm, setSearchTerm] = useState(""); const inputRef = useRef(null); + const [shouldFocusInput, setShouldFocusInput] = useState(false); useEffect(() => { const handleScroll = () => { @@ -48,25 +50,32 @@ function Navbar() { }; }, []); - useEffect(() => { - const handleKeyDown = (event: KeyboardEvent) => { - if (event.key === "/") { - inputRef.current?.focus(); - event.preventDefault(); - } - }; + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === "/") { + setShouldFocusInput(true); + event.preventDefault(); + } + }; - document.addEventListener("keydown", handleKeyDown); - return () => { - document.removeEventListener("keydown", handleKeyDown); - }; - }, []); + document.addEventListener("keydown", handleKeyDown); + return () => { + document.removeEventListener("keydown", handleKeyDown); + }; + }, []); + + useEffect(() => { + if (shouldFocusInput) { + inputRef.current?.focus(); + setShouldFocusInput(false); + } + }, [shouldFocusInput]); const fetchLinks = async () => { try { const res = await pb.collection("categories").getFullList({ expand: "items", - requestKey : "navbar" + requestKey: "navbar", }); setLinks(res as unknown as Category[]); } catch (error) { @@ -78,22 +87,12 @@ function Navbar() { 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 ( <>

@@ -107,7 +106,7 @@ function Navbar() {

- + setShouldFocusInput(false)}>

Scripts

- handleSearch(e.target.value)} - ref={inputRef} - /> - {filteredLinks.map((category) => ( + {links.map((category) => (
- {/* */}