"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import type { OnScreen, ScreenMedia } from "@/lib/screen"; // What the host has put on the scoreboard: a question above the standings, a sound, or a // video that takes over the screen. // // Each of these is mounted with a key of item + shownAt. That restarts it when the host shows // it again, and lets it keep the media URL it started with: an upload's signed URL is // different in every payload, and following it would restart the file on every refetch. type Question = Extract; type Sound = Extract; type Video = Extract; export const onScreenKey = (item: OnScreen) => `${item.id}:${item.shownAt}`; const SOUND_OFF_NOTE = "Sound is off — click “Turn sound on”"; /** Long questions get smaller type so they still fit above the standings. */ function questionSize(length: number): string { if (length <= 60) return "text-[clamp(1.5rem,8vh,7rem)]"; if (length <= 140) return "text-[clamp(1.25rem,6vh,5rem)]"; if (length <= 280) return "text-[clamp(1.125rem,4.5vh,3.5rem)]"; return "text-[clamp(1rem,3.4vh,2.5rem)]"; } export function QuestionPanel({ question }: { question: Question }) { const [imageUrl] = useState(question.imageUrl); return (
{imageUrl && ( // A plain : the picture comes from any host or a signed storage URL, which // next/image would need configuring for and can't cache anyway. // eslint-disable-next-line @next/next/no-img-element )}

{question.text}

{question.answer && }
); } function Answer({ text }: { text: string }) { return (

{text}

); } /** A sound has nothing to look at: a slim banner says something is playing. */ export function SoundBanner({ sound, audible }: { sound: Sound; audible: boolean }) { return (

♪ {sound.paused ? "Paused" : "Listen…"}

{!audible &&

{SOUND_OFF_NOTE}

} {sound.answer && } {/* A YouTube link used as a sound: the player is there, but nobody sees the video. */}
); } /** A video (karaoke, a clip) replaces the standings and fills the screen. */ export function VideoStage({ video, audible }: { video: Video; audible: boolean }) { return (
{!audible && (

{SOUND_OFF_NOTE}

)}
); } type PlayerProps = { media: ScreenMedia; paused: boolean; /** False until someone has clicked the page: browsers only allow muted playback before that. */ audible: boolean; audioOnly?: boolean; }; function Player(props: PlayerProps) { // Pinned: see the note at the top of this file. const [media] = useState(props.media); return media.type === "youtube" ? ( ) : ( ); } function FilePlayer({ url, paused, audible, audioOnly }: PlayerProps & { url: string }) { const element = useRef(null); useEffect(() => { const media = element.current; if (!media) return; // Set here, not as a prop: React doesn't keep the `muted` attribute in sync. media.muted = !audible; if (paused) media.pause(); // Rejected if the browser still blocks playback; the next change tries again. else void media.play().catch(() => {}); }, [paused, audible]); return audioOnly ? (