"use client"; import React, { FC, useCallback, useRef, useState } from "react"; import { observer } from "mobx-react"; import { useParams } from "next/navigation"; // icons import { ListFilter, Search, X } from "lucide-react"; // helpers import { cn } from "@plane/editor-core"; // types import { TModuleFilters } from "@plane/types"; // ui import { Tooltip } from "@plane/ui"; // components import { FiltersDropdown } from "@/components/issues"; import { ModuleFiltersSelection, ModuleOrderByDropdown } from "@/components/modules/dropdowns"; // constants import { MODULE_VIEW_LAYOUTS } from "@/constants/module"; // helpers import { calculateTotalFilters } from "@/helpers/filter.helper"; // hooks import { useMember, useModuleFilter } from "@/hooks/store"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; import { usePlatformOS } from "@/hooks/use-platform-os"; export const ModuleViewHeader: FC = observer(() => { // refs const inputRef = useRef(null); // router const { projectId } = useParams(); // hooks const { isMobile } = usePlatformOS(); // store hooks const { workspace: { workspaceMemberIds }, } = useMember(); const { currentProjectDisplayFilters: displayFilters, currentProjectFilters: filters, searchQuery, updateDisplayFilters, updateFilters, updateSearchQuery, } = useModuleFilter(); // states const [isSearchOpen, setIsSearchOpen] = useState(searchQuery !== "" ? true : false); // handlers const handleFilters = useCallback( (key: keyof TModuleFilters, value: string | string[]) => { if (!projectId) return; const newValues = filters?.[key] ?? []; if (Array.isArray(value)) value.forEach((val) => { if (!newValues.includes(val)) newValues.push(val); else newValues.splice(newValues.indexOf(val), 1); }); else { if (filters?.[key]?.includes(value)) newValues.splice(newValues.indexOf(value), 1); else newValues.push(value); } updateFilters(projectId.toString(), { [key]: newValues }); }, [filters, projectId, updateFilters] ); 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); }); const isFiltersApplied = calculateTotalFilters(filters ?? {}) !== 0 || displayFilters?.favorites; return (
{!isSearchOpen && ( )}
updateSearchQuery(e.target.value)} onKeyDown={handleInputKeyDown} /> {isSearchOpen && ( )}
{ if (!projectId || val === displayFilters?.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} />
{MODULE_VIEW_LAYOUTS.map((layout) => ( ))}
); });