Files
CoHaiSe-CLOUD/frontend/src/components/MediaViewer.tsx
T
Gitea ActionsandEsiah 4d725d9a3b
CD — Deployment / deploy (push) Successful in 23s
ci(deploy): back up before migrating and run migrations explicitly (#9)
- dump the database to ./backups before applying migrations
- run the one-shot migrate service on every deploy instead of relying only on
  the api depends_on, so a migration is never skipped
- translate the workflow comments to English

---------

Co-authored-by: xEsiah <[email protected]>
Reviewed-on: #9
Reviewed-by: Esiah <[email protected]>
Co-authored-by: Gitea Actions <-[email protected]>
2026-10-17 12:34:56 +00:00

222 lines
6.9 KiB
TypeScript

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<string | null>(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 <p className="text-sm text-white/70">{t("Lecture impossible.")}</p>;
if (text === null) return <p className="text-sm text-white/70">{t("Chargement…")}</p>;
return (
<pre className="max-h-full w-full max-w-3xl overflow-auto rounded-md bg-black/60 p-4 text-left text-sm whitespace-pre-wrap text-white/90">
{text}
</pre>
);
};
const Panel = ({
item,
message,
openable,
}: {
item: MediaViewerItem;
message: string;
openable: boolean;
}) => {
const { t } = useI18n();
return (
<div className="flex w-full max-w-md flex-col items-center gap-4 rounded-xl border border-white/10 bg-black/40 p-8 text-center">
<FileIcon className="h-12 w-12 text-white/60" />
<div className="min-w-0">
<p className="truncate text-sm font-medium text-white">{item.name}</p>
<p className="mt-1 text-xs text-white/60">{message}</p>
</div>
<div className="flex flex-wrap items-center justify-center gap-2">
{openable ? (
<a
href={api.previewUrl(item.id)}
target="_blank"
rel="noopener noreferrer"
className={actionClass}
>
{t("Ouvrir")}
</a>
) : null}
<a href={api.downloadUrl(item.id)} download={item.name} className={actionClass}>
<DownloadIcon className="h-4 w-4" />
{t("Télécharger")}
</a>
</div>
</div>
);
};
const Body = ({ item }: { item: MediaViewerItem }) => {
const { t } = useI18n();
const kind = kindOf(item.mime_type);
if (kind === "image") {
return (
<img
src={api.previewUrl(item.id)}
alt={item.name}
className="max-h-full max-w-full rounded-md object-contain shadow-2xl"
/>
);
}
if (kind === "video") {
return (
<video
src={api.previewUrl(item.id)}
controls
autoPlay
className="max-h-full max-w-full rounded-md bg-black shadow-2xl"
/>
);
}
if (kind === "audio") {
return (
<div className="flex w-full max-w-md flex-col items-center gap-4 rounded-lg bg-black/40 p-6">
<FileIcon className="h-10 w-10 text-white/70" />
<p className="max-w-full truncate text-sm text-white/90">{item.name}</p>
<audio src={api.previewUrl(item.id)} controls autoPlay className="w-full" />
</div>
);
}
if (kind === "text") return <TextViewer id={item.id} />;
if (kind === "pdf") {
return <Panel item={item} message={t("Le PDF s'ouvre dans un nouvel onglet.")} openable />;
}
return (
<Panel item={item} message={t("Aperçu indisponible pour ce format.")} openable={false} />
);
};
export const MediaViewer = ({
items,
startIndex,
onClose,
}: {
items: MediaViewerItem[];
startIndex: number;
onClose: () => void;
}) => {
const { t } = useI18n();
const [index, setIndex] = useState(startIndex);
const current = items[index];
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") onClose();
if (event.key === "ArrowLeft") setIndex((value) => Math.max(0, value - 1));
if (event.key === "ArrowRight") setIndex((value) => Math.min(items.length - 1, value + 1));
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [items.length, onClose]);
if (!current) return null;
return (
<div
className="fixed inset-0 z-50 flex flex-col bg-black/90 backdrop-blur-sm"
role="dialog"
aria-modal="true"
aria-label={current.name}
>
<div className="flex shrink-0 items-center justify-between gap-4 px-4 py-3 text-white">
<p className="min-w-0 truncate text-sm font-medium">{current.name}</p>
<div className="flex shrink-0 items-center gap-2">
<a
href={api.downloadUrl(current.id)}
download={current.name}
className={controlClass}
aria-label={t("Télécharger")}
>
<DownloadIcon className="h-4 w-4" />
</a>
<button type="button" onClick={onClose} className={controlClass} aria-label={t("Fermer")}>
<CloseIcon className="h-4 w-4" />
</button>
</div>
</div>
<div
className="relative flex min-h-0 flex-1 items-center justify-center px-4 pb-6"
onClick={(event) => {
if (event.target === event.currentTarget) onClose();
}}
>
{index > 0 ? (
<button
type="button"
onClick={() => setIndex((value) => value - 1)}
className={cn(controlClass, "absolute left-4 z-10")}
aria-label={t("Fichier précédent")}
>
<ArrowLeftIcon className="h-5 w-5" />
</button>
) : null}
<Body item={current} />
{index < items.length - 1 ? (
<button
type="button"
onClick={() => setIndex((value) => value + 1)}
className={cn(controlClass, "absolute right-4 z-10")}
aria-label={t("Fichier suivant")}
>
<ArrowRightIcon className="h-5 w-5" />
</button>
) : null}
</div>
</div>
);
};