import { FC, useState, useRef } from "react"; import { observer } from "mobx-react-lite"; import { Search, X } from "lucide-react"; import { cn } from "@/helpers/common.helper"; import { useProjectPages } from "@/hooks/store"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; export const PageSearchInput: FC = observer(() => { // hooks const { filters: { searchQuery }, updateFilters, } = useProjectPages(); // states const [isSearchOpen, setIsSearchOpen] = useState(false); // refs const inputRef = useRef(null); // outside click detector hook useOutsideClickDetector(inputRef, () => { if (isSearchOpen && searchQuery.trim() === "") setIsSearchOpen(false); }); const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { if (searchQuery && searchQuery.trim() !== "") updateFilters("searchQuery", ""); else { setIsSearchOpen(false); inputRef.current?.blur(); } } }; return ( <> {!isSearchOpen && ( )}
updateFilters("searchQuery", e.target.value)} onKeyDown={handleInputKeyDown} /> {isSearchOpen && ( )}
); });