Files
upstream-plane/apps/web/core/components/navigation/project-header.tsx
upstream-mirror 23059a1de3
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
snapshot of https://github.com/makeplane/plane.git @ d868349a55de79979eb8b98701bf1702c67ad634 (2026-09-15)
2026-09-15 15:14:58 +05:00

112 lines
3.7 KiB
TypeScript

/**
* Copyright (c) 2023-present Plane Software, Inc. and contributors
* SPDX-License-Identifier: AGPL-3.0-only
* See the LICENSE file for details.
*/
import { useCallback, useMemo } from "react";
import { observer } from "mobx-react";
// plane imports
import { ProjectsOutline } from "@makeplane/propel/icons";
import type { ICustomSearchSelectOption } from "@plane/types";
import { CustomSearchSelect } from "@plane/ui";
// hooks
import { useProject } from "@/hooks/store/use-project";
import { useUserPermissions } from "@/hooks/store/user";
import { useAppRouter } from "@/hooks/use-app-router";
// local imports
import { SwitcherLabel } from "../common/switcher-label";
import { ProjectHeaderButton } from "./project-header-button";
import { getTabUrl } from "./tab-navigation-utils";
import { useTabPreferences } from "./use-tab-preferences";
import { useNavigationItems } from "./use-navigation-items";
type TProjectHeaderProps = {
workspaceSlug: string;
projectId: string;
};
export const ProjectHeader = observer(function ProjectHeader(props: TProjectHeaderProps) {
const { workspaceSlug, projectId } = props;
// router
const router = useAppRouter();
// store hooks
const { joinedProjectIds, getPartialProjectById } = useProject();
const { allowPermissions } = useUserPermissions();
// Get current project details
const currentProjectDetails = getPartialProjectById(projectId);
// Get available navigation items for this project
const navigationItems = useNavigationItems({
workspaceSlug: workspaceSlug,
projectId,
project: currentProjectDetails,
allowPermissions,
});
// Get preferences from hook
const { tabPreferences } = useTabPreferences(workspaceSlug, projectId);
// Memoize available tab keys
const availableTabKeys = useMemo(() => navigationItems.map((item) => item.key), [navigationItems]);
// Memoize validated default tab key
const validatedDefaultTabKey = useMemo(
() =>
availableTabKeys.includes(tabPreferences.defaultTab)
? tabPreferences.defaultTab
: availableTabKeys[0] || "work_items",
[availableTabKeys, tabPreferences.defaultTab]
);
// Memoize switcher options to prevent recalculation on every render
const switcherOptions = useMemo<ICustomSearchSelectOption[]>(
() =>
joinedProjectIds
.map((id): ICustomSearchSelectOption | null => {
const project = getPartialProjectById(id);
if (!project) return null;
return {
value: id,
query: project.name,
content: (
<SwitcherLabel
name={project.name}
logo_props={project.logo_props}
LabelIcon={ProjectsOutline}
type="material"
/>
),
};
})
.filter((option): option is ICustomSearchSelectOption => option !== null),
[joinedProjectIds, getPartialProjectById]
);
// Memoize onChange handler
const handleProjectChange = useCallback(
(value: string) => {
if (value !== currentProjectDetails?.id) {
router.push(getTabUrl(workspaceSlug, value, validatedDefaultTabKey));
}
},
[currentProjectDetails?.id, router, workspaceSlug, validatedDefaultTabKey]
);
// Early return if no project details
if (!currentProjectDetails) return null;
return (
<CustomSearchSelect
options={switcherOptions}
value={currentProjectDetails.id}
onChange={handleProjectChange}
customButton={currentProjectDetails ? <ProjectHeaderButton project={currentProjectDetails} /> : null}
className="h-full rounded"
customButtonClassName="group flex items-center gap-0.5 rounded-sm hover:bg-surface-2 outline-none cursor-pointer h-full"
/>
);
});