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