import { Download } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { ProgressCard } from "@/components/common/progress-card"; import { useToolProcessor } from "@/hooks/use-tool-processor"; import { useFileStore } from "@/stores/file-store"; const EXTEND_PRESETS = [ { label: "16:9", aspect: 16 / 9 }, { label: "1:1", aspect: 1 }, { label: "4:3", aspect: 4 / 3 }, { label: "3:2", aspect: 3 / 2 }, { label: "9:16", aspect: 9 / 16 }, { label: "4:5", aspect: 4 / 5 }, ]; export function ContentAwareCropSettings() { const { files } = useFileStore(); const { processFiles, processAllFiles, processing, error, downloadUrl, progress } = useToolProcessor("content-aware-crop"); const [extendTop, setExtendTop] = useState(0); const [extendRight, setExtendRight] = useState(0); const [extendBottom, setExtendBottom] = useState(0); const [extendLeft, setExtendLeft] = useState(0); const [imgDimensions, setImgDimensions] = useState<{ width: number; height: number } | null>( null, ); const firstFile = files[0]; useEffect(() => { if (!firstFile) { setImgDimensions(null); return; } const url = URL.createObjectURL(firstFile); const img = new Image(); img.onload = () => setImgDimensions({ width: img.naturalWidth, height: img.naturalHeight }); img.src = url; return () => URL.revokeObjectURL(url); }, [firstFile]); const handleExtendPreset = useCallback( (targetAspect: number) => { if (!imgDimensions) return; const { width: w, height: h } = imgDimensions; const currentAspect = w / h; let top = 0; let right = 0; let bottom = 0; let left = 0; if (targetAspect > currentAspect) { const newWidth = Math.round(h * targetAspect); const extra = newWidth - w; left = Math.round(extra / 2); right = extra - left; } else { const newHeight = Math.round(w / targetAspect); const extra = newHeight - h; top = Math.round(extra / 2); bottom = extra - top; } setExtendTop(top); setExtendRight(right); setExtendBottom(bottom); setExtendLeft(left); }, [imgDimensions], ); const hasFile = files.length > 0; const hasExtension = extendTop > 0 || extendRight > 0 || extendBottom > 0 || extendLeft > 0; const handleProcess = () => { const settings = { extendTop, extendRight, extendBottom, extendLeft }; if (files.length > 1) { processAllFiles(files, settings); } else { processFiles(files, settings); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (hasFile && hasExtension && !processing) handleProcess(); }; return (
); }