2026-03-25 09:27:12 +08:00
|
|
|
import { Download, Loader2 } from "lucide-react";
|
2026-03-22 04:20:54 +08:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useFileStore } from "@/stores/file-store";
|
|
|
|
|
|
|
|
|
|
function getToken(): string {
|
|
|
|
|
return localStorage.getItem("stirling-token") || "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function VectorizeSettings() {
|
2026-03-25 09:27:12 +08:00
|
|
|
const { files, processing, error, setProcessing, setError, setProcessedUrl, setSizes, setJobId } =
|
|
|
|
|
useFileStore();
|
2026-03-22 04:20:54 +08:00
|
|
|
const [colorMode, setColorMode] = useState<"bw" | "color">("bw");
|
|
|
|
|
const [threshold, setThreshold] = useState(128);
|
|
|
|
|
const [detail, setDetail] = useState<"low" | "medium" | "high">("medium");
|
|
|
|
|
const [downloadUrl, setDownloadUrl] = useState<string | null>(null);
|
|
|
|
|
const [originalSize, setOriginalSize] = useState<number | null>(null);
|
|
|
|
|
const [processedSize, setProcessedSize] = useState<number | null>(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]);
|
|
|
|
|
formData.append("settings", JSON.stringify({ colorMode, threshold, detail }));
|
|
|
|
|
|
|
|
|
|
const res = await fetch("/api/v1/tools/vectorize", {
|
|
|
|
|
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 : "Vectorization failed");
|
|
|
|
|
} finally {
|
|
|
|
|
setProcessing(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasFile = files.length > 0;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<div>
|
2026-03-26 01:10:13 +08:00
|
|
|
<p className="text-xs text-muted-foreground">Color Mode</p>
|
2026-03-22 04:20:54 +08:00
|
|
|
<div className="flex gap-1 mt-1">
|
|
|
|
|
<button
|
2026-03-26 01:10:13 +08:00
|
|
|
type="button"
|
2026-03-22 04:20:54 +08:00
|
|
|
onClick={() => setColorMode("bw")}
|
|
|
|
|
className={`flex-1 text-xs py-1.5 rounded ${colorMode === "bw" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"}`}
|
|
|
|
|
>
|
|
|
|
|
Black & White
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
2026-03-26 01:10:13 +08:00
|
|
|
type="button"
|
2026-03-22 04:20:54 +08:00
|
|
|
onClick={() => setColorMode("color")}
|
|
|
|
|
className={`flex-1 text-xs py-1.5 rounded ${colorMode === "color" ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground"}`}
|
|
|
|
|
>
|
|
|
|
|
Color
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<div className="flex justify-between items-center">
|
2026-03-26 01:10:13 +08:00
|
|
|
<label htmlFor="vectorize-threshold" className="text-xs text-muted-foreground">
|
|
|
|
|
Threshold
|
|
|
|
|
</label>
|
2026-03-22 04:20:54 +08:00
|
|
|
<span className="text-xs font-mono text-foreground">{threshold}</span>
|
|
|
|
|
</div>
|
2026-03-25 09:27:12 +08:00
|
|
|
<input
|
2026-03-26 01:10:13 +08:00
|
|
|
id="vectorize-threshold"
|
2026-03-25 09:27:12 +08:00
|
|
|
type="range"
|
|
|
|
|
min={0}
|
|
|
|
|
max={255}
|
|
|
|
|
value={threshold}
|
|
|
|
|
onChange={(e) => setThreshold(Number(e.target.value))}
|
|
|
|
|
className="w-full mt-1"
|
|
|
|
|
/>
|
2026-03-22 04:20:54 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
2026-03-26 01:10:13 +08:00
|
|
|
<label htmlFor="vectorize-detail" className="text-xs text-muted-foreground">
|
|
|
|
|
Detail Level
|
|
|
|
|
</label>
|
2026-03-22 04:20:54 +08:00
|
|
|
<select
|
2026-03-26 01:10:13 +08:00
|
|
|
id="vectorize-detail"
|
2026-03-22 04:20:54 +08:00
|
|
|
value={detail}
|
|
|
|
|
onChange={(e) => setDetail(e.target.value as "low" | "medium" | "high")}
|
|
|
|
|
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
|
|
|
|
|
>
|
|
|
|
|
<option value="low">Low (simpler, smaller SVG)</option>
|
|
|
|
|
<option value="medium">Medium</option>
|
|
|
|
|
<option value="high">High (detailed, larger SVG)</option>
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && <p className="text-xs text-red-500">{error}</p>}
|
|
|
|
|
|
|
|
|
|
{originalSize != null && processedSize != null && (
|
|
|
|
|
<div className="text-xs text-muted-foreground space-y-0.5">
|
|
|
|
|
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
|
|
|
|
|
<p>SVG: {(processedSize / 1024).toFixed(1)} KB</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<button
|
2026-03-26 01:10:13 +08:00
|
|
|
type="button"
|
2026-03-28 11:19:09 +08:00
|
|
|
data-testid="vectorize-submit"
|
2026-03-22 04:20:54 +08:00
|
|
|
onClick={handleProcess}
|
|
|
|
|
disabled={!hasFile || processing}
|
|
|
|
|
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
|
|
|
|
>
|
|
|
|
|
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
|
|
|
|
|
{processing ? "Vectorizing..." : "Vectorize"}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
{downloadUrl && (
|
2026-03-25 09:27:12 +08:00
|
|
|
<a
|
|
|
|
|
href={downloadUrl}
|
|
|
|
|
download
|
2026-03-28 11:19:09 +08:00
|
|
|
data-testid="vectorize-download"
|
2026-03-25 09:27:12 +08:00
|
|
|
className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5"
|
|
|
|
|
>
|
2026-03-22 04:20:54 +08:00
|
|
|
<Download className="h-4 w-4" />
|
|
|
|
|
Download SVG
|
|
|
|
|
</a>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|