Files
Kai-Sa-Cam/src/components/MediaGallery.tsx
T
Esiah dee938eede
CD — Kai-Sa-Cam Deployment / deploy (push) Successful in 10s
fix: move lightbox to root, fix nav hover
2026-10-31 00:13:09 +01:00

186 lines
6.8 KiB
TypeScript

import { useState, useEffect } from "react";
import { apiFetch, SessionExpiredError } from "../lib/api";
interface MediaItem {
filename: string;
type: "video" | "image";
size: number;
mtime: string;
url: string;
}
interface MediaGalleryProps {
showToast?: (message: string, type?: "info" | "success" | "error" | "warning") => void;
onMediaChange?: (media: MediaItem[]) => void;
onSelectMedia?: (item: MediaItem) => void;
}
export function MediaGallery({ showToast, onMediaChange, onSelectMedia }: MediaGalleryProps) {
const [media, setMedia] = useState<MediaItem[]>([]);
const [itemToDelete, setItemToDelete] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const fetchMedia = async () => {
try {
const res = await apiFetch("/api/media");
if (!res.ok) throw new Error();
const data = await res.json();
setMedia(data);
onMediaChange?.(data);
} catch (e) {
const msg =
e instanceof SessionExpiredError
? "Session expirée, reconnecte-toi"
: "Erreur chargement médias";
setError(msg);
showToast?.(msg, e instanceof SessionExpiredError ? "warning" : "error");
}
};
useEffect(() => {
fetchMedia();
}, []);
const handleDelete = async (filename: string) => {
try {
const res = await apiFetch(`/api/media/${encodeURIComponent(filename)}`, {
method: "DELETE",
});
if (!res.ok) throw new Error();
setMedia((prev) => {
const next = prev.filter((m) => m.filename !== filename);
onMediaChange?.(next);
return next;
});
setItemToDelete(null);
showToast?.("Fichier supprimé", "success");
} catch (e) {
const msg =
e instanceof SessionExpiredError
? "Session expirée, reconnecte-toi"
: "Erreur suppression";
setError(msg);
showToast?.(msg, e instanceof SessionExpiredError ? "warning" : "error");
}
};
const handleExportAll = async () => {
try {
const res = await apiFetch("/api/media/export", { method: "POST" });
if (!res.ok) throw new Error();
const blob = await res.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "kai-sa-cam-media.zip";
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
fetchMedia();
showToast?.("Export terminé", "success");
} catch (e) {
const msg =
e instanceof SessionExpiredError
? "Session expirée, reconnecte-toi"
: "Erreur export";
setError(msg);
showToast?.(msg, e instanceof SessionExpiredError ? "warning" : "error");
}
};
return (
<div className="flex-1 flex flex-col p-1 sm:p-2 md:p-3 gap-2 sm:gap-3 min-h-0 h-full">
<div className="flex justify-between items-center glass-card px-3 sm:px-4 md:px-5 py-1.5 sm:py-2 rounded-[30px] sm:rounded-[50px] flex-wrap gap-2 shrink-0">
<span className="text-xs sm:text-sm md:text-base opacity-80">{media.length} fichiers</span>
<div className="flex gap-2">
<button
onClick={fetchMedia}
className="px-3 sm:px-4 md:px-5 py-1 sm:py-1.5 text-xs sm:text-sm glass-button rounded-[50px] active:scale-95 transition-transform"
>
Actualiser
</button>
{media.length > 0 && (
<button
onClick={handleExportAll}
className="px-3 sm:px-4 md:px-5 py-1 sm:py-1.5 text-xs sm:text-sm glass-button rounded-[50px] active:scale-95 transition-transform"
>
Tout télécharger et vider
</button>
)}
</div>
</div>
<div className="flex-1 min-h-0 overflow-y-auto no-scrollbar grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-2 sm:gap-3 content-start">
{media.map((item) => (
<div
key={item.filename}
className="glass-card rounded-[16px] sm:rounded-[20px] overflow-hidden flex flex-col transition-transform hover:scale-[1.02] active:scale-[0.98]"
>
<div
className="relative aspect-video bg-black/40 flex items-center justify-center cursor-pointer"
onClick={() => onSelectMedia?.(item)}
>
{item.type === "video" ? (
<video src={item.url} className="w-full h-full object-cover" />
) : (
<img
src={item.url}
alt={item.filename}
className="w-full h-full object-cover"
/>
)}
</div>
<div className="p-2 sm:p-3 flex items-center justify-between gap-1 sm:gap-2">
<span className="text-[10px] sm:text-xs truncate opacity-90">
{item.filename}
</span>
<div className="flex gap-1">
<a
href={item.url}
download={item.filename}
className="px-1.5 sm:px-2 py-0.5 sm:py-1 text-[10px] sm:text-xs glass-button rounded-[50px] active:scale-95 transition-transform"
>
DL
</a>
<button
onClick={() => setItemToDelete(item.filename)}
className="px-1.5 sm:px-2 py-0.5 sm:py-1 text-[10px] sm:text-xs bg-red-500/30 text-red-100 rounded-[50px] hover:bg-red-500/40 transition-colors border border-red-400/40 active:scale-95"
>
Supprimer
</button>
</div>
</div>
</div>
))}
</div>
{itemToDelete && (
<div className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-50 p-4 animate-fade-in">
<div className="glass-card p-6 rounded-[30px] max-w-sm w-full flex flex-col gap-4">
<h3 className="text-lg font-semibold">
Confirmer la suppression
</h3>
<p className="text-sm opacity-80">
Voulez-vous vraiment supprimer ce fichier ?
</p>
<div className="flex gap-2 justify-end">
<button
onClick={() => setItemToDelete(null)}
className="px-4 py-2 text-sm glass-button rounded-[50px] active:scale-95 transition-transform"
>
Annuler
</button>
<button
onClick={() => handleDelete(itemToDelete)}
className="px-4 py-2 text-sm bg-red-500/40 text-red-100 rounded-[50px] hover:bg-red-500/50 transition-colors border border-red-400/40 active:scale-95"
>
Confirmer
</button>
</div>
</div>
</div>
)}
</div>
);
}