// 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 page by themselves when they don't fit (5.5b); widgets sit in corners and bars.
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 { boardPages, frameStyle, localNow, mediaUrls, menuMs, nextSlide, pageAt, playlistNow, SlideView, slideMs } from '@xenia/signage'
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 (