"use client"; import { IconBrandVscode, IconDeviceDesktop, IconFileText, IconFolder, IconGitBranch, IconGitPullRequest, IconHistory, IconLayoutGrid, IconListCheck, IconNetwork, } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { DropdownMenuCheckboxItem, DropdownMenuItem, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, } from "@kandev/ui/dropdown-menu"; import type { DockviewApi } from "dockview-react"; import { prPanelLabel, prIdentitySlug, prTaskKey } from "@/components/github/pr-utils"; import { useDockviewStore } from "@/lib/state/dockview-store"; import { reviewPanelId } from "@/lib/state/dockview-review-panel-id"; import { pluginRegistry, usePluginRegistry } from "@/lib/plugins/registry"; import { resolvePluginIcon } from "@/lib/plugins/icons"; import type { ReviewItemSummary } from "@/lib/plugins/types"; import type { TaskPR } from "@/lib/types/github"; import type { TaskMR } from "@/lib/types/gitlab"; import { useAppStore } from "@/components/state-provider"; import { useFeature } from "@/hooks/domains/features/use-feature"; import { useTaskCanvases } from "@/hooks/domains/task/use-task-canvases"; import type { Canvas } from "@/lib/api/domains/canvas-api"; 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"; import { useNormalizedTaskReviews } from "./review-panel-provider"; import { reviewItemId } from "./review-selection"; import type { PortForwardingVisibility } from "./port-forwarding-visibility-provider"; 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[]; portForwarding?: PortForwardingVisibility; }; 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"; // i18n-exempt: Dockview panel identity prefix, not user-facing copy. const CANVAS_PANEL_ID_PREFIX = "canvas:"; const DISCOVERABLE_TASK_CANVAS_STATUSES = new Set(["active", "pending", "error"]); export function isDiscoverableTaskCanvas(canvas: Pick): boolean { return DISCOVERABLE_TASK_CANVAS_STATUSES.has(canvas.status); } type ReviewMenuIdentity = Pick & Partial>; type ReviewPanelLookup = Pick; /** Reads the params of a live dockview panel, or undefined when no panel with * that id is open. */ function panelParams(api: ReviewPanelLookup, id: string): Record | undefined { return api.getPanel(id)?.params as Record | undefined; } /** True when a panel's params identify the built-in GitHub/GitLab review — * either through the legacy `prKey`/`mrKey` param or the provider + reviewKey * pair. */ function matchesBuiltInReview( params: Record | undefined, providerId: "github" | "gitlab", reviewKey: string, legacyKey: "prKey" | "mrKey", ): boolean { return ( params?.[legacyKey] === reviewKey || ((params?.providerId === providerId || params?.provider === providerId) && params.reviewKey === reviewKey) ); } /** True when a panel's params match a fully-specified registered (non-GitHub/ * GitLab) review by provider, connection scope, repository, and change-request * number. */ function matchesRegisteredReview( params: Record | undefined, review: Required, ): boolean { return ( params?.providerId === review.providerId && params.connectionScope === review.connectionScope && params.repositoryId === review.repositoryId && params.changeRequestNumber !== undefined && String(params.changeRequestNumber) === String(review.changeRequestNumber) ); } /** True only when this exact review already has a canonical or keyed live panel. */ export function isReviewPanelOpen( api: ReviewPanelLookup | null, review: ReviewMenuIdentity, ): boolean { if (!api) return false; const canonicalParams = panelParams(api, "pr-detail"); if (review.providerId === "github") { return ( Boolean(api.getPanel(`pr-detail|${review.reviewKey}`)) || matchesBuiltInReview(canonicalParams, "github", review.reviewKey, "prKey") ); } if (review.providerId === "gitlab") { return ( Boolean(api.getPanel(`mr-detail|${review.reviewKey}`)) || matchesBuiltInReview(canonicalParams, "gitlab", review.reviewKey, "mrKey") || matchesBuiltInReview(panelParams(api, "mr-detail"), "gitlab", review.reviewKey, "mrKey") ); } const { connectionScope, repositoryId, changeRequestNumber } = review; if ( connectionScope === undefined || repositoryId === undefined || changeRequestNumber === undefined ) { return false; } const registeredReview = { ...review, connectionScope, repositoryId, changeRequestNumber }; return ( Boolean(api.getPanel(reviewPanelId(registeredReview))) || matchesRegisteredReview(canonicalParams, registeredReview) ); } /** * 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 }) { const { t } = useTranslation(); 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 ( {t("task:pullRequests")} {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} ); })} ); } function TaskCanvasMenuItems({ groupId, taskId }: { groupId: string; taskId: string | null }) { const enabled = useFeature("canvases"); const workspaceId = useAppStore((state) => state.workspaces.activeId); const canvases = useTaskCanvases(taskId, workspaceId, enabled).filter(isDiscoverableTaskCanvas); const api = useDockviewStore((s) => s.api); if (!enabled || canvases.length === 0) return null; return ( <> {canvases.map((canvas) => ( api?.addPanel({ id: `${CANVAS_PANEL_ID_PREFIX}${canvas.id}`, component: "canvas", title: canvas.title, params: { canvasId: canvas.id }, position: { referenceGroup: groupId }, }) } className={MENU_ITEM_CLASS} data-testid={`add-panel-canvas-item-${canvas.id}`} > {canvas.title} ))} ); } /** "+" menu row that opens a prompt-history panel in the given group. */ function PromptHistoryPanelMenuItem({ groupId }: { groupId: string }) { const { t } = useTranslation(); const addPromptHistoryPanel = useDockviewStore((s) => s.addPromptHistoryPanel); return ( addPromptHistoryPanel({ groupId })} className={MENU_ITEM_CLASS} > {t("task:promptHistory")} ); } /** Filters the linked PRs/MRs down to those whose review panel isn't already * open, so the "+" menu doesn't offer duplicates. */ function missingBuiltInReviews( api: ReviewPanelLookup | null, state: Pick, ): Pick { return { prs: state.prs.filter( (pr) => !isReviewPanelOpen(api, { providerId: "github", reviewKey: prTaskKey(pr) }), ), mrs: state.mrs.filter( (mr) => !isReviewPanelOpen(api, { providerId: "gitlab", reviewKey: mrTaskKey(mr) }), ), }; } /** Renders the dockview "+" menu: session/terminal reopen entries, browser, * VS Code, plan, port-forwarding toggle, plugin task panels, task canvases, * todos, prompt history, changes/files, review panels, and repository scripts. */ export function AddPanelMenuItems({ groupId, state, onNewSession, onAddTerminal, onRunScript, onRunDevScript, }: AddPanelMenuItemsProps) { const { t } = useTranslation(); const addBrowserPanel = useDockviewStore((s) => s.addBrowserPanel); const addVscodePanel = useDockviewStore((s) => s.addVscodePanel); const addPlanPanel = useDockviewStore((s) => s.addPlanPanel); const addTodosPanel = useDockviewStore((s) => s.addTodosPanel); const addFilesPanel = useDockviewStore((s) => s.addFilesPanel); const addChangesPanel = useDockviewStore((s) => s.addChangesPanel); const api = useDockviewStore((s) => s.api); return ( <> {state.taskId && ( )} addBrowserPanel(undefined, groupId)} className={MENU_ITEM_CLASS} > {t("task:browser")} addVscodePanel()} className={MENU_ITEM_CLASS}> {t("task:vsCode")} {!state.isPassthrough && ( addPlanPanel({ groupId })} className={MENU_ITEM_CLASS}> {t("task:plan")} )} {state.portForwarding && ( void state.portForwarding?.togglePortForwarding({ openDialogOnEnable: true }) } className={MENU_ITEM_CLASS} data-testid="port-forwarding-menu-item" > {t("task:portForwarding")} )} {!state.isPassthrough && ( addTodosPanel({ groupId })} className={MENU_ITEM_CLASS}> {t("common:todos")} )} {!state.isPassthrough && } {!state.hasChanges && ( addChangesPanel(groupId)} className={MENU_ITEM_CLASS}> {t("task:changes")} )} {!state.hasFiles && ( addFilesPanel(groupId)} className={MENU_ITEM_CLASS}> {t("task:files")} )} ); } /** Renders the "+" menu's review rows: linked PRs/MRs (skipping ones already * open) plus registered non-GitHub/GitLab review panels. */ function ReviewPanelMenuItems({ groupId, state, api, }: Pick & { api: ReviewPanelLookup | null }) { const { t } = useTranslation(); const addPRPanel = useDockviewStore((s) => s.addPRPanel); const addMRPanel = useDockviewStore((s) => s.addMRPanel); const addReviewPanel = useDockviewStore((s) => s.addReviewPanel); const { prs, mrs } = missingBuiltInReviews(api, state); const reviews = useNormalizedTaskReviews(state.taskId) .filter((review) => review.providerId !== "github" && review.providerId !== "gitlab") .filter((review) => !isReviewPanelOpen(api, review)); return ( <> addPRPanel(prTaskKey(pr), { groupId })} /> {mrs.map((mr) => ( addMRPanel(mrTaskKey(mr), { groupId })} className={MENU_ITEM_CLASS} data-testid={`add-panel-mr-item-${mr.id}`} > {mrs.length > 1 ? `MR !${mr.mr_iid} - ${mr.project_path}` : t("task:mergeRequest", { mriid: mr.mr_iid })} ))} {reviews.map((review) => ( addReviewPanel(review, { groupId })} className={MENU_ITEM_CLASS} data-testid={`add-panel-review-item-${reviewItemId(review)}`} > {review.title} ))} ); }