import { useEffect, useState } from "react"; import { AlertTriangle, LoaderCircle, RotateCcw, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import type { RecoveryState } from "@/lib/types"; interface RecoveryNoticeProps { state: RecoveryState; onContinue: () => Promise; onDismiss: () => Promise; } export function RecoveryNotice({ state, onContinue, onDismiss }: RecoveryNoticeProps) { const { t } = useTranslation(); const [pending, setPending] = useState<"continue" | "dismiss" | null>(null); const [error, setError] = useState(null); const [hiddenRecoveryId, setHiddenRecoveryId] = useState(null); useEffect(() => { // A continuation can be interrupted again with the same recovery ID. // Reveal the decision surface when the server returns to a waiting state. if (state.status === "awaiting_user" || state.status === "failed") { setHiddenRecoveryId(null); } }, [state.recovery_id, state.status]); if (state.status === "recovered" || hiddenRecoveryId === state.recovery_id) return null; const waiting = state.status === "awaiting_user" || state.status === "failed"; const contextUnavailable = state.can_continue === false; const title = state.status === "failed" ? t("recovery.failed", { defaultValue: "Task recovery failed" }) : waiting ? t("recovery.interrupted", { defaultValue: "Task interrupted" }) : t("recovery.resuming", { defaultValue: "Restoring interrupted task…" }); const detail = state.status === "failed" || contextUnavailable ? t("recovery.failedHelp", { defaultValue: "The saved task could not be restored safely. Review it before continuing.", }) : waiting ? t("recovery.review", { defaultValue: "Review the task before continuing. Tools will not be replayed automatically." }) : t("recovery.safeResume", { defaultValue: "Continuing from saved conversation context." }); const run = (action: "continue" | "dismiss") => { setPending(action); setError(null); // ``resuming`` is an internal transition, not another task for the user // to monitor. Hide the notice optimistically and only bring it back if // the explicit action is rejected. if (action === "continue") setHiddenRecoveryId(state.recovery_id); const operation = action === "continue" ? onContinue() : onDismiss(); void operation.catch(() => { if (action === "continue") setHiddenRecoveryId(null); setError(t("recovery.actionFailed", { defaultValue: "Recovery action failed. Try again." })); }).finally(() => setPending(null)); }; return (
{waiting ? ( ) : ( )}

{title}

{error ?? detail}

{waiting ? (
{!contextUnavailable ? ( ) : null}
) : null}
); }