Files
Kai-Sa-Cam/apps/kai-sa-cam/src/App.tsx
T

114 lines
3.5 KiB
TypeScript

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";
export function App() {
const [activeTab, setActiveTab] = useState<"live" | "media">("live");
const [isRecording, setIsRecording] = useState(false);
const [isTalking, setIsTalking] = useState(false);
const [gain, setGain] = useState(1);
const [recordingTime, setRecordingTime] = useState(0);
const [snapshots, setSnapshots] = useState<string[]>([]);
const {
startRecording,
stopRecording,
error: recError,
} = useRecorder({
onStart: () => {
setIsRecording(true);
setRecordingTime(0);
},
onStop: () => {
setIsRecording(false);
setRecordingTime(0);
},
onTimeUpdate: (t) => setRecordingTime(t),
});
const { startTalk, stopTalk, error: talkError } = useTalk({ gain });
const handleRecord = async () => {
if (isRecording) {
await stopRecording();
} else {
await startRecording();
}
};
const handleSnapshot = async () => {
try {
const res = await fetch("/api/media/snapshots", { method: "POST" });
if (!res.ok) throw new Error("Snapshot failed");
const data = await res.json();
setSnapshots((prev) => [data.filename, ...prev]);
} catch (e) {
console.error("Snapshot error:", e);
}
};
const handleTalk = async () => {
if (isTalking) {
stopTalk();
setIsTalking(false);
} else {
await startTalk();
setIsTalking(true);
}
};
return (
<div className="min-h-screen bg-gray-900 text-white flex flex-col">
<header className="bg-gray-800 border-b border-gray-700 px-4 py-3 flex items-center justify-between">
<h1 className="text-xl font-semibold flex items-center gap-3">
<img src="/logo.png" alt="Logo" className="w-8 h-8 object-contain" />
Kai-Sa-Cam
</h1>
</header>
<main className="flex-1 flex flex-col">
<nav className="flex border-b border-gray-700 bg-gray-800">
<button
className={`flex-1 py-3 text-center font-medium ${activeTab === "live" ? "text-blue-400 border-b-2 border-blue-400" : "text-gray-400"}`}
onClick={() => setActiveTab("live")}
>
Live
</button>
<button
className={`flex-1 py-3 text-center font-medium ${activeTab === "media" ? "text-blue-400 border-b-2 border-blue-400" : "text-gray-400"}`}
onClick={() => setActiveTab("media")}
>
Médias
</button>
</nav>
{activeTab === "live" && (
<div className="flex-1 flex flex-col p-4 gap-4 overflow-hidden">
<VideoPlayer streamUrl="https://stream.esiah.dev/stream.html?src=webcam&mode=mse" />
<Controls
isRecording={isRecording}
recordingTime={recordingTime}
isTalking={isTalking}
gain={gain}
onRecord={handleRecord}
onSnapshot={handleSnapshot}
onTalk={handleTalk}
disabled={Boolean(recError || talkError)}
/>
{(recError || talkError) && (
<div className="text-red-400 text-sm text-center px-4">
{recError || talkError}
</div>
)}
</div>
)}
{activeTab === "media" && <MediaGallery />}
</main>
</div>
);
}