import { useState } from "react"; import { useFileStore } from "@/stores/file-store"; import { Loader2 } from "lucide-react"; function getToken(): string { return localStorage.getItem("stirling-token") || ""; } interface DuplicateGroup { files: Array<{ filename: string; similarity: number }>; } interface DuplicateResult { totalImages: number; duplicateGroups: DuplicateGroup[]; uniqueImages: number; } export function FindDuplicatesSettings() { const { files, processing, error, setProcessing, setError } = useFileStore(); const [result, setResult] = useState(null); const handleProcess = async () => { if (files.length < 2) return; setProcessing(true); setError(null); setResult(null); try { const formData = new FormData(); for (const file of files) { formData.append("file", file); } const res = await fetch("/api/v1/tools/find-duplicates", { 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 data: DuplicateResult = await res.json(); setResult(data); } catch (err) { setError(err instanceof Error ? err.message : "Detection failed"); } finally { setProcessing(false); } }; const hasFiles = files.length >= 2; return (

Upload 2 or more images to find near-duplicates using perceptual hashing.

{error &&

{error}

} {result && (

Total images: {result.totalImages}

Unique images: {result.uniqueImages}

Duplicate groups: {result.duplicateGroups.length}

{result.duplicateGroups.length === 0 ? (

No duplicates found.

) : ( result.duplicateGroups.map((group, gi) => (

Group {gi + 1}

{group.files.map((f, fi) => (
{f.filename} {f.similarity}%
))}
)) )}
)}
); }