Files
Kai-Sa-Cam/src/App.tsx
T
Esiah f4745878c5
CD — Kai-Sa-Cam Deployment / deploy (push) Successful in 9s
fix: nav hover, lightbox padding
2026-10-31 01:14:39 +01:00

291 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, useEffect } from "react";
import { VideoPlayer } from "./components/VideoPlayer";
import { Controls } from "./components/Controls";
import { MediaGallery } from "./components/MediaGallery";
import { useRecorder } from "./hooks/useRecorder";
import { useTalk } from "./hooks/useTalk";
import {
apiFetch,
onSessionChange,
reauthenticate,
startSessionMonitor,
} from "./lib/api";
type ToastType = "info" | "success" | "error" | "warning";
interface Toast {
message: string;
type: ToastType;
}
interface MediaItem {
filename: string;
type: "video" | "image";
size: number;
mtime: string;
url: string;
}
export function App() {
const [activeTab, setActiveTab] = useState<"live" | "media">("live");
const [isRecording, setIsRecording] = useState(false);
const [gain, setGain] = useState(1);
const [recordingTime, setRecordingTime] = useState(0);
const [snapshots, setSnapshots] = useState<string[]>([]);
const [sessionExpired, setSessionExpired] = useState(false);
const [streamKey, setStreamKey] = useState(0);
const [toast, setToast] = useState<Toast | null>(null);
const [mediaList, setMediaList] = useState<MediaItem[]>([]);
const [selectedMedia, setSelectedMedia] = useState<MediaItem | null>(null);
const showToast = (message: string, type: ToastType = "info") => {
setToast({ message, type });
setTimeout(() => setToast(null), 2500);
};
const {
startRecording,
stopRecording,
error: recError,
} = useRecorder({
onStart: () => {
setIsRecording(true);
setRecordingTime(0);
},
onStop: () => {
setIsRecording(false);
setRecordingTime(0);
},
onTimeUpdate: (t) => setRecordingTime(t),
});
const {
isTalking,
startTalk,
stopTalk,
error: talkError,
} = useTalk({ gain });
// Surveillance de la session Cloudflare Access + bandeau de reconnexion
useEffect(() => {
const off = onSessionChange((expired) => {
setSessionExpired(expired);
if (expired) {
showToast("Session expirée", "warning");
} else {
showToast("Session reconnectée", "success");
setStreamKey((k) => k + 1); // recharge le flux vidéo après reconnexion
}
});
const stop = startSessionMonitor();
return () => {
off();
stop();
};
}, []);
const handleRecord = async () => {
if (isRecording) {
await stopRecording();
} else {
await startRecording();
}
};
const handleSnapshot = async () => {
try {
const res = await apiFetch("/api/media/snapshots", { method: "POST" });
if (!res.ok) throw new Error("Snapshot failed");
const data = await res.json();
setSnapshots((prev) => [data.filename, ...prev]);
showToast("Capture d'écran prise", "success");
} catch (e) {
console.error("Snapshot error:", e);
showToast("Erreur lors de la capture", "error");
}
};
const handleTalk = async () => {
if (isTalking) stopTalk();
else await startTalk();
};
return (
<div className="h-screen max-h-screen overflow-hidden flex flex-col">
<section className="relative mx-auto w-[96%] h-[96vh] max-h-[96vh] mt-[2vh] flex flex-col">
<header className="relative z-[2] flex items-center shrink-0">
<img
src="/logo.png"
alt="Logo"
className="h-[6vh] sm:h-[7vh] md:h-[8vh] max-h-[70px] object-contain drop-shadow-[0px_4px_20px_rgba(0,0,0,0.25)]"
/>
</header>
{sessionExpired && (
<div className="absolute z-[3] top-[calc(6vh+0.5rem)] sm:top-[calc(7vh+0.5rem)] md:top-[calc(8vh+0.5rem)] left-0 right-0 glass-card px-4 sm:px-6 py-1.5 sm:py-2 flex items-center justify-between rounded-[50px] text-sm sm:text-base animate-fade-in">
<span>Session expirée.</span>
<button
className="glass-button px-3 sm:px-4 py-1 rounded-[50px] text-sm sm:text-base font-medium transition-transform active:scale-95"
onClick={() => reauthenticate().catch(() => {})}
>
Se reconnecter
</button>
</div>
)}
<main className="flex-1 flex flex-col min-h-0 relative">
{toast && (
<div
className={`absolute top-2 right-2 z-50 px-4 py-2 rounded-[50px] text-sm animate-fade-in shadow-lg ${
toast.type === "success"
? "bg-green-500/40 text-green-100 border border-green-400/60"
: toast.type === "error"
? "bg-red-500/40 text-red-100 border border-red-400/60"
: toast.type === "warning"
? "bg-amber-500/40 text-amber-100 border border-amber-400/60"
: "glass-card"
}`}
>
{toast.message}
</div>
)}
<div className="mx-auto mt-[1vh] sm:mt-[1.5vh] w-[min(95vw,380px)] shrink-0">
<div className="relative flex rounded-full overflow-hidden bg-[rgba(237,237,237,0.12)] backdrop-blur-[25px] border border-white/10 hover:bg-[rgba(237,237,237,0.18)] transition-colors duration-300">
<div
className={`absolute inset-y-0 left-0 w-1/2 rounded-full bg-[rgba(237,237,237,0.28)] backdrop-blur-[10px] transition-all duration-300 ease-in-out ${
activeTab === "media" ? "translate-x-full" : "translate-x-0"
}`}
/>
<button
className={`relative z-10 flex-1 py-2 sm:py-2.5 md:py-3 text-center text-sm sm:text-base md:text-lg transition-all duration-300 active:scale-95 ${
activeTab === "live" ? "font-medium" : ""
}`}
onClick={() => setActiveTab("live")}
>
Live
</button>
<button
className={`relative z-10 flex-1 py-2 sm:py-2.5 md:py-3 text-center text-sm sm:text-base md:text-lg transition-all duration-300 active:scale-95 ${
activeTab === "media" ? "font-medium" : ""
}`}
onClick={() => setActiveTab("media")}
>
Médias
</button>
</div>
</div>
<div className="flex-1 min-h-0 relative overflow-hidden">
<div
className={`absolute inset-0 flex flex-col transition-transform duration-300 ease-in-out ${
activeTab === "live" ? "translate-x-0" : "-translate-x-full"
}`}
>
<div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 md:gap-4 mt-2 sm:mt-3 px-1 sm:px-2 min-h-0">
<div className="w-full flex-1 min-h-0 rounded-[20px] sm:rounded-[30px] overflow-hidden glass-card">
{/* Même origine => même session Access, plus de stream.esiah.dev.
Le key force le rechargement du flux après une reconnexion. */}
<VideoPlayer
key={streamKey}
streamUrl="/go2rtc/stream.html?src=webcam&mode=mse"
/>
</div>
<div className="w-full max-w-6xl shrink-0">
<Controls
isRecording={isRecording}
recordingTime={recordingTime}
isTalking={isTalking}
gain={gain}
onGainChange={setGain}
onRecord={handleRecord}
onSnapshot={handleSnapshot}
onTalk={handleTalk}
disabled={Boolean(recError || talkError)}
/>
</div>
{(recError || talkError) && (
<div className="text-red-400 text-xs sm:text-sm text-center px-4 error-message animate-fade-in shrink-0">
{recError || talkError}
</div>
)}
</div>
</div>
<div
className={`absolute inset-0 flex flex-col transition-transform duration-300 ease-in-out ${
activeTab === "media" ? "translate-x-0" : "translate-x-full"
}`}
>
<div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 md:gap-4 mt-2 sm:mt-3 px-1 sm:px-2 min-h-0">
<div className="w-full flex-1 min-h-0">
<MediaGallery
showToast={showToast}
onMediaChange={setMediaList}
onSelectMedia={setSelectedMedia}
/>
</div>
</div>
</div>
</div>
</main>
</section>
{selectedMedia && (
<div
className="fixed inset-0 w-screen h-screen bg-black/90 backdrop-blur-md flex items-center justify-center z-[999999] animate-fade-in p-12"
onClick={() => setSelectedMedia(null)}
>
<button
className="fixed left-6 sm:left-8 top-1/2 -translate-y-1/2 glass-button rounded-full w-10 h-10 flex items-center justify-center z-[1000000] text-lg"
onClick={(e) => {
e.stopPropagation();
const idx = mediaList.findIndex(
(m) => m.filename === selectedMedia.filename,
);
if (idx > 0) setSelectedMedia(mediaList[idx - 1]);
}}
>
‹
</button>
<button
className="fixed right-6 sm:right-8 top-1/2 -translate-y-1/2 glass-button rounded-full w-10 h-10 flex items-center justify-center z-[1000000] text-lg"
onClick={(e) => {
e.stopPropagation();
const idx = mediaList.findIndex(
(m) => m.filename === selectedMedia.filename,
);
if (idx < mediaList.length - 1) setSelectedMedia(mediaList[idx + 1]);
}}
>
›
</button>
<button
className="fixed top-6 right-6 glass-button rounded-full w-10 h-10 flex items-center justify-center z-[1000000] text-lg"
onClick={() => setSelectedMedia(null)}
>
×
</button>
<div
className="w-full h-full flex items-center justify-center"
onClick={(e) => e.stopPropagation()}
>
{selectedMedia.type === "video" ? (
<video
src={selectedMedia.url}
controls
autoPlay
className="w-full h-full object-contain"
/>
) : (
<img
src={selectedMedia.url}
alt={selectedMedia.filename}
className="w-full h-full object-contain"
/>
)}
</div>
</div>
)}
</div>
);
}