File size: 2,348 Bytes
a9b163f
25e8d02
 
a9b163f
 
93acb05
25e8d02
 
 
a9b163f
 
93acb05
25e8d02
 
93acb05
 
a9b163f
93acb05
 
 
a9b163f
25e8d02
a9b163f
 
 
25e8d02
 
 
a9b163f
 
25e8d02
a9b163f
 
93acb05
25e8d02
 
 
 
a9b163f
 
93acb05
25e8d02
 
a9b163f
25e8d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9b163f
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { useState, useCallback } from "react";
import { ExportJob, ExportOptions } from "../types";
import { DEFAULT_EXPORT_OPTIONS } from "../constants";
import { exportClient } from "@/lib/api/export-client";
import { useInterpolationStore } from "@/store/interpolation-store";
import { useValidationStore } from "@/store/validation-store";

export function useExport() {
  const [options, setOptions] = useState<ExportOptions>(DEFAULT_EXPORT_OPTIONS);
  const [jobs, setJobs] = useState<ExportJob[]>([]);
  const interpolationStore = useInterpolationStore();
  const validationStore = useValidationStore();

  const startExport = useCallback(() => {
    let targetFileId = interpolationStore.outputFileId || validationStore.artifactId;
    
    if (!targetFileId) {
      const userInput = window.prompt("No generated file detected in session. Enter the File/Artifact ID you wish to download:");
      if (!userInput) return;
      targetFileId = userInput;
    }
    
    const newJobId = `job-${Date.now()}`;
    const downloadUrl = exportClient.getDownloadUrl(targetFileId);

    const newJob: ExportJob = {
      id: newJobId,
      format: options.format,
      status: "completed",
      progress: 100,
      createdAt: new Date().toISOString(),
      completedAt: new Date().toISOString(),
      downloadUrl: downloadUrl,
      fileSize: "Unknown" 
    };

    setJobs((prev) => [newJob, ...prev]);

    // Open download directly
    window.open(downloadUrl, "_blank");
  }, [options, interpolationStore.outputFileId, validationStore.artifactId]);

  const cancelExport = useCallback((jobId: string) => {
    // Not applicable since it's instant
  }, []);

  const updateOptions = useCallback((newOptions: Partial<ExportOptions>) => {
    setOptions((prev) => ({ ...prev, ...newOptions }));
  }, []);

  const activeJobs = jobs.filter((j) => j.status === "preparing" || j.status === "exporting");
  const historyJobs = jobs.filter((j) => j.status === "completed" || j.status === "error");

  const stats = {
    total: jobs.length,
    successful: jobs.filter(j => j.status === "completed").length,
    failed: jobs.filter(j => j.status === "error").length,
    queueLength: activeJobs.length,
  };

  return {
    options,
    jobs,
    activeJobs,
    historyJobs,
    stats,
    startExport,
    cancelExport,
    updateOptions,
  };
}