"use client"; import { useCallback, useState } from "react"; import { type IDockviewHeaderActionsProps } from "dockview-react"; import { IconPlus, IconTerminal2, IconPlayerPlay, IconLayoutSidebarRightCollapse, } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@kandev/ui/dropdown-menu"; import { useDockviewStore, performLayoutSwitch } from "@/lib/state/dockview-store"; import { useAppStore, useAppStoreApi } from "@/components/state-provider"; import { useEnvironmentId } from "@/hooks/use-environment-session-id"; import { useTaskPR } from "@/hooks/domains/github/use-task-pr"; import { useTaskMRs } from "@/hooks/domains/gitlab/use-task-mr"; import { createUserShell } from "@/lib/api/domains/user-shell-api"; import { useRepositoryScripts } from "@/hooks/domains/workspace/use-repository-scripts"; import { replaceTaskUrl } from "@/lib/links"; import type { Task } from "@/lib/types/http"; import { sessionId as toSessionId } from "@/lib/types/ids"; import { AddPanelMenuItems, MENU_ITEM_CLASS } from "./dockview-add-panel-items"; import { useUserShells } from "@/hooks/domains/session/use-user-shells"; import { useEnsureDefaultTerminalOrdinary } from "@/hooks/domains/session/use-ensure-default-terminal-ordinary"; import { NewSessionDialog } from "./new-session-dialog"; import { NewTaskDropdown } from "./new-task-dropdown"; import { useActiveSessionDevScript } from "./repository-scripts-menu"; import { DevServerPreviewButton } from "./dev-server-preview-button"; import { GroupSplitCloseActionsView, useDockviewGroupWidth } from "./dockview-group-actions"; import { useTranslation } from "react-i18next"; import { useOptionalPortForwardingVisibility } from "./port-forwarding-visibility-provider"; const HEADER_ACTION_BUTTON_CLASS = "h-6 w-6 p-0 cursor-pointer text-muted-foreground hover:bg-muted/70 hover:text-foreground focus-visible:ring-1 focus-visible:ring-ring"; const RAW_HEADER_ACTION_BUTTON_CLASS = "inline-flex h-6 w-6 items-center justify-center rounded-[5px] text-muted-foreground transition-colors hover:bg-muted/70 hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring cursor-pointer"; const HEADER_ICON_CLASS = "h-3.5 w-3.5"; function useLeftHeaderState( groupId: string, containerApi: IDockviewHeaderActionsProps["containerApi"], ) { const sidebarGroupId = useDockviewStore((s) => s.sidebarGroupId); const centerGroupId = useDockviewStore((s) => s.centerGroupId); const activeSessionId = useAppStore((state) => state.tasks.activeSessionId); const taskId = useAppStore((state) => state.tasks.activeTaskId); const isPassthrough = useAppStore((state) => { if (!activeSessionId) return false; return state.taskSessions.items[activeSessionId]?.is_passthrough === true; }); const { prs } = useTaskPR(taskId); const mrs = useTaskMRs(taskId); const portForwarding = useOptionalPortForwardingVisibility(); const hasChanges = Boolean( containerApi.getPanel("changes") ?? containerApi.getPanel("diff-files"), ); const hasFiles = Boolean(containerApi.getPanel("files") ?? containerApi.getPanel("all-files")); return { isSidebarGroup: groupId === sidebarGroupId, isCenterGroup: groupId === centerGroupId, activeSessionId, taskId, isPassthrough, prs, mrs, hasChanges, hasFiles, portForwarding, }; } export function LeftHeaderActions(props: IDockviewHeaderActionsProps) { const { t } = useTranslation(); const { group, containerApi } = props; const state = useLeftHeaderState(group.id, containerApi); const environmentId = useEnvironmentId(); const taskID = useAppStore((s) => s.tasks?.activeTaskId ?? null); const addTerminalPanel = useDockviewStore((s) => s.addTerminalPanel); const addUserShell = useAppStore((s) => s.addUserShell); const devScript = useActiveSessionDevScript(); const [showNewSessionDialog, setShowNewSessionDialog] = useState(false); // Eagerly populate the user-shell store for this env+task so the // dockview "+" menu's Terminals submenu and the per-tab badge logic // both have the data ready on first open. Without this call, the // store stays empty on desktop (the mobile right-panel hook is the // only other call site) and the menu would render an empty section. useUserShells(environmentId, taskID); // Migrate the static `terminal-default` panel into a DB-backed // ordinary terminal so its tab gets a real seq badge alongside any // user-created terminals. useEnsureDefaultTerminalOrdinary(); const handleAddTerminal = useCallback(async () => { if (!environmentId) return; try { // Pass taskId so the backend creates a DB-backed ordinary terminal // (with seq/kind) instead of a legacy passthrough shell. Also stamp // taskID into the dockview panel params so the destroy cleanup // passes ownership verification. const result = await createUserShell(environmentId, { taskId: taskID ?? undefined }); // Push the new ordinary terminal into the user-shell store // synchronously so TerminalTab's store lookup finds it on first // render. Without this, the tab would briefly show the panel's // initial title ("Terminal {seq}") before the next WS list // catches up and rewrites it to the normalized "Terminal". addUserShell(environmentId, { terminalId: result.terminalId, kind: result.kind, seq: result.seq, displayName: result.displayName, customName: null, state: result.state ?? "open", ptyStatus: result.ptyStatus ?? "stopped", running: result.ptyStatus === "running", label: result.label, closable: result.closable ?? true, initialCommand: result.initialCommand, }); // Panel title is always set to "Terminal" — TerminalTab pushes the // custom name (or "Terminal") onto api.title via setTitle once it // mounts; passing "Terminal" here avoids a "Terminal 1" → "Terminal" // flash on first paint. addTerminalPanel(result.terminalId, group.id, environmentId, taskID ?? undefined, "Terminal"); } catch (error) { console.error("Failed to create terminal:", error); } }, [environmentId, taskID, addTerminalPanel, addUserShell, group.id]); const handleRunScript = useCallback( async (scriptId: string) => { if (!environmentId) return; try { const result = await createUserShell(environmentId, { scriptId }); const title = result.label ?? t("common:script"); addTerminalPanel(result.terminalId, group.id, environmentId, undefined, title); } catch (error) { console.error("Failed to run script:", error); } }, [environmentId, addTerminalPanel, group.id], ); const handleRunDevScript = useCallback(async () => { if (!environmentId || !devScript) return; try { const result = await createUserShell(environmentId, { command: devScript, label: t("task:devServer"), }); addTerminalPanel(result.terminalId, group.id, environmentId, undefined, t("task:devServer")); } catch (error) { console.error("Failed to start dev script:", error); } }, [environmentId, devScript, addTerminalPanel, group.id]); if (state.isSidebarGroup) return null; return (