import { Download, Loader2 } from "lucide-react"; import { useState } from "react"; import { useFileStore } from "@/stores/file-store"; function getToken(): string { return localStorage.getItem("stirling-token") || ""; } export function BulkRenameSettings() { const { files, processing, error, setProcessing, setError } = useFileStore(); const [pattern, setPattern] = useState("image-{{index}}"); const [startIndex, setStartIndex] = useState(1); const [downloadReady, setDownloadReady] = useState(false); const handleProcess = async () => { if (files.length === 0) return; setProcessing(true); setError(null); setDownloadReady(false); try { const formData = new FormData(); for (const file of files) { formData.append("file", file); } formData.append("settings", JSON.stringify({ pattern, startIndex })); const res = await fetch("/api/v1/tools/bulk-rename", { method: "POST", headers: { Authorization: `Bearer ${getToken()}` }, body: formData, }); if (!res.ok) { const text = await res.text(); throw new Error(text || `Failed: ${res.status}`); } const blob = await res.blob(); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "renamed.zip"; a.click(); URL.revokeObjectURL(url); setDownloadReady(true); } catch (err) { setError(err instanceof Error ? err.message : "Rename failed"); } finally { setProcessing(false); } }; const hasFiles = files.length > 0; // Preview names const previewNames = hasFiles ? files.slice(0, 5).map((f, i) => { const ext = f.name.includes(".") ? f.name.slice(f.name.lastIndexOf(".")) : ""; const idx = startIndex + i; const padded = String(idx).padStart(String(files.length + startIndex).length, "0"); return ( pattern .replace(/\{\{index\}\}/g, String(idx)) .replace(/\{\{padded\}\}/g, padded) .replace(/\{\{original\}\}/g, f.name.replace(ext, "")) + ext ); }) : []; return (
Variables: {"{{index}}"}, {"{{padded}}"}, {"{{original}}"}
Preview
... and {files.length - 5} more
)}{error}
} {downloadReady && (