"use client"; import { useTranslations } from "next-intl"; import { useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { adminFetch } from "@/lib/admin-fetch"; export function ImportAttachmentRecovery({ jobId, classname, disabled, onResumed, }: { jobId: string; classname: string; disabled: boolean; onResumed: () => Promise; }) { const t = useTranslations("admin.studio.importDiagnostics"); const lock = useRef(false); const [busy, setBusy] = useState(false); const [attachmentId, setAttachmentId] = useState(""); const [error, setError] = useState(""); async function attach(file: File) { if (disabled || lock.current) return; lock.current = true; setBusy(true); setError(""); setAttachmentId(""); try { const form = new FormData(); form.set("classname", classname); form.set("file", file); const response = await adminFetch("/api/admin/studio/import-attachment", { method: "POST", body: form, }); const data = await response.json(); if (!response.ok || typeof data.attachmentId !== "string") { setError( t(data.code === "mismatchedFile" ? "mismatchedFile" : "invalidFile"), ); return; } setAttachmentId(data.attachmentId); } catch { setError(t("network")); } finally { lock.current = false; setBusy(false); } } async function resume() { if (disabled || lock.current || !attachmentId) return; lock.current = true; setBusy(true); setError(""); try { const response = await adminFetch("/api/admin/studio/import-jobs", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: jobId, action: "retry", attachments: { [classname]: attachmentId }, }), }); if (!response.ok) { const data = await response.json(); const code = data.code === "alreadyStarted" ? "alreadyStarted" : data.code === "notEligible" ? "notEligible" : data.code === "invalidFile" ? "invalidFile" : "resumeError"; setError(t(code)); return; } setAttachmentId(""); await onResumed(); } catch { setError(t("network")); } finally { lock.current = false; setBusy(false); } } return (
{t("recover")}

{t("recoveryHint", { file: `${classname.split("*")[0]}.nitro` })}

{busy &&

{t("working")}

} {attachmentId && ( <>

{t("attached")}

)} {error && (

{error}

)}
); }