Spaces:
Configuration error
Configuration error
Create App.tsx
Browse files- src/App.tsx +109 -0
src/App.tsx
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState, useEffect, useRef, useCallback, useMemo } from "react";
|
| 2 |
+
import LoadingScreen from "./components/LoadingScreen";
|
| 3 |
+
import CaptioningView from "./components/CaptioningView";
|
| 4 |
+
import WelcomeScreen from "./components/WelcomeScreen";
|
| 5 |
+
import WebcamPermissionDialog from "./components/WebcamPermissionDialog";
|
| 6 |
+
import type { AppState } from "./types";
|
| 7 |
+
|
| 8 |
+
export default function App() {
|
| 9 |
+
const [appState, setAppState] = useState<AppState>("requesting-permission");
|
| 10 |
+
const [webcamStream, setWebcamStream] = useState<MediaStream | null>(null);
|
| 11 |
+
const [isVideoReady, setIsVideoReady] = useState(false);
|
| 12 |
+
const videoRef = useRef<HTMLVideoElement | null>(null);
|
| 13 |
+
|
| 14 |
+
const handlePermissionGranted = useCallback((stream: MediaStream) => {
|
| 15 |
+
setWebcamStream(stream);
|
| 16 |
+
setAppState("welcome");
|
| 17 |
+
}, []);
|
| 18 |
+
|
| 19 |
+
const handleStart = useCallback(() => {
|
| 20 |
+
setAppState("loading");
|
| 21 |
+
}, []);
|
| 22 |
+
|
| 23 |
+
const handleLoadingComplete = useCallback(() => {
|
| 24 |
+
setAppState("captioning");
|
| 25 |
+
}, []);
|
| 26 |
+
|
| 27 |
+
const playVideo = useCallback(async (video: HTMLVideoElement) => {
|
| 28 |
+
try {
|
| 29 |
+
await video.play();
|
| 30 |
+
} catch (error) {
|
| 31 |
+
console.error("Failed to play video:", error);
|
| 32 |
+
}
|
| 33 |
+
}, []);
|
| 34 |
+
|
| 35 |
+
const setupVideo = useCallback(
|
| 36 |
+
(video: HTMLVideoElement, stream: MediaStream) => {
|
| 37 |
+
video.srcObject = stream;
|
| 38 |
+
|
| 39 |
+
const handleCanPlay = () => {
|
| 40 |
+
setIsVideoReady(true);
|
| 41 |
+
playVideo(video);
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
video.addEventListener("canplay", handleCanPlay, { once: true });
|
| 45 |
+
|
| 46 |
+
return () => {
|
| 47 |
+
video.removeEventListener("canplay", handleCanPlay);
|
| 48 |
+
};
|
| 49 |
+
},
|
| 50 |
+
[playVideo],
|
| 51 |
+
);
|
| 52 |
+
|
| 53 |
+
useEffect(() => {
|
| 54 |
+
if (webcamStream && videoRef.current) {
|
| 55 |
+
const video = videoRef.current;
|
| 56 |
+
|
| 57 |
+
video.srcObject = null;
|
| 58 |
+
video.load();
|
| 59 |
+
|
| 60 |
+
const cleanup = setupVideo(video, webcamStream);
|
| 61 |
+
return cleanup;
|
| 62 |
+
}
|
| 63 |
+
}, [webcamStream, setupVideo]);
|
| 64 |
+
|
| 65 |
+
const videoBlurState = useMemo(() => {
|
| 66 |
+
switch (appState) {
|
| 67 |
+
case "requesting-permission":
|
| 68 |
+
return "blur(20px) brightness(0.2) saturate(0.5)";
|
| 69 |
+
case "welcome":
|
| 70 |
+
return "blur(12px) brightness(0.3) saturate(0.7)";
|
| 71 |
+
case "loading":
|
| 72 |
+
return "blur(8px) brightness(0.4) saturate(0.8)";
|
| 73 |
+
case "captioning":
|
| 74 |
+
return "none";
|
| 75 |
+
default:
|
| 76 |
+
return "blur(20px) brightness(0.2) saturate(0.5)";
|
| 77 |
+
}
|
| 78 |
+
}, [appState]);
|
| 79 |
+
|
| 80 |
+
return (
|
| 81 |
+
<div className="App relative h-screen overflow-hidden bg-[#FFFAEB]">
|
| 82 |
+
<div className="absolute inset-0 bg-[#FFFAEB]" />
|
| 83 |
+
{webcamStream && (
|
| 84 |
+
<video
|
| 85 |
+
ref={videoRef}
|
| 86 |
+
autoPlay
|
| 87 |
+
muted
|
| 88 |
+
playsInline
|
| 89 |
+
className="absolute inset-0 w-full h-full object-cover transition-all duration-1000 ease-out"
|
| 90 |
+
style={{
|
| 91 |
+
filter: videoBlurState,
|
| 92 |
+
opacity: isVideoReady ? 1 : 0,
|
| 93 |
+
}}
|
| 94 |
+
/>
|
| 95 |
+
)}
|
| 96 |
+
{appState !== "captioning" && (
|
| 97 |
+
<div className="absolute inset-0 bg-[#FFFAEB]/90 backdrop-blur-sm" />
|
| 98 |
+
)}
|
| 99 |
+
{appState === "requesting-permission" && (
|
| 100 |
+
<WebcamPermissionDialog onPermissionGranted={handlePermissionGranted} />
|
| 101 |
+
)}
|
| 102 |
+
{appState === "welcome" && <WelcomeScreen onStart={handleStart} />}
|
| 103 |
+
{appState === "loading" && (
|
| 104 |
+
<LoadingScreen onComplete={handleLoadingComplete} />
|
| 105 |
+
)}
|
| 106 |
+
{appState === "captioning" && <CaptioningView videoRef={videoRef} />}
|
| 107 |
+
</div>
|
| 108 |
+
);
|
| 109 |
+
}
|