feat(terminal): the signage player: playlists on a TV, offline, transitions, rotation (platform-v2 step 5.5a)
- terminal/src/apps/signage: plays the screen's playlist (or the one its schedule picks now, switching by itself) full screen: slides in order with their time, a video to its end (a 15 min safety limit), only in their days / hours / dates; transitions fade / slide / zoom / none (CSS, reduced motion respected); the frame turned 90/180/270 for a TV mounted sideways. Picture, video and text slides drawn; a first menu board (5.5b lays it out fully). - Offline: the last GET /api/signage/screen kept in localStorage, every picture and video (up to 120 MB) as an in-memory copy; reads again on signage.changed / device.updated / reconnect / every 5 min. - logic.js: the server's schedule pick ported (same test cases), slide windows, next slide, frame; tests/signage.test.mjs (6). - Signage screens get no offline banner over the slides, a small mark instead (registry QUIET_OFFLINE). - e2e signage.mjs (in run-all): a paired TV plays picture → text → video → menu in order, the text's slide transition, rotation, keeps playing with the server stopped (mark, no banner), catches up when it is back. terminal.mjs passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
// The signage player (plan step 5.5a): a TV paired as a signage screen, no sign-in. It plays its
|
||||
// playlist (or the playlist its schedule picks now) full screen: slides in order, each for its
|
||||
// time (a video to its end), with its transition, only on its days / hours / dates; turned for a
|
||||
// TV mounted sideways. Everything it needs is kept on the device: the last answer of
|
||||
// GET /api/signage/screen in localStorage and every picture and video as an in-memory copy, so it
|
||||
// keeps playing (and switching by its schedule) while the server is down, and catches up on
|
||||
// `signage.changed`, a reconnect, or every few minutes (prices that change by time alone).
|
||||
// Menu boards and widgets are drawn in 5.5b.
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import client from '../../api/client'
|
||||
import { useDevice, useLiveEvent, useLiveEvents } from '../../device/context'
|
||||
import { frameStyle, localNow, mediaUrls, nextSlide, playlistNow, slideMs } from './logic'
|
||||
import { MenuSlide, TextSlide } from './Slides'
|
||||
|
||||
const quiet = { xenia: { noOverride: true } }
|
||||
const CACHE_KEY = 'xenia:signage:screen'
|
||||
const REFRESH_MS = 5 * 60_000
|
||||
const VIDEO_MAX_MS = 15 * 60_000 // a video "to its end" that never ends moves on after this
|
||||
const KEEP_VIDEO_BYTES = 120 * 1024 * 1024 // bigger videos stream from the server (not kept offline)
|
||||
const TRANSITION_MS = 800
|
||||
|
||||
function readCache() {
|
||||
try { return JSON.parse(localStorage.getItem(CACHE_KEY) || 'null') } catch { return null }
|
||||
}
|
||||
function writeCache(data) {
|
||||
try { localStorage.setItem(CACHE_KEY, JSON.stringify(data)) } catch { /* full or blocked: the memory copy still plays */ }
|
||||
}
|
||||
|
||||
/** url → an in-memory copy (object URL) of every file the playlists need; old copies are let go. */
|
||||
function useKeptMedia(urls) {
|
||||
const [kept, setKept] = useState({})
|
||||
const keptRef = useRef({})
|
||||
const key = urls.slice().sort().join('|')
|
||||
useEffect(() => {
|
||||
let live = true
|
||||
const wanted = key ? key.split('|') : []
|
||||
;(async () => {
|
||||
for (const url of wanted) {
|
||||
if (keptRef.current[url]) continue
|
||||
try {
|
||||
const res = await fetch(url)
|
||||
if (!res.ok) continue
|
||||
const size = Number(res.headers.get('content-length') || 0)
|
||||
if (size > KEEP_VIDEO_BYTES) continue
|
||||
const blob = await res.blob()
|
||||
if (!live) return
|
||||
keptRef.current = { ...keptRef.current, [url]: URL.createObjectURL(blob) }
|
||||
setKept(keptRef.current)
|
||||
} catch { /* offline: the next answer tries again; the address still works while online */ }
|
||||
}
|
||||
const gone = Object.keys(keptRef.current).filter(u => !wanted.includes(u))
|
||||
if (gone.length) {
|
||||
const next = { ...keptRef.current }
|
||||
for (const u of gone) { URL.revokeObjectURL(next[u]); delete next[u] }
|
||||
keptRef.current = next
|
||||
if (live) setKept(next)
|
||||
}
|
||||
})()
|
||||
return () => { live = false }
|
||||
}, [key])
|
||||
useEffect(() => () => Object.values(keptRef.current).forEach(u => URL.revokeObjectURL(u)), [])
|
||||
return kept
|
||||
}
|
||||
|
||||
export default function SignageApp() {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { offline } = useDevice()
|
||||
const [data, setData] = useState(readCache)
|
||||
const [now, setNow] = useState(() => new Date())
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const { data: d } = await client.get('/api/signage/screen', quiet)
|
||||
setData(prev => (prev?.version === d.version ? prev : d))
|
||||
writeCache(d)
|
||||
} catch { /* keep playing what we have */ }
|
||||
}, [])
|
||||
useEffect(() => { load() }, [load])
|
||||
useLiveEvent('ready', load)
|
||||
useLiveEvents(['signage.changed', 'device.updated'], load)
|
||||
useEffect(() => {
|
||||
const id = setInterval(load, REFRESH_MS)
|
||||
return () => clearInterval(id)
|
||||
}, [load])
|
||||
useEffect(() => {
|
||||
const id = setInterval(() => setNow(new Date()), 1000)
|
||||
return () => clearInterval(id)
|
||||
}, [])
|
||||
|
||||
const kept = useKeptMedia(useMemo(() => mediaUrls(data), [data]))
|
||||
const playlist = playlistNow(data, now)
|
||||
const tz = data?.schedule?.timezone
|
||||
const screen = data?.screen
|
||||
return (
|
||||
<main className="fixed inset-0 overflow-hidden select-none cursor-none" style={{ background: '#000' }}
|
||||
data-testid="app-signage" data-playlist={playlist?.id ?? ''} data-orientation={screen?.orientation ?? ''} data-offline={offline ? 'true' : 'false'}>
|
||||
<div style={frameStyle(screen?.rotation)} data-testid="signage-frame" data-rotation={screen?.rotation ?? 0}>
|
||||
{playlist?.slides?.length
|
||||
? <Player key={playlist.id} playlist={playlist} kept={kept} tz={tz} venue={data?.venue} now={now} />
|
||||
: <Empty text={data ? t('signage.nothing') : t('signage.starting')} />}
|
||||
</div>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
|
||||
function Empty({ text }) {
|
||||
return (
|
||||
<div className="absolute inset-0 flex items-end justify-end p-4" data-testid="signage-empty">
|
||||
<span style={{ color: 'rgba(255,255,255,0.25)', fontSize: '1.4vmin' }}>{text}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** Plays one playlist: the current slide, the next when its time is up, with the transition. */
|
||||
function Player({ playlist, kept, tz, venue, now }) {
|
||||
const local = localNow(tz, now)
|
||||
const slides = playlist.slides
|
||||
const [cur, setCur] = useState(() => ({ index: nextSlide(slides, -1, local), at: Date.now(), n: 0 }))
|
||||
const [prev, setPrev] = useState(null)
|
||||
const media = id => {
|
||||
const m = playlist.media?.[String(id)]
|
||||
return m ? { ...m, src: kept[m.url] || m.url } : null
|
||||
}
|
||||
|
||||
const curRef = useRef(cur)
|
||||
useEffect(() => { curRef.current = cur })
|
||||
const advance = useCallback(() => {
|
||||
const c = curRef.current
|
||||
const next = nextSlide(slides, c.index, localNow(tz, new Date()))
|
||||
setPrev(c.index >= 0 ? c : null)
|
||||
setCur({ index: next, at: Date.now(), n: c.n + 1 })
|
||||
}, [slides, tz])
|
||||
|
||||
// the slide list changed (an edit): keep going from a slide that still exists
|
||||
useEffect(() => {
|
||||
setCur(c => (c.index >= 0 && c.index < slides.length ? c : { index: nextSlide(slides, -1, localNow(tz, new Date())), at: Date.now(), n: c.n + 1 }))
|
||||
}, [slides, tz])
|
||||
|
||||
const slide = cur.index >= 0 ? slides[cur.index] : null
|
||||
// time's up (videos to their end call advance themselves, with a safety limit)
|
||||
useEffect(() => {
|
||||
if (!slide) return undefined
|
||||
const ms = slideMs(slide) ?? VIDEO_MAX_MS
|
||||
const id = setTimeout(advance, ms)
|
||||
return () => clearTimeout(id)
|
||||
}, [slide, cur.n, advance])
|
||||
// a slide that may no longer show (its window ended) is skipped at once; none showing → wait
|
||||
useEffect(() => {
|
||||
if (cur.index < 0 && nextSlide(slides, -1, local) >= 0) advance()
|
||||
}, [cur.index, slides, local.minutes, advance]) // eslint-disable-line react-hooks/exhaustive-deps
|
||||
useEffect(() => {
|
||||
if (!prev) return undefined
|
||||
const id = setTimeout(() => setPrev(null), TRANSITION_MS)
|
||||
return () => clearTimeout(id)
|
||||
}, [prev])
|
||||
|
||||
if (!slide) return <Empty text="" />
|
||||
const transition = slide.transition || playlist.transition || 'fade'
|
||||
return (
|
||||
<>
|
||||
{prev && slides[prev.index] && (
|
||||
<Layer key={`out-${prev.n}`} slide={slides[prev.index]} media={media} venue={venue} now={now} className={`sg-out sg-out-${transition}`} />
|
||||
)}
|
||||
<Layer key={`in-${cur.n}`} slide={slide} media={media} venue={venue} now={now} onEnded={advance}
|
||||
className={cur.n === 0 ? '' : `sg-in sg-in-${transition}`} current />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function Layer({ slide, media, venue, now, onEnded, className, current = false }) {
|
||||
return (
|
||||
<section className={`absolute inset-0 overflow-hidden ${className}`} style={{ animationDuration: `${TRANSITION_MS}ms` }}
|
||||
data-testid={current ? 'signage-slide' : 'signage-slide-out'} data-kind={slide.kind} data-slide={slide.id}>
|
||||
{slide.kind === 'image' && <ImageSlide content={slide.content} media={media} />}
|
||||
{slide.kind === 'video' && <VideoSlide slide={slide} media={media} onEnded={current ? onEnded : undefined} />}
|
||||
{slide.kind === 'text' && <TextSlide content={slide.content} media={media} venue={venue} now={now} />}
|
||||
{slide.kind === 'menu' && <MenuSlide slide={slide} media={media} venue={venue} now={now} />}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function ImageSlide({ content, media }) {
|
||||
const m = media(content.media_id)
|
||||
return (
|
||||
<div className="absolute inset-0" style={{ background: content.background_colour || '#000' }}>
|
||||
{m && <img src={m.src} alt="" className="w-full h-full" style={{ objectFit: content.fit || 'cover' }} data-testid="signage-image" />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function VideoSlide({ slide, media, onEnded }) {
|
||||
const c = slide.content
|
||||
const m = media(c.media_id)
|
||||
const ref = useRef(null)
|
||||
useEffect(() => { ref.current?.play?.().catch(() => {}) }, [m?.src])
|
||||
return (
|
||||
<div className="absolute inset-0" style={{ background: c.background_colour || '#000' }}>
|
||||
{m && <video ref={ref} src={m.src} autoPlay muted={c.muted !== false} playsInline loop={!slide.video_to_end}
|
||||
onEnded={slide.video_to_end ? onEnded : undefined} onError={slide.video_to_end ? onEnded : undefined}
|
||||
className="w-full h-full" style={{ objectFit: c.fit || 'cover' }} data-testid="signage-video" />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user