import { Download, Loader2 } from "lucide-react"; import { useState } from "react"; import { formatHeaders } from "@/lib/api"; import { useFileStore } from "@/stores/file-store"; type Direction = "horizontal" | "vertical" | "grid"; type ResizeMode = "fit" | "original" | "stretch" | "crop"; type Alignment = "start" | "center" | "end"; type OutputFormat = "png" | "jpeg" | "webp" | "avif" | "jxl"; export function StitchSettings() { const { files, processing, error, setProcessing, setError, setProcessedUrl, setSizes, setJobId } = useFileStore(); const [direction, setDirection] = useState("horizontal"); const [gridColumns, setGridColumns] = useState(3); const [resizeMode, setResizeMode] = useState("fit"); const [alignment, setAlignment] = useState("center"); const [gap, setGap] = useState(0); const [border, setBorder] = useState(0); const [cornerRadius, setCornerRadius] = useState(0); const [backgroundColor, setBackgroundColor] = useState("#FFFFFF"); const [format, setFormat] = useState("png"); const [quality, setQuality] = useState(90); const [downloadUrl, setDownloadUrl] = useState(null); const handleProcess = async () => { if (files.length < 2) return; setProcessing(true); setError(null); setDownloadUrl(null); try { const formData = new FormData(); for (const file of files) { formData.append("file", file); } formData.append( "settings", JSON.stringify({ direction, gridColumns, resizeMode, alignment, gap, border, cornerRadius, backgroundColor, format, quality, }), ); const res = await fetch("/api/v1/tools/stitch", { method: "POST", headers: formatHeaders(), 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); setSizes(result.originalSize, result.processedSize); } catch (err) { setError(err instanceof Error ? err.message : "Stitch failed"); } finally { setProcessing(false); } }; const hasEnoughFiles = files.length >= 2; return (
{/* Layout */}

Direction

{(["horizontal", "vertical", "grid"] as const).map((d) => ( ))}
{direction === "grid" && (
{gridColumns}
setGridColumns(Number(e.target.value))} className="w-full mt-1" />
)}
{/* Size Handling */}

Resize Mode

{(["fit", "original", "stretch", "crop"] as const).map((m) => ( ))}

Alignment

{(["start", "center", "end"] as const).map((a) => ( ))}
{/* Spacing & Border */}
{gap}px
setGap(Number(e.target.value))} className="w-full mt-1" />
{border}px
setBorder(Number(e.target.value))} className="w-full mt-1" />
{cornerRadius}px
setCornerRadius(Number(e.target.value))} className="w-full mt-1" />
setBackgroundColor(e.target.value)} className="w-full mt-0.5 h-8 rounded border border-border" />
{/* Output */}

Format

{(["png", "jpeg", "webp", "avif", "jxl"] as const).map((f) => ( ))}
{(format === "jpeg" || format === "webp" || format === "avif" || format === "jxl") && (
{quality}%
setQuality(Number(e.target.value))} className="w-full mt-1" />
)} {error &&

{error}

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