import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import type { TFunction } from "i18next"; import { Trash2 } from "lucide-react"; import { useTranslation } from "react-i18next"; import { currentLocale } from "@/i18n"; import { fmtDateTime } from "@/lib/format"; import type { SessionAutomationJob } from "@/lib/types"; interface DeleteConfirmProps { open: boolean; title: string; automations?: SessionAutomationJob[]; onCancel: () => void; onConfirm: () => void; } export function DeleteConfirm({ open, title, automations = [], onCancel, onConfirm, }: DeleteConfirmProps) { const { t } = useTranslation(); const locale = currentLocale(); const hasAutomations = automations.length > 0; const visibleAutomations = automations.slice(0, 4); const hiddenCount = Math.max(0, automations.length - visibleAutomations.length); return ( (!o ? onCancel() : undefined)}>
{t("deleteConfirm.title", { title })} {hasAutomations ? t("deleteConfirm.automationsDescription") : t("deleteConfirm.description")} {hasAutomations ? (
{visibleAutomations.map((job) => (
{job.name || job.id}
{formatAutomationSchedule(job, t, locale)} ยท {formatAutomationNextRun(job, t, locale)}
))} {hiddenCount > 0 ? (
{t("deleteConfirm.moreAutomations", { count: hiddenCount, })}
) : null}
) : null}
{t("deleteConfirm.cancel")} {hasAutomations ? t("deleteConfirm.confirmWithAutomations") : t("deleteConfirm.confirm")}
); } function formatAutomationSchedule( job: SessionAutomationJob, t: TFunction, locale: string, ): string { if (job.schedule.kind === "at" && job.schedule.at_ms) { return t("deleteConfirm.schedule.at", { time: fmtDateTime(job.schedule.at_ms, locale) }); } if (job.schedule.kind === "every" && job.schedule.every_ms) { return t("deleteConfirm.schedule.every", { duration: formatDuration(job.schedule.every_ms, locale), }); } if (job.schedule.kind === "cron" && job.schedule.expr) { return job.schedule.tz ? t("deleteConfirm.schedule.cronWithTz", { expr: job.schedule.expr, tz: job.schedule.tz, }) : t("deleteConfirm.schedule.cron", { expr: job.schedule.expr }); } if (job.schedule.kind === "local" || job.payload.kind === "local_trigger") { return t("deleteConfirm.schedule.local", { defaultValue: "Local trigger" }); } return t("deleteConfirm.schedule.unknown"); } function formatAutomationNextRun( job: SessionAutomationJob, t: TFunction, locale: string, ): string { if (!job.enabled) return t("deleteConfirm.next.disabled"); if (job.schedule.kind === "local" || job.payload.kind === "local_trigger") { return t("deleteConfirm.next.local", { defaultValue: "Waiting for trigger" }); } const next = job.state.next_run_at_ms; if (!next) return t("deleteConfirm.next.none"); return t("deleteConfirm.next.label", { time: fmtDateTime(next, locale) }); } function formatDuration(ms: number, locale: string): string { const units: Array<[Intl.NumberFormatOptions["unit"], number]> = [ ["day", 86_400_000], ["hour", 3_600_000], ["minute", 60_000], ["second", 1000], ]; for (const [unit, size] of units) { if (ms >= size && ms % size === 0) { return new Intl.NumberFormat(locale, { style: "unit", unit, unitDisplay: "long", maximumFractionDigits: 0, }).format(ms / size); } } return new Intl.NumberFormat(locale, { style: "unit", unit: "minute", unitDisplay: "long", maximumFractionDigits: 1, }).format(ms / 60_000); }