Spaces:
Running

WorkPilot-AI / web /src /components /inbox /undo-countdown.tsx
Aynkhn's picture
Deploy WorkPilot demo (part 3)
03608f2 verified
Raw History Blame Contribute Delete
2.4 kB
"use client";
import { TimerIcon, Undo2Icon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { api } from "@/lib/api";
import { formatDuration } from "@/lib/format";
import { useI18n } from "@/lib/i18n";
import { useNow } from "@/lib/use-now";
/** Queued T2 sends (Phase 7) wait out an undo window before they go. Shows the time left and
* lets the operator cancel. Renders nothing for items without ``send_after``. */
export function UndoCountdown({
approvalId,
sendAfter,
undoUrl,
onUndone,
}: {
approvalId: string;
sendAfter: string | null | undefined;
undoUrl?: string | null;
onUndone?: () => void;
}) {
const { t } = useI18n();
const now = useNow(1000);
const [state, setState] = useState<"idle" | "busy" | "undone" | "error">("idle");
if (!sendAfter) return null;
const due = new Date(sendAfter).getTime();
if (Number.isNaN(due)) return null;
const left = Math.ceil((due - now) / 1000);
if (state === "undone") {
return (
<p className="text-sm text-muted-foreground" data-testid="undo-done" role="status">
{t("inbox.undone")}
</p>
);
}
if (left <= 0) {
return (
<p className="text-sm text-muted-foreground" data-testid="undo-sent">
{t("inbox.sent")}
</p>
);
}
const undo = async () => {
setState("busy");
try {
await api(undoUrl ?? `/approvals/${approvalId}/undo`, { method: "POST" });
setState("undone");
onUndone?.();
} catch {
setState("error");
}
};
return (
<div
className="flex items-center justify-between gap-3 rounded-md border border-info/30 bg-info-soft px-3 py-2"
data-testid="undo-countdown"
>
<span className="flex items-center gap-2 text-sm text-info" aria-live="polite">
<TimerIcon className="size-4" aria-hidden />
<span className="tabular">{t("inbox.undoIn", { time: formatDuration(left) })}</span>
</span>
<Button size="sm" variant="outline" onClick={undo} disabled={state === "busy"}>
<Undo2Icon aria-hidden />
{t("inbox.undo")}
</Button>
{state === "error" && (
<span role="alert" className="text-xs text-destructive">
{t("common.error")}
</span>
)}
</div>
);
}