import { useEffect, useState, type FormEvent } from "react"; import { Button, Input, cn } from "../ui/index.js"; import { ApiError, api, formatDate, type DirectoryUser, type Folder, type FolderPermission, } from "../lib/api.js"; import { useI18n } from "../lib/i18n.js"; import { Alert, Modal } from "./feedback.js"; import { TrashIcon } from "./icons.js"; const LEVELS = ["read", "write", "admin"] as const; const LEVEL_LABEL: Record = { read: "Lecture", write: "Écriture", admin: "Administration", }; const messageOf = (caught: unknown, fallback: string): string => caught instanceof ApiError ? caught.message : fallback; interface Target { type: "folder" | "file"; id: string; name: string; } export const CreateFolderDialog = ({ parentId, onClose, onCreated, }: { parentId: string | null; onClose: () => void; onCreated: () => void; }) => { const { t } = useI18n(); const [name, setName] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const submit = async (event: FormEvent) => { event.preventDefault(); setBusy(true); setError(null); try { await api.createFolder(name, parentId); onCreated(); } catch (caught) { setError(messageOf(caught, t("Création impossible."))); } finally { setBusy(false); } }; return (
void submit(event)}> setName(event.target.value)} /> {error ? {error} : null}
); }; export const RenameDialog = ({ target, onClose, onDone, }: { target: Target; onClose: () => void; onDone: () => void; }) => { const { t } = useI18n(); const [name, setName] = useState(target.name); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const submit = async (event: FormEvent) => { event.preventDefault(); setBusy(true); setError(null); try { if (target.type === "folder") await api.updateFolder(target.id, name); else await api.renameFile(target.id, name); onDone(); } catch (caught) { setError(messageOf(caught, t("Renommage impossible."))); } finally { setBusy(false); } }; return (
void submit(event)}> setName(event.target.value)} /> {error ? {error} : null}
); }; interface TreeEntry { id: string; label: string; } const loadTree = async (): Promise => { const entries: TreeEntry[] = []; const walk = async (parentId: string | null, prefix: string, depth: number): Promise => { if (depth > 6) return; const { folders } = await api.folders(parentId); for (const folder of folders) { entries.push({ id: folder.id, label: `${prefix}${folder.name}` }); await walk(folder.id, `${prefix}${folder.name} / `, depth + 1); } }; await walk(null, "", 0); return entries; }; export const MoveDialog = ({ target, onClose, onDone, }: { target: Target; onClose: () => void; onDone: () => void; }) => { const { t } = useI18n(); const [entries, setEntries] = useState(null); const [destination, setDestination] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); useEffect(() => { loadTree() .then(setEntries) .catch((caught: unknown) => setError(messageOf(caught, t("Chargement des dossiers impossible.")))); }, []); const submit = async (event: FormEvent) => { event.preventDefault(); setBusy(true); setError(null); const parentId = destination === "" ? null : destination; try { if (target.type === "folder") await api.moveFolder(target.id, parentId); else await api.moveFile(target.id, parentId); onDone(); } catch (caught) { setError(messageOf(caught, t("Déplacement impossible."))); } finally { setBusy(false); } }; return (
void submit(event)}>

{t("Destination de {name}.", { name: target.name })}

{entries === null && !error ? ( {t("Chargement des dossiers…")} ) : ( )} {error ? {error} : null}
); }; export const ShareDialog = ({ folder, onClose, }: { folder: Folder; onClose: () => void; }) => { const { t } = useI18n(); const [permissions, setPermissions] = useState(null); const [members, setMembers] = useState(null); const [email, setEmail] = useState(""); const [level, setLevel] = useState("read"); const [expiresAt, setExpiresAt] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const load = async () => { try { const [{ permissions: list }, { users }] = await Promise.all([ api.folderPermissions(folder.id), api.directory(), ]); setPermissions(list); setMembers(users); setError(null); } catch (caught) { setError(messageOf(caught, t("Chargement des partages impossible."))); setPermissions([]); setMembers([]); } }; useEffect(() => { void load(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [folder.id]); const candidates = (members ?? []).filter( (member) => !(permissions ?? []).some((permission) => permission.user_id === member.id), ); const grant = async (event: FormEvent) => { event.preventDefault(); setBusy(true); setError(null); try { const iso = expiresAt ? new Date(expiresAt).toISOString() : null; await api.grantPermission(folder.id, email, level, expiresAt ? iso : null); setEmail(""); setExpiresAt(""); await load(); } catch (caught) { setError(messageOf(caught, t("Partage impossible."))); } finally { setBusy(false); } }; const revoke = async (userId: string) => { setError(null); try { await api.revokePermission(folder.id, userId); await load(); } catch (caught) { setError(messageOf(caught, t("Révocation impossible."))); } }; return (
void grant(event)}> {members !== null && candidates.length === 0 && permissions !== null ? (

{t("Aucun autre membre à ajouter.")}

) : null}
setExpiresAt(event.target.value)} />
{error ? {error} : null}

{t("Accès accordés")}

{permissions === null ? ( {t("Chargement…")} ) : permissions.length === 0 ? (

{t("Aucun partage pour ce dossier.")}

) : (
    {permissions.map((permission) => (
  • {permission.first_name} {permission.last_name} {permission.email} ·{" "} {permission.expires_at ? t("{level} · jusqu'au {date}", { level: t(LEVEL_LABEL[permission.level] ?? permission.level), date: formatDate(permission.expires_at), }) : t(LEVEL_LABEL[permission.level] ?? permission.level)}
  • ))}
)}
); };