"use client";
import {
IconBrandVscode,
IconDeviceDesktop,
IconFileText,
IconFolder,
IconGitBranch,
IconGitPullRequest,
} from "@tabler/icons-react";
import {
DropdownMenuItem,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
} from "@kandev/ui/dropdown-menu";
import { prPanelLabel, prIdentitySlug, prTaskKey } from "@/components/github/pr-utils";
import { useDockviewStore } from "@/lib/state/dockview-store";
import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry";
import { resolvePluginIcon } from "@/lib/plugins/icons";
import type { TaskPR } from "@/lib/types/github";
import type { TaskMR } from "@/lib/types/gitlab";
import { mrTaskKey } from "@/components/gitlab/mr-detail-panel";
import { RepositoryScriptsMenuItems } from "./repository-scripts-menu";
import { SessionReopenMenuItems } from "./session-reopen-menu";
import { TerminalReopenMenuItems } from "./terminal-reopen-menu";
export type AddPanelMenuState = {
taskId: string | null;
isPassthrough: boolean;
hasChanges: boolean;
hasFiles: boolean;
/** All PRs linked to the task; multi-repo tasks render one menu item per PR. */
prs: TaskPR[];
mrs: TaskMR[];
};
type AddPanelMenuItemsProps = {
groupId: string;
state: AddPanelMenuState;
onNewSession: () => void;
onAddTerminal: () => void;
onRunScript: (scriptId: string) => void;
onRunDevScript: () => void;
};
export const MENU_ICON_CLASS = "h-3.5 w-3.5 mr-1.5 shrink-0";
export const MENU_ITEM_CLASS = "cursor-pointer text-xs";
const PR_SUBMENU_TEST_ID = "add-panel-pr-submenu";
/**
* Linked GitHub PR entries for the dockview "+" menu. A single PR renders as
* one inline row; multiple PRs collapse behind a "Pull requests" sub-menu so
* tasks with up to ten linked PRs don't stretch the main menu too tall.
*/
function PRPanelMenuItems({ prs, onOpenPR }: { prs: TaskPR[]; onOpenPR: (pr: TaskPR) => void }) {
if (prs.length === 0) return null;
if (prs.length === 1) {
const pr = prs[0];
return (
onOpenPR(pr)}
className={MENU_ITEM_CLASS}
data-testid={`add-panel-pr-item-${prIdentitySlug(pr)}`}
>
{prPanelLabel(pr.pr_number)}
);
}
return (
Pull requests
{prs.map((pr) => (
onOpenPR(pr)}
className={MENU_ITEM_CLASS}
data-testid={`add-panel-pr-item-${prIdentitySlug(pr)}`}
>
{`${prPanelLabel(pr.pr_number)} — ${pr.owner}/${pr.repo}`}
))}
);
}
/** One "+" menu row per plugin-registered task panel (AC1), rendered after Plan. */
function PluginTaskPanelMenuItems({ groupId }: { groupId: string }) {
usePluginRegistry();
const addPluginPanel = useDockviewStore((s) => s.addPluginPanel);
const panels = pluginRegistry.getTaskPanels();
return (
<>
{panels.map((registration) => {
const Icon = resolvePluginIcon(registration.icon);
return (
addPluginPanel(registration.pluginId, registration.id, registration.title, {
groupId,
})
}
className={MENU_ITEM_CLASS}
data-testid={`add-panel-plugin-item-${registration.pluginId}-${registration.id}`}
>
{registration.title}
);
})}
>
);
}
export function AddPanelMenuItems({
groupId,
state,
onNewSession,
onAddTerminal,
onRunScript,
onRunDevScript,
}: AddPanelMenuItemsProps) {
const addBrowserPanel = useDockviewStore((s) => s.addBrowserPanel);
const addVscodePanel = useDockviewStore((s) => s.addVscodePanel);
const addPlanPanel = useDockviewStore((s) => s.addPlanPanel);
const addFilesPanel = useDockviewStore((s) => s.addFilesPanel);
const addChangesPanel = useDockviewStore((s) => s.addChangesPanel);
const addPRPanel = useDockviewStore((s) => s.addPRPanel);
const addMRPanel = useDockviewStore((s) => s.addMRPanel);
return (
<>
{state.taskId && (
)}
addBrowserPanel(undefined, groupId)}
className={MENU_ITEM_CLASS}
>
Browser
addVscodePanel()} className={MENU_ITEM_CLASS}>
VS Code
{!state.isPassthrough && (
addPlanPanel({ groupId })} className={MENU_ITEM_CLASS}>
Plan
)}
{!state.hasChanges && (
addChangesPanel(groupId)} className={MENU_ITEM_CLASS}>
Changes
)}
{!state.hasFiles && (
addFilesPanel(groupId)} className={MENU_ITEM_CLASS}>
Files
)}
addPRPanel(prTaskKey(pr))} />
{state.mrs.map((mr) => (
addMRPanel(mrTaskKey(mr))}
className={MENU_ITEM_CLASS}
data-testid={`add-panel-mr-item-${mr.id}`}
>
{state.mrs.length > 1
? `MR !${mr.mr_iid} - ${mr.project_path}`
: `Merge Request !${mr.mr_iid}`}
))}
>
);
}