refactor: @xenia/signage: the slides and the player's logic shared by the terminal and the manager (platform-v2 step 5.6a)

The signage slides (SlideView, TextSlide, MenuSlide, Widgets) and the pure
logic (schedule pick, slide windows, menu pages) move from the terminal into
packages/signage, so the manager's previews draw a slide exactly as the TV
does. Sizes are container units (cqmin) instead of viewport units: a slide
draws inside any box with container-type: size (frameStyle sets it on the
TV). Tests move with it (10). Dockerfiles copy its package.json; both apps'
Tailwind content include it; run-all runs its tests. Signage e2e and the
compose stack passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:59:33 +02:00
co-authored by Claude Opus 5.5
parent e90c2f4f8f
commit 314e9c2457
16 changed files with 146 additions and 76 deletions
+184
View File
@@ -0,0 +1,184 @@
// Text and menu slides of the signage player, and their widgets (plan steps 5.5a, 5.5b). Colours,
// fonts and sizes here are the venue's own choices for its TV (slide content = data, CS-16), so
// they are inline styles, not tokens. Sizes scale with the screen (cqmin), so a slide looks the
// same on any TV.
import { Cloud, CloudFog, CloudLightning, CloudMoon, CloudRain, CloudSnow, CloudSun, Moon, Sun } from 'lucide-react'
import { QRCodeSVG } from 'qrcode.react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { FONTS, weatherIcon } from './logic'
/** A slide's background: a colour, or a photo (cover / contain) with a darkening layer. */
export function Background({ bg, media }) {
const m = bg?.kind === 'media' ? media(bg.media_id) : null
return (
<div className="absolute inset-0" style={{ background: bg?.colour || '#111827' }} data-testid="signage-background">
{m && <img src={m.src} alt="" className="absolute inset-0 w-full h-full" style={{ objectFit: bg.fit || 'cover' }} />}
{m && bg.dim > 0 && <div className="absolute inset-0" style={{ background: `rgba(0,0,0,${bg.dim / 100})` }} />}
</div>
)
}
const JUSTIFY = { top: 'flex-start', middle: 'center', bottom: 'flex-end' }
const hasBar = (c, where) => (c.widgets || []).some(w => w.position === where)
export function TextSlide({ content: c, media, venue, now }) {
const scale = (c.text_size || 100) / 100
return (
<div className="absolute inset-0" style={{ color: c.text_colour || '#fff', fontFamily: FONTS[c.font] || FONTS.sans }}>
<Background bg={c.background} media={media} />
<div className="absolute inset-0 flex flex-col" style={{
justifyContent: JUSTIFY[c.vertical] || 'center', textAlign: c.align || 'center',
padding: `${hasBar(c, 'top-bar') ? 14 : 8}cqmin 8cqmin ${hasBar(c, 'bottom-bar') ? 14 : 8}cqmin`, gap: `${3 * scale}cqmin`,
}}>
{c.title && <h1 style={{ fontSize: `${9 * scale}cqmin`, lineHeight: 1.1, fontWeight: 800, margin: 0 }} data-testid="signage-title">{c.title}</h1>}
{c.body && <p style={{ fontSize: `${4.5 * scale}cqmin`, lineHeight: 1.35, margin: 0, whiteSpace: 'pre-line' }} data-testid="signage-body">{c.body}</p>}
</div>
<Widgets widgets={c.widgets} media={media} venue={venue} now={now} colour={c.text_colour} />
</div>
)
}
/** A menu board (5.5b): its page of items (pages flip on their own), photos, descriptions, badges, sold out. */
export function MenuSlide({ slide, media, venue, now, page, pages }) {
const { t } = useTranslation('terminal')
const { formatMoney } = useFormat()
const c = slide.content
const scale = (c.text_size || 100) / 100
const accent = c.accent_colour || '#f59e0b'
const groups = pages[page] || []
return (
<div className="absolute inset-0" style={{ color: c.text_colour || '#fff', fontFamily: FONTS[c.font] || FONTS.sans }}>
<Background bg={c.background} media={media} />
<div className="absolute inset-0 flex flex-col"
style={{ padding: `${hasBar(c, 'top-bar') ? 12 : 5}cqmin 5cqmin ${hasBar(c, 'bottom-bar') ? 12 : 5}cqmin` }}
data-testid="signage-menu" data-page={page} data-pages={pages.length}>
{c.title && <h1 style={{ fontSize: `${7 * scale}cqmin`, fontWeight: 800, margin: '0 0 2cqmin', color: accent, lineHeight: 1.1 }}>{c.title}</h1>}
<div style={{ columnCount: c.columns || 2, columnGap: '5cqmin', fontSize: `${3.2 * scale}cqmin`, flex: 1, columnFill: 'auto' }}>
{groups.map((g, i) => (
<div key={`${g.category?.id ?? 'all'}-${i}`} style={{ marginBottom: '1.6cqmin' }}>
{g.category && c.category_titles !== false && (
<h2 style={{ fontSize: '1.15em', color: accent, margin: '0 0 0.6cqmin', fontWeight: 700, breakAfter: 'avoid' }}
data-testid="signage-menu-heading">{g.category.name}</h2>
)}
{g.items.map(it => (
<div key={it.id} data-testid="signage-menu-item" data-id={it.id} data-available={it.available ? 'true' : 'false'}
style={{ display: 'flex', alignItems: 'center', gap: '1.5cqmin', breakInside: 'avoid', padding: '0.5cqmin 0',
opacity: it.available ? 1 : 0.5 }}>
{c.show_photos && (it.image
? <img src={it.image} alt="" style={{ width: '7cqmin', height: '7cqmin', objectFit: 'cover', borderRadius: '1cqmin', flexShrink: 0 }} />
: <span style={{ width: '7cqmin', height: '7cqmin', flexShrink: 0 }} />)}
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ display: 'flex', alignItems: 'baseline', gap: '1cqmin' }}>
<span style={{ textDecoration: it.available ? 'none' : 'line-through', fontWeight: 600 }}>{it.name}</span>
{it.badge && (
<span style={{ background: accent, color: '#111', fontSize: '0.55em', fontWeight: 800, padding: '0.2cqmin 0.9cqmin', borderRadius: '99cqmin',
textTransform: 'uppercase', letterSpacing: '0.05em', whiteSpace: 'nowrap' }} data-testid="signage-badge">{t(`signage.badges.${it.badge}`)}</span>
)}
{!it.available && <span style={{ fontSize: '0.6em', whiteSpace: 'nowrap' }} data-testid="signage-sold-out">{t('signage.soldOut')}</span>}
<span style={{ flex: 1, borderBottom: '0.25cqmin dotted currentColor', opacity: 0.35, minWidth: '2cqmin' }} />
{it.price != null && <span style={{ color: accent, fontVariantNumeric: 'tabular-nums', fontWeight: 700, whiteSpace: 'nowrap' }}
data-testid="signage-menu-price">{formatMoney(it.price)}</span>}
</div>
{it.description && <div style={{ fontSize: '0.7em', opacity: 0.75, lineHeight: 1.25 }}>{it.description}</div>}
</div>
</div>
))}
</div>
))}
</div>
{pages.length > 1 && (
<div style={{ display: 'flex', gap: '1cqmin', justifyContent: 'center', paddingTop: '1cqmin' }} aria-hidden="true">
{pages.map((_, i) => <span key={i} style={{ width: '1.2cqmin', height: '1.2cqmin', borderRadius: '50%', background: i === page ? accent : 'currentColor', opacity: i === page ? 1 : 0.3 }} />)}
</div>
)}
</div>
<Widgets widgets={c.widgets} media={media} venue={venue} now={now} colour={c.text_colour} />
</div>
)
}
// ── widgets (5.5b) ──────────────────────────────────────────────────────────
const SIZE = { small: 0.75, medium: 1, large: 1.4 }
const CORNER = {
'top-left': { top: '3cqmin', left: '3cqmin', alignItems: 'flex-start' },
'top-center': { top: '3cqmin', left: '50%', transform: 'translateX(-50%)', alignItems: 'center' },
'top-right': { top: '3cqmin', right: '3cqmin', alignItems: 'flex-end' },
'bottom-left': { bottom: '3cqmin', left: '3cqmin', alignItems: 'flex-start' },
'bottom-center': { bottom: '3cqmin', left: '50%', transform: 'translateX(-50%)', alignItems: 'center' },
'bottom-right': { bottom: '3cqmin', right: '3cqmin', alignItems: 'flex-end' },
}
/** The widgets of a slide, in their corners and bars. One that has nothing to show is left out. */
export function Widgets({ widgets, media, venue, now, colour }) {
if (!widgets?.length) return null
const bar = where => widgets.filter(w => w.position === where)
const bars = [['top-bar', { top: 0 }], ['bottom-bar', { bottom: 0 }]]
return (
<>
{bars.map(([where, pos]) => bar(where).length > 0 && (
<div key={where} className="absolute inset-x-0" data-testid={`signage-${where}`}
style={{ ...pos, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '3cqmin', padding: '2cqmin 4cqmin',
background: 'rgba(0,0,0,0.35)', color: colour || '#fff', minHeight: '9cqmin' }}>
{bar(where).map((w, i) => <Widget key={i} w={w} media={media} venue={venue} now={now} />)}
</div>
))}
{widgets.filter(w => CORNER[w.position]).map((w, i) => (
<div key={`c${i}`} className="absolute" style={{ ...CORNER[w.position], display: 'flex', flexDirection: 'column', color: colour || '#fff' }}>
<Widget w={w} media={media} venue={venue} now={now} />
</div>
))}
</>
)
}
const ICONS = { sun: Sun, moon: Moon, 'cloud-sun': CloudSun, 'cloud-moon': CloudMoon, cloud: Cloud, fog: CloudFog, rain: CloudRain, snow: CloudSnow, storm: CloudLightning }
function Widget({ w, media, venue, now }) {
const { formatDate, formatTime } = useFormat()
const s = SIZE[w.size] || 1
const text = { fontSize: `${3.4 * s}cqmin`, fontWeight: 700, lineHeight: 1.15, textShadow: '0 0.2cqmin 0.8cqmin rgba(0,0,0,0.35)' }
if (w.kind === 'logo') {
const m = media(w.media_id)
return m ? <img src={m.src} alt="" style={{ height: `${9 * s}cqmin`, maxWidth: `${30 * s}cqmin`, objectFit: 'contain' }} data-testid="signage-w-logo" /> : null
}
if (w.kind === 'name') return venue?.name ? <span style={{ ...text, fontSize: `${4.2 * s}cqmin` }} data-testid="signage-w-name">{venue.name}</span> : null
if (w.kind === 'date') return <span style={text} data-testid="signage-w-date">{formatDate(now, w.date_style === 'short' ? 'date' : 'dateLong')}</span>
if (w.kind === 'clock') return <span style={{ ...text, fontSize: `${4.6 * s}cqmin`, fontVariantNumeric: 'tabular-nums' }} data-testid="signage-w-clock">{formatTime(now, w.clock_seconds ? 'timeSeconds' : 'time')}</span>
if (w.kind === 'message') {
const msg = w.text || venue?.message
return msg ? <span style={{ ...text, fontWeight: 600 }} data-testid="signage-w-message">{msg}</span> : null
}
if (w.kind === 'qr') {
return w.link ? (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '0.8cqmin' }} data-testid="signage-w-qr" data-link={w.link}>
<div style={{ background: '#fff', padding: '1cqmin', borderRadius: '1cqmin', lineHeight: 0 }}>
<QRCodeSVG value={w.link} size={256} style={{ width: `${14 * s}cqmin`, height: `${14 * s}cqmin` }} />
</div>
{w.qr_caption && <span style={{ ...text, fontSize: `${2.4 * s}cqmin` }}>{w.qr_caption}</span>}
</div>
) : null
}
if (w.kind === 'weather') {
const wx = venue?.weather
if (!wx || wx.temperature == null) return null // no internet / no place: hidden (5.4c)
const Icon = ICONS[weatherIcon(wx.code, wx.is_day)] || Cloud
const deg = wx.units === 'fahrenheit' ? '°F' : '°C'
return (
<div style={{ display: 'flex', alignItems: 'center', gap: '1.5cqmin' }} data-testid="signage-w-weather">
<Icon style={{ width: `${6 * s}cqmin`, height: `${6 * s}cqmin` }} aria-hidden="true" />
<span style={{ ...text, fontSize: `${4.4 * s}cqmin` }}>{Math.round(wx.temperature)}{deg}</span>
{(w.weather_days || 1) > 1 && (wx.days || []).slice(1, w.weather_days).map(d => {
const DayIcon = ICONS[weatherIcon(d.code, true)] || Cloud
return (
<span key={d.date} style={{ display: 'flex', alignItems: 'center', gap: '0.6cqmin', ...text, fontSize: `${2.4 * s}cqmin`, fontWeight: 600 }}>
{formatDate(`${d.date}T12:00:00Z`, 'weekday').slice(0, 3)} <DayIcon style={{ width: `${3.4 * s}cqmin`, height: `${3.4 * s}cqmin` }} aria-hidden="true" />
{Math.round(d.max)}° / {Math.round(d.min)}°
</span>
)
})}
</div>
)
}
return null
}