diff --git a/frontend/src/components/IconAction.tsx b/frontend/src/components/IconAction.tsx index ffd1d79..f1e3225 100644 --- a/frontend/src/components/IconAction.tsx +++ b/frontend/src/components/IconAction.tsx @@ -11,7 +11,7 @@ export const IconAction = ({ }: { label: string; icon: ReactNode; - onClick: () => void; + onClick: (event: React.MouseEvent) => void; isLoading?: boolean; tone?: "neutral" | "danger"; badge?: number; diff --git a/frontend/src/components/dialogs.tsx b/frontend/src/components/dialogs.tsx index fb67baf..b84cb65 100644 --- a/frontend/src/components/dialogs.tsx +++ b/frontend/src/components/dialogs.tsx @@ -157,14 +157,19 @@ const loadTree = async (): Promise => { export const MoveDialog = ({ target, + targets, onClose, onDone, }: { - target: Target; + target?: Target; + targets?: Target[]; onClose: () => void; onDone: () => void; }) => { const { t } = useI18n(); + const items = targets && targets.length > 0 ? targets : target ? [target] : []; + const single = items.length === 1 ? items[0] : null; + const title = single ? t("Déplacer") : t("Déplacer {count} éléments", { count: items.length }); const [entries, setEntries] = useState(null); const [destination, setDestination] = useState(""); const [error, setError] = useState(null); @@ -182,8 +187,10 @@ export const MoveDialog = ({ 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); + for (const item of items) { + if (item.type === "folder") await api.moveFolder(item.id, parentId); + else await api.moveFile(item.id, parentId); + } onDone(); } catch (caught) { setError(messageOf(caught, t("Déplacement impossible."))); @@ -193,10 +200,12 @@ export const MoveDialog = ({ }; return ( - +
void submit(event)}>

- {t("Destination de {name}.", { name: target.name })} + {single + ? t("Destination de {name}.", { name: single.name }) + : t("{count} éléments seront déplacés.", { count: items.length })}

{entries === null && !error ? ( {t("Chargement des dossiers…")} @@ -214,13 +223,13 @@ export const MoveDialog = ({ )} > - {(entries ?? []) - .filter((entry) => entry.id !== target.id) - .map((entry) => ( - - ))} + {(entries ?? []) + .filter((entry) => !items.some((item) => item.type === "folder" && item.id === entry.id)) + .map((entry) => ( + + ))} )} diff --git a/frontend/src/components/feedback.tsx b/frontend/src/components/feedback.tsx index 4e80b9d..33024d4 100644 --- a/frontend/src/components/feedback.tsx +++ b/frontend/src/components/feedback.tsx @@ -42,7 +42,7 @@ export const Modal = ({ role="dialog" aria-modal="true" aria-label={title} - className="animate-panel-in relative w-full max-w-md rounded-lg border border-border bg-surface p-4 shadow-lg" + className="animate-panel-in relative w-full max-w-lg sm:max-w-md rounded-lg border border-border bg-surface p-4 sm:p-5 shadow-lg" >

diff --git a/frontend/src/components/toast.tsx b/frontend/src/components/toast.tsx new file mode 100644 index 0000000..4a36596 --- /dev/null +++ b/frontend/src/components/toast.tsx @@ -0,0 +1,79 @@ +import { createContext, useCallback, useContext, useState, type ReactNode } from "react"; +import { cn } from "../ui/index.js"; +import { CloseIcon } from "./icons.js"; + +type ToastTone = "success" | "error" | "info"; + +interface Toast { + id: number; + message: string; + tone: ToastTone; + duration: number; +} + +interface ToastContextValue { + toast: (message: string, tone?: ToastTone, duration?: number) => void; + success: (message: string, duration?: number) => void; + error: (message: string, duration?: number) => void; + info: (message: string, duration?: number) => void; +} + +const ToastContext = createContext(null); + +export const useToast = () => { + const ctx = useContext(ToastContext); + if (!ctx) throw new Error("useToast must be used within ToastProvider"); + return ctx; +}; + +let toastId = 0; + +export const ToastProvider = ({ children }: { children: ReactNode }) => { + const [toasts, setToasts] = useState([]); + + const remove = useCallback((id: number) => { + setToasts((prev) => prev.filter((t) => t.id !== id)); + }, []); + + const toast = useCallback((message: string, tone: ToastTone = "info", duration = 4000) => { + const id = ++toastId; + setToasts((prev) => [...prev, { id, message, tone, duration }]); + if (duration > 0) { + setTimeout(() => remove(id), duration); + } + }, [remove]); + + const success = useCallback((message: string, duration = 4000) => toast(message, "success", duration), [toast]); + const error = useCallback((message: string, duration = 5000) => toast(message, "error", duration), [toast]); + const info = useCallback((message: string, duration = 4000) => toast(message, "info", duration), [toast]); + + return ( + + {children} +
+ {toasts.map((t) => ( +
+

{t.message}

+ +
+ ))} +
+
+ ); +}; diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index a7f5f42..d70e6a1 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -6,6 +6,7 @@ import { SessionProvider, useSession } from "./lib/session.js"; import { I18nProvider } from "./lib/i18n.js"; import { ThemeProvider } from "./lib/theme.js"; import { ConfirmProvider } from "./components/confirm.js"; +import { ToastProvider } from "./components/toast.js"; import "./styles/index.css"; const container = document.getElementById("root"); @@ -23,7 +24,9 @@ const Providers = ({ children }: { children: ReactNode }) => { return ( - {children} + + {children} + ); diff --git a/frontend/src/pages/Browser.tsx b/frontend/src/pages/Browser.tsx index 0ae76ef..9186820 100644 --- a/frontend/src/pages/Browser.tsx +++ b/frontend/src/pages/Browser.tsx @@ -1,4 +1,10 @@ -import { useCallback, useEffect, useRef, useState, type DragEvent } from "react"; +import { + useCallback, + useEffect, + useRef, + useState, + type DragEvent, +} from "react"; import { Button, Card, Spinner, cn } from "../ui/index.js"; import { ApiError, @@ -11,12 +17,13 @@ import { useSession } from "../lib/session.js"; import { useI18n } from "../lib/i18n.js"; import { getLocale } from "../lib/locale.js"; import { mediaKind } from "../lib/media.js"; -import { Alert, Badge, EmptyState, PageHeader } from "../components/feedback.js"; +import { 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 { FolderChat, type ChatFolder } from "../components/FolderChat.js"; +import { useToast } from "../components/toast.js"; import { CreateFolderDialog, MoveDialog, @@ -28,7 +35,6 @@ import { ChatIcon, DownloadIcon, FolderIcon, - MoveIcon, PenIcon, PlusIcon, ShareIcon, @@ -48,6 +54,7 @@ type Dialog = | { kind: "create" } | { kind: "rename"; target: Target } | { kind: "move"; target: Target } + | { kind: "moveMany"; targets: Target[] } | { kind: "share"; folder: Folder } | null; @@ -65,12 +72,13 @@ export const Browser = () => { const { user, refresh } = useSession(); const { t } = useI18n(); const confirm = useConfirm(); + const toast = useToast(); 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 [notice, setNotice] = useState(null); + const [, setError] = useState(null); + const [, setNotice] = useState(null); const [uploading, setUploading] = useState(false); const [removing, setRemoving] = useState(null); const [dialog, setDialog] = useState(null); @@ -84,28 +92,41 @@ export const Browser = () => { // quand le pointeur passe d'un element enfant a un autre. const [dragDepth, setDragDepth] = useState(0); const [dropTargetId, setDropTargetId] = useState(null); + const [selected, setSelected] = useState>(new Set()); + const [uploadProgress, setUploadProgress] = useState<{ + current: number; + total: number; + } | null>(null); const current = path[path.length - 1] ?? { id: null, name: "Racine" }; const dragActive = dragDepth > 0; - 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 : t("Chargement impossible.")); - setFolders([]); - setFiles([]); - } finally { - setLoading(false); - } - }, [t]); + 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) { + const msg = + caught instanceof ApiError + ? caught.message + : t("Chargement impossible."); + setError(msg); + toast.error(msg); + setFolders([]); + setFiles([]); + } finally { + setLoading(false); + } + }, + [t], + ); const refreshUnread = useCallback(async () => { try { @@ -136,23 +157,33 @@ export const Browser = () => { setPath((previous) => [...previous, { id: folder.id, name: folder.name }]); }; - const goTo = (index: number) => setPath((previous) => previous.slice(0, index + 1)); + const goTo = (index: number) => + setPath((previous) => previous.slice(0, index + 1)); - const upload = async (selected: FileList | null) => { - if (!selected || selected.length === 0) return; + const upload = async (selectedFiles: FileList | null) => { + if (!selectedFiles || selectedFiles.length === 0) return; setUploading(true); setError(null); setNotice(null); + const total = selectedFiles.length; + setUploadProgress({ current: 0, total }); try { - for (const file of Array.from(selected)) { - await api.uploadFile(file, current.id); + const filesArr = Array.from(selectedFiles); + for (let i = 0; i < filesArr.length; i++) { + const f = filesArr[i] as File; + setUploadProgress({ current: i + 1, total }); + await api.uploadFile(f, current.id); } void refresh(); await reload(); } catch (caught) { - setError(caught instanceof ApiError ? caught.message : t("Envoi impossible.")); + const msg = + caught instanceof ApiError ? caught.message : t("Envoi impossible."); + setError(msg); + toast.error(msg); } finally { setUploading(false); + setUploadProgress(null); if (fileInputRef.current) fileInputRef.current.value = ""; } }; @@ -162,31 +193,46 @@ export const Browser = () => { * dossier affiche ou l'un des sous-dossiers listes ; le serveur revalide les * droits d'ecriture dans tous les cas. */ - const uploadInto = async (selected: FileList | null, folderId: string | null, label?: string) => { - if (!selected || selected.length === 0) return; + const uploadInto = async ( + selectedFiles: FileList | null, + folderId: string | null, + label?: string, + ) => { + if (!selectedFiles || selectedFiles.length === 0) return; setUploading(true); setError(null); setNotice(null); + const total = selectedFiles.length; + setUploadProgress({ current: 0, total }); try { - for (const file of Array.from(selected)) { - await api.uploadFile(file, folderId); + const filesArr = Array.from(selectedFiles); + for (let i = 0; i < filesArr.length; i++) { + const f = filesArr[i] as File; + setUploadProgress({ current: i + 1, total }); + await api.uploadFile(f, folderId); } void refresh(); // Seule la vue courante est rechargée : un depot dans un sous-dossier ne // change rien a l'ecran tant qu'on ne l'ouvre pas. if (folderId === current.id) await reload(); - setNotice( - label - ? t("{count} fichier(s) déposé(s) dans {name}.", { - count: selected.length, - name: label, - }) - : t("{count} fichier(s) déposé(s) dans ce dossier.", { count: selected.length }), - ); + const msg = label + ? t("{count} fichier(s) déposé(s) dans {name}.", { + count: filesArr.length, + name: label, + }) + : t("{count} fichier(s) déposé(s) dans ce dossier.", { + count: filesArr.length, + }); + setNotice(msg); + toast.success(msg); } catch (caught) { - setError(caught instanceof ApiError ? caught.message : t("Envoi impossible.")); + const msg = + caught instanceof ApiError ? caught.message : t("Envoi impossible."); + setError(msg); + toast.error(msg); } finally { setUploading(false); + setUploadProgress(null); } }; @@ -263,7 +309,10 @@ export const Browser = () => { const kind = target.type === "folder" ? t("ce dossier") : t("ce fichier"); const ok = await confirm({ title: t("Mettre à la corbeille"), - message: t("Placer {name} ({kind}) dans la corbeille ?", { name: target.name, kind }), + message: t("Placer {name} ({kind}) dans la corbeille ?", { + name: target.name, + kind, + }), confirmLabel: t("Corbeille"), tone: "danger", }); @@ -279,7 +328,12 @@ export const Browser = () => { } await reload(); } catch (caught) { - setError(caught instanceof ApiError ? caught.message : t("Suppression impossible.")); + const msg = + caught instanceof ApiError + ? caught.message + : t("Suppression impossible."); + setError(msg); + toast.error(msg); } finally { setRemoving(null); } @@ -289,9 +343,14 @@ export const Browser = () => { setError(null); try { const { file: updated } = await api.starFile(file.id); - setFiles((previous) => previous.map((item) => (item.id === updated.id ? updated : item))); + setFiles((previous) => + previous.map((item) => (item.id === updated.id ? updated : item)), + ); } catch (caught) { - setError(caught instanceof ApiError ? caught.message : t("Action impossible.")); + const msg = + caught instanceof ApiError ? caught.message : t("Action impossible."); + setError(msg); + toast.error(msg); } }; @@ -300,10 +359,32 @@ export const Browser = () => { void refreshUnread(); }; - const canWrite = user?.role_slug === "admin" || user?.role_slug === "user"; + const toggleSelection = (key: string) => { + setSelected((previous) => { + const next = new Set(previous); + if (next.has(key)) next.delete(key); + else next.add(key); + return next; + }); + }; + + const clearSelection = () => setSelected(new Set()); + + const selectedItems: Target[] = []; + for (const folder of folders) { + if (selected.has(`f:${folder.id}`)) + selectedItems.push({ type: "folder", id: folder.id, name: folder.name }); + } + for (const file of files) { + if (selected.has(`i:${file.id}`)) + selectedItems.push({ type: "file", id: file.id, name: file.name }); + } const empty = folders.length === 0 && files.length === 0; - const visibleFiles = kind ? files.filter((file) => mediaKind(file.mime_type) === kind) : files; - const currentUnread = current.id ? unread[current.id] ?? 0 : 0; + const canWrite = true; + const visibleFiles = kind + ? files.filter((file) => mediaKind(file.mime_type) === kind) + : files; + const currentUnread = current.id ? (unread[current.id] ?? 0) : 0; const quotaPercent = user && user.quota_bytes > 0 ? Math.min(100, Math.round((user.used_bytes / user.quota_bytes) * 100)) @@ -330,60 +411,72 @@ export const Browser = () => { - {path.length > 1 ? ( - - ) : null} + {path.length > 1 ? ( + + ) : null} - + {index > 0 ? ( + + ) : null} + {index === path.length - 1 ? ( + + {t(crumb.name)} + + ) : ( + + )} + + ))} + + - {current.id ? ( - - ) : null} + {current.id ? ( + + ) : null}

} /> @@ -392,7 +485,9 @@ export const Browser = () => {
- {t("Espace utilisé")} + + {t("Espace utilisé")} + {t("{used} sur {quota}", { used: formatBytes(user?.used_bytes ?? 0), @@ -408,6 +503,27 @@ export const Browser = () => {
+ {uploading && uploadProgress ? ( +
+
+ + {t("Envoi en cours")} + + + {t("{current}/{total}", uploadProgress)} + +
+
+
+
+
+ ) : null} + {canWrite ? (
{ isLoading={uploading} onClick={() => fileInputRef.current?.click()} > - {t("Envoyer")} + {uploading && uploadProgress + ? t("Envoi {current}/{total}…", uploadProgress) + : t("Envoyer")}
) : null} - {error ? {error} : null} - {notice ? {notice} : null} + {/* Alerts replaced by toasts */} + + {canWrite && selectedItems.length > 0 ? ( +
+
+

+ {t("{count} élément(s) sélectionné(s).", { + count: selectedItems.length, + })} +

+
+ + + +
+
+
+ ) : null} {loading ? (
@@ -472,7 +637,9 @@ export const Browser = () => { } title={t("Emplacement vide")} - description={t("Créez un sous-dossier ou envoyez un fichier pour commencer.")} + description={t( + "Créez un sous-dossier ou envoyez un fichier pour commencer.", + )} /> ) : (
@@ -488,14 +655,39 @@ export const Browser = () => { onDragOver={(event) => onFolderDragOver(event, folder.id)} onDragLeave={(event) => onFolderDragLeave(event, folder.id)} onDrop={(event) => onFolderDrop(event, folder)} + onClick={() => toggleSelection(`f:${folder.id}`)} className={cn( - "flex items-center gap-1 rounded-lg border border-border bg-surface p-2 transition-colors hover:border-border-strong hover:bg-surface-hover", - dropTargetId === folder.id && "border-accent bg-accent/10 ring-1 ring-accent/50", + "group relative flex items-center gap-1 rounded-lg border border-border bg-surface p-2 transition-colors hover:border-border-strong hover:bg-surface-hover cursor-pointer", + dropTargetId === folder.id && + "border-accent bg-accent/10 ring-1 ring-accent/50", + selected.has(`f:${folder.id}`) && + "border-accent/60 ring-1 ring-accent/40", )} > + 0 + ? "opacity-100" + : "opacity-100 md:opacity-0 md:group-hover:opacity-100 md:focus:opacity-100", + )} + checked={selected.has(`f:${folder.id}`)} + aria-label={t("Sélectionner {name}", { + name: folder.name, + })} + onChange={(event) => { + event.stopPropagation(); + toggleSelection(`f:${folder.id}`); + }} + /> - } - badge={unread[folder.id] ?? 0} - onClick={() => setChat({ id: folder.id, name: folder.name })} - /> - {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} +
+ } + badge={unread[folder.id] ?? 0} + onClick={(event) => { + event.stopPropagation(); + setChat({ id: folder.id, name: folder.name }); + }} + /> + {canWrite ? ( + <> + } + onClick={(event) => { + event.stopPropagation(); + setDialog({ kind: "share", folder }); + }} + /> + } + onClick={(event) => { + event.stopPropagation(); + setDialog({ + kind: "rename", + target: { + type: "folder", + id: folder.id, + name: folder.name, + }, + }); + }} + /> + } + isLoading={removing === folder.id} + tone="danger" + onClick={(event) => { + event.stopPropagation(); + void removeTarget({ + type: "folder", + id: folder.id, + name: folder.name, + }); + }} + /> + + ) : null} +
))} @@ -566,86 +768,135 @@ export const Browser = () => { {t("Fichiers")} {visibleFiles.length === 0 ? ( -

{t("Rien à afficher pour ce filtre.")}

+

+ {t("Rien à afficher pour ce filtre.")} +

) : ( -
    - {visibleFiles.map((file, index) => ( -
  • - - - - {file.name} + 0 + ? "opacity-100" + : "opacity-100 md:opacity-0 md:group-hover:opacity-100 md:focus:opacity-100", + )} + checked={selected.has(`i:${file.id}`)} + aria-label={t("Sélectionner {name}", { + name: file.name, + })} + onChange={(event) => { + event.stopPropagation(); + toggleSelection(`i:${file.id}`); + }} + /> + + + + {file.name} + + + {formatBytes(file.size_bytes)} ·{" "} + {new Date(file.created_at).toLocaleDateString( + getLocale(), + )} + - - {formatBytes(file.size_bytes)} ·{" "} - {new Date(file.created_at).toLocaleDateString(getLocale())} - - - - } - onClick={() => void toggleStar(file)} - /> +
    + + } + onClick={(event) => { + event.stopPropagation(); + void toggleStar(file); + }} + /> - - - {t("Télécharger")} - + event.stopPropagation()} + > + + + {t("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} -
  • - ))} -
+ {canWrite ? ( + <> + } + onClick={(event) => { + event.stopPropagation(); + setDialog({ + kind: "rename", + target: { + type: "file", + id: file.id, + name: file.name, + }, + }); + }} + /> + } + isLoading={removing === file.id} + tone="danger" + onClick={(event) => { + event.stopPropagation(); + void removeTarget({ + type: "file", + id: file.id, + name: file.name, + }); + }} + /> + + ) : null} +
+ + ))} + )} ) : null} @@ -674,13 +925,15 @@ export const Browser = () => { /> ) : null} - {dialog?.kind === "move" ? ( + {dialog?.kind === "moveMany" ? ( setDialog(null)} onDone={() => { setDialog(null); + clearSelection(); void reload(); + void refresh(); }} /> ) : null} @@ -691,7 +944,11 @@ export const Browser = () => { {viewer !== null ? ( ({ id: item.id, name: item.name, mime_type: item.mime_type }))} + items={visibleFiles.map((item) => ({ + id: item.id, + name: item.name, + mime_type: item.mime_type, + }))} startIndex={viewer} onClose={() => setViewer(null)} />