import { useEffect, useState, useRef } from "react"; import useDebounce from "../hooks/useDebounce"; import { useLocationStore, useDialogStore } from "../store/module"; import Icon from "./Icon"; const SearchBar = () => { const locationStore = useLocationStore(); const dialogStore = useDialogStore(); const [queryText, setQueryText] = useState(""); const inputRef = useRef(null); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (!inputRef.current) { return; } if (dialogStore.getState().dialogStack.length) { return; } const isMetaKey = event.ctrlKey || event.metaKey; if (isMetaKey && event.key === "f") { event.preventDefault(); inputRef.current.focus(); return; } }; document.body.addEventListener("keydown", handleKeyDown); return () => { document.body.removeEventListener("keydown", handleKeyDown); }; }, []); useEffect(() => { const text = locationStore.getState().query.text; setQueryText(text === undefined ? "" : text); }, [locationStore.state.query.text]); useDebounce( () => { locationStore.setTextQuery(queryText.length === 0 ? undefined : queryText); }, 200, [queryText] ); const handleTextQueryInput = (event: React.FormEvent) => { const text = event.currentTarget.value; setQueryText(text); }; return (
); }; export default SearchBar;