Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

3.2 KB
Raw Blame
import { Link, useLocation, useSearch } from "@tanstack/react-router";
import { ChevronLeft } from "lucide-react";

import {
  Badge,
  SidebarGroup,
  SidebarGroupLabel,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
  useSidebarScope
} from "@app/components/v3";
import { useOrganization, useProject } from "@app/context";
import { isOrgScopedProduct } from "@app/helpers/project";

import type { Submenu } from "./types";
import { PROJECT_TYPE_PATH } from "./types";

// --- Generic submenu view for projects ---

export const ProjectSubmenuView = ({
  submenu,
  onBack
}: {
  submenu: Submenu;
  onBack: () => void;
}) => {
  const { currentOrg } = useOrganization();
  const { currentProject } = useProject();
  const { pathname } = useLocation();
  const searchParams = useSearch({ strict: false }) as Record<string, string>;

  const typePath = PROJECT_TYPE_PATH[currentProject.type];
  const sidebarScope = useSidebarScope();
  const isOrgScoped = isOrgScopedProduct(currentProject.type);
  const basePath = isOrgScoped
    ? `/organizations/${currentOrg.id}/${typePath}`
    : `/organizations/${currentOrg.id}/projects/${typePath}/${currentProject.id}`;
  const isOnExactPage = pathname.startsWith(`${basePath}/${submenu.pathSuffix}`);
  const currentTab = searchParams?.selectedTab;
  const anyItemMatchesDetail = submenu.items.some((s) => Boolean(s.activeMatch?.test(pathname)));

  return (
    <SidebarGroup>
      <SidebarGroupLabel asChild>
        <button
          className="cursor-pointer hover:bg-foreground/[0.025]"
          type="button"
          onClick={onBack}
        >
          <ChevronLeft />
          <span>{submenu.title}</span>
        </button>
      </SidebarGroupLabel>
      <SidebarMenu>
        {submenu.items.map((sub) => {
          const matchesDetail = Boolean(sub.activeMatch?.test(pathname));
          const isActive =
            matchesDetail ||
            (isOnExactPage &&
              !anyItemMatchesDetail &&
              (currentTab === sub.tab || (!currentTab && sub.tab === submenu.defaultTab)));

          return (
            <SidebarMenuItem key={sub.label}>
              <SidebarMenuButton
                size="lg"
                scope={sidebarScope}
                asChild
                closeOnMobile
                isActive={isActive}
                tooltip={sub.label}
              >
                <Link
                  to={
                    isOrgScoped
                      ? (`/organizations/$orgId/${typePath}/${submenu.pathSuffix}` as any)
                      : (`/organizations/$orgId/projects/${typePath}/$projectId/${submenu.pathSuffix}` as any)
                  }
                  params={{ orgId: currentOrg.id, projectId: currentProject.id } as any}
                  search={{ selectedTab: sub.tab } as any}
                >
                  <sub.icon className="size-4" />
                  <span>{sub.label}</span>
                </Link>
              </SidebarMenuButton>
              {Boolean(sub.badgeCount) && (
                <Badge variant="warning" className="absolute top-[10.5px] right-4">
                  {sub.badgeCount}
                </Badge>
              )}
            </SidebarMenuItem>
          );
        })}
      </SidebarMenu>
    </SidebarGroup>
  );
};