import { Download } from "lucide-react"; import { useState } from "react"; import { ProgressCard } from "@/components/common/progress-card"; import { useTranslation } from "@/contexts/i18n-context"; import { useToolProcessor } from "@/hooks/use-tool-processor"; import { format } from "@/lib/format"; import { useFileStore } from "@/stores/file-store"; type BgType = "color" | "gradient"; type OutputFormat = "png" | "webp"; export function BackgroundReplaceSettings() { const { t } = useTranslation(); const { files } = useFileStore(); const { processFiles, processAllFiles, processing, error, downloadUrl, progress } = useToolProcessor("background-replace"); const [backgroundType, setBackgroundType] = useState("color"); const [color, setColor] = useState("#ffffff"); const [gradientColor1, setGradientColor1] = useState("#ffffff"); const [gradientColor2, setGradientColor2] = useState("#000000"); const [gradientAngle, setGradientAngle] = useState(180); const [feather, setFeather] = useState(0); const [outputFormat, setOutputFormat] = useState("png"); const ts = t.toolSettings["background-replace"]; const hasFile = files.length > 0; const hasMultiple = files.length > 1; const handleProcess = () => { const settings = { backgroundType, color, ...(backgroundType === "gradient" && { gradientColor1, gradientColor2, gradientAngle, }), feather, format: outputFormat, }; if (hasMultiple) { processAllFiles(files, settings); } else { processFiles(files, settings); } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); handleProcess(); }; return (
{/* Background Type Toggle */}
Background Type
{(["color", "gradient"] as const).map((type) => ( ))}
{/* Solid Color Picker */} {backgroundType === "color" && (
setColor(e.target.value)} className="h-10 w-14 cursor-pointer rounded-md border border-border" /> { const v = e.target.value; if (/^#[0-9a-fA-F]{0,6}$/.test(v)) setColor(v); }} className="border-border bg-background w-28 rounded-md border px-3 py-2 font-mono text-sm" maxLength={7} />
)} {/* Gradient Controls */} {backgroundType === "gradient" && ( <>
setGradientColor1(e.target.value)} className="h-9 w-12 cursor-pointer rounded-md border border-border" /> { const v = e.target.value; if (/^#[0-9a-fA-F]{0,6}$/.test(v)) setGradientColor1(v); }} className="border-border bg-background w-full rounded-md border px-2 py-1.5 font-mono text-xs" maxLength={7} />
setGradientColor2(e.target.value)} className="h-9 w-12 cursor-pointer rounded-md border border-border" /> { const v = e.target.value; if (/^#[0-9a-fA-F]{0,6}$/.test(v)) setGradientColor2(v); }} className="border-border bg-background w-full rounded-md border px-2 py-1.5 font-mono text-xs" maxLength={7} />
setGradientAngle(Number(e.target.value))} className="w-full" />
)} {/* Edge Feather */}
setFeather(Number(e.target.value))} className="w-full" />
{/* Output Format Toggle */}
Output Format
{(["png", "webp"] as const).map((fmt) => ( ))}
{/* Progress / Submit */} {processing && progress ? ( ) : ( )} {/* Error */} {error && (
{error}
)} {/* Download */} {downloadUrl && ( {t.common.download} )} ); }