File size: 1,447 Bytes
834b620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * File     : frontend/src/components/ExportBackupButton.tsx (new)
 * Purpose  : one button that saves a project's backup file.
 * Consumers: App.tsx (project detail view).
 * Read-only: it never changes any project data.
 * Styles   : existing classes from index.css only (secondary-btn, locked-note, form-error).
 */
import { useState } from "react";
import { BackupApiError, downloadBackup } from "../api/backup";

interface Props {
  protocolId: string;
}

export function ExportBackupButton({ protocolId }: Props) {
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [done, setDone] = useState(false);

  async function handleClick() {
    setBusy(true);
    setError(null);
    setDone(false);
    try {
      await downloadBackup(protocolId);
      setDone(true);
    } catch (err) {
      setError(err instanceof BackupApiError ? err.message : "Backup failed. Please try again.");
    } finally {
      setBusy(false);
    }
  }

  return (
    <div style={{ marginBottom: 14 }}>
      <button type="button" className="secondary-btn" onClick={handleClick} disabled={busy}>
        {busy ? "Preparing backup…" : "Export project backup"}
      </button>
      {done && <div className="locked-note" role="status">Backup saved to your downloads.</div>}
      {error && <div className="form-error" role="alert" style={{ whiteSpace: "pre-line" }}>{error}</div>}
    </div>
  );
}