"use client"; import { useEffect, useRef, useState } from "react"; import { IconArrowsMaximize, IconX } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { useAppStore } from "@/components/state-provider"; import type { UseEnsureTaskSessionResult } from "@/hooks/domains/session/use-ensure-task-session"; import type { Task } from "./kanban-card"; import { PreviewSessionTabs } from "./task/preview-session-tabs"; import { TaskMoveErrorBanner } from "./task/task-move-error-banner"; import { MinimalWorkflowStepper, type DisclosureMove, type WorkflowStepperStep, } from "./task/workflow-step-disclosure"; import type { WorkflowStepProgress } from "@/hooks/domains/kanban/use-workflow-step-progress"; import { TaskActionsMenuTrigger } from "./task/task-actions-menu-trigger"; import { TaskActionsMenuDialogs } from "./task/task-actions-menu-dialogs"; import { useTaskActionsMenu, type TaskActionsMenuBoardRow } from "@/hooks/use-task-actions-menu"; import { useTaskCRUD } from "@/hooks/use-task-crud"; import { useTranslation } from "react-i18next"; interface TaskPreviewPanelProps { task: Task | null; sessionId?: string | null; ensureSession?: UseEnsureTaskSessionResult; onClose: () => void; onMaximize?: (task: Task) => void; onSessionChange?: (sessionId: string | null) => void; workflowSteps?: WorkflowStepperStep[]; currentStepId?: string | null; taskWorkflowId?: string | null; isArchived?: boolean; movingToStepId?: string | null; progressByStepId?: Readonly>; agentLabelsByProfileId?: Readonly>; onMoveStep?: DisclosureMove; onDisclosureOpenChange?: (open: boolean) => void; moveError?: unknown; /** Lets the enclosing surface skip its own Escape-close while this menu is open. */ onActionsMenuOpenChange?: (open: boolean) => void; } function buildBoardRow(task: Task | null): TaskActionsMenuBoardRow | null { if (!task) return null; return { id: task.id, title: task.title, description: task.description, workflowStepId: task.workflowStepId, state: task.state, priority: task.priority, repositoryId: task.repositoryId, repositories: task.repositories, parentTaskId: task.parentTaskId, primaryExecutorType: task.primaryExecutorType, workspaceMode: task.workspaceMode, }; } /** * Menu open state lives here (not inside the Radix primitive alone) so a * subject-identity change can force it closed without retargeting * (AC-TASKS-TASK-ACTIONS-MENU-004.5a), and so the panel's own Escape handler * can tell whether an open menu already owns the keypress * (AC-TASKS-TASK-ACTIONS-MENU-001.11). */ function useActionsMenuOpenState( taskId: string | null, onActionsMenuOpenChange?: (open: boolean) => void, ) { const [open, setOpenState] = useState(false); const setOpen = (next: boolean) => { setOpenState(next); onActionsMenuOpenChange?.(next); }; const prevTaskIdRef = useRef(taskId); useEffect(() => { if (prevTaskIdRef.current !== taskId) { prevTaskIdRef.current = taskId; setOpen(false); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [taskId]); return { open, setOpen }; } function usePreviewActionsMenu( task: Task | null, taskId: string | null, taskTitle: string, workspaceId: string | null, onClose: () => void, ) { const taskCRUD = useTaskCRUD(); const boardRow = buildBoardRow(task); const isArchiving = task ? taskCRUD.archivingTaskId === task.id : false; const isDeleting = task ? taskCRUD.deletingTaskId === task.id : false; const menu = useTaskActionsMenu({ taskId, taskTitle, workspaceId, // The board excludes archived tasks, so the preview panel never holds one. isArchived: false, boardRow, isArchiving, isDeleting, // `useTaskCRUD` only prunes `kanban.tasks`, so a task also present in // `kanbanMulti.snapshots` (multi-workflow board view) would otherwise // keep resolving via that snapshot until a WS lifecycle event catches // up, leaving the preview open on an archived/deleted task. Close it // directly on HTTP success instead of depending on cache completeness. onArchive: (opts) => (task ? taskCRUD.handleArchive(task, opts).then(onClose) : undefined), onDelete: (opts) => (task ? taskCRUD.handleDelete(task, opts).then(onClose) : undefined), }); return { menu, boardRow, isArchiving, isDeleting }; } interface PreviewPanelHeaderProps { task: Task | null; onClose: () => void; onMaximize?: (task: Task) => void; workflowSteps: WorkflowStepperStep[]; currentStepId: string | null; taskWorkflowId: string | null; isArchived: boolean; movingToStepId: string | null; progressByStepId?: Readonly>; agentLabelsByProfileId?: Readonly>; onMoveStep?: DisclosureMove; onDisclosureOpenChange?: (open: boolean) => void; menuEntries: ReturnType["entries"]; triggerRef: ReturnType["triggerRef"]; actionsMenuOpen: boolean; setActionsMenuOpen: (open: boolean) => void; } function PreviewPanelHeader({ task, onClose, onMaximize, workflowSteps, currentStepId, taskWorkflowId, isArchived, movingToStepId, progressByStepId, agentLabelsByProfileId, onMoveStep, onDisclosureOpenChange, menuEntries, triggerRef, actionsMenuOpen, setActionsMenuOpen, }: PreviewPanelHeaderProps) { const { t } = useTranslation(); const currentIndex = workflowSteps.findIndex((step) => step.id === currentStepId); const handleMoveStep = task && workflowSteps.length > 0 ? onMoveStep : undefined; return (

{task?.title ?? t("task:taskChat")}

{handleMoveStep && (
normally sizes to its content regardless of `display`, // so without an explicit width it overflows this shrink-capped // wrapper instead of shrinking to it (defeating truncation). className="min-w-0 max-w-[50%] shrink [&>button]:w-full" >
)}
{task && ( )} {onMaximize && task && ( )}
); } export function TaskPreviewPanel({ task, sessionId = null, ensureSession, onClose, onMaximize, onSessionChange, workflowSteps = [], currentStepId = null, taskWorkflowId = null, isArchived = false, movingToStepId = null, progressByStepId, agentLabelsByProfileId, onMoveStep, onDisclosureOpenChange, moveError = null, onActionsMenuOpenChange, }: TaskPreviewPanelProps) { const { t } = useTranslation(); const activeWorkspaceId = useAppStore((s) => s.workspaces.activeId); const workspaceId = activeWorkspaceId ?? null; const taskId = task?.id ?? null; const taskTitle = task?.title ?? ""; const { open: actionsMenuOpen, setOpen: setActionsMenuOpen } = useActionsMenuOpenState( taskId, onActionsMenuOpenChange, ); const { menu, boardRow, isArchiving, isDeleting } = usePreviewActionsMenu( task, taskId, taskTitle, workspaceId, onClose, ); return (
{moveError !== null && } {/* Content */}
{task ? ( ) : (
{t("task:selectATaskToStartChatting")}
)}
); }