/** * The live transcript preview: what is being said while a meeting is * recorded, one turn per stretch of speech on a track. It is a preview; the * meeting's real transcript (with speakers told apart) replaces it once the * recording is done. */ import { useOpenGeniText, createOpenGeniText, type OpenGeniText } from "../messages/runtime"; import type { RecordingTrack } from "@opengeni/contracts"; import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { ArrowDownIcon } from "lucide-react"; import { Button } from "../ui/button"; import { cn } from "../app/lib/utils"; import { formatClock, groupLiveTurns, tidyLiveText, tidyPartialText, withoutLiveMicrophoneEchoes, type LiveTranscriptLine, } from "../recordings"; import { SpeakerDot } from "./meeting-transcript"; const defaultText = createOpenGeniText(); export { groupLiveTurns, tidyLiveText, tidyPartialText, type LiveTranscriptLine }; const NO_PARTIALS: readonly { trackNumber: number; text: string }[] = []; const NO_TRACKS: readonly { trackNumber: number; source: RecordingTrack["source"] }[] = []; export function LiveTranscript({ lines, partials = NO_PARTIALS, tracks = NO_TRACKS, speakerName, status, empty, heading = true, className, }: { lines: readonly LiveTranscriptLine[]; /** Words still being heard per track, which may change. */ partials?: readonly { trackNumber: number; text: string }[]; /** * The recording's tracks. With computer or tab audio beside the * microphone, the microphone's echo of the others is left out. */ tracks?: readonly { trackNumber: number; source: RecordingTrack["source"] }[]; speakerName: (trackNumber: number) => string; /** A short line about the connection, such as reconnecting. */ status?: { text: string; tone: "muted" | "waiting" } | null; /** Shown before anything has been said. */ empty: string; /** Show the "Live transcript ยท preview" heading; off where the page already has one. */ heading?: boolean; className?: string; }) { const msg = useOpenGeniText(); const turns = useMemo( () => groupLiveTurns(withoutLiveMicrophoneEchoes(lines, tracks)), [lines, tracks], ); const heard = partials .map((partial) => ({ ...partial, text: tidyPartialText(partial.text) })) .filter((partial) => partial.text); // Words still being heard from whoever spoke last continue their turn. const lastTrack = turns.at(-1)?.trackNumber; const continuing = heard.find((partial) => partial.trackNumber === lastTrack) ?? null; const newTurns = heard.filter((partial) => partial !== continuing); const scrollRef = useRef(null); // Follow the newest words unless the reader scrolled up to reread. const following = useRef(true); const [scroll, setScroll] = useState({ above: false, behind: false }); useEffect(() => { const element = scrollRef.current; if (!element) return; const onScroll = () => { const atEnd = element.scrollHeight - element.scrollTop - element.clientHeight < 24; following.current = atEnd; const above = element.scrollTop > 4; setScroll((previous) => previous.above === above && previous.behind === !atEnd ? previous : { above, behind: !atEnd }, ); }; element.addEventListener("scroll", onScroll, { passive: true }); return () => element.removeEventListener("scroll", onScroll); }, []); const content = `${turns.length}:${turns.at(-1)?.texts.length ?? 0}:${heard .map((partial) => partial.text.length) .join(",")}`; useLayoutEffect(() => { const element = scrollRef.current; if (!element) return; if (following.current) element.scrollTop = element.scrollHeight; const above = element.scrollTop > 4; setScroll((previous) => (previous.above === above ? previous : { ...previous, above })); }, [content]); const jumpToLatest = () => { const element = scrollRef.current; if (!element) return; following.current = true; element.scrollTop = element.scrollHeight; }; const statusLine = status ? ( status.tone === "waiting" ? ( // A dropped connection is a state of the whole box, so it reads as a banner over it.

) : (

{status.text}

) ) : null; return (
{heading ? ( <>

{msg("meetings.liveTranscript.liveTranscript")}{" "} {msg("meetings.liveTranscript.preview")}

{status?.tone === "muted" ? statusLine : null}
{status?.tone === "waiting" ? statusLine : null} ) : ( statusLine )}
{turns.length === 0 && heard.length === 0 ? (

{empty}

) : (
    {turns.map((turn, index) => (
  1. {speakerName(turn.trackNumber)} {formatClock(turn.startMilliseconds)}

    {tidyLiveText(turn.texts)} {continuing && index === turns.length - 1 ? ( {continuing.text} ) : null}

  2. ))} {newTurns.map((partial) => (
  3. {speakerName(partial.trackNumber)}

    {partial.text}

  4. ))}
)}
{scroll.behind ? ( ) : null}
); } /** The local recorder's live state as the panel's status line, or null when all is well. */ export function liveStatus( tracks: readonly { state: string; error: { message: string } | null }[], msg: OpenGeniText = defaultText, ): { text: string; tone: "muted" | "waiting" } | null { if (tracks.length === 0) return null; if (tracks.every((track) => track.state === "unavailable")) { return { text: msg("meetings.liveTranscript.livePreviewUnavailableTheRecording"), tone: "muted", }; } if (tracks.some((track) => track.state === "reconnecting")) { return { text: msg("meetings.liveTranscript.reconnectingTheLiveTextThe"), tone: "waiting", }; } if (tracks.every((track) => track.state === "connecting")) { return { text: msg("meetings.liveTranscript.connecting"), tone: "muted" }; } return null; }