import { Download, Loader2 } from "lucide-react"; import { useState } from "react"; import { useFileStore } from "@/stores/file-store"; function getToken(): string { return localStorage.getItem("stirling-token") || ""; } export function SvgToRasterSettings() { const { files, processing, error, setProcessing, setError, setProcessedUrl, setSizes, setJobId } = useFileStore(); const [width, setWidth] = useState(1024); const [height, setHeight] = useState(""); const [backgroundColor, setBackgroundColor] = useState("#00000000"); const [outputFormat, setOutputFormat] = useState<"png" | "jpg" | "webp">("png"); const [transparent, setTransparent] = useState(true); const [downloadUrl, setDownloadUrl] = useState(null); const [originalSize, setOriginalSize] = useState(null); const [processedSize, setProcessedSize] = useState(null); const handleProcess = async () => { if (files.length === 0) return; setProcessing(true); setError(null); setDownloadUrl(null); try { const formData = new FormData(); formData.append("file", files[0]); const settings: Record = { width, outputFormat, backgroundColor: transparent ? "#00000000" : backgroundColor, }; if (height) settings.height = Number(height); formData.append("settings", JSON.stringify(settings)); const res = await fetch("/api/v1/tools/svg-to-raster", { 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(); setJobId(result.jobId); setProcessedUrl(result.downloadUrl); setDownloadUrl(result.downloadUrl); setOriginalSize(result.originalSize); setProcessedSize(result.processedSize); setSizes(result.originalSize, result.processedSize); } catch (err) { setError(err instanceof Error ? err.message : "Conversion failed"); } finally { setProcessing(false); } }; const hasFile = files.length > 0; return (
setWidth(Number(e.target.value))} min={1} max={8192} className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground" />
setHeight(e.target.value)} placeholder="Auto" className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground" />
{!transparent && (
setBackgroundColor(e.target.value)} className="w-full mt-0.5 h-8 rounded border border-border" />
)} {error &&

{error}

} {originalSize != null && processedSize != null && (

SVG: {(originalSize / 1024).toFixed(1)} KB

Output: {(processedSize / 1024).toFixed(1)} KB

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