import { FC, useCallback, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import { useParams } from "next/navigation"; // icons import { ListFilter, Search, X } from "lucide-react"; // types import type { TModuleFilters } from "@plane/types"; // components import { ArchiveTabsList } from "@/components/archives"; import { FiltersDropdown } from "@/components/issues"; import { ModuleFiltersSelection, ModuleOrderByDropdown } from "@/components/modules"; // helpers import { cn } from "@/helpers/common.helper"; import { calculateTotalFilters } from "@/helpers/filter.helper"; // hooks import { useMember, useModuleFilter } from "@/hooks/store"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; export const ArchivedModulesHeader: FC = observer(() => { // router const { projectId } = useParams(); // refs const inputRef = useRef(null); // hooks const { currentProjectArchivedFilters, currentProjectDisplayFilters, archivedModulesSearchQuery, updateFilters, updateDisplayFilters, updateArchivedModulesSearchQuery, } = useModuleFilter(); const { workspace: { workspaceMemberIds }, } = useMember(); // states const [isSearchOpen, setIsSearchOpen] = useState(archivedModulesSearchQuery !== "" ? true : false); // outside click detector hook useOutsideClickDetector(inputRef, () => { if (isSearchOpen && archivedModulesSearchQuery.trim() === "") setIsSearchOpen(false); }); const handleFilters = useCallback( (key: keyof TModuleFilters, value: string | string[]) => { if (!projectId) return; const newValues = currentProjectArchivedFilters?.[key] ?? []; if (Array.isArray(value)) value.forEach((val) => { if (!newValues.includes(val)) newValues.push(val); else newValues.splice(newValues.indexOf(val), 1); }); else { if (currentProjectArchivedFilters?.[key]?.includes(value)) newValues.splice(newValues.indexOf(value), 1); else newValues.push(value); } updateFilters(projectId.toString(), { [key]: newValues }, "archived"); }, [currentProjectArchivedFilters, projectId, updateFilters] ); const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { if (archivedModulesSearchQuery && archivedModulesSearchQuery.trim() !== "") updateArchivedModulesSearchQuery(""); else { setIsSearchOpen(false); inputRef.current?.blur(); } } }; const isFiltersApplied = calculateTotalFilters(currentProjectArchivedFilters ?? {}) !== 0; return (
{/* filter options */}
{!isSearchOpen && ( )}
updateArchivedModulesSearchQuery(e.target.value)} onKeyDown={handleInputKeyDown} /> {isSearchOpen && ( )}
{ if (!projectId || val === currentProjectDisplayFilters?.order_by) return; updateDisplayFilters(projectId.toString(), { order_by: val, }); }} /> } title="Filters" placement="bottom-end" isFiltersApplied={isFiltersApplied} > { if (!projectId) return; updateDisplayFilters(projectId.toString(), val); }} handleFiltersUpdate={handleFilters} memberIds={workspaceMemberIds ?? undefined} isArchived />
); });