// The widgets of a text, menu or blank slide (plan step 5.6b; reworked 2026-11-21): logo or picture,
// name, date, clock, message of the day, a text of its own, QR code, weather, a menu, and stacks.
// Each in its own card: its name first, then its settings on one row (where, size, alignment, its
// own options), what needs more room on the rows under it. Where / size read "Custom" once it was
// moved or sized by hand on the preview (WidgetCanvas); picking a corner or a bar puts it back. The
// card of the widget picked on the preview is highlighted (the page doesn't jump to it: the preview
// stays in view while you drag).
// Owner review 2 (2026-11-21): the widgets' order (arrows, or drag a card by its handle; a later
// widget is drawn over an earlier one, and comes later in a bar); a text widget; weather for
// tomorrow / the day after and in three styles; a menu widget (a board's list in its box); stacks:
// parts side by side or one over the other, each its share (%), showing a widget, another stack (one
// level deep) or a whole slide from the Slides library.
// Owner review 3 (2026-11-21): a stack's parts sized by dragging the lines between them on its bar;
// "Add a widget" beside what a part shows; Delete removes the picked widget (WidgetCanvas).
// Owner review 4 (2026-11-21): a part holds several widgets, added with "Add a widget".
// Owner review 5 (2026-11-21): each widget's own look ("Text and colours": colour, a plate behind
// it, font, bold / italic / underline, text size; a QR code's colours); a widget in a stack's part
// has every setting a slide's widget has, aligned both ways in its place or moved and sized there on
// the preview (Custom); the message of the day changed from its widget. The picked widget is a path
// ("2", "2.1.0": see WidgetCanvas).
// Up to the venue's limit (10 recommended; Signage → Message & settings → Performance).
import { Fragment, useEffect, useRef, useState } from 'react'
import {
AlignCenter, AlignLeft, AlignRight, AlignVerticalJustifyCenter, AlignVerticalJustifyEnd, AlignVerticalJustifyStart, ArrowDown, ArrowUp, Bold, Italic, Palette, RotateCcw, Underline, BookImage, CalendarDays, ChevronDown, Clock, CloudSun, Columns3, GripVertical,
ImageIcon, LayoutList, MessageSquare, Plus, QrCode, Rows3, Store, Trash2, Type, UtensilsCrossed,
} from 'lucide-react'
import { cellWidgets, evenShares, setShare } from '@xenia/signage'
import { useTranslation } from '@xenia/i18n'
import { Button, ColourField, Dialog, Dropdown, Field, FieldRow, IconButton, Input, Popover, Segmented, Slider, Switch } from '@xenia/ui'
import MessageOfTheDay from './MessageOfTheDay'
import { usePerformance } from './usePerformance'
import * as api from './signageApi'
import MenuEditor from './MenuEditor'
import { MediaPick, Section } from './SignageFields'
import {
CELL_KINDS, cellWidget, fillPlace, FONT_KEYS, WIDGET_COLOURS, moved, newWidget, pickedAfterMove, presetWidget, WEATHER_CHOICES, weatherChoice, WIDGET_KINDS, WIDGET_POSITIONS,
} from './signageLogic'
const ICON = { logo: ImageIcon, name: Store, date: CalendarDays, clock: Clock, message: MessageSquare, text: Type, qr: QrCode, weather: CloudSun, menu: UtensilsCrossed, hstack: Columns3, vstack: Rows3 }
const iconOf = w => (w.kind === 'stack' ? (w.direction === 'column' ? Rows3 : Columns3) : ICON[w.kind] || LayoutList)
const kindLabel = (t, w) => t(`signage.widgets.kinds.${w.kind === 'stack' ? (w.direction === 'column' ? 'vstack' : 'hstack') : w.kind}`)
const ROW = 'grid gap-4 items-end grid-cols-[repeat(auto-fill,minmax(11rem,1fr))]'
let library = null // the Slides library (for stacks' parts), read once
function useLibrary() {
const [list, setList] = useState(library || [])
useEffect(() => {
api.listSaved().then(r => { library = r.slides; setList(r.slides) }).catch(() => {})
}, [])
return list
}
export default function WidgetsEditor({ widgets = [], onChange, media, onMedia, selected = null, onSelect, extras = {} }) {
const { t } = useTranslation('manager')
const most = usePerformance().max_widgets // the venue's limit (Performance; 10 recommended)
const [drag, setDrag] = useState(null) // { from, at }: the card dragged and where it would land
const set = (i, patch) => onChange(widgets.map((w, j) => (j === i ? { ...w, ...patch } : w)))
const add = kind => {
onChange([...widgets, newWidget(kind)])
onSelect?.(String(widgets.length))
}
const move = (from, to) => {
if (to < 0 || to >= widgets.length || from === to) return
onChange(moved(widgets, from, to))
const top = selected != null && !String(selected).includes('.') ? Number(selected) : null
onSelect?.(top == null ? null : String(pickedAfterMove(top, from, to)))
}
const over = (e, i) => {
if (!drag) return
e.preventDefault()
const r = e.currentTarget.getBoundingClientRect()
const at = e.clientY < r.top + r.height / 2 ? i : i + 1
if (at !== drag.at) setDrag(d => ({ ...d, at }))
}
const drop = () => {
if (drag && drag.at != null) move(drag.from, drag.at > drag.from ? drag.at - 1 : drag.at)
setDrag(null)
}
const line = i => drag && drag.at === i && drag.at !== drag.from && drag.at !== drag.from + 1 && (
}
)
}
/** How the widget lines up in its place: auto, left, centre, right. */
function AlignField({ w, set, testid = 'signage-widget-align' }) {
const { t } = useTranslation('manager')
return (
set({ align })} aria-label={t('signage.widgets.align')} data-testid={testid} fill
options={[
{ value: 'auto', label: t('signage.widgets.alignAuto'), testId: `${testid}-auto` },
{ value: 'left', icon: , ariaLabel: t('signage.aligns.left'), testId: `${testid}-left` },
{ value: 'center', icon: , ariaLabel: t('signage.aligns.center'), testId: `${testid}-center` },
{ value: 'right', icon: , ariaLabel: t('signage.aligns.right'), testId: `${testid}-right` },
]} />
)
}
/** How a widget in a stack's part lines up from top to bottom in its place. */
function VerticalField({ w, set }) {
const { t } = useTranslation('manager')
return (
set({ vertical })} fill data-testid="signage-cell-vertical"
options={[['top', AlignVerticalJustifyStart], ['middle', AlignVerticalJustifyCenter], ['bottom', AlignVerticalJustifyEnd]].map(([v, Icon]) => (
{ value: v, icon: , ariaLabel: t(`signage.verticals.${v}`), testId: `signage-cell-vertical-${v}` }))} />
)
}
// which widgets have text (their colour, font, style and size), a QR code's own colours, a plate
const TEXT_KINDS = ['name', 'date', 'clock', 'message', 'text', 'qr', 'weather']
/**
* A widget's own look (owner review 5), folded under "Text and colours": its colour (text and
* pictures; empty = the slide's), a plate behind it, its font, bold / italic / underline and text
* size; a QR code's dots and background. A logo has only the plate; a menu has its own look.
*/
function LookFields({ w, set }) {
const { t } = useTranslation('manager')
const [open, setOpen] = useState(false)
if (w.kind === 'menu' || w.kind === 'stack') return null
const text = TEXT_KINDS.includes(w.kind)
const changed = !!(w.colour || w.background_colour || w.font || w.bold != null || w.italic || w.underline || (w.text_size && w.text_size !== 100) || w.qr_colour || w.qr_background)
const styles = [w.bold !== false && 'bold', w.italic && 'italic', w.underline && 'underline'].filter(Boolean)
return (
)
}
/** The message of the day's hint, and the message itself changed right here (owner review 5). */
function MessageHint() {
const { t } = useTranslation('manager')
const [open, setOpen] = useState(false)
return (
{t('signage.widgets.ownTextHint')}
{open && (
)}
)
}
/** The options only some widgets have, on the same row as where / size / alignment. */
function InlineFields({ w, set }) {
const { t } = useTranslation('manager')
if (w.kind === 'message') {
return (
set({ text: e.target.value || null })} data-testid="signage-widget-text" />
)
}
if (w.kind === 'text') {
return (
set({ text: e.target.value || null })} data-testid="signage-widget-own-text" />
)
}
if (w.kind === 'date') {
return (
set({ date_style })}
options={[{ value: 'long', label: t('signage.widgets.dateLong') }, { value: 'short', label: t('signage.widgets.dateShort') }]} />
)
}
if (w.kind === 'clock') {
return (
set({ clock_seconds: on })} label={t('signage.widgets.seconds')} data-testid="signage-widget-seconds" />
)
}
if (w.kind === 'qr') {
return (
set({ qr_target })}
options={['ordering', 'menu', 'custom'].map(q => ({ value: q, label: t(`display.qrTargets.${q}`) }))} />
)
}
if (w.kind === 'weather') {
return (<>
{ const [from, days] = v.split('-').map(Number); set({ weather_from: from, weather_days: days }) }}
options={WEATHER_CHOICES.map(([from, days]) => ({ value: `${from}-${days}`, label: t(`signage.widgets.weatherChoices.${from}-${days}`) }))} />
set({ weather_style })} data-testid="signage-widget-weather-style"
options={['both', 'icon', 'text'].map(s => ({ value: s, label: t(`signage.widgets.weatherStyles.${s}`) }))} />
>)
}
return null
}
/** What needs more room, under the row: the picture, hints, the QR's own link, a menu's list, a stack's parts. */
function BelowFields({ w, set, media, onMedia, extras, path, depth, selected, onSelect }) {
const { t } = useTranslation('manager')
if (w.kind === 'logo') {
return { onMedia(item); set({ media_id: item.id }) }} />
}
if (w.kind === 'message') return
if (w.kind === 'weather') return
)
}
if (w.kind === 'menu') return
if (w.kind === 'stack') return
return null
}
/** A menu widget's list and look: the menu board's editor, folded away until it is opened. */
function MenuBoardFields({ w, set, extras, path }) {
const { t } = useTranslation('manager')
const [open, setOpen] = useState(false)
const menu = extras?.[path]?.menu
return (
{open && (
set({ board: s.content })} />
)}
)
}
/**
* A stack's parts: side by side or one over the other, how many (2–6), their sizes (drag the lines
* between them on the bar, or focus one and use the arrow keys; "Split evenly"), and what each
* shows: widgets added with "Add a widget" (several share the part, owner review 4: one over the
* other in a side-by-side stack, side by side in the other; each with its settings, moved up or
* down, removed), another stack (one level deep), or a slide from the Slides library.
*/
function StackFields({ w, set, media, onMedia, extras, path, depth, selected, onSelect }) {
const { t } = useTranslation('manager')
const slides = useLibrary()
const cells = w.cells || []
const shares = cells.map(c => Math.round(c.share || 0))
const setCells = next => set({ cells: next })
const setCell = (j, patch) => setCells(cells.map((c, k) => {
if (k !== j) return c
const { widget: _old, ...rest } = { ...c, ...patch } // a part stored with one `widget`: now `widgets`
return rest
}))
const setShares = next => setCells(cells.map((c, k) => ({ ...c, share: next[k] })))
const count = n => {
const even = evenShares(n)
setCells(Array.from({ length: n }, (_, k) => ({ ...(cells[k] || {}), share: even[k] })))
}
const setWidgets = (j, list) => setCell(j, { widgets: list, slide_id: null })
const add = (j, v) => {
if (v === 'slide') setCell(j, { widgets: [], slide_id: slides[0]?.id ?? null })
else setWidgets(j, [...cellWidgets(cells[j]), cellWidget(v)])
}
const kinds = [...CELL_KINDS, ...(depth === 0 ? ['hstack', 'vstack'] : [])]
const label = c => {
const ws = cellWidgets(c)
if (ws.length) return ws.map(x => kindLabel(t, x)).join(' · ')
if (c.slide_id != null) return slides.find(x => x.id === c.slide_id)?.name || t('signage.widgets.stack.slide')
return t('signage.widgets.stack.nothing')
}
return (