diff --git a/src/components/cases/documents-tab.tsx b/src/components/cases/documents-tab.tsx index 26b44e4..73d6c26 100644 --- a/src/components/cases/documents-tab.tsx +++ b/src/components/cases/documents-tab.tsx @@ -255,6 +255,56 @@ export function CaseDocumentsTab({ caseId }: { caseId: string }) { load(); }; + const renameFolder = async (full: string) => { + const parts = full.split("/"); + const currentName = parts[parts.length - 1]; + const parent = parts.slice(0, -1).join("/"); + const raw = window.prompt(`Rename folder "${currentName}" to:`, currentName); + if (raw === null) return; + const trimmed = raw.trim(); + if (!trimmed || trimmed === currentName) return; + const safe = sanitizeName(trimmed); + const newFull = parent ? `${parent}/${safe}` : safe; + if (folders.includes(newFull)) { + toast.error("A folder with that name already exists here"); + return; + } + + // Update all document rows whose folder == full or starts with full + "/" + const affectedDocs = docs.filter( + (d) => (d.folder || "") === full || (d.folder || "").startsWith(`${full}/`), + ); + for (const d of affectedDocs) { + const next = d.folder === full ? newFull : `${newFull}${d.folder.slice(full.length)}`; + const { error } = await supabase.from("documents").update({ folder: next }).eq("id", d.id); + if (error) { toast.error("Rename failed", { description: error.message }); return; } + } + + // Update folder rows (this folder + all descendants) + const affectedFolders = folders.filter((f) => f === full || f.startsWith(`${full}/`)); + for (const f of affectedFolders) { + const next = f === full ? newFull : `${newFull}${f.slice(full.length)}`; + // Try update; if conflict (rare), delete old row instead + const { error } = await supabase + .from("document_folders") + .update({ path: next }) + .eq("case_id", caseId) + .eq("path", f); + if (error) { + await supabase.from("document_folders").delete().eq("case_id", caseId).eq("path", f); + await ensureFolder(next); + } + } + + // If we are currently inside the renamed folder, update the breadcrumb path + if (path === full || path.startsWith(`${full}/`)) { + setPath(newFull + path.slice(full.length)); + } + + toast.success("Folder renamed"); + load(); + }; + const crumbs = path ? path.split("/") : []; return (