feat(files): add bulk selection, move and upload progress

Allow selecting multiple files and folders to move them together in one
dialog. Show upload progress (current/total) during single-file and
drag-and-drop uploads.
This commit is contained in:
2026-10-22 14:44:08 +02:00
parent 699c523caa
commit 540dc0cc1a
2 changed files with 109 additions and 27 deletions
+21 -12
View File
@@ -157,14 +157,19 @@ const loadTree = async (): Promise<TreeEntry[]> => {
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<TreeEntry[] | null>(null);
const [destination, setDestination] = useState<string>("");
const [error, setError] = useState<string | null>(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 (
<Modal open title={t("Déplacer")} onClose={onClose}>
<Modal open title={title} onClose={onClose}>
<form className="flex flex-col gap-4" onSubmit={(event) => void submit(event)}>
<p className="text-sm text-muted-foreground">
{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 })}
</p>
{entries === null && !error ? (
<Alert tone="info">{t("Chargement des dossiers…")}</Alert>
@@ -214,13 +223,13 @@ export const MoveDialog = ({
)}
>
<option value="">{t("Racine")}</option>
{(entries ?? [])
.filter((entry) => entry.id !== target.id)
.map((entry) => (
<option key={entry.id} value={entry.id}>
{entry.label}
</option>
))}
{(entries ?? [])
.filter((entry) => !items.some((item) => item.type === "folder" && item.id === entry.id))
.map((entry) => (
<option key={entry.id} value={entry.id}>
{entry.label}
</option>
))}
</select>
</label>
)}
+88 -15
View File
@@ -48,6 +48,7 @@ type Dialog =
| { kind: "create" }
| { kind: "rename"; target: Target }
| { kind: "move"; target: Target }
| { kind: "moveMany"; targets: Target[] }
| { kind: "share"; folder: Folder }
| null;
@@ -84,6 +85,8 @@ export const Browser = () => {
// quand le pointeur passe d'un element enfant a un autre.
const [dragDepth, setDragDepth] = useState(0);
const [dropTargetId, setDropTargetId] = useState<string | null>(null);
const [selected, setSelected] = useState<Set<string>>(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;
@@ -138,14 +141,19 @@ export const Browser = () => {
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();
@@ -153,6 +161,7 @@ export const Browser = () => {
setError(caught instanceof ApiError ? caught.message : t("Envoi impossible."));
} finally {
setUploading(false);
setUploadProgress(null);
if (fileInputRef.current) fileInputRef.current.value = "";
}
};
@@ -162,14 +171,19 @@ 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
@@ -178,15 +192,16 @@ export const Browser = () => {
setNotice(
label
? t("{count} fichier(s) déposé(s) dans {name}.", {
count: selected.length,
count: filesArr.length,
name: label,
})
: t("{count} fichier(s) déposé(s) dans ce dossier.", { count: selected.length }),
: t("{count} fichier(s) déposé(s) dans ce dossier.", { count: filesArr.length }),
);
} catch (caught) {
setError(caught instanceof ApiError ? caught.message : t("Envoi impossible."));
} finally {
setUploading(false);
setUploadProgress(null);
}
};
@@ -300,8 +315,26 @@ 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 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 =
@@ -424,7 +457,9 @@ export const Browser = () => {
isLoading={uploading}
onClick={() => fileInputRef.current?.click()}
>
{t("Envoyer")}
{uploading && uploadProgress
? t("Envoi {current}/{total}…", uploadProgress)
: t("Envoyer")}
</Button>
<Button
variant="secondary"
@@ -464,6 +499,24 @@ export const Browser = () => {
{error ? <Alert>{error}</Alert> : null}
{notice ? <Alert tone="success">{notice}</Alert> : null}
{canWrite && selectedItems.length > 0 ? (
<Card>
<div className="flex flex-wrap items-center justify-between gap-2 p-3">
<p className="text-sm text-foreground">
{t("{count} élément(s) sélectionné(s).", { count: selectedItems.length })}
</p>
<div className="flex flex-wrap items-center gap-2">
<Button size="sm" variant="secondary" onClick={() => setDialog({ kind: "moveMany", targets: selectedItems })}>
{t("Déplacer")}
</Button>
<Button size="sm" variant="ghost" onClick={clearSelection}>
{t("Annuler la sélection")}
</Button>
</div>
</div>
</Card>
) : null}
{loading ? (
<div className="flex justify-center py-16">
<Spinner size="md" />
@@ -491,8 +544,16 @@ export const Browser = () => {
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",
selected.has(`f:${folder.id}`) && "border-accent/60 ring-1 ring-accent/40",
)}
>
<input
type="checkbox"
className="ml-1 h-4 w-4 rounded border-border-control"
checked={selected.has(`f:${folder.id}`)}
aria-label={t("Sélectionner {name}", { name: folder.name })}
onChange={() => toggleSelection(`f:${folder.id}`)}
/>
<button
type="button"
onClick={() => open(folder)}
@@ -570,7 +631,17 @@ export const Browser = () => {
) : (
<ul className="flex flex-col divide-y divide-border rounded-lg border border-border bg-surface">
{visibleFiles.map((file, index) => (
<li key={file.id} className="flex items-center gap-2 px-3 py-2.5">
<li
key={file.id}
className={cn("flex items-center gap-2 px-3 py-2.5", selected.has(`i:${file.id}`) && "bg-accent/5")}
>
<input
type="checkbox"
className="h-4 w-4 rounded border-border-control"
checked={selected.has(`i:${file.id}`)}
aria-label={t("Sélectionner {name}", { name: file.name })}
onChange={() => toggleSelection(`i:${file.id}`)}
/>
<button
type="button"
onClick={() => setViewer(index)}
@@ -674,13 +745,15 @@ export const Browser = () => {
/>
) : null}
{dialog?.kind === "move" ? (
{dialog?.kind === "moveMany" ? (
<MoveDialog
target={dialog.target}
targets={dialog.targets}
onClose={() => setDialog(null)}
onDone={() => {
setDialog(null);
clearSelection();
void reload();
void refresh();
}}
/>
) : null}