import { LoaderCircle, Radio, X } from "lucide-react" import { type ReactNode, useEffect, useRef } from "react" import { useOnScreen } from "@/hooks/useOnScreen" import { isRef, useArtifactUrl } from "@/lib/media" import { cn } from "@/lib/utils" import { useBoundValue } from "./dataContext" import { config, text } from "./ui/core/config" import { useLiveDevice, useLiveView } from "./useLiveView" import type { WidgetProps } from "./widgets" /** * What a message's bytes look like: a frame, a clip, a segment. * * Media never travels as a message — the reference does. The bytes come down * the socket in front of it where the engine is holding the frame in memory, * and are fetched from the artifact store otherwise; either way this tile only * asks for what it is drawing. A camera serving its own stream is still played * from source: point `stream_url` at it and the browser does the work. */ export function MediaWidget({ widget }: WidgetProps) { const cfg = config(widget) return ( ) } /** * The tile's body, for anywhere a message's media is shown. * * Frames are asked for only while the tile is on screen, in a tab being * looked at: a camera is only sent while somebody watches it, and a tile * scrolled away or behind another tab is nobody. A clip keeps playing either * way. * * A camera whose device streams live is tapped for it. Frames pause while * the live view plays, so the device encodes one picture rather than two, * and come back when it ends. */ export function MediaView({ message, stream = "", dtype = "", fit = "cover", autoplay = false, label, }: { message: string stream?: string dtype?: string fit?: "cover" | "contain" autoplay?: boolean label?: string }) { const box = useRef(null) const shown = useOnScreen(box) const bound = useBoundValue(message || undefined) const value = bound?.value const ref = isRef(value) ? value : null const kind = (ref?.media_type ?? dtype).split("/")[0] const playing = kind === "audio" || kind === "video" const camera = useLiveDevice(stream ? "" : message) const live = useLiveView(camera) const onAir = live.phase === "playing" const active = (shown || playing) && !onAir const url = useArtifactUrl( active ? ref : null, active ? message || undefined : undefined, ) const objectFit = fit === "contain" ? "object-contain" : "object-cover" const alt = label || ref?.name || message // Scrolled away, or the tab hidden: nobody is watching. const { stop } = live useEffect(() => { if (!shown) stop() }, [shown, stop]) return (
{camera ? ( {body()} ) : ( body() )}
) function body() { // A camera serving its own stream is played from source: the engine // carries references at whatever rate the flow publishes them, which is // not video. if (stream && kind !== "audio") { return ( {alt} ) } if (!message && !stream) { return

Pick a message.

} if (!ref) { return

Nothing published yet.

} if (!url) { return

Loading…

} if (kind === "audio") { return ( ) } if (kind === "video") { return ( ) } return ( {alt} ) } } /** * A camera tile that streams live when tapped, and stops when tapped again. * * The whole tile is the control, so it is as easy to hit on a wall panel as * on a desk. The video waits under the frames until its first picture, then * takes their place. */ function LiveFrame({ live, label, fit, children, }: { live: ReturnType label: string fit: string children: ReactNode }) { const { phase, stream, note } = live const onAir = phase === "playing" const chip = "pointer-events-none absolute z-20 flex items-center gap-1 rounded-full border border-border px-2.5 py-1 text-xs font-medium shadow-e2" const frosted = "bg-card/80 text-foreground backdrop-blur-md" return ( ) }