import { useEffect, useState } from "react"; import { cn } from "../ui/index.js"; import { api } from "../lib/api.js"; import { useI18n } from "../lib/i18n.js"; import { ArrowLeftIcon, ArrowRightIcon, CloseIcon, DownloadIcon, FileIcon } from "./icons.js"; export interface MediaViewerItem { id: string; name: string; mime_type: string | null; } type Kind = "image" | "video" | "audio" | "pdf" | "text" | "document"; const kindOf = (mime: string | null): Kind => { const value = (mime ?? "").toLowerCase(); if (value === "image/svg+xml") return "document"; if (value.startsWith("image/")) return "image"; if (value.startsWith("video/")) return "video"; if (value.startsWith("audio/")) return "audio"; if (value === "application/pdf") return "pdf"; if (value === "text/plain") return "text"; return "document"; }; const controlClass = "inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-full border border-white/15 bg-black/40 text-white transition-colors hover:bg-black/60"; const actionClass = "inline-flex items-center gap-2 rounded-md border border-white/20 bg-white/10 px-3.5 py-2 text-sm font-medium text-white transition-colors hover:bg-white/20"; const TextViewer = ({ id }: { id: string }) => { const { t } = useI18n(); const [text, setText] = useState(null); const [failed, setFailed] = useState(false); useEffect(() => { let active = true; setText(null); setFailed(false); void fetch(api.previewUrl(id)) .then((response) => (response.ok ? response.text() : Promise.reject(new Error("failed")))) .then((body) => { if (active) setText(body); }) .catch(() => { if (active) setFailed(true); }); return () => { active = false; }; }, [id]); if (failed) return

{t("Lecture impossible.")}

; if (text === null) return

{t("Chargement…")}

; return (
      {text}
    
); }; const Panel = ({ item, message, openable, }: { item: MediaViewerItem; message: string; openable: boolean; }) => { const { t } = useI18n(); return (

{item.name}

{message}

{openable ? ( {t("Ouvrir")} ) : null} {t("Télécharger")}
); }; const Body = ({ item }: { item: MediaViewerItem }) => { const { t } = useI18n(); const kind = kindOf(item.mime_type); if (kind === "image") { return ( {item.name} ); } if (kind === "video") { return (