Files
kandev/apps/web/components/task/dockview-header-actions.tsx
T
João Salavisa 7771155c8f feat(task): add minimize button to dockview group header (#1039)
* feat(task): add per-group minimize state to dockview store

Adds `minimizedGroupIds: Set<string>` plus `toggleGroupMinimized` and
`clearMinimizedGroups` actions. The set is cleared on maximize,
exit-maximize, preset apply, custom layout apply, env switch,
default-layout rebuild, and release-to-default so minimize state never
leaks across task environments and never overlaps with maximize.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(task): add minimize button to dockview group header

Adds an IconMinus toggle button alongside Maximize/Split/Close in each
non-chat dockview group header. Clicking it adds the group ID to the
store's `minimizedGroupIds`; a new `useMinimizedGroupSync` hook walks
`api.groups`, stamps a `data-kandev-minimized` attribute on each group
element, and calls `group.api.setSize` to shrink the group to the
splitview minimum (the freed space redistributes to siblings). A CSS
rule hides the content container so only the tab strip remains visible.

The hook also auto-restores a minimized group when a new panel is added
to it (e.g. via the `+` menu) and evicts removed groups from the set.
Clicking Minimize while maximized first exits maximize so the user
doesn't end up with a header-only view.

In wide mode the button is inline; in narrow mode it folds into the
existing overflow dropdown as its first item. Chat group never sees the
button (same exclusion as Close).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(task): tighten minimize-icon test, drop unused TID

The previous "renders Restore tooltip and icon swap" case had two weak
assertions: `getAttribute(...) !== null || btn !== null` is
tautologically true, and `btn.innerHTML.toContain("svg")` matches both
icon variants. Replaced with two focused tests that assert the Tabler
class differs between the minimized and non-minimized state. Also
dropped the unused `minimizeMenuItem` entry from the TID map.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(task): minimize-from-maximize used stale group ID

`handleMinimize` previously called `toggleGroupMinimized(group.id)`
after `exitMaximizedLayout()`. During maximize, `group.id` is the
ephemeral overlay group's ID — once the original layout is restored
that ID is gone from `api.groups`, and the minimize was silently lost.
Read `maximizedGroupId` (the pre-maximize group ID stored at maximize
time) from the store and target that instead.

Reported on PR #1039 by claude-review, CodeRabbit, and Greptile.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* perf(task): skip redundant data-attribute writes in minimize sync

The sync hook re-runs on every `onDidAddGroup` / `onDidAddPanel`, which
previously rewrote `data-kandev-minimized` on every group regardless of
whether the value actually changed. Guard with a getAttribute-before-set
check.

Reported on PR #1039 by claude-review.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(task): cover useMinimizedGroupSync hook

Adds 11 unit tests for the hook: mount-time attribute stamping, null-api
no-op, minimize/restore round-trip with prior-size replay, auto-restore
on panel add into a minimized group, no auto-restore for unrelated
groups, group-removal eviction from the store and prior-size map, fresh
size on re-added group with the same ID, sync of newly added groups,
the perf guard on redundant attribute writes, and clean unmount.

Reported on PR #1039 by claude-review as a blocker (CLAUDE.md mandates
tests for new hooks).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(task): defer minimize toggle to next RAF when exiting maximize

`exitMaximizedLayout` schedules `api.layout(w, h)` in a requestAnimation
Frame after the synchronous fromJSON rebuild. Calling `toggleGroup
Minimized` immediately afterwards races with that layout pass, and the
resulting `setSize({ width: 0, height: 0 })` could be overridden — the
content was still hidden by CSS but the group kept its full splitview
footprint. Defer the toggle one frame so the maximize-exit settles
first.

Reported on PR #1039 by claude-review, Greptile, and cubic.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(task): guard deferred minimize against env-switch + redundant setSize

Two small polish fixes from PR #1039 review:

- The RAF callback in `handleMinimize` now verifies the target group
  still exists in `api.groups` before toggling. Within the frame, an
  env-switch (or another maximize) could clear `minimizedGroupIds`;
  blindly re-adding a stale ID would leave it in the set, and if the
  next env happens to reuse the same well-known constant ID (e.g.
  "right-top"), the wrong group would render as minimized.
  (cubic P2 finding.)
- `applyGroupMinimized` now early-returns when re-syncing an already-
  minimized group (`priorSizes.has(groupId)`), instead of re-emitting
  setSize(0,0) and its size-change events. New test covers this.
  (claude-review suggestion.)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 13:43:29 +01:00

576 lines
20 KiB
TypeScript

"use client";
import { useCallback, useState } from "react";
import { type IDockviewHeaderActionsProps } from "dockview-react";
import {
IconPlus,
IconDeviceDesktop,
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 { startProcess } from "@/lib/api";
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, ProcessInfo } from "@/lib/types/http";
import type { ProcessStatusEntry } from "@/lib/state/slices";
import { AddPanelMenuItems, MENU_ITEM_CLASS } from "./dockview-add-panel-items";
import { NewSessionDialog } from "./new-session-dialog";
import { NewTaskDropdown } from "./new-task-dropdown";
import { useActiveSessionDevScript } from "./repository-scripts-menu";
import { GroupSplitCloseActionsView, useDockviewGroupWidth } from "./dockview-group-actions";
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";
/** Map a ProcessInfo response to a ProcessStatusEntry for the store. */
function mapProcessToStatus(process: ProcessInfo): ProcessStatusEntry {
return {
processId: process.id,
sessionId: process.session_id,
kind: process.kind,
scriptName: process.script_name,
status: process.status,
command: process.command,
workingDir: process.working_dir,
exitCode: process.exit_code ?? null,
startedAt: process.started_at,
updatedAt: process.updated_at,
};
}
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 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,
hasChanges,
hasFiles,
};
}
export function LeftHeaderActions(props: IDockviewHeaderActionsProps) {
const { group, containerApi } = props;
const state = useLeftHeaderState(group.id, containerApi);
const environmentId = useEnvironmentId();
const addTerminalPanel = useDockviewStore((s) => s.addTerminalPanel);
const devScript = useActiveSessionDevScript();
const [showNewSessionDialog, setShowNewSessionDialog] = useState(false);
const handleAddTerminal = useCallback(async () => {
if (!environmentId) return;
try {
const result = await createUserShell(environmentId);
addTerminalPanel(result.terminalId, group.id, environmentId);
} catch (error) {
console.error("Failed to create terminal:", error);
}
}, [environmentId, addTerminalPanel, group.id]);
const handleRunScript = useCallback(
async (scriptId: string) => {
if (!environmentId) return;
try {
const result = await createUserShell(environmentId, { scriptId });
addTerminalPanel(result.terminalId, group.id, environmentId);
} 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: "Dev Server",
});
addTerminalPanel(result.terminalId, group.id, environmentId);
} catch (error) {
console.error("Failed to start dev script:", error);
}
}, [environmentId, devScript, addTerminalPanel, group.id]);
if (state.isSidebarGroup) return null;
return (
<div className="flex items-center gap-0.5 pl-0.5">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
size="sm"
variant="ghost"
className={HEADER_ACTION_BUTTON_CLASS}
data-testid="dockview-add-panel-btn"
aria-label="Add panel"
title="Add panel"
>
<IconPlus className={HEADER_ICON_CLASS} />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-44">
<AddPanelMenuItems
groupId={group.id}
state={state}
onNewSession={() => setShowNewSessionDialog(true)}
onAddTerminal={handleAddTerminal}
onRunScript={handleRunScript}
onRunDevScript={handleRunDevScript}
/>
</DropdownMenuContent>
</DropdownMenu>
{state.taskId && (
<NewSessionDialog
open={showNewSessionDialog}
onOpenChange={setShowNewSessionDialog}
taskId={state.taskId}
groupId={group.id}
/>
)}
</div>
);
}
/** Faded maximize, minimize, split, and close buttons for any non-sidebar group. */
function GroupSplitCloseActions({ group, containerApi }: IDockviewHeaderActionsProps) {
const centerGroupId = useDockviewStore((s) => s.centerGroupId);
const isChatGroup = group.id === centerGroupId;
const isMaximized = useDockviewStore((s) => s.preMaximizeLayout !== null);
const isMinimized = useDockviewStore((s) => s.minimizedGroupIds.has(group.id));
const preMaximizeGroupId = useDockviewStore((s) => s.maximizedGroupId);
const storeMaximize = useDockviewStore((s) => s.maximizeGroup);
const storeExitMaximize = useDockviewStore((s) => s.exitMaximizedLayout);
const toggleGroupMinimized = useDockviewStore((s) => s.toggleGroupMinimized);
const width = useDockviewGroupWidth(group);
const handleMaximize = useCallback(() => {
if (isMaximized) {
storeExitMaximize();
} else {
storeMaximize(group.id);
}
}, [group.id, isMaximized, storeMaximize, storeExitMaximize]);
const handleMinimize = useCallback(() => {
// Minimize and maximize are orthogonal — maximize is a global single-group
// overlay, minimize is per-group in-place collapse. Clicking Minimize while
// maximized would minimize the lone visible group and leave the user with a
// header-only screen; exit maximize first so they see the regular layout.
//
// During maximize, `group.id` is the ephemeral overlay group's ID — once
// exitMaximizedLayout rebuilds the original layout, that ID no longer
// exists in api.groups, and toggling minimize on it would silently no-op.
// Use the pre-maximize group ID (stored at maximize time) instead.
//
// exitMaximizedLayout schedules `api.layout(w, h)` in a requestAnimationFrame
// after rebuilding the layout; calling toggleGroupMinimized synchronously
// would race with that layout pass and the resulting setSize(0,0) could be
// overridden. Defer the toggle to the next frame so the maximize-exit
// settles first.
if (isMaximized) {
const target = preMaximizeGroupId ?? group.id;
storeExitMaximize();
requestAnimationFrame(() => {
// The world may have changed across the frame (env switch, another
// maximize, the target group removed). Only toggle if the target still
// exists as a real group in the current dockview api.
const live = useDockviewStore.getState();
const stillThere = live.api?.groups.some((g) => g.id === target) ?? false;
if (stillThere) live.toggleGroupMinimized(target);
});
return;
}
toggleGroupMinimized(group.id);
}, [group.id, isMaximized, preMaximizeGroupId, storeExitMaximize, toggleGroupMinimized]);
const handleSplitRight = useCallback(() => {
containerApi.addGroup({ referenceGroup: group, direction: "right" });
}, [group, containerApi]);
const handleSplitDown = useCallback(() => {
containerApi.addGroup({ referenceGroup: group, direction: "below" });
}, [group, containerApi]);
const handleCloseGroup = useCallback(() => {
const panels = [...group.panels];
if (panels.length === 0) {
try {
containerApi.removeGroup(group);
} catch {
/* already removed */
}
return;
}
for (const panel of panels) {
try {
containerApi.removePanel(panel);
} catch {
/* already removed */
}
}
}, [group, containerApi]);
return (
<GroupSplitCloseActionsView
width={width}
isChatGroup={isChatGroup}
isMaximized={isMaximized}
onMaximize={handleMaximize}
isMinimized={isMinimized}
onMinimize={handleMinimize}
onSplitRight={handleSplitRight}
onSplitDown={handleSplitDown}
onCloseGroup={handleCloseGroup}
/>
);
}
export function RightHeaderActions(props: IDockviewHeaderActionsProps) {
const { group } = props;
const centerGroupId = useDockviewStore((s) => s.centerGroupId);
const sidebarGroupId = useDockviewStore((s) => s.sidebarGroupId);
const rightTopGroupId = useDockviewStore((s) => s.rightTopGroupId);
const rightBottomGroupId = useDockviewStore((s) => s.rightBottomGroupId);
const isSidebarGroup = group.id === sidebarGroupId;
if (isSidebarGroup) return <SidebarRightActions />;
const isCenterGroup = group.id === centerGroupId;
const isRightTopGroup = group.id === rightTopGroupId;
const isTerminalGroup = group.id === rightBottomGroupId;
return (
<div className="flex items-center gap-0.5 pr-0.5">
{isCenterGroup && <CenterRightActions />}
{isRightTopGroup && <RightTopGroupActions />}
{isTerminalGroup && <TerminalGroupRightActions />}
<GroupSplitCloseActions {...props} />
</div>
);
}
function SidebarRightActions() {
const workspaceId = useAppStore((state) => state.workspaces.activeId);
const kanban = useAppStore((state) => state.kanban);
// Use kanban.workflowId (task context) not workflows.activeId so "All Workflows" isn't clobbered when viewing a task.
const workflowId = kanban.workflowId;
const activeTaskId = useAppStore((state) => state.tasks.activeTaskId);
const activeTaskTitle = useAppStore((state) => {
const id = state.tasks.activeTaskId;
if (!id) return "";
return state.kanban.tasks.find((t: { id: string }) => t.id === id)?.title ?? "";
});
const setActiveTask = useAppStore((state) => state.setActiveTask);
const setActiveSession = useAppStore((state) => state.setActiveSession);
const appStore = useAppStoreApi();
const steps = (kanban?.steps ?? []).map(
(s: {
id: string;
title: string;
color?: string;
events?: {
on_enter?: Array<{ type: string; config?: Record<string, unknown> }>;
on_turn_complete?: Array<{ type: string; config?: Record<string, unknown> }>;
};
}) => ({
id: s.id,
title: s.title,
color: s.color,
events: s.events,
}),
);
const handleTaskCreated = useCallback(
(task: Task, _mode: "create" | "edit", meta?: { taskSessionId?: string | null }) => {
const state = appStore.getState();
const oldSessionId = state.tasks.activeSessionId;
const oldEnvId = oldSessionId ? (state.environmentIdBySessionId[oldSessionId] ?? null) : null;
setActiveTask(task.id);
if (meta?.taskSessionId) {
setActiveSession(task.id, meta.taskSessionId);
const newEnvId = appStore.getState().environmentIdBySessionId[meta.taskSessionId] ?? null;
if (newEnvId) performLayoutSwitch(oldEnvId, newEnvId, meta.taskSessionId);
}
replaceTaskUrl(task.id);
},
[setActiveTask, setActiveSession, appStore],
);
return (
<div className="flex items-center gap-1 pr-2">
<NewTaskDropdown
workspaceId={workspaceId}
workflowId={workflowId}
steps={steps}
activeTaskId={activeTaskId}
activeTaskTitle={activeTaskTitle}
onTaskCreated={handleTaskCreated}
/>
</div>
);
}
function RightTopGroupActions() {
const toggleRightPanels = useDockviewStore((s) => s.toggleRightPanels);
return (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
className={RAW_HEADER_ACTION_BUTTON_CLASS}
onClick={toggleRightPanels}
aria-label="Hide right panels"
>
<IconLayoutSidebarRightCollapse className={HEADER_ICON_CLASS} />
</button>
</TooltipTrigger>
<TooltipContent>Hide right panels</TooltipContent>
</Tooltip>
);
}
function CenterRightActions() {
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
const repository = useAppStore((state) => {
if (!activeSessionId) return null;
const session = state.taskSessions.items[activeSessionId];
if (!session) return null;
const repoId = session.repository_id;
if (!repoId) return null;
const allRepos = Object.values(state.repositories.itemsByWorkspaceId).flat();
return allRepos.find((r) => r.id === repoId) ?? null;
});
const hasDevScript = Boolean(repository?.dev_script?.trim());
const addBrowserPanel = useDockviewStore((s) => s.addBrowserPanel);
const upsertProcessStatus = useAppStore((state) => state.upsertProcessStatus);
const setActiveProcess = useAppStore((state) => state.setActiveProcess);
const handleStartBrowser = useCallback(async () => {
addBrowserPanel();
if (hasDevScript && activeSessionId) {
try {
const resp = await startProcess(activeSessionId, { kind: "dev" });
if (resp?.process) {
upsertProcessStatus(mapProcessToStatus(resp.process));
setActiveProcess(resp.process.session_id, resp.process.id);
}
} catch {
// Process may already be running
}
}
}, [addBrowserPanel, hasDevScript, activeSessionId, upsertProcessStatus, setActiveProcess]);
return (
<div className="flex items-center gap-1">
{/* Mode is shown in the chat input ModeSelector instead */}
{hasDevScript && (
<Button
size="sm"
variant="ghost"
className={HEADER_ACTION_BUTTON_CLASS}
onClick={handleStartBrowser}
aria-label="Open browser preview"
title="Open browser preview"
>
<IconDeviceDesktop className={HEADER_ICON_CLASS} />
</Button>
)}
</div>
);
}
function TerminalGroupRightActions() {
const environmentId = useEnvironmentId();
const repositoryId = useAppStore((state) => {
const sessionId = state.tasks.activeSessionId;
if (!sessionId) return null;
return state.taskSessions.items[sessionId]?.repository_id ?? null;
});
const hasDevScript = Boolean(useActiveSessionDevScript());
const { scripts } = useRepositoryScripts(repositoryId);
const rightBottomGroupId = useDockviewStore((s) => s.rightBottomGroupId);
if (scripts.length === 0 && !hasDevScript) return null;
return (
<>
<TerminalScriptsDropdown
scripts={scripts}
environmentId={environmentId}
rightBottomGroupId={rightBottomGroupId}
/>
<TerminalDevPreviewButton
environmentId={environmentId}
rightBottomGroupId={rightBottomGroupId}
visible={hasDevScript}
/>
</>
);
}
type TerminalScriptsDropdownProps = {
scripts: ReturnType<typeof useRepositoryScripts>["scripts"];
environmentId: string | null;
rightBottomGroupId: string | null;
};
function TerminalScriptsDropdown({
scripts,
environmentId,
rightBottomGroupId,
}: TerminalScriptsDropdownProps) {
const addTerminalPanel = useDockviewStore((s) => s.addTerminalPanel);
const handleRunScript = useCallback(
async (scriptId: string) => {
if (!environmentId) return;
try {
const result = await createUserShell(environmentId, { scriptId });
addTerminalPanel(result.terminalId, rightBottomGroupId ?? undefined, environmentId);
} catch (error) {
console.error("Failed to run script:", error);
}
},
[environmentId, addTerminalPanel, rightBottomGroupId],
);
if (scripts.length === 0) return null;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
size="sm"
variant="ghost"
className={HEADER_ACTION_BUTTON_CLASS}
aria-label="Run script"
title="Run script"
>
<IconPlayerPlay className={HEADER_ICON_CLASS} />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-[min(18rem,calc(100vw-1rem))]">
{scripts.map((script) => (
<DropdownMenuItem
key={script.id}
onClick={() => handleRunScript(script.id)}
className={`${MENU_ITEM_CLASS} grid grid-cols-[auto_minmax(0,1fr)] items-start gap-x-2 gap-y-0 py-1.5`}
>
<IconTerminal2 className="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span className="min-w-0 overflow-hidden">
<span className="block truncate leading-4" title={script.name}>
{script.name}
</span>
<span
className="mt-0.5 block truncate font-mono text-[10px] leading-3 text-muted-foreground"
title={script.command}
>
{script.command}
</span>
</span>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
);
}
type TerminalDevPreviewButtonProps = {
environmentId: string | null;
rightBottomGroupId: string | null;
visible: boolean;
};
function TerminalDevPreviewButton({
environmentId,
rightBottomGroupId,
visible,
}: TerminalDevPreviewButtonProps) {
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
const addBrowserPanel = useDockviewStore((s) => s.addBrowserPanel);
const addTerminalPanel = useDockviewStore((s) => s.addTerminalPanel);
const upsertProcessStatus = useAppStore((state) => state.upsertProcessStatus);
const setActiveProcess = useAppStore((state) => state.setActiveProcess);
const handleStartPreview = useCallback(async () => {
if (!activeSessionId || !environmentId) return;
addBrowserPanel();
try {
const resp = await startProcess(activeSessionId, { kind: "dev" });
if (resp?.process) {
upsertProcessStatus(mapProcessToStatus(resp.process));
setActiveProcess(resp.process.session_id, resp.process.id);
}
} catch {
// Process may already be running
}
try {
const shell = await createUserShell(environmentId);
addTerminalPanel(shell.terminalId, rightBottomGroupId ?? undefined, environmentId);
} catch {
// Terminal creation is best-effort
}
}, [
activeSessionId,
environmentId,
addBrowserPanel,
upsertProcessStatus,
setActiveProcess,
addTerminalPanel,
rightBottomGroupId,
]);
if (!visible) return null;
return (
<Button
size="sm"
variant="ghost"
className={HEADER_ACTION_BUTTON_CLASS}
onClick={handleStartPreview}
aria-label="Start dev server preview"
title="Start dev server preview"
>
<IconDeviceDesktop className={HEADER_ICON_CLASS} />
</Button>
);
}