import { useState } from "react"; import { useFileStore } from "@/stores/file-store"; import { useToolProcessor } from "@/hooks/use-tool-processor"; import { Download, Loader2 } from "lucide-react"; const OUTPUT_FORMATS = ["jpg", "png", "webp", "avif", "tiff", "gif"] as const; const LOSSY_FORMATS = new Set(["jpg", "webp", "avif"]); export function ConvertSettings() { const { files } = useFileStore(); const { processFiles, processing, error, downloadUrl, originalSize, processedSize } = useToolProcessor("convert"); const [format, setFormat] = useState("png"); const [quality, setQuality] = useState(85); // Detect source format from filename const sourceFile = files[0]; const sourceExt = sourceFile ? sourceFile.name.split(".").pop()?.toLowerCase() || "unknown" : "none"; const isLossy = LOSSY_FORMATS.has(format); const handleProcess = () => { const settings: Record = { format }; if (isLossy) { settings.quality = quality; } processFiles(files, settings); }; const hasFile = files.length > 0; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (hasFile && !processing) handleProcess(); }; return (
{/* Source format */} {hasFile && (
{sourceExt}
)} {/* Target format */}
{/* Quality slider (lossy only) */} {isLossy && (
{quality}
setQuality(Number(e.target.value))} className="w-full mt-1" />
)} {/* Error */} {error &&

{error}

} {/* Size info */} {originalSize != null && processedSize != null && (

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

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

Savings:{" "} {originalSize > 0 ? ((1 - processedSize / originalSize) * 100).toFixed(1) : "0"} %

)} {/* Process */} {/* Download */} {downloadUrl && ( Download )}
); }