PR — Ouverture automatique / create-pr (push) Successful in 3s
- MediaViewer (image/video/audio/pdf/texte) et MediaThumb (vignette video 1re frame, icones audio/archive) - Gallery/Browser cables sur le viewer et les vignettes ; lib/media.ts + previewUrl - Admin: bandeau de succes au lieu du popup "Identifiants generes" - AppShell/PageHeader: hauteur fixe du viewport + en-tete de page sticky (filtres, recherche, stats figes)
155 lines
5.3 KiB
TypeScript
155 lines
5.3 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
|
import { Button, Spinner, cn } from "../ui/index.js";
|
|
import { ApiError, api, formatBytes, type MediaItem } from "../lib/api.js";
|
|
import { Alert, EmptyState, PageHeader } from "../components/feedback.js";
|
|
import { MediaViewer } from "../components/MediaViewer.js";
|
|
import { MediaThumb } from "../components/MediaThumb.js";
|
|
import { ImageIcon } from "../components/icons.js";
|
|
|
|
const KINDS = [
|
|
{ value: "", label: "Tout" },
|
|
{ value: "image", label: "Images" },
|
|
{ value: "video", label: "Vidéos" },
|
|
{ value: "audio", label: "Audio" },
|
|
{ value: "document", label: "Documents" },
|
|
{ value: "archive", label: "Archives" },
|
|
{ value: "other", label: "Autres" },
|
|
] as const;
|
|
|
|
const PAGE = 60;
|
|
|
|
export const Gallery = () => {
|
|
const [kind, setKind] = useState<string>("image");
|
|
const [items, setItems] = useState<MediaItem[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [loadingMore, setLoadingMore] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [viewer, setViewer] = useState<number | null>(null);
|
|
|
|
useEffect(() => {
|
|
setLoading(true);
|
|
void api
|
|
.gallery(kind, PAGE, 0)
|
|
.then(({ items: first }) => {
|
|
setItems(first);
|
|
setError(null);
|
|
})
|
|
.catch((caught: unknown) => {
|
|
setError(caught instanceof ApiError ? caught.message : "Chargement impossible.");
|
|
setItems([]);
|
|
})
|
|
.finally(() => setLoading(false));
|
|
}, [kind]);
|
|
|
|
const loadMore = useCallback(async () => {
|
|
setLoadingMore(true);
|
|
setError(null);
|
|
try {
|
|
const { items: more } = await api.gallery(kind, PAGE, items.length);
|
|
setItems((previous) => [...previous, ...more]);
|
|
} catch (caught) {
|
|
setError(caught instanceof ApiError ? caught.message : "Chargement impossible.");
|
|
} finally {
|
|
setLoadingMore(false);
|
|
}
|
|
}, [kind, items.length]);
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5">
|
|
<PageHeader
|
|
title="Galerie"
|
|
description="Parcourez les médias auxquels vous avez accès."
|
|
toolbar={
|
|
<div className="flex flex-wrap gap-1.5" role="tablist" aria-label="Filtrer par type">
|
|
{KINDS.map((option) => (
|
|
<button
|
|
key={option.value}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={kind === option.value}
|
|
onClick={() => {
|
|
setKind(option.value);
|
|
setItems([]);
|
|
}}
|
|
className={cn(
|
|
"rounded-sm border px-3 py-1.5 font-display text-[0.6875rem] font-semibold uppercase tracking-label transition-colors",
|
|
kind === option.value
|
|
? "border-accent/50 bg-accent/10 text-accent"
|
|
: "border-border-control text-muted-foreground hover:bg-surface-hover hover:text-foreground",
|
|
)}
|
|
>
|
|
{option.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
}
|
|
/>
|
|
|
|
{error ? <Alert>{error}</Alert> : null}
|
|
|
|
{loading ? (
|
|
<div className="flex justify-center py-16">
|
|
<Spinner size="md" />
|
|
</div>
|
|
) : items.length === 0 ? (
|
|
<EmptyState
|
|
icon={<ImageIcon className="h-8 w-8" />}
|
|
title="Aucun média"
|
|
description="Rien à afficher pour ce filtre."
|
|
/>
|
|
) : (
|
|
<>
|
|
<ul className="grid grid-cols-2 gap-3 sm:grid-cols-3 lg:grid-cols-4">
|
|
{items.map((item, index) => {
|
|
const cardClass =
|
|
"group flex w-full flex-col overflow-hidden rounded-lg border border-border bg-surface text-left transition-colors hover:border-border-strong";
|
|
const inner = (
|
|
<>
|
|
<span className="flex aspect-square items-center justify-center overflow-hidden bg-surface-muted text-muted-foreground">
|
|
<MediaThumb id={item.id} mime={item.mime_type} name={item.name} size="lg" />
|
|
</span>
|
|
<span className="flex flex-col gap-0.5 p-2">
|
|
<span className="truncate text-xs font-medium text-foreground">
|
|
{item.name}
|
|
</span>
|
|
<span className="text-[0.6875rem] text-subtle-foreground">
|
|
{formatBytes(item.size_bytes)}
|
|
</span>
|
|
</span>
|
|
</>
|
|
);
|
|
return (
|
|
<li key={item.id}>
|
|
<button type="button" onClick={() => setViewer(index)} className={cardClass}>
|
|
{inner}
|
|
</button>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
|
|
{items.length >= PAGE ? (
|
|
<div className="flex justify-center">
|
|
<Button variant="secondary" size="md" isLoading={loadingMore} onClick={() => void loadMore()}>
|
|
Charger plus
|
|
</Button>
|
|
</div>
|
|
) : null}
|
|
</>
|
|
)}
|
|
|
|
{viewer !== null ? (
|
|
<MediaViewer
|
|
items={items.map((item) => ({
|
|
id: item.id,
|
|
name: item.name,
|
|
mime_type: item.mime_type,
|
|
}))}
|
|
startIndex={viewer}
|
|
onClose={() => setViewer(null)}
|
|
/>
|
|
) : null}
|
|
</div>
|
|
);
|
|
};
|