3422794932
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
290 lines
8.8 KiB
TypeScript
290 lines
8.8 KiB
TypeScript
"use client";
|
|
|
|
import { memo, useMemo, useRef, useState } from "react";
|
|
import { cn } from "@kandev/ui/lib/utils";
|
|
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@kandev/ui/hover-card";
|
|
import { Button } from "@kandev/ui/button";
|
|
import { IconAdjustments, IconArrowRight } from "@tabler/icons-react";
|
|
import type { WorkflowMoveEntryOptions } from "@/lib/api/domains/kanban-api";
|
|
import {
|
|
WorkflowMoveOptionsFields,
|
|
useWorkflowMoveOptionsForm,
|
|
workflowMoveOptionsPayload,
|
|
} from "./workflow-move-options";
|
|
import { StepCapabilityIcons } from "@/components/step-capability-icons";
|
|
import { useToolbarCollapsed } from "@/hooks/use-toolbar-collapsed";
|
|
import {
|
|
usePresentationToken,
|
|
useWorkflowStepMove,
|
|
} from "@/hooks/domains/kanban/use-workflow-step-move";
|
|
import { sortWorkflowStepsByPosition } from "@/lib/kanban/workflow-step-order";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
MinimalWorkflowStepper,
|
|
StepCircleIndicator,
|
|
canMoveToStep,
|
|
getStepLabelClass,
|
|
type WorkflowStepperStep,
|
|
} from "./workflow-step-disclosure";
|
|
|
|
type Step = WorkflowStepperStep;
|
|
|
|
type WorkflowStepperProps = {
|
|
steps: Step[];
|
|
currentStepId: string | null;
|
|
taskId?: string | null;
|
|
workflowId?: string | null;
|
|
isArchived?: boolean;
|
|
onMoveStart?: () => void;
|
|
onMoveError?: (error: unknown) => void;
|
|
};
|
|
|
|
const WorkflowStepper = memo(function WorkflowStepper({
|
|
steps,
|
|
currentStepId,
|
|
taskId,
|
|
workflowId,
|
|
isArchived,
|
|
onMoveStart,
|
|
onMoveError,
|
|
}: WorkflowStepperProps) {
|
|
const { t } = useTranslation();
|
|
// The task route's continuous presentation of this task: a navigation away
|
|
// and back changes taskId and back, which must invalidate a request left
|
|
// over from the earlier presentation the same way a preview close-and-reopen
|
|
// does for the kanban preview header.
|
|
const presentationToken = usePresentationToken(taskId ?? null);
|
|
const { movingToStepId, handleMove } = useWorkflowStepMove({
|
|
taskId,
|
|
workflowId,
|
|
presentationToken,
|
|
onMoveStart,
|
|
onMoveError,
|
|
});
|
|
|
|
const sortedSteps = useMemo(() => sortWorkflowStepsByPosition(steps), [steps]);
|
|
|
|
const currentIndex = useMemo(
|
|
() => sortedSteps.findIndex((s) => s.id === currentStepId),
|
|
[sortedSteps, currentStepId],
|
|
);
|
|
|
|
// Collapse to a minimal view when the full stepper can't fit (w-full keeps the measurement track-driven).
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const isCollapsed = useToolbarCollapsed(containerRef);
|
|
|
|
if (sortedSteps.length === 0) return null;
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
data-testid="workflow-stepper"
|
|
className="flex w-full min-w-0 items-center justify-center gap-0 overflow-hidden"
|
|
>
|
|
{isCollapsed ? (
|
|
<MinimalWorkflowStepper
|
|
sortedSteps={sortedSteps}
|
|
currentIndex={currentIndex}
|
|
isArchived={isArchived}
|
|
taskId={taskId}
|
|
workflowId={workflowId}
|
|
movingToStepId={movingToStepId}
|
|
onMove={handleMove}
|
|
/>
|
|
) : (
|
|
<>
|
|
<div className="flex items-center gap-0">
|
|
{sortedSteps.map((step, index) => (
|
|
<WorkflowStepItem
|
|
key={step.id}
|
|
step={step}
|
|
index={index}
|
|
currentIndex={currentIndex}
|
|
isArchived={isArchived}
|
|
taskId={taskId}
|
|
workflowId={workflowId}
|
|
movingToStepId={movingToStepId}
|
|
onMove={handleMove}
|
|
/>
|
|
))}
|
|
</div>
|
|
{isArchived && (
|
|
<>
|
|
<div className="h-px w-6 shrink-0 bg-border" />
|
|
<span className="text-[11px] font-medium text-amber-500 bg-amber-500/15 px-2 py-0.5 rounded-md whitespace-nowrap">
|
|
{t("task:filterDimensionArchived")}
|
|
</span>
|
|
</>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
});
|
|
|
|
/** Individual step in the workflow stepper */
|
|
function WorkflowStepItem({
|
|
step,
|
|
index,
|
|
currentIndex,
|
|
isArchived,
|
|
taskId,
|
|
workflowId,
|
|
movingToStepId,
|
|
onMove,
|
|
}: {
|
|
step: Step;
|
|
index: number;
|
|
currentIndex: number;
|
|
isArchived?: boolean;
|
|
taskId?: string | null;
|
|
workflowId?: string | null;
|
|
movingToStepId: string | null;
|
|
onMove: (stepId: string, entryOptions?: WorkflowMoveEntryOptions) => Promise<boolean>;
|
|
}) {
|
|
const isCompleted = !isArchived && currentIndex >= 0 && index < currentIndex;
|
|
const isCurrent = !isArchived && index === currentIndex;
|
|
const isAdjacent =
|
|
currentIndex >= 0 && (index === currentIndex - 1 || index === currentIndex + 1);
|
|
const canMove = canMoveToStep({
|
|
isArchived,
|
|
isCurrent,
|
|
taskId,
|
|
workflowId,
|
|
isAdjacent,
|
|
allowManualMove: step.allow_manual_move,
|
|
});
|
|
|
|
return (
|
|
<div className="flex items-center">
|
|
{index > 0 && <StepConnector isActive={isCompleted || isCurrent} />}
|
|
<HoverCard openDelay={200} closeDelay={100}>
|
|
<HoverCardTrigger asChild>
|
|
<div
|
|
data-testid={`workflow-step-${step.name}`}
|
|
aria-current={isCurrent ? "step" : undefined}
|
|
className={cn(
|
|
"flex items-center gap-1.5 rounded-md px-2 py-0.5 text-xs whitespace-nowrap transition-colors cursor-default",
|
|
isCurrent ? "bg-muted/40" : "hover:bg-muted/30",
|
|
)}
|
|
>
|
|
<StepCircleIndicator isCurrent={isCurrent} isCompleted={isCompleted} />
|
|
<span className={cn("text-xs leading-none", getStepLabelClass(isCurrent, isCompleted))}>
|
|
{step.name}
|
|
</span>
|
|
</div>
|
|
</HoverCardTrigger>
|
|
<StepHoverContent
|
|
step={step}
|
|
isCurrent={isCurrent}
|
|
canMove={canMove}
|
|
isMoving={movingToStepId === step.id}
|
|
onMove={onMove}
|
|
/>
|
|
</HoverCard>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Connector line between steps */
|
|
function StepConnector({ isActive }: { isActive: boolean }) {
|
|
return (
|
|
<div className={cn("h-px w-6 shrink-0", isActive ? "bg-muted-foreground/40" : "bg-border")} />
|
|
);
|
|
}
|
|
|
|
/** Hover content for a workflow step */
|
|
function StepHoverContent({
|
|
step,
|
|
isCurrent,
|
|
canMove,
|
|
isMoving,
|
|
onMove,
|
|
}: {
|
|
step: Step;
|
|
isCurrent: boolean;
|
|
canMove: boolean;
|
|
isMoving: boolean;
|
|
onMove: (stepId: string, entryOptions?: WorkflowMoveEntryOptions) => Promise<boolean>;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<HoverCardContent
|
|
side="bottom"
|
|
align="center"
|
|
data-testid="workflow-step-popover"
|
|
className="p-1.5 flex flex-col gap-1.5 items-center w-auto min-w-28 max-w-[calc(100vw-1rem)]"
|
|
>
|
|
{canMove && <StepMoveControls step={step} isMoving={isMoving} onMove={onMove} />}
|
|
{isCurrent && (
|
|
<div className="text-[11px] text-muted-foreground">{t("task:currentStep")}</div>
|
|
)}
|
|
<StepCapabilityIcons events={step.events} agentProfileId={step.agent_profile_id} />
|
|
</HoverCardContent>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Move button plus an opt-in inline options draft. The options stay hidden
|
|
* until the user reveals them, so a quick hover-and-click keeps the original
|
|
* zero-config move; a revealed, filled draft rides along as one-shot
|
|
* entry_options. Rendered only while the hover card is open, so the draft
|
|
* hook subscribes lazily and resets whenever the pointer leaves the step.
|
|
*/
|
|
function StepMoveControls({
|
|
step,
|
|
isMoving,
|
|
onMove,
|
|
}: {
|
|
step: Step;
|
|
isMoving: boolean;
|
|
onMove: (stepId: string, entryOptions?: WorkflowMoveEntryOptions) => Promise<boolean>;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const [showOptions, setShowOptions] = useState(false);
|
|
const { draft, patchDraft } = useWorkflowMoveOptionsForm();
|
|
|
|
return (
|
|
<div className="flex w-full flex-col items-stretch gap-1.5">
|
|
<Button
|
|
size="sm"
|
|
variant="default"
|
|
className="cursor-pointer text-xs h-6 px-2.5 rounded-sm"
|
|
disabled={isMoving}
|
|
onClick={() => void onMove(step.id, workflowMoveOptionsPayload(draft))}
|
|
data-testid="workflow-step-move-here"
|
|
>
|
|
<IconArrowRight className="h-3 w-3" />
|
|
{isMoving ? t("task:moving") : t("task:moveHere")}
|
|
</Button>
|
|
{showOptions ? (
|
|
<div
|
|
className="w-64 max-w-[calc(100vw-2rem)]"
|
|
onKeyDown={(event) => event.stopPropagation()}
|
|
>
|
|
<WorkflowMoveOptionsFields
|
|
draft={draft}
|
|
onDraftChange={patchDraft}
|
|
isTouchSurface={false}
|
|
instructionsRows={3}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className="cursor-pointer text-xs h-6 px-2.5 rounded-sm text-muted-foreground"
|
|
onClick={() => setShowOptions(true)}
|
|
data-testid="workflow-step-move-options-trigger"
|
|
>
|
|
<IconAdjustments className="h-3 w-3" />
|
|
{t("task:workflowMoveOptions")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { WorkflowStepper };
|
|
export type { WorkflowStepperStep } from "./workflow-step-disclosure";
|