// What the two slide editors share (a playlist's slide, a library slide; 2026-11-21): the draft // resolved by the server for its preview, and the preview's widget handles. import { useEffect, useState } from 'react' import { WIDGET_HOSTS } from '@xenia/signage' import * as api from './signageApi' import { placeWidget, updateWidgetAt } from './signageLogic' // what doesn't change what the server adds (where things sit, how they look): left out of the key const LOOK = new Set(['box', 'position', 'size', 'align', 'background', 'text_colour', 'accent_colour', 'font', 'text_size', 'board_width', 'board_align', 'title', 'body', 'vertical', 'columns', 'page_seconds', 'direction', 'share', 'clock_seconds', 'date_style', 'heading_style', 'item_style', 'price_style', 'dot_style', 'line_height', 'letter_spacing', 'photo_size', 'photo_shape', 'badge_text_colour']) /** * The draft as the server resolves it for the preview (2026-11-21): a menu board's products * (`menu`, hidden ones kept and marked) and its widgets' extras (a menu widget's products, a * library slide inside a stack, QR links) with the media they show (`onMedia(map)`). */ export function useSlidePreview(draft, setDraft, onMedia) { const host = draft && WIDGET_HOSTS.includes(draft.kind) const key = host ? JSON.stringify([draft.kind, draft.content || {}], (k, v) => (LOOK.has(k) ? undefined : v)) : null useEffect(() => { if (!key) return undefined const kind = draft.kind const content = draft.content || {} const h = setTimeout(() => { api.previewSlide(kind, content).then(r => { if (r.media && Object.keys(r.media).length) onMedia?.(r.media) setDraft(d => (d && d.kind === kind ? { ...d, extras: r.extras, ...(kind === 'menu' ? { menu: r.menu } : {}) } : d)) }).catch(() => {}) }, 400) return () => clearTimeout(h) // the key holds what counts in the draft // eslint-disable-next-line react-hooks/exhaustive-deps }, [key, setDraft]) } /** * The widget picked on the preview and how a move / resize there changes the draft. * → { widget, setWidget, editor } (editor: SlidePreview's `editor` prop). Picks nothing when the * slide changes (`slideKey`). */ export function useWidgetEditor(setDraft, slideKey, label) { const [state, setState] = useState({ key: slideKey, widget: null }) const widget = state.key === slideKey ? state.widget : null const setWidget = w => setState({ key: slideKey, widget: w }) // `widget` is a path: "2", or "2.1.0" for a widget inside a stack's part (owner review 5) const onPlace = (path, box) => setDraft(d => ({ ...d, content: { ...d.content, widgets: updateWidgetAt(d?.content?.widgets || [], path, w => placeWidget(w, box)) } })) const onRemove = path => { setDraft(d => ({ ...d, content: { ...d.content, widgets: updateWidgetAt(d?.content?.widgets || [], path, () => null) } })) setWidget(null) } return { widget, setWidget, editor: { selected: widget, onSelect: setWidget, onPlace, onRemove, label } } }