"use client";
import { useState } from "react";
import {
IconCheck,
IconCircleDashed,
IconChevronLeft,
IconChevronRight,
IconHelpCircle,
} from "@tabler/icons-react";
import { cn } from "@kandev/ui/lib/utils";
import { getTaskStateIcon } from "@/lib/ui/state-icons";
import type { Task } from "@/components/kanban-card";
import type { WorkflowStep } from "@/components/kanban-column";
import { useTaskPendingInput } from "@/hooks/use-task-pending-input";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { useTranslation } from "react-i18next";
type StepPhase = "past" | "current" | "future";
function isRunningState(state?: string): boolean {
return state === "IN_PROGRESS" || state === "SCHEDULING";
}
export type Graph2StepNodeProps = {
step: WorkflowStep;
phase: StepPhase;
task: Task;
hasPrev: boolean;
hasNext: boolean;
onMoveTask: (task: Task, targetStepId: string) => void;
onOpenTask?: (task: Task) => void;
prevStepId?: string;
nextStepId?: string;
prevStepTitle?: string;
nextStepTitle?: string;
prevStepHidden?: boolean;
nextStepHidden?: boolean;
isMoving?: boolean;
};
const NODE_CLASS =
"w-[130px] h-[36px] rounded-lg shrink-0 px-2.5 flex flex-col items-start justify-center";
/**
* A completed step: every step keeps its own labelled pill, matching the
* Kanban card's step run. Muted rather than green — the run's only color
* signal is the current step's accent border and the row's amber
* needs-attention edge; a third hue on every past step competed with both.
*/
function PastNode({ step }: { step: WorkflowStep }) {
return (
{step.title}
);
}
/** A not-yet-reached step: dashed to distinguish it from a completed one. */
function FutureNode({ step }: { step: WorkflowStep }) {
return (
{step.title}
);
}
/**
* The synthetic labelled marker rendered when a task has no resolvable
* current step: `workflowStepId` is empty, so it matches no displayed step.
* It carries fixed unassigned-step copy rather than a step title, and no move
* controls, there being no current step for either direction.
*/
export function Graph2UnassignedStepMarker() {
const { t } = useTranslation();
const label = t("kanban:pipelineUnassignedStep");
return (
{label}
);
}
function MoveButton({
direction,
isMoving,
label,
showTooltip = true,
onClick,
}: {
direction: "left" | "right";
isMoving?: boolean;
label: string;
showTooltip?: boolean;
onClick: (e: React.MouseEvent) => void;
}) {
const posClass = direction === "left" ? "-left-3" : "-right-3";
const Icon = direction === "left" ? IconChevronLeft : IconChevronRight;
const button = (
);
// The tooltip names the destination step. Keep the trigger wrapper for the
// default card/pipeline behavior, while allowing visible destinations to
// opt out when the caller already renders that name in the run.
if (!showTooltip) return button;
return (
{button}
{label}
);
}
export function Graph2StepNode({
step,
phase,
task,
hasPrev,
hasNext,
onMoveTask,
onOpenTask,
prevStepId,
nextStepId,
prevStepTitle,
nextStepTitle,
prevStepHidden,
nextStepHidden,
isMoving,
}: Graph2StepNodeProps) {
const { t } = useTranslation();
const [isHovered, setIsHovered] = useState(false);
const [isFocused, setIsFocused] = useState(false);
const pendingInput = useTaskPendingInput(task.primarySessionId, {
taskId: task.id,
taskPendingAction: task.taskPendingAction,
statusSummary: task.statusSummary,
primarySessionState: task.primarySessionState,
primarySessionPendingAction: task.primarySessionPendingAction,
});
if (phase === "past") return ;
if (phase === "future") return ;
// Current phase
const running = isRunningState(task.state);
const showMoveControls = isHovered || isFocused;
return (
setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onFocusCapture={() => setIsFocused(true)}
onBlurCapture={(event) => {
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {
setIsFocused(false);
}
}}
>
{showMoveControls && hasPrev && prevStepId && (
{
e.stopPropagation();
onMoveTask(task, prevStepId);
}}
/>
)}
{/* onOpenTask is not yet threaded through PipelineStepNodes, so this
onClick is inert today; an unhandled right-click still bubbles to
the row's own context menu, same as the title. */}
{showMoveControls && hasNext && nextStepId && (
{
e.stopPropagation();
onMoveTask(task, nextStepId);
}}
/>
)}