"use client"; import { Button } from "@kandev/ui/button"; import { cn } from "@kandev/ui/lib/utils"; import { IconInfoCircle, IconLoader2, IconRefresh } from "@tabler/icons-react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import type { WorkflowMovePreviewApplicability, WorkflowMovePreviewResponse } from "@/lib/api"; import type { WorkflowMovePreviewState } from "@/hooks/domains/kanban/use-workflow-move-preview"; type Translation = ReturnType["t"]; const UNKNOWN_PREVIEW_LABEL_KEY = "task:workflowMovePreviewUnknown"; // i18n-exempt: these are closed backend field and value codes used to select // localized preview copy, never user-facing text. const PREVIEW_FIELD_TRANSLATION_KEYS: Record = { mode: "workflowMovePreviewFieldMode", step_prompt: "workflowMovePreviewFieldStepPrompt", reasoning_effort: "workflowMovePreviewFieldReasoningEffort", verbosity: "workflowMovePreviewFieldVerbosity", }; // i18n-exempt: these are closed backend setting values used to select // localized preview copy, never user-facing text. const PREVIEW_VALUE_TRANSLATION_KEYS: Record> = { step_prompt: { configured: "workflowMovePreviewValueConfigured", skipped: "workflowMovePreviewValueSkipped", }, mode: { default: "workflowMovePreviewValueModeDefault", plan: "workflowMovePreviewValueModePlan", }, reasoning_effort: { low: "workflowMovePreviewValueLow", medium: "workflowMovePreviewValueMedium", high: "workflowMovePreviewValueHigh", max: "workflowMovePreviewValueMax", }, verbosity: { low: "workflowMovePreviewValueLow", medium: "workflowMovePreviewValueMedium", high: "workflowMovePreviewValueHigh", }, }; // i18n-exempt: these are closed backend diagnostic codes used to select // localized preview copy, never user-facing text. const PREVIEW_NOTICE_TRANSLATION_KEYS: Record = { retained_model_override: "workflowMovePreviewNoticeRetainedOverride", missing_original_snapshot: "workflowMovePreviewNoticeMissingOriginal", ambiguous_session_configuration: "workflowMovePreviewNoticeAmbiguous", conflicting_session_configuration: "workflowMovePreviewNoticeConflicting", session_configuration_skipped: "workflowMovePreviewNoticeInapplicable", session_configuration_unavailable: "workflowMovePreviewNoticeConfigurationUnavailable", invalid_session_configuration: "workflowMovePreviewNoticeInvalidConfiguration", target_unavailable: "workflowMovePreviewNoticeTargetUnavailable", profile_unavailable: "workflowMovePreviewNoticeProfileUnavailable", }; export type WorkflowMovePreviewDisclosureProps = { state: WorkflowMovePreviewState; isTouchSurface: boolean; className?: string; }; export function workflowMovePreviewChangeCount(preview: WorkflowMovePreviewResponse): number { const fieldChanges = (preview.changes ?? []).filter( (change) => change.key !== "model" && (change.applicability === "planned" || change.applicability === "unknown"), ).length; const contextReset = preview.context_reset && (preview.context_reset_state === "planned" || preview.context_reset_state === "unknown"); return fieldChanges + (contextReset ? 1 : 0); } function outcomeLabel(t: Translation, preview: WorkflowMovePreviewResponse): string { switch (preview.outcome) { case "reuse_current": return t("task:workflowMovePreviewReuseCurrent"); case "reuse_other": return preview.recipient?.session_name ? t("task:workflowMovePreviewReuseOtherNamed", { session: preview.recipient.session_name, }) : t("task:workflowMovePreviewReuseOther"); case "create_new": return t("task:workflowMovePreviewNewSession"); case "no_session": return t("task:workflowMovePreviewNoSession"); default: return t("task:workflowMovePreviewUnknownSession"); } } function modelSummaryParts(t: Translation, preview: WorkflowMovePreviewResponse) { const after = preview.model.after; const before = preview.model.before; const modelUnknown = !after.known || !after.label; const changeCount = workflowMovePreviewChangeCount(preview); if (modelUnknown) { return { label: t("task:workflowMovePreviewModelUnknown"), detail: undefined, changeCount, }; } let label: string; if (before.known && before.label && before.label !== after.label) { label = t("task:workflowMovePreviewModelChange", { before: before.label, after: after.label, }); } else if (preview.model.after_source === "override") { label = t("task:workflowMovePreviewModelRetainedOverride", { model: after.label }); } else { label = t("task:workflowMovePreviewModel", { model: after.label }); } return { label, changeCount }; } function applicabilityLabel(t: Translation, applicability: WorkflowMovePreviewApplicability) { switch (applicability) { case "planned": return t("task:workflowMovePreviewPlanned"); case "skipped": return t("task:workflowMovePreviewSkipped"); case "unchanged": return t("task:workflowMovePreviewUnchanged"); default: return t(UNKNOWN_PREVIEW_LABEL_KEY); } } function sourceDispositionLabel( t: Translation, value: WorkflowMovePreviewResponse["source_disposition"], ): string { switch (value) { case "keep": return t("task:workflowMovePreviewSourceKeep"); case "park": return t("task:workflowMovePreviewSourcePark"); case "complete": return t("task:workflowMovePreviewSourceComplete"); default: return t(UNKNOWN_PREVIEW_LABEL_KEY); } } function dispatchLabel(t: Translation, value: WorkflowMovePreviewResponse["dispatch"]): string { switch (value) { case "prompt": return t("task:workflowMovePreviewDispatchPrompt"); case "no_prompt": return t("task:workflowMovePreviewDispatchNoPrompt"); case "deferred": return t("task:workflowMovePreviewDispatchDeferred"); case "no_session": return t("task:workflowMovePreviewDispatchNoSession"); default: return t(UNKNOWN_PREVIEW_LABEL_KEY); } } function noticeLabel(t: Translation, code: string): string { const translationKey = PREVIEW_NOTICE_TRANSLATION_KEYS[code]; return t(`task:${translationKey ?? "workflowMovePreviewNoticeUnavailable"}`); } function changeFieldLabel(t: Translation, key: string, providerLabel: string): string { const translationKey = PREVIEW_FIELD_TRANSLATION_KEYS[key]; return translationKey ? t(`task:${translationKey}`) : providerLabel || t("task:workflowMovePreviewFieldOther"); } function changeValueLabel(t: Translation, key: string, value: string | undefined): string { if (!value) return t(UNKNOWN_PREVIEW_LABEL_KEY); const translationKey = PREVIEW_VALUE_TRANSLATION_KEYS[key]?.[value]; return translationKey ? t(`task:${translationKey}`) : value; } function displayValue(t: Translation, value: string | undefined, known = true): string { return known && value ? value : t(UNKNOWN_PREVIEW_LABEL_KEY); } function WorkflowMovePreviewDetails({ preview, t, }: { preview: WorkflowMovePreviewResponse; t: Translation; }) { const recipient = preview.recipient; return (
{t("task:workflowMovePreviewSession")} {displayValue(t, recipient?.session_name)}
{t("task:workflowMovePreviewProfile")} {displayValue(t, recipient?.profile_name)}
{t("task:workflowMovePreviewModelDetails")} {displayValue(t, preview.model.before.label, preview.model.before.known)}{" "} {t("task:workflowMovePreviewTo")}{" "} {displayValue(t, preview.model.after.label, preview.model.after.known)}
{(preview.changes ?? []).length > 0 && (
{t("task:workflowMovePreviewChanges")} {preview.changes?.map((change) => { const label = changeFieldLabel(t, change.key, change.label); return (
{label} {changeValueLabel(t, change.key, change.before)} {t("task:workflowMovePreviewTo")}{" "} {changeValueLabel(t, change.key, change.after)} ({applicabilityLabel(t, change.applicability)})
); })}
)} {preview.context_reset && (
{t("task:workflowMovePreviewContext")}:{" "} {t("task:workflowMovePreviewContextReset")} ( {applicabilityLabel(t, preview.context_reset_state)})
)}
{t("task:workflowMovePreviewSource")}: {sourceDispositionLabel(t, preview.source_disposition)}
{t("task:workflowMovePreviewDispatch")}:{" "} {dispatchLabel(t, preview.dispatch)}
{(preview.notices ?? []).length > 0 && (
{t("task:workflowMovePreviewNotes")} {preview.notices?.map((notice, index) => ( {noticeLabel(t, notice.code)} ))}
)} {t("task:workflowMovePreviewExecutionCheck")}
); } type CompactWorkflowMovePreviewProps = Omit & { expanded: boolean; }; export function CompactWorkflowMovePreview({ state, isTouchSurface, expanded, }: CompactWorkflowMovePreviewProps) { const { t } = useTranslation(); if (state.status === "idle") return null; if (state.status === "loading") { return (
{t("task:workflowMovePreviewChecking")}
); } if (state.status === "error" || !state.preview) { return (
{t("task:workflowMovePreviewUnavailable")}
); } const model = modelSummaryParts(t, state.preview); const summary = [ outcomeLabel(t, state.preview), model.label, ...(model.changeCount > 0 ? [t("task:workflowMovePreviewAdditionalChanges", { count: model.changeCount })] : []), ].join(" ยท "); return (
{summary}
{expanded && }
); } export function WorkflowMovePreviewDisclosure({ state, isTouchSurface, className, }: WorkflowMovePreviewDisclosureProps) { const { t } = useTranslation(); const [detailsOpen, setDetailsOpen] = useState(false); const retryButtonClass = isTouchSurface ? "min-h-11" : "h-7"; const detailsButtonClass = isTouchSurface ? "min-h-11 min-w-11 p-0" : "h-7 w-7 p-0"; if (state.status === "idle") return null; if (state.status === "loading") { return (
); } if (state.status === "error" || !state.preview) { return (
{t("task:workflowMovePreviewUnavailable")}
); } const preview = state.preview; const model = modelSummaryParts(t, preview); const firstLine = outcomeLabel(t, preview); return (
{firstLine}
{model.label} {model.changeCount > 0 && ( {t("task:workflowMovePreviewAdditionalChanges", { count: model.changeCount })} )}
{detailsOpen && }
); }