import { useCallback, useEffect, useRef, useState } from "react"; import { Button, Card, Spinner } from "../ui/index.js"; import { ApiError, api, formatBytes, type Folder, type UploadedFile, } from "../lib/api.js"; import { useSession } from "../lib/session.js"; import { Alert, Badge, EmptyState, PageHeader } from "../components/feedback.js"; import { useConfirm } from "../components/confirm.js"; import { MediaViewer } from "../components/MediaViewer.js"; import { MediaThumb } from "../components/MediaThumb.js"; import { IconAction } from "../components/IconAction.js"; import { CreateFolderDialog, MoveDialog, RenameDialog, ShareDialog, } from "../components/dialogs.js"; import { ArrowLeftIcon, DownloadIcon, FolderIcon, MoveIcon, PenIcon, PlusIcon, ShareIcon, StarIcon, TrashIcon, UploadIcon, } from "../components/icons.js"; interface Crumb { id: string | null; name: string; } type Target = { type: "folder" | "file"; id: string; name: string }; type Dialog = | { kind: "create" } | { kind: "rename"; target: Target } | { kind: "move"; target: Target } | { kind: "share"; folder: Folder } | null; export const Browser = () => { const { user, refresh } = useSession(); const confirm = useConfirm(); const [path, setPath] = useState([{ id: null, name: "Racine" }]); const [folders, setFolders] = useState([]); const [files, setFiles] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [uploading, setUploading] = useState(false); const [removing, setRemoving] = useState(null); const [dialog, setDialog] = useState(null); const [viewer, setViewer] = useState(null); const fileInputRef = useRef(null); const current = path[path.length - 1] ?? { id: null, name: "Racine" }; const load = useCallback(async (parentId: string | null) => { setLoading(true); setError(null); try { const [folderResult, fileResult] = await Promise.all([ api.folders(parentId), api.files(parentId), ]); setFolders(folderResult.folders); setFiles(fileResult.files); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Chargement impossible."); setFolders([]); setFiles([]); } finally { setLoading(false); } }, []); useEffect(() => { void load(current.id); }, [current.id, load]); const reload = useCallback(() => load(current.id), [current.id, load]); const open = (folder: Folder) => { setPath((previous) => [...previous, { id: folder.id, name: folder.name }]); }; const goTo = (index: number) => setPath((previous) => previous.slice(0, index + 1)); const upload = async (selected: FileList | null) => { if (!selected || selected.length === 0) return; setUploading(true); setError(null); try { for (const file of Array.from(selected)) { await api.uploadFile(file, current.id); } void refresh(); await reload(); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Envoi impossible."); } finally { setUploading(false); if (fileInputRef.current) fileInputRef.current.value = ""; } }; const removeTarget = async (target: Target) => { const label = target.type === "folder" ? "ce dossier" : "ce fichier"; const ok = await confirm({ title: "Mettre à la corbeille", message: ( <> Placer {target.name} ({label}) dans la corbeille ? ), confirmLabel: "Corbeille", tone: "danger", }); if (!ok) return; setRemoving(target.id); setError(null); try { if (target.type === "folder") { await api.deleteFolder(target.id); } else { await api.deleteFile(target.id); void refresh(); } await reload(); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Suppression impossible."); } finally { setRemoving(null); } }; const toggleStar = async (file: UploadedFile) => { setError(null); try { const { file: updated } = await api.starFile(file.id); setFiles((previous) => previous.map((item) => (item.id === updated.id ? updated : item))); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Action impossible."); } }; const canWrite = user?.role_slug === "admin" || user?.role_slug === "user"; const empty = folders.length === 0 && files.length === 0; const quotaPercent = user && user.quota_bytes > 0 ? Math.min(100, Math.round((user.used_bytes / user.quota_bytes) * 100)) : 0; return (
{path.length > 1 ? ( ) : null}
} />
Espace utilisé {formatBytes(user?.used_bytes ?? 0)} sur {formatBytes(user?.quota_bytes ?? 0)}
{canWrite ? (
void upload(event.target.files)} />
) : null}
{error ? {error} : null} {loading ? (
) : empty ? ( } title="Emplacement vide" description="Créez un sous-dossier ou envoyez un fichier pour commencer." /> ) : (
{folders.length > 0 ? (

Dossiers

    {folders.map((folder) => (
  • {canWrite ? (
    } onClick={() => setDialog({ kind: "share", folder })} /> } onClick={() => setDialog({ kind: "rename", target: { type: "folder", id: folder.id, name: folder.name }, }) } /> } onClick={() => setDialog({ kind: "move", target: { type: "folder", id: folder.id, name: folder.name }, }) } /> } isLoading={removing === folder.id} tone="danger" onClick={() => void removeTarget({ type: "folder", id: folder.id, name: folder.name }) } />
    ) : null}
  • ))}
) : null} {files.length > 0 ? (

Fichiers

    {files.map((file, index) => (
  • {file.name} {formatBytes(file.size_bytes)} ·{" "} {new Date(file.created_at).toLocaleDateString("fr-FR")} } onClick={() => void toggleStar(file)} /> Télécharger {canWrite ? ( <> } onClick={() => setDialog({ kind: "rename", target: { type: "file", id: file.id, name: file.name }, }) } /> } onClick={() => setDialog({ kind: "move", target: { type: "file", id: file.id, name: file.name }, }) } /> } isLoading={removing === file.id} tone="danger" onClick={() => void removeTarget({ type: "file", id: file.id, name: file.name }) } /> ) : null}
  • ))}
) : null}
)} {dialog?.kind === "create" ? ( setDialog(null)} onCreated={() => { setDialog(null); void reload(); }} /> ) : null} {dialog?.kind === "rename" ? ( setDialog(null)} onDone={() => { setDialog(null); void reload(); }} /> ) : null} {dialog?.kind === "move" ? ( setDialog(null)} onDone={() => { setDialog(null); void reload(); }} /> ) : null} {dialog?.kind === "share" ? ( setDialog(null)} /> ) : null} {viewer !== null ? ( ({ id: item.id, name: item.name, mime_type: item.mime_type }))} startIndex={viewer} onClose={() => setViewer(null)} /> ) : null}
); };