import { useState, useEffect, useEffectEvent, useCallback, useRef } from "react"; import { useNavigate } from "react-router-dom"; import { useAuthStore } from "@/stores/auth-store"; import { apiClient } from "@/api/client"; import { isParticleDeleted, type Particle, REACTION_EMOJIS } from "@/api/types"; import { parseParticlePath, particlePath, toFirestoreDocPath, type ParticlePath } from "@/lib/particle-path"; import { ComposeOverlay, type ComposeStep } from "@/features/compose/compose-overlay"; import { PlaybackPageIndicator } from "@/features/particles/playback-page-indicator"; import { MediaParticleView, type MediaParticleHandle } from "@/features/particles/media-particle-view"; import { TextParticleView } from "@/features/particles/text-particle-view"; import { FallbackParticleView } from "@/features/particles/fallback-particle-view"; import { DeletedParticleView } from "@/features/particles/deleted-particle-view"; import { VideoAudioToggle } from "@/components/video-audio-toggle"; import { useMediaSettingsStore } from "@/stores/media-settings-store"; import { KeybindingsOverlay, type KeybindingGroup } from "@/components/keybindings-overlay"; import { useNetwork } from "@/hooks/use-networks"; import { toggleParticleReaction } from "@/lib/firestore-particles"; import { ReactionBar } from "@/features/particles/reaction-bar"; import { TopBar } from "@/features/particles/stream-top-bar"; import { useStreamPlayback } from "@/hooks/use-stream-playback"; import { usePrefetchAdjacentMedia } from "@/hooks/use-prefetch-adjacent-media"; import { usePresencePositions } from "@/hooks/use-presence-positions"; import { StreamPresenceProvider, useStreamPresence, useStreamComposing, useStreamComposingBroadcast, type ComposingMode } from "@/features/particles/stream-presence-context"; import { ComposingIndicator } from "@/components/composing-indicator"; import { cn } from "@/lib/utils"; import { useMount } from "react-use"; import { usePlaybackSuspenderStore } from "@/stores/playback-suspender-store"; function getReactions(particle: Particle): Record | undefined { if (isParticleDeleted(particle)) return undefined; if (particle.type === "media" || particle.type === "text") return particle.reactions; return undefined; } // --- Exit countdown hook --- const EXIT_DELAY_MS = 5000; const EXIT_TICK_MS = 100; const SEEK_DELTA_SEC = 5; type PlaybackStatus = "idle" | "playing" | "ended"; function useExitCountdown( status: PlaybackStatus, disabled: boolean, onExit: () => void, ) { const [remainingMs, setRemainingMs] = useState(null); const handleExit = useEffectEvent(() => { onExit(); }); // Start/cancel countdown based on playback status useEffect(() => { if (status === "ended") { setRemainingMs(EXIT_DELAY_MS); } else { setRemainingMs(null); } }, [status]); // Tick the countdown down (pauses when compose is active) useEffect(() => { if (remainingMs === null || remainingMs <= 0 || disabled) return; const interval = setInterval(() => { setRemainingMs((prev) => { if (prev === null) return null; const next = prev - EXIT_TICK_MS; return next <= 0 ? 0 : next; }); }, EXIT_TICK_MS); return () => clearInterval(interval); }, [remainingMs !== null && remainingMs > 0, disabled]); // Navigate once countdown hits zero useEffect(() => { if (remainingMs !== null && remainingMs <= 0) { handleExit(); } }, [remainingMs]); return remainingMs; } // --- Keybindings --- const STREAM_VIEW_KEYBINDINGS: KeybindingGroup[] = [ { label: "Navigation", bindings: [ { keys: ["←", "→", "↑", "↓"], description: "Previous / next particle" }, { keys: ["Esc"], description: "Back to network" }, ], }, { label: "Playback", bindings: [ { keys: ["Hold", "Space"], description: "Pause" }, { keys: ["Hold", "Shift"], description: "1.5× speed" }, { keys: ["Shift", "←", "→"], description: "Seek ±5s" }, ], }, { label: "Compose", bindings: [ { keys: ["Hold", "`"], description: "Reply" }, { keys: ["S"], description: "Screen record" }, { keys: ["T"], description: "Text compose" }, { keys: ["V"], description: "Toggle video / audio" }, { keys: ["H"], description: "Join huddle" }, ], }, { label: "Reactions", bindings: [ { keys: ["1-7"], description: "Toggle emoji reaction" }, ], }, ]; // --- StreamView --- interface StreamViewProps { streamParticle: Particle & { type: "stream" }; path: ParticlePath; } export function StreamView({ path, streamParticle }: StreamViewProps) { const { networkId } = parseParticlePath(path); return ( ); } function StreamViewInner({ path, streamParticle }: StreamViewProps) { const { networkId } = parseParticlePath(path); const navigate = useNavigate(); useMount(() => { window.electronAutoplay.dismiss(); }); const { children, currentParticle, currentIndex, status, paused, next, prev, goTo, pause, resume, } = useStreamPlayback(streamParticle, path); usePrefetchAdjacentMedia(children, currentIndex); const authedUser = useAuthStore((s) => s.user); const recordingMode = useMediaSettingsStore((s) => s.recordingMode); const setRecordingMode = useMediaSettingsStore((s) => s.setRecordingMode); const network = useNetwork(networkId); const presenceBySegment = usePresencePositions( streamParticle.playback_markers, children, network?.humans, authedUser?.id, ); // --- Stream presence (realtime via pusher) --- const { onlineHumanIds } = useStreamPresence(); const { composingUsers } = useStreamComposing(); const { startComposing, stopComposing } = useStreamComposingBroadcast(); const mediaRef = useRef(null); const handleToggleReaction = useCallback((emoji: string) => { if (!authedUser || !currentParticle) return; if (isParticleDeleted(currentParticle)) return; const currentParticleDocPath = currentParticle ? toFirestoreDocPath(particlePath(networkId, [streamParticle.id, currentParticle.id])) : null; if (!currentParticleDocPath) return; const reactions = getReactions(currentParticle); toggleParticleReaction(currentParticleDocPath, emoji, authedUser.id, reactions); }, [authedUser, currentParticle]); const [composeActive, setComposeActive] = useState(false); const [composeStep, setComposeStep] = useState("idle"); const playbackSuspended = usePlaybackSuspenderStore((s) => s.suspendCount > 0); const playbackBlocked = composeActive || playbackSuspended; const [progress, setProgress] = useState(0); const [fastPlayback, setFastPlayback] = useState(false); const [showKeybindings, setShowKeybindings] = useState(false); // Broadcast composing state to other viewers useEffect(() => { const stepToMode: Record = { idle: null, submitting: null, recording: "recording", typing: "typing", reviewing: "typing", configuring: "typing", picking: "screen", }; const mode = stepToMode[composeStep] ?? null; if (mode) { startComposing(mode); } else { stopComposing(); } }, [composeStep, startComposing, stopComposing]); // Show/hide chrome on mouse activity (YouTube-style) const [showControls, setShowControls] = useState(true); const idleTimerRef = useRef>(undefined); const handleMouseActivity = useCallback(() => { setShowControls(true); clearTimeout(idleTimerRef.current); idleTimerRef.current = setTimeout(() => setShowControls(false), 3000); }, []); useEffect(() => () => clearTimeout(idleTimerRef.current), []); // Always show controls when compose is active or exit countdown is visible const controlsVisible = showControls || composeActive || status === "ended"; const handleExitNavigate = useCallback(() => { navigate(`/${networkId}`); }, [navigate, networkId]); const exitRemainingMs = useExitCountdown( status, playbackBlocked, handleExitNavigate, ); // Reset progress when particle changes useEffect(() => { setProgress(0); }, [currentParticle?.id]); // Pause/resume playback when compose overlay or lightbox is open. useEffect(() => { if (playbackBlocked) pause(); else resume(); }, [playbackBlocked, pause, resume]); // Playback keyboard: arrows, escape, hold-space-to-pause useEffect(() => { function isInputTarget(e: KeyboardEvent) { const target = e.target as HTMLElement; return ( target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable ); } const handleKeyDown = (e: KeyboardEvent) => { if (playbackBlocked) return; if (isInputTarget(e)) return; switch (e.key) { case "ArrowRight": e.preventDefault(); if (!e.shiftKey || !mediaRef.current?.seek(SEEK_DELTA_SEC)) next(); break; case "ArrowDown": e.preventDefault(); next(); break; case "ArrowLeft": e.preventDefault(); if (!e.shiftKey || !mediaRef.current?.seek(-SEEK_DELTA_SEC)) prev(); break; case "ArrowUp": e.preventDefault(); prev(); break; case " ": e.preventDefault(); if (!e.repeat) pause(); break; case "Shift": if (!e.repeat) { mediaRef.current?.setPlaybackRate(1.5); setFastPlayback(true); } break; case "Escape": e.preventDefault(); navigate(-1); break; case "h": { e.preventDefault(); apiClient.getLivekitToken(networkId, streamParticle.id).then(({ token, server_url }) => { window.electronWindow.openHuddle({ token, serverUrl: server_url }); }); navigate(`/${networkId}`); break; } case "v": e.preventDefault(); setRecordingMode(recordingMode === "video" ? "audio" : "video"); break; case "?": e.preventDefault(); setShowKeybindings((v) => !v); break; case "1": case "2": case "3": case "4": case "5": case "6": case "7": e.preventDefault(); handleToggleReaction(REACTION_EMOJIS[parseInt(e.key) - 1]); break; } }; const handleKeyUp = (e: KeyboardEvent) => { if (playbackBlocked) return; if (isInputTarget(e)) return; if (e.key === " ") { e.preventDefault(); resume(); } if (e.key === "Shift") { mediaRef.current?.setPlaybackRate(1); setFastPlayback(false); } }; window.addEventListener("keydown", handleKeyDown); window.addEventListener("keyup", handleKeyUp); return () => { window.removeEventListener("keydown", handleKeyDown); window.removeEventListener("keyup", handleKeyUp); }; }, [playbackBlocked, next, prev, pause, resume, navigate, networkId, streamParticle.id, setShowKeybindings, handleToggleReaction, recordingMode, setRecordingMode], ); if (children.length === 0) { return (

No particles in this stream yet

setShowKeybindings(true)} />
); } // Render particle content inline function renderParticle(particle: Particle) { if (isParticleDeleted(particle)) { return ( ); } switch (particle.type) { case "media": return ( ); case "text": return ( ); default: return ; } } return (
setShowControls(false)} > {/* Top gradient safe zone */}
{/* TopBar — always visible */}
{/* Main playback area */}
{currentParticle && (
{renderParticle(currentParticle)} {fastPlayback && (
1.5x
)}
)}
{/* Reaction bar — always visible */} {currentParticle && !isParticleDeleted(currentParticle) && (
)} {/* Composing indicator — left edge, always visible */} {/* Bottom gradient safe zone for keyboard hints */}
{/* BottomBar */} setShowKeybindings(true)} /> setShowKeybindings(false)} groups={STREAM_VIEW_KEYBINDINGS} title="Stream View" />
); } function BottomBar({ visible, total, current, progress, onGoTo, presenceBySegment, onlineHumanIds, exitRemainingMs, onOpenKeybindings, }: { visible: boolean; total: number; current: number; progress: number; onGoTo: (index: number) => void; presenceBySegment: Map; onlineHumanIds: Set; exitRemainingMs: number | null; onOpenKeybindings: () => void; }) { return (
{/* Presence avatars — above the blurred background */} {/* Blurred background container — tracks + controls */}
{exitRemainingMs !== null && (
Closing in {Math.ceil(exitRemainingMs / 1000)}s
)}
); } function StreamViewControls({ showEscape, onOpenKeybindings, }: { showEscape?: boolean; onOpenKeybindings: () => void; }) { return (
{showEscape && ( Esc {" "} back )} Hold ` {" "} to reply T {" "} text H {" "} huddle ?
); }