import { Download, Loader2, Upload } from "lucide-react"; import { useRef, useState } from "react"; import { useFileStore } from "@/stores/file-store"; function getToken(): string { return localStorage.getItem("stirling-token") || ""; } export function CompareSettings() { const { files, processing, error, setProcessing, setError, setProcessedUrl } = useFileStore(); const [secondFile, setSecondFile] = useState(null); const [similarity, setSimilarity] = useState(null); const [downloadUrl, setDownloadUrl] = useState(null); const secondInputRef = useRef(null); const handleProcess = async () => { if (files.length === 0 || !secondFile) return; setProcessing(true); setError(null); setDownloadUrl(null); setSimilarity(null); try { const formData = new FormData(); formData.append("file", files[0]); formData.append("file", secondFile); const res = await fetch("/api/v1/tools/compare", { method: "POST", headers: { Authorization: `Bearer ${getToken()}` }, body: formData, }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error || `Failed: ${res.status}`); } const result = await res.json(); setSimilarity(result.similarity); setDownloadUrl(result.downloadUrl); setProcessedUrl(result.downloadUrl); } catch (err) { setError(err instanceof Error ? err.message : "Comparison failed"); } finally { setProcessing(false); } }; const hasFile = files.length > 0; return (
setSecondFile(e.target.files?.[0] ?? null)} className="hidden" />
{error &&

{error}

} {similarity !== null && (

Similarity: {similarity.toFixed(1)}%

)} {downloadUrl && ( Download Diff Image )}
); }