Skip to content

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

4.6 KB
Raw Blame
import { Link, useLocation } from "@tanstack/react-router";
import { ChevronRight, CircleAlertIcon, TriangleAlertIcon } from "lucide-react";
import { twMerge } from "tailwind-merge";

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

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

// Important, because the menu button sizes every icon inside it and recolors them while the item is active.
const NavAlertIcon = ({ variant }: { variant: NonNullable<NavItem["alertVariant"]> }) => (
  <span className="ml-auto flex shrink-0">
    {variant === "danger" ? (
      <CircleAlertIcon aria-hidden className="size-3.5! text-danger!" />
    ) : (
      <TriangleAlertIcon aria-hidden className="size-3.5! text-warning!" />
    )}
    <span className="sr-only">Needs attention</span>
  </span>
);

// --- Project nav link (handles submenu chevron or normal link) ---

export const ProjectNavLink = ({
  item,
  onSubmenuOpen
}: {
  item: NavItem;
  onSubmenuOpen?: (submenu: Submenu) => void;
}) => {
  const { currentOrg } = useOrganization();
  const { currentProject } = useProject();
  const { pathname, search: locationSearch } = useLocation();
  const sidebarScope = useSidebarScope();

  const typePath = PROJECT_TYPE_PATH[currentProject.type];
  const isOrgScoped = isOrgScopedProduct(currentProject.type);
  const basePath = isOrgScoped
    ? `/organizations/${currentOrg.id}/${typePath}`
    : `/organizations/${currentOrg.id}/projects/${typePath}/${currentProject.id}`;
  const fullPath = `${basePath}/${item.pathSuffix}`;

  const activeMatchResult = (() => {
    if (!item.activeMatch) return false;
    if (typeof item.activeMatch === "function") {
      return item.activeMatch(pathname, (locationSearch as Record<string, unknown>) ?? {});
    }
    return item.activeMatch.test(pathname);
  })();
  const pathMatch =
    (item.exactPath ? pathname === fullPath : pathname.startsWith(fullPath)) || activeMatchResult;
  const isActive = item.search
    ? pathMatch &&
      Object.entries(item.search).every(([key, value]) => {
        const urlValue = (locationSearch as Record<string, unknown>)?.[key];
        return urlValue === value || (urlValue === undefined && item.isDefaultSearch);
      })
    : pathMatch;

  if (item.submenu && onSubmenuOpen) {
    return (
      <SidebarMenuItem>
        <SidebarMenuButton
          size="lg"
          scope={sidebarScope}
          isActive={isActive}
          tooltip={item.label}
          onClick={() => onSubmenuOpen(item.submenu!)}
        >
          <item.icon className="size-4" />
          <span>{item.label}</span>
          {Boolean(item.badgeCount) && (
            <Badge variant={item.badgeVariant ?? "warning"} isSquare className="ml-auto">
              {item.badgeCount}
            </Badge>
          )}
          {item.alertVariant && <NavAlertIcon variant={item.alertVariant} />}
          <ChevronRight
            className={twMerge(
              "size-4 opacity-50",
              !item.badgeCount && !item.alertVariant && "ml-auto"
            )}
          />
        </SidebarMenuButton>
      </SidebarMenuItem>
    );
  }

  return (
    <SidebarMenuItem>
      <SidebarMenuButton
        size="lg"
        scope={sidebarScope}
        asChild
        closeOnMobile
        isActive={isActive}
        tooltip={item.label}
      >
        <Link
          to={
            isOrgScoped
              ? (`/organizations/$orgId/${typePath}/${item.pathSuffix}` as any)
              : (`/organizations/$orgId/projects/${typePath}/$projectId/${item.pathSuffix}` as any)
          }
          params={{ orgId: currentOrg.id, projectId: currentProject.id } as any}
          search={item.search as any}
        >
          <item.icon className="size-4" />
          <span>{item.label}</span>
          {Boolean(item.badgeCount) && (
            <Badge variant={item.badgeVariant ?? "warning"} isSquare className="ml-auto">
              {item.badgeCount}
            </Badge>
          )}
          {item.alertVariant && <NavAlertIcon variant={item.alertVariant} />}
        </Link>
      </SidebarMenuButton>
    </SidebarMenuItem>
  );
};

export const ProjectNavList = ({
  items,
  onSubmenuOpen
}: {
  items: NavItem[];
  onSubmenuOpen: (submenu: Submenu) => void;
}) => (
  <SidebarMenu>
    {items
      .filter((i) => !i.hidden)
      .map((item) => (
        <ProjectNavLink
          key={item.label}
          item={item}
          onSubmenuOpen={item.submenu ? onSubmenuOpen : undefined}
        />
      ))}
  </SidebarMenu>
);