import { useCallback, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import { Search, Plus, Briefcase, X, ListFilter } from "lucide-react"; // hooks import { useApplication, useEventTracker, useMember, useProject, useProjectFilter, useUser } from "hooks/store"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { BreadcrumbLink } from "components/common"; import { SidebarHamburgerToggle } from "components/core/sidebar/sidebar-menu-hamburger-toggle"; // ui import { Breadcrumbs, Button } from "@plane/ui"; // helpers import { cn } from "helpers/common.helper"; // constants import { EUserWorkspaceRoles } from "constants/workspace"; import { FiltersDropdown } from "components/issues"; import { ProjectFiltersSelection, ProjectOrderByDropdown } from "components/project"; import { TProjectFilters } from "@plane/types"; export const ProjectsHeader = observer(() => { // states const [isSearchOpen, setIsSearchOpen] = useState(false); // refs const inputRef = useRef(null); // store hooks const { commandPalette: commandPaletteStore, router: { workspaceSlug }, } = useApplication(); const { setTrackElement } = useEventTracker(); const { membership: { currentWorkspaceRole }, } = useUser(); const { workspaceProjectIds } = useProject(); const { currentWorkspaceDisplayFilters: displayFilters, currentWorkspaceFilters: filters, updateFilters, updateDisplayFilters, searchQuery, updateSearchQuery, } = useProjectFilter(); const { workspace: { workspaceMemberIds }, } = useMember(); // outside click detector hook useOutsideClickDetector(inputRef, () => { if (isSearchOpen && searchQuery.trim() === "") setIsSearchOpen(false); }); // auth const isAuthorizedUser = !!currentWorkspaceRole && currentWorkspaceRole >= EUserWorkspaceRoles.MEMBER; const handleFilters = useCallback( (key: keyof TProjectFilters, value: string | string[]) => { if (!workspaceSlug) return; const newValues = filters?.[key] ?? []; if (Array.isArray(value)) value.forEach((val) => { if (!newValues.includes(val)) newValues.push(val); }); else { if (filters?.[key]?.includes(value)) newValues.splice(newValues.indexOf(value), 1); else newValues.push(value); } updateFilters(workspaceSlug, { [key]: newValues }); }, [filters, updateFilters, workspaceSlug] ); const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { if (searchQuery && searchQuery.trim() !== "") updateSearchQuery(""); else setIsSearchOpen(false); } }; return (
} />} />
{workspaceProjectIds && workspaceProjectIds?.length > 0 && (
{!isSearchOpen && ( )}
updateSearchQuery(e.target.value)} onKeyDown={handleInputKeyDown} /> {isSearchOpen && ( )}
)} { if (!workspaceSlug || val === displayFilters?.order_by) return; updateDisplayFilters(workspaceSlug, { order_by: val, }); }} /> } title="Filters" placement="bottom-end"> { if (!workspaceSlug) return; updateDisplayFilters(workspaceSlug, val); }} memberIds={workspaceMemberIds ?? undefined} /> {isAuthorizedUser && ( )}
); });