"use client";
import { forwardRef, type ComponentPropsWithoutRef } from "react";
import { useTranslation } from "react-i18next";
import { IconBriefcase, IconChevronDown, IconLayoutKanban, IconPlus } from "@tabler/icons-react";
import { DropdownMenuItem, DropdownMenuSeparator } from "@kandev/ui/dropdown-menu";
import { ActiveWorkspaceBadge } from "@/components/settings/record-badges";
import { cn } from "@/lib/utils";
/**
* The one workspace switcher design, shared by the sidebar header picker and
* the workspace settings pages. Both surfaces draw the same trigger and the
* same rows — a switcher that reads differently depending on where it is
* opened is worse than two obviously different controls.
*
* Only behaviour differs: the sidebar picker changes the active workspace,
* while the settings switcher navigates between settings pages. That is why
* selection is a callback here rather than logic baked into the rows.
*/
export type WorkspaceType = "kanban" | "office";
export type WorkspaceItem = {
id: string;
name: string;
office_workflow_id?: string | null;
};
export function workspaceType(workspace: WorkspaceItem | undefined): WorkspaceType {
return workspace?.office_workflow_id ? "office" : "kanban";
}
/** Returns a catalog key, not copy: this is a plain function with no hook. */
function workspaceTypeLabelKey(type: WorkspaceType) {
return type === "office" ? "sidebar:office" : "sidebar:kanban";
}
function WorkspaceTypeIcon({ type, className }: { type: WorkspaceType; className: string }) {
if (type === "office") {
return ;
}
return ;
}
/**
* The switcher's button: a bordered control carrying the current workspace
* name and a chevron. `nameClassName` exists for the sidebar's expand
* animation (`sidebar-fade-in`), which must not leak into the settings header.
*
* forwardRef + prop spread so `DropdownMenuTrigger asChild` can wire the
* trigger (ref, onClick, aria-*, data-state) onto the underlying button.
*/
export const WorkspaceTrigger = forwardRef<
HTMLButtonElement,
ComponentPropsWithoutRef<"button"> & {
activeName: string;
chevronTestId?: string;
nameClassName?: string;
}
>(function WorkspaceTrigger(
{ activeName, chevronTestId, nameClassName, className, ...props },
ref,
) {
const { t } = useTranslation();
return (
);
});
export type WorkspacePickerContentProps = {
workspaces: WorkspaceItem[];
activeId: string | null;
itemTestIdPrefix: string;
officeEnabled: boolean;
onWorkspaceSelect: (workspace: WorkspaceItem) => void;
onNavigate: (href: string) => void;
};
export function WorkspacePickerContent({
workspaces,
activeId,
itemTestIdPrefix,
officeEnabled,
onWorkspaceSelect,
onNavigate,
}: WorkspacePickerContentProps) {
return (
<>
>
);
}
function WorkspaceList({
workspaces,
activeId,
itemTestIdPrefix,
onWorkspaceSelect,
}: Pick<
WorkspacePickerContentProps,
"workspaces" | "activeId" | "itemTestIdPrefix" | "onWorkspaceSelect"
>) {
const { t } = useTranslation();
if (workspaces.length === 0) {
return {t("sidebar:noWorkspaces")};
}
return workspaces.map((ws) => {
const type = workspaceType(ws);
return (
onWorkspaceSelect(ws)}
className="cursor-pointer gap-2"
>
{ws.name}
{/* The same "Active" pill the settings menu and the workspace list
draw, sitting with the type badge in the row's qualifier column
rather than splitting the icon from the name. */}
{ws.id === activeId && }
{t(workspaceTypeLabelKey(type))}
);
});
}
function WorkspaceCreateItems({
officeEnabled,
onNavigate,
}: Pick) {
const { t } = useTranslation();
if (!officeEnabled) {
return (
onNavigate("/settings/workspaces")}
>
{t("sidebar:addWorkspace")}
);
}
return (
<>
onNavigate("/settings/workspaces")}
>
{t("sidebar:newKanbanWorkspace")} onNavigate("/office/setup?mode=new")}
>
{t("sidebar:newOfficeWorkspace")}
>
);
}