feat(terminal): the menu tablet app (platform-v2 step 5.7a)
terminal/src/apps/menu-tablet: the venue's menu for staff (a quick price
check) and guests. Category chips that jump to their section, or one A–Z
list; compact rows, expanded rows (photo, description) or full cards; tap a
product to see it big (photo, price, description, tags); search that ignores
accents and also finds names typed in Latin letters ("souvl"); sold out
marked; the guest's language for the tablet's texts; back to the start (search
cleared, first language, top) after the set time without a touch; light or
dark; the last menu kept for offline; live on menu_tablet.changed /
device.updated. @xenia/i18n exports ./latin for plain-JS use.
tests: logic (3), e2e menu_tablet.mjs (10 checks, in run-all).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// The menu tablet's logic (plan step 5.7a). Pure: unit-tested in Node (tests/menu-tablet.test.mjs).
|
||||
import { latin } from '@xenia/i18n/latin' // plain JS (Node tests)
|
||||
|
||||
/** Lowercase, no accents: "Σουβλάκι" and "σουβλακι" match. */
|
||||
export function fold(text) {
|
||||
return String(text ?? '').normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim()
|
||||
}
|
||||
|
||||
/** Does a product match a search? By its name in Greek or in Latin letters ("souvlaki"), or a tag. */
|
||||
export function matches(product, query) {
|
||||
const q = fold(query)
|
||||
if (!q) return true
|
||||
const name = fold(product.name)
|
||||
return name.includes(q) || fold(latin(product.name)).includes(q) || (product.tags || []).some(t => fold(t).includes(q))
|
||||
}
|
||||
|
||||
/** Every product once, A to Z (the ungrouped list and the search's results). */
|
||||
export function allProducts(data, language = 'el') {
|
||||
return Object.values(data?.products || {}).sort((a, b) => a.name.localeCompare(b.name, language))
|
||||
}
|
||||
|
||||
/** What a search finds: the products whose name (or tag) matches, A to Z; a name that starts with it first. */
|
||||
export function search(data, query, language = 'el') {
|
||||
const q = fold(query)
|
||||
if (!q) return []
|
||||
const found = allProducts(data, language).filter(p => matches(p, q))
|
||||
const starts = p => fold(p.name).startsWith(q) || fold(latin(p.name)).startsWith(q)
|
||||
return [...found.filter(starts), ...found.filter(p => !starts(p))]
|
||||
}
|
||||
|
||||
/** The sections to show: grouped = each category with its products; not grouped = one A–Z list. */
|
||||
export function sections(data, grouped, language = 'el') {
|
||||
if (!data) return []
|
||||
if (!grouped) return [{ id: 'all', name: null, depth: 0, products: allProducts(data, language) }]
|
||||
return (data.categories || [])
|
||||
.map(c => ({ ...c, products: c.products.map(id => data.products[String(id)]).filter(Boolean) }))
|
||||
.filter(c => c.products.length)
|
||||
}
|
||||
|
||||
/** Back to the start? After `seconds` without a touch (0 = never). */
|
||||
export function idle(lastTouchMs, seconds, nowMs = Date.now()) {
|
||||
return Number(seconds) > 0 && nowMs - lastTouchMs >= Number(seconds) * 1000
|
||||
}
|
||||
|
||||
/** The languages a guest can pick, the first one being where the tablet starts. */
|
||||
export function languagesOf(config) {
|
||||
if (config?.languages === 'el') return ['el']
|
||||
if (config?.languages === 'en') return ['en']
|
||||
return ['el', 'en']
|
||||
}
|
||||
Reference in New Issue
Block a user