"use client"; import { useEffect, useState } from "react"; import { IconArrowsShuffle, IconBolt, IconBrandGithub, IconBrandGitlab, IconBrandSentry, IconBrandSlack, IconFolder, IconGitBranch, IconHexagon, IconPlugConnected, IconTicket, } from "@tabler/icons-react"; import type { Icon as TablerIcon } from "@tabler/icons-react"; import { useAppStore } from "@/components/state-provider"; import { SettingsGroup, SettingsLeaf } from "./settings-nav-primitives"; const ROOT_HREF = "/settings/workspace"; const INTEGRATIONS: Array<{ slug: string; label: string; icon: TablerIcon }> = [ { slug: "github", label: "GitHub", icon: IconBrandGithub }, { slug: "gitlab", label: "GitLab", icon: IconBrandGitlab }, { slug: "jira", label: "Jira", icon: IconTicket }, { slug: "linear", label: "Linear", icon: IconHexagon }, { slug: "sentry", label: "Sentry", icon: IconBrandSentry }, { slug: "slack", label: "Slack", icon: IconBrandSlack }, ]; const ACTIVE_WORKSPACE_LABEL = ( Active ); type WorkspacesGroupProps = { pathname: string; expanded?: boolean; onToggle?: () => void; }; function isWorkspaceRoute(pathname: string, workspaceId: string): boolean { const workspacePath = `${ROOT_HREF}/${workspaceId}`; return pathname === workspacePath || pathname.startsWith(`${workspacePath}/`); } function activeWorkspaceIdFor( workspaces: Array<{ id: string }>, storeActiveWorkspaceId: string | null, ): string | null { return workspaces.some((workspace) => workspace.id === storeActiveWorkspaceId) ? storeActiveWorkspaceId : null; } function activeWorkspaceFirst(workspaces: T[], activeId: string | null) { if (!activeId) return workspaces; return [ ...workspaces.filter((workspace) => workspace.id === activeId), ...workspaces.filter((workspace) => workspace.id !== activeId), ]; } export function WorkspacesGroup({ pathname, expanded, onToggle }: WorkspacesGroupProps) { const workspaces = useAppStore((s) => s.workspaces.items); const storeActiveWorkspaceId = useAppStore((s) => s.workspaces.activeId); const routeWorkspaceId = workspaces.find((workspace) => isWorkspaceRoute(pathname, workspace.id))?.id ?? null; const activeWorkspaceId = activeWorkspaceIdFor(workspaces, storeActiveWorkspaceId); const orderedWorkspaces = activeWorkspaceFirst(workspaces, activeWorkspaceId); const defaultExpandedWorkspaceId = routeWorkspaceId ?? activeWorkspaceId ?? workspaces[0]?.id; const [expandedWorkspaceId, setExpandedWorkspaceId] = useState( defaultExpandedWorkspaceId ?? null, ); useEffect(() => { setExpandedWorkspaceId(defaultExpandedWorkspaceId ?? null); }, [defaultExpandedWorkspaceId]); const toggleWorkspace = (workspaceId: string) => { setExpandedWorkspaceId((current) => (current === workspaceId ? null : workspaceId)); }; return ( {orderedWorkspaces.map((workspace) => { const workspacePath = `${ROOT_HREF}/${workspace.id}`; const repositoriesPath = `${workspacePath}/repositories`; const workflowsPath = `${workspacePath}/workflows`; const automationsPath = `${workspacePath}/automations`; const integrationsPath = `${workspacePath}/integrations`; const integrationsActive = pathname === integrationsPath || pathname.startsWith(`${integrationsPath}/`); const workspaceIsActive = activeWorkspaceId === workspace.id; return ( toggleWorkspace(workspace.id)} depth={1} > {INTEGRATIONS.map(({ slug, label, icon }) => { const href = `${integrationsPath}/${slug}`; return ( ); })} ); })} ); }