|
1 | | -import type { FC } from "react"; |
2 | | -// plane imports |
3 | | -import { Logo } from "@plane/propel/emoji-icon-picker"; |
4 | | -import type { TLogoProps } from "@plane/types"; |
5 | | - |
6 | | -type ProjectHeaderProps = { |
7 | | - project: { |
8 | | - name: string; |
9 | | - logo_props: TLogoProps; |
10 | | - }; |
| 1 | +import { useCallback, useMemo } from "react"; |
| 2 | +import { observer } from "mobx-react"; |
| 3 | +// plane ui imports |
| 4 | +import type { ICustomSearchSelectOption } from "@plane/types"; |
| 5 | +import { CustomSearchSelect } from "@plane/ui"; |
| 6 | +// plane propel imports |
| 7 | +import { ProjectIcon } from "@plane/propel/icons"; |
| 8 | +// hooks |
| 9 | +import { useAppRouter } from "@/hooks/use-app-router"; |
| 10 | +import { useProject } from "@/hooks/store/use-project"; |
| 11 | +import { useUserPermissions } from "@/hooks/store/user"; |
| 12 | +import { useNavigationItems } from "@/plane-web/components/navigations"; |
| 13 | +// local components |
| 14 | +import { SwitcherLabel } from "../common/switcher-label"; |
| 15 | +import { ProjectHeaderButton } from "./project-header-button"; |
| 16 | +// utils |
| 17 | +import { getTabUrl } from "./tab-navigation-utils"; |
| 18 | +import { useTabPreferences } from "./use-tab-preferences"; |
| 19 | + |
| 20 | +type TProjectHeaderProps = { |
| 21 | + workspaceSlug: string; |
| 22 | + projectId: string; |
11 | 23 | }; |
12 | 24 |
|
13 | | -export const ProjectHeader: FC<ProjectHeaderProps> = ({ project }) => ( |
14 | | - <div className="flex items-center gap-1.5 text-left select-none w-full"> |
15 | | - <div className="size-7 rounded-md bg-custom-background-90 flex items-center justify-center flex-shrink-0"> |
16 | | - <Logo logo={project.logo_props} size={16} /> |
17 | | - </div> |
18 | | - <p className="truncate text-base font-medium text-custom-sidebar-text-200 flex-shrink-0">{project.name}</p> |
19 | | - </div> |
20 | | -); |
| 25 | +export const ProjectHeader = observer((props: TProjectHeaderProps) => { |
| 26 | + const { workspaceSlug, projectId } = props; |
| 27 | + // router |
| 28 | + const router = useAppRouter(); |
| 29 | + // store hooks |
| 30 | + const { joinedProjectIds, getPartialProjectById } = useProject(); |
| 31 | + const { allowPermissions } = useUserPermissions(); |
| 32 | + |
| 33 | + // Get current project details |
| 34 | + const currentProjectDetails = getPartialProjectById(projectId); |
| 35 | + |
| 36 | + // Get available navigation items for this project |
| 37 | + const navigationItems = useNavigationItems({ |
| 38 | + workspaceSlug: workspaceSlug, |
| 39 | + projectId, |
| 40 | + project: currentProjectDetails, |
| 41 | + allowPermissions, |
| 42 | + }); |
| 43 | + |
| 44 | + // Get preferences from hook |
| 45 | + const { tabPreferences } = useTabPreferences(workspaceSlug, projectId); |
| 46 | + |
| 47 | + // Memoize available tab keys |
| 48 | + const availableTabKeys = useMemo(() => navigationItems.map((item) => item.key), [navigationItems]); |
| 49 | + |
| 50 | + // Memoize validated default tab key |
| 51 | + const validatedDefaultTabKey = useMemo( |
| 52 | + () => |
| 53 | + availableTabKeys.includes(tabPreferences.defaultTab) |
| 54 | + ? tabPreferences.defaultTab |
| 55 | + : availableTabKeys[0] || "work_items", |
| 56 | + [availableTabKeys, tabPreferences.defaultTab] |
| 57 | + ); |
| 58 | + |
| 59 | + // Memoize switcher options to prevent recalculation on every render |
| 60 | + const switcherOptions = useMemo<ICustomSearchSelectOption[]>( |
| 61 | + () => |
| 62 | + joinedProjectIds |
| 63 | + .map((id): ICustomSearchSelectOption | null => { |
| 64 | + const project = getPartialProjectById(id); |
| 65 | + if (!project) return null; |
| 66 | + |
| 67 | + return { |
| 68 | + value: id, |
| 69 | + query: project.name, |
| 70 | + content: ( |
| 71 | + <SwitcherLabel |
| 72 | + name={project.name} |
| 73 | + logo_props={project.logo_props} |
| 74 | + LabelIcon={ProjectIcon} |
| 75 | + type="material" |
| 76 | + /> |
| 77 | + ), |
| 78 | + }; |
| 79 | + }) |
| 80 | + .filter((option): option is ICustomSearchSelectOption => option !== null), |
| 81 | + [joinedProjectIds, getPartialProjectById] |
| 82 | + ); |
| 83 | + |
| 84 | + // Memoize onChange handler |
| 85 | + const handleProjectChange = useCallback( |
| 86 | + (value: string) => { |
| 87 | + if (value !== currentProjectDetails?.id) { |
| 88 | + router.push(getTabUrl(workspaceSlug, value, validatedDefaultTabKey)); |
| 89 | + } |
| 90 | + }, |
| 91 | + [currentProjectDetails?.id, router, workspaceSlug, validatedDefaultTabKey] |
| 92 | + ); |
| 93 | + |
| 94 | + // Early return if no project details |
| 95 | + if (!currentProjectDetails) return null; |
| 96 | + |
| 97 | + return ( |
| 98 | + <CustomSearchSelect |
| 99 | + options={switcherOptions} |
| 100 | + value={currentProjectDetails.id} |
| 101 | + onChange={handleProjectChange} |
| 102 | + customButton={currentProjectDetails ? <ProjectHeaderButton project={currentProjectDetails} /> : null} |
| 103 | + className="h-full rounded" |
| 104 | + customButtonClassName="group flex items-center gap-0.5 rounded hover:bg-custom-background-90 outline-none cursor-pointer h-full" |
| 105 | + /> |
| 106 | + ); |
| 107 | +}); |
0 commit comments