Files
xenia-pos-local/manager_dashboard/src/pages/signage/MenuEditor.jsx
T
bonaminandClaude Opus 5.5 e8c8ec825e feat(signage): menu look part by part, board-only names, stack bar, Delete key (owner review 3)
- Menus (slides and widgets): headings / names / prices each with size,
  colour, font, bold / italic / underline and alignment; dots size, colour
  and style; line and letter spacing; photo size and shape (menuLook)
- Names aligned right end together, prices in a page-wide column
- Names for one board only (names, category_names) with a pencil on the
  board's list; the catalogue name stays for the waiters
- Fixed: menu lists inherited the page's fixed line height (big text
  overlapped); a menu slide's list grew past the screen (no 2nd column)
- Stacks: parts sized by dragging the lines on the bar; "Add a widget"
- Delete / Backspace removes the picked widget

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 10:51:03 +02:00

266 lines
18 KiB
React

// A menu board's settings (plan step 5.6b): which products (everything, whole categories that stay
// up to date, single products, minus hidden ones), the board's own order of categories and products
// (arrows on what the board shows), badges, prices of a sales point, photos / descriptions, sold-out
// style, columns and pages, the look, and the widgets. `slide.menu` is the server's resolution of
// the draft (the editor asks for it as you change things).
// 2026-11-21 (owner review): sections with titles; what each line shows in fewer rows (the four
// switches on one); the layout: columns, how wide the list is and where it sits (left / centre /
// right), products per page, seconds per page.
// Owner review 2 (2026-11-21): a product hidden from the board stays in the list, greyed, with a red
// crossed eye (a green eye on the ones shown): a click shows / hides it; seconds per page explained;
// the text's look on one row. `board` mode: the same editor for a menu widget's list (no background,
// widgets, width or place; its own products per page).
// Owner review 3 (2026-11-21): the look part by part (MenuLook: headings, names, prices, dots,
// spacing, photos); a pencil beside each category and product on the board gives it a name for
// this board only (the catalogue keeps its own: the waiters' names stay as they are).
import { useEffect, useMemo, useState } from 'react'
import { AlignCenter, AlignLeft, AlignRight, ArrowDown, ArrowUp, Eye, EyeOff, Pencil, RotateCcw, X } from 'lucide-react'
import { MENU_LANGUAGES, SALES_POINTS } from '@xenia/access'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Dropdown, Field, IconButton, Input, Segmented, Select, Switch } from '@xenia/ui'
import client from '../../api/client'
import { BADGES, boardOrder, moveGroup, moveItem, toggled } from './signageLogic'
import MenuLook from './MenuLook'
import { BackgroundField, Range, Section } from './SignageFields'
import WidgetsEditor from './WidgetsEditor'
let catalogue = null // categories and products, read once for every menu editor on the page
function useCatalogue() {
const [data, setData] = useState(catalogue || { cats: [], products: [] })
useEffect(() => {
if (catalogue) return
Promise.all([client.get('/api/products/categories'), client.get('/api/products/')]).then(([c, p]) => {
catalogue = { cats: c.data, products: p.data.filter(x => x.lifecycle_status === 'active' && !x.is_service_item) }
setData(catalogue)
}).catch(() => {})
}, [])
return data
}
export default function MenuEditor({ slide, onChange, media, onMedia, misfit = [], widget = null, onWidget, extras, board = false, testid = 'signage-menu-editor' }) {
const { t } = useTranslation('manager')
const { formatMoney } = useFormat()
const c = slide.content || {}
const setC = patch => onChange({ ...slide, content: { ...c, ...patch } })
const { cats, products } = useCatalogue()
const [q, setQ] = useState('')
const byId = useMemo(() => Object.fromEntries(products.map(p => [p.id, p])), [products])
const top = cats.filter(x => !x.parent_id)
const subsOf = id => cats.filter(x => x.parent_id === id)
const groups = slide.menu?.groups || []
const listed = new Set(groups.flatMap(g => g.items.map(i => i.id)))
const found = q.trim().length < 1 ? [] : products.filter(p => p.name.toLowerCase().includes(q.trim().toLowerCase()) && !listed.has(p.id)).slice(0, 8)
const reorder = next => setC(boardOrder(next))
const badges = c.badges || {}
const digits = v => Number(String(v).replace(/\D/g, ''))
const hidden = new Set(c.exclude_ids || [])
// this board's own names: empty = the catalogue's again
const rename = (key, id, name) => {
const next = { ...(c[key] || {}) }
if (name && name.trim()) next[String(id)] = name.trim()
else delete next[String(id)]
setC({ [key]: next })
}
return (
<div className="grid gap-7" data-testid={testid}>
{/* what it lists */}
<Section title={t('signage.menu.what')}>
<div className="grid gap-4 md:grid-cols-2 items-end">
<Field label={t('signage.title')}><Input value={c.title || ''} maxLength={120} onChange={e => setC({ title: e.target.value })} data-testid="signage-menu-title" /></Field>
<Field label={t('signage.menu.which')}>
<Dropdown value={c.select || 'custom'} onChange={v => setC({ select: v })} data-testid="signage-menu-select"
options={[{ value: 'all', label: t('signage.menu.all') }, { value: 'custom', label: t('signage.menu.custom') }]} />
</Field>
</div>
{c.select !== 'all' && <>
<div className="grid gap-1.5">
<span className="text-label font-medium text-ink">{t('signage.menu.categories')}</span>
<p className="text-caption text-ink-3">{t('signage.menu.categoriesHint')}</p>
<div className="flex flex-wrap gap-1.5" data-testid="signage-menu-categories">
{top.flatMap(cat => [cat, ...subsOf(cat.id)]).map(cat => {
const on = (c.category_ids || []).includes(cat.id)
return (
<button key={cat.id} type="button" aria-pressed={on} onClick={() => setC({ category_ids: toggled(c.category_ids, cat.id) })}
data-testid="signage-menu-category" data-name={cat.name}
className={`px-2.5 py-1 rounded-control border text-label transition-colors duration-fast
${on ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface-2 text-ink hover:bg-surface-3'}`}>
{cat.parent_id ? `↳ ${cat.name}` : cat.name}
</button>
)
})}
</div>
</div>
<div className="grid gap-1.5">
<span className="text-label font-medium text-ink">{t('signage.menu.products')}</span>
{(c.product_ids || []).length > 0 && (
<div className="flex flex-wrap gap-1.5">
{(c.product_ids || []).map(id => (
<span key={id} className="inline-flex items-center gap-1 pl-2.5 pr-1 py-0.5 rounded-control bg-surface-2 text-label text-ink">
{byId[id]?.name ?? `#${id}`}
<IconButton size="sm" variant="ghost" label={t('media.removePhoto')}
onClick={() => setC({ product_ids: (c.product_ids || []).filter(x => x !== id), exclude_ids: (c.exclude_ids || []).filter(x => x !== id) })}><X size={12} /></IconButton>
</span>
))}
</div>
)}
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('signage.menu.searchProduct')} aria-label={t('signage.menu.searchProduct')} data-testid="signage-menu-search" />
{found.length > 0 && (
<ul className="grid gap-1" data-testid="signage-menu-found">
{found.map(p => (
<li key={p.id}>
<button type="button" className="w-full text-left px-2 py-1 rounded-control hover:bg-surface-2 text-label text-ink" data-testid="signage-menu-found-item"
onClick={() => { setC({ product_ids: toggled(c.product_ids, p.id), exclude_ids: (c.exclude_ids || []).filter(x => x !== p.id) }); setQ('') }}>
+ {p.name}
</button>
</li>
))}
</ul>
)}
</div>
</>}
</Section>
{/* the board's own order, badges, hiding (hidden ones stay here, greyed) */}
<Section title={t('signage.menu.onTheBoard', { count: slide.menu?.count ?? 0 })}>
<p className="text-caption text-ink-3 -mt-1">{t('signage.menu.orderHint')}</p>
<div className="grid gap-3 max-h-[28rem] overflow-auto pr-1 rounded-control border border-line bg-surface-2 p-3" data-testid="signage-menu-board">
{groups.map((g, gi) => (
<div key={`${g.category?.id ?? 'all'}`} className="grid gap-1" data-testid="signage-menu-group" data-name={g.category?.name ?? ''}>
{g.category && (
<div className="flex items-center gap-1">
<OwnName name={g.category.name} original={g.category.original} onSave={v => rename('category_names', g.category.id, v)}
className="text-label font-semibold text-ink" testid="signage-category-name" />
<IconButton size="sm" variant="ghost" label={t('display.earlier')} disabled={gi === 0} onClick={() => reorder(moveGroup(groups, gi, -1))} data-testid="signage-group-up"><ArrowUp size={14} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('display.later')} disabled={gi === groups.length - 1} onClick={() => reorder(moveGroup(groups, gi, 1))}><ArrowDown size={14} /></IconButton>
</div>
)}
<ul className="grid gap-0.5">
{g.items.map((it, ii) => {
const off = it.hidden || hidden.has(it.id)
return (
<li key={it.id} className="flex items-center gap-1.5 pl-2 rounded-control hover:bg-surface"
data-testid="signage-menu-row" data-id={it.id} data-hidden={off ? 'true' : undefined}>
<OwnName name={it.name} original={it.original} onSave={v => rename('names', it.id, v)} testid="signage-item-name"
className={`text-label ${off ? 'text-ink-3 opacity-70' : it.available ? 'text-ink' : 'text-ink-3 line-through'}`} />
{off && <span className="text-caption text-ink-3">{t('signage.menu.hiddenTag')}</span>}
{it.price != null && <span className={`text-caption tabular-nums ${off ? 'text-ink-3 opacity-70' : 'text-ink-2'}`}>{formatMoney(it.price)}</span>}
<Select size="sm" value={badges[String(it.id)] || ''} aria-label={t('signage.menu.badge')} data-testid="signage-menu-badge" disabled={off}
onChange={e => {
const next = { ...badges }
if (e.target.value) next[String(it.id)] = e.target.value
else delete next[String(it.id)]
setC({ badges: next })
}}
options={[{ value: '', label: t('signage.menu.noBadge') }, ...BADGES.map(b => ({ value: b, label: t(`signage.badges.${b}`) }))]} />
<IconButton size="sm" variant="ghost" label={t('display.earlier')} disabled={ii === 0} onClick={() => reorder(moveItem(groups, gi, ii, -1))} data-testid="signage-item-up"><ArrowUp size={14} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('display.later')} disabled={ii === g.items.length - 1} onClick={() => reorder(moveItem(groups, gi, ii, 1))}><ArrowDown size={14} /></IconButton>
<IconButton size="sm" variant="ghost" label={t(off ? 'signage.menu.showAgain' : 'signage.menu.hide')} data-testid="signage-item-hide" aria-pressed={off}
onClick={() => setC({ exclude_ids: toggled(c.exclude_ids, it.id) })}>
{off ? <EyeOff size={15} className="text-danger-ink" /> : <Eye size={15} className="text-success-ink" />}
</IconButton>
</li>
)
})}
</ul>
</div>
))}
{groups.length === 0 && <p className="text-label text-ink-3">{t('signage.menu.empty')}</p>}
</div>
</Section>
{/* what each line shows: the four switches on one row, then the rest (2026-11-21) */}
<Section title={t('signage.menu.options')}>
<div className="grid gap-3 grid-cols-2 xl:grid-cols-4">
<Switch boxed checked={c.group_by_category !== false} onChange={on => setC({ group_by_category: on })} label={t('signage.menu.group')} data-testid="signage-menu-group-switch" />
<Switch boxed checked={c.category_titles !== false} onChange={on => setC({ category_titles: on })} label={t('signage.menu.headings')} />
<Switch boxed checked={!!c.show_photos} onChange={on => setC({ show_photos: on })} label={t('signage.menu.photos')} data-testid="signage-menu-photos" />
<Switch boxed checked={!!c.show_descriptions} onChange={on => setC({ show_descriptions: on })} label={t('signage.menu.descriptions')} />
</div>
<div className="grid gap-4 md:grid-cols-3 items-start">
<Field label={t('signage.menu.prices')}>
<Dropdown value={c.sales_point ?? ''} onChange={v => setC({ sales_point: v || null })} data-testid="signage-menu-prices"
options={[{ value: '', label: t('signage.menu.noPrices') }, ...SALES_POINTS.map(sp => ({ value: sp, label: t(`common:salesPoints.${sp}.name`) }))]} />
</Field>
<Field label={t('signage.menu.language')} hint={t('signage.menu.languageHint')}>
<Dropdown value={c.language ?? ''} onChange={v => setC({ language: v || null })} data-testid="signage-menu-language"
options={[{ value: '', label: t('signage.menu.asTyped') }, ...MENU_LANGUAGES.map(l => ({ value: l, label: t(`products.editor.lang.${l}`) }))]} />
</Field>
<Field label={t('signage.menu.soldOut')}>
<Dropdown value={c.sold_out || 'strike'} onChange={v => setC({ sold_out: v })}
options={[{ value: 'strike', label: t('signage.menu.strike') }, { value: 'hide', label: t('signage.menu.hideSoldOut') }]} />
</Field>
</div>
</Section>
{/* the layout: columns, how wide the list is and where it sits, the pages (2026-11-21) */}
<Section title={t('signage.menu.layout')}>
<div className={`grid gap-4 grid-cols-2 items-start ${board ? 'xl:grid-cols-3' : 'xl:grid-cols-5'}`}>
<Field label={t('signage.columns')}>
<Segmented value={String(c.columns || (board ? 1 : 2))} onChange={v => setC({ columns: Number(v) })} data-testid="signage-columns" fill
options={[1, 2, 3].map(n => ({ value: String(n), label: String(n), testId: `signage-columns-${n}` }))} />
</Field>
{!board && <>
<Range label={t('signage.menu.width')} value={c.board_width || 100} min={40} max={100} step={5} suffix="%"
onChange={v => setC({ board_width: v })} testid="signage-menu-width" />
<Field label={t('signage.menu.place')} hint={(c.board_width || 100) >= 100 ? t('signage.menu.placeHint') : undefined}>
<Segmented value={c.board_align || 'left'} onChange={v => setC({ board_align: v })} data-testid="signage-menu-align" fill
options={[['left', AlignLeft], ['center', AlignCenter], ['right', AlignRight]].map(([v, Icon]) => (
{ value: v, icon: <Icon size={16} aria-hidden="true" />, ariaLabel: t(`signage.aligns.${v}`), testId: `signage-menu-align-${v}` }))} />
</Field>
</>}
<Field label={t('signage.menu.perPage')} hint={t(board ? 'signage.menu.perPageWidgetHint' : 'signage.menu.perPageHint')}>
<Input inputMode="numeric" value={c.page_size ? String(c.page_size) : ''} placeholder={board ? '8' : t('signage.menu.auto')}
onChange={e => { const n = digits(e.target.value); setC({ page_size: n ? Math.min(60, Math.max(3, n)) : null }) }} data-testid="signage-menu-per-page" />
</Field>
<Field label={t('signage.menu.pageSeconds')} hint={t('signage.menu.pageSecondsHint')}>
<Input inputMode="numeric" value={String(c.page_seconds || 10)} onChange={e => setC({ page_seconds: Math.min(120, Math.max(3, digits(e.target.value) || 3)) })} />
</Field>
</div>
</Section>
<MenuLook c={c} setC={setC} />
{!board && <>
<BackgroundField value={c.background} onChange={background => setC({ background })} media={media} onMedia={onMedia}
misfit={misfit.includes(c.background?.media_id)} />
<WidgetsEditor widgets={c.widgets || []} onChange={widgets => setC({ widgets })} media={media} onMedia={onMedia} selected={widget} onSelect={onWidget}
extras={extras} />
</>}
</div>
)
}
/**
* A category's or product's name on the board, with a pencil (shown on hover or focus) to give it a
* name for this board only; the catalogue's name shows under it then, with a button to go back to it.
*/
function OwnName({ name, original, onSave, className = '', testid }) {
const { t } = useTranslation('manager')
const [edit, setEdit] = useState(null)
if (edit != null) {
const done = () => { onSave(edit); setEdit(null) }
return (
<span className="flex-1 min-w-0">
<Input size="sm" autoFocus value={edit} maxLength={80} aria-label={t('signage.menu.ownName')} data-testid={`${testid}-input`}
onChange={e => setEdit(e.target.value)} onBlur={done}
onKeyDown={e => { if (e.key === 'Enter') done(); if (e.key === 'Escape') setEdit(null) }} />
</span>
)
}
return (
<span className="group/name flex-1 min-w-0 flex items-center gap-1">
<span className="min-w-0 grid">
<span className={`truncate ${className}`} data-testid={testid}>{name}</span>
{original && <span className="truncate text-caption text-ink-3" title={t('signage.menu.catalogueName')}>{t('signage.menu.catalogueNameIs', { name: original })}</span>}
</span>
<IconButton size="sm" variant="ghost" label={t('signage.menu.rename')} onClick={() => setEdit(name)} data-testid={`${testid}-edit`}
className="opacity-0 group-hover/name:opacity-100 focus-visible:opacity-100 shrink-0"><Pencil size={13} /></IconButton>
{original && (
<IconButton size="sm" variant="ghost" label={t('signage.menu.renameBack')} onClick={() => onSave('')} data-testid={`${testid}-reset`}
className="opacity-0 group-hover/name:opacity-100 focus-visible:opacity-100 shrink-0"><RotateCcw size={13} /></IconButton>
)}
</span>
)
}