@wix/himalaya-cli 0.2024.0 → 0.2026.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/schema.d.ts +20 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/validator.d.ts +38 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/core.mjs +24 -3
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/sdui.mjs +17 -1
- package/dist/cli.mjs +26 -4
- package/package.json +2 -2
- package/templates/beauty-salon-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/community-club-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/events-meetups-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/gym-fitness-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/nonprofit-cause-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/owner-lite/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/podcast-creator-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/portfolio-creative-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/restaurant-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/retail-store-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
- package/templates/yoga-studio-owner/tier5-src/himi-owner-lite-today/index.ts +146 -13
|
@@ -23,7 +23,7 @@ import { queryEvents } from "@himalaya/wix-events";
|
|
|
23
23
|
import { queryDraftPosts } from "@himalaya/wix-blog";
|
|
24
24
|
import {
|
|
25
25
|
str, hhmm, sameLocalDay, dayLabel, rankItems, glanceFields, bookingItems, orderItems, eventItems, blogItems, shownUntil,
|
|
26
|
-
GLANCE_COPY, focusFields, focusList, type GlanceFields, type GlanceFocus, type GlanceFocusOption,
|
|
26
|
+
GLANCE_COPY, focusFields, focusList, type GlanceFields, type GlanceFocus, type GlanceFocusOption, type GlanceItem,
|
|
27
27
|
} from "./glance.js";
|
|
28
28
|
|
|
29
29
|
export { isSectionUnavailable, isSignedOut, dayLabel };
|
|
@@ -45,9 +45,24 @@ export interface TodayBooking {
|
|
|
45
45
|
|
|
46
46
|
/** The glance ("what matters now", glance.ts) rides on the same state: the existing fields stay
|
|
47
47
|
* for the embedding hosts that bind them (the embedding guide), the glance fields are added. */
|
|
48
|
+
/** One future frame of a focus's timeline: the glance as it will read from `startsAt` (epoch
|
|
49
|
+
* SECONDS), re-ranked at that moment, with the score of what it headlines. */
|
|
50
|
+
export type GlanceFrame = GlanceFields & { startsAt: number; score: number; durationSeconds: number };
|
|
51
|
+
|
|
52
|
+
/** One focus's glance now, and the frames that follow it. */
|
|
53
|
+
export type FocusGlance = GlanceFields & {
|
|
54
|
+
validUntil: number;
|
|
55
|
+
/** The headline's score (0 when nothing is waiting): the widget's Smart Stack relevance. */
|
|
56
|
+
score: number;
|
|
57
|
+
/** Future frames, ascending, at most `MAX_FRAMES - 1` within 24 h; each differs from the one before. */
|
|
58
|
+
frames: GlanceFrame[];
|
|
59
|
+
/** Epoch SECONDS the last frame stops being true; equals `validUntil` without frames. */
|
|
60
|
+
timelineUntil: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
48
63
|
export interface TodayState extends GlanceFields {
|
|
49
64
|
/** The glance for each focus a widget may be set to ("now" + the provisioned areas). */
|
|
50
|
-
focuses: Record<GlanceFocus,
|
|
65
|
+
focuses: Record<GlanceFocus, FocusGlance>;
|
|
51
66
|
/** Those focuses with owner-facing titles, in order — the widget picker's list. */
|
|
52
67
|
focusList: GlanceFocusOption[];
|
|
53
68
|
eventsReady: boolean;
|
|
@@ -150,8 +165,46 @@ function upcoming(rows: Rec[] | null, statuses: string[], now: Date): Rec[] {
|
|
|
150
165
|
.sort((a, b) => Date.parse(startOf(a)) - Date.parse(startOf(b)) || str(a, "id").localeCompare(str(b, "id")));
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
/** A widget timeline is small: WidgetKit budgets its reloads, and a frame is a whole tree. */
|
|
169
|
+
export const MAX_FRAMES = 8;
|
|
170
|
+
export const TIMELINE_HOURS = 24;
|
|
171
|
+
/** A count read now is not vouched for past this: a later frame drops it. */
|
|
172
|
+
export const COUNTS_FRESH_MS = 3 * 60 * 60 * 1000;
|
|
173
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
174
|
+
|
|
175
|
+
interface ShapeOptions {
|
|
176
|
+
/** Compute the future frames (off for the frames themselves). */
|
|
177
|
+
timeline?: boolean;
|
|
178
|
+
/** This frame is far enough from the read that its counts are dropped. */
|
|
179
|
+
countsStale?: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The moments a glance changes on its own, with no new read: a booking enters its last hour
|
|
183
|
+
* and starts, an event enters its last 3 h and starts, a post goes live, the day turns, and the
|
|
184
|
+
* counts stop being fresh. Ascending, after `now`, within 24 h. Only the `MAX_FRAMES` soonest
|
|
185
|
+
* starts of each source: no focus keeps more frames than that, and every moment costs a full
|
|
186
|
+
* `shape`, so a day of 50 bookings must not mean 100 of them on every publish. */
|
|
187
|
+
function frameTimes(sources: Sources, now: Date): number[] {
|
|
188
|
+
const t0 = now.getTime();
|
|
189
|
+
const at = (s: string) => Date.parse(s);
|
|
190
|
+
// A second past a start: an item is still upcoming AT its start (`start >= now`).
|
|
191
|
+
const started = (s: number) => s + 1000;
|
|
192
|
+
const soonest = (starts: number[]) =>
|
|
193
|
+
starts.filter((s) => Number.isFinite(s) && started(s) > t0).sort((a, b) => a - b).slice(0, MAX_FRAMES);
|
|
194
|
+
const times = [
|
|
195
|
+
...soonest((sources.bookings ?? []).map((b) => at(startOf(b)))).flatMap((s) => [s - HOUR_MS, started(s)]),
|
|
196
|
+
...soonest((sources.events ?? []).map((e) => at(str(e, "dateAndTimeSettings.startDate")))).flatMap((s) => [s - 3 * HOUR_MS, started(s)]),
|
|
197
|
+
...soonest((sources.drafts ?? []).map((d) => at(str(d, "scheduledPublishDate")))).map(started),
|
|
198
|
+
new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime(),
|
|
199
|
+
t0 + COUNTS_FRESH_MS,
|
|
200
|
+
];
|
|
201
|
+
return [...new Set(times.filter((t) => Number.isFinite(t) && t > t0 && t <= t0 + TIMELINE_HOURS * HOUR_MS))]
|
|
202
|
+
.sort((a, b) => a - b);
|
|
203
|
+
}
|
|
204
|
+
|
|
153
205
|
/** Pure: shape what was read into the widget state. `now` is injectable for tests. */
|
|
154
|
-
export function shape(sources: Sources, now: Date, siteName: string, provisioned: Verticals = buildVerticals()
|
|
206
|
+
export function shape(sources: Sources, now: Date, siteName: string, provisioned: Verticals = buildVerticals(),
|
|
207
|
+
options: ShapeOptions = { timeline: true }): TodayState {
|
|
155
208
|
const bookingsReady = sources.bookings !== null && sources.requests !== null;
|
|
156
209
|
const ordersReady = sources.orders !== null;
|
|
157
210
|
// Re-filtered even though the queries filter: a server (or a fixture) that returns a
|
|
@@ -176,7 +229,7 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
176
229
|
const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime();
|
|
177
230
|
const nextStart = next ? Date.parse(startOf(next)) : Infinity;
|
|
178
231
|
const nextShown = next ? booking(next, now) : undefined;
|
|
179
|
-
const
|
|
232
|
+
const allRanked = rankItems([
|
|
180
233
|
...(bookingsReady ? bookingItems(
|
|
181
234
|
nextShown && { start: new Date(startOf(next!)), service: nextShown.service, firstName: str(next, "contactDetails.firstName") },
|
|
182
235
|
requestCount, requestsCapped, now, LIMIT) : []),
|
|
@@ -184,14 +237,18 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
184
237
|
...(eventsReady ? eventItems(sources.events ?? [], now) : []),
|
|
185
238
|
...(blogReady ? blogItems(sources.drafts ?? [], now, LIMIT) : []),
|
|
186
239
|
]);
|
|
240
|
+
// A frame far from the read keeps what is true by the clock (a booking, an event, a post) and
|
|
241
|
+
// drops the counts, which may have changed since: it never shows a morning's "3 orders" at night.
|
|
242
|
+
const ranked = options.countsStale ? allRanked.filter((i) => i.count === undefined) : allRanked;
|
|
243
|
+
const dropped = options.countsStale ? allRanked.filter((i) => i.count !== undefined) : [];
|
|
187
244
|
// A shown item stops being true when it starts (a booking or event is no longer "next"), as
|
|
188
245
|
// the next booking already did: the frame's lifetime ends at the first of them.
|
|
189
246
|
const headAt = shownUntil(ranked);
|
|
190
247
|
// Nothing could be read at all: the glance shows the degrade line, never "All clear".
|
|
191
|
-
const glance = nothing ? degradeGlance(COPY.nothing) : glanceFields(ranked, GLANCE_COPY);
|
|
248
|
+
const glance = nothing ? degradeGlance(COPY.nothing) : withDropped(glanceFields(ranked, GLANCE_COPY), ranked, dropped);
|
|
192
249
|
// One glance per focus a widget may be set to (PR 2): "now" is the glance above, an area keeps
|
|
193
250
|
// only its own items. The phone publishes one frame per focus from the same descriptors.
|
|
194
|
-
const
|
|
251
|
+
const focusOptions = focusList(provisioned);
|
|
195
252
|
// Each focus's frame ends when an item IT shows starts — an Events widget headlines an event
|
|
196
253
|
// that may be 4th in "now" — so it carries its own `validUntil`, laid over the state's.
|
|
197
254
|
const ownEnd = (f: GlanceFocus) => f === "now"
|
|
@@ -202,16 +259,21 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
202
259
|
const areaReady: Record<GlanceFocus, boolean> = {
|
|
203
260
|
now: !nothing, bookings: bookingsReady, store: ordersReady, events: eventsReady, blog: blogReady,
|
|
204
261
|
};
|
|
205
|
-
const
|
|
262
|
+
const own = (items: GlanceItem[], f: GlanceFocus) => f === "now" ? items : items.filter((i) => i.area === f);
|
|
263
|
+
const nowFocuses = Object.fromEntries(focusOptions.map((f) => [f.id, {
|
|
206
264
|
...(!areaReady[f.id]
|
|
207
265
|
? { ...degradeGlance(COPY.nothing), glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel }
|
|
208
|
-
: focusFields(ranked, f.id, GLANCE_COPY)),
|
|
266
|
+
: withDropped(focusFields(ranked, f.id, GLANCE_COPY), own(ranked, f.id), own(dropped, f.id))),
|
|
209
267
|
validUntil: ownEnd(f.id),
|
|
210
|
-
|
|
268
|
+
score: areaReady[f.id] ? own(ranked, f.id)[0]?.score ?? 0 : 0,
|
|
269
|
+
frames: [] as GlanceFrame[],
|
|
270
|
+
timelineUntil: ownEnd(f.id),
|
|
271
|
+
}])) as Record<GlanceFocus, FocusGlance>;
|
|
272
|
+
const focuses = options.timeline ? withTimeline(nowFocuses, sources, now, siteName, provisioned) : nowFocuses;
|
|
211
273
|
return {
|
|
212
274
|
...glance,
|
|
213
275
|
focuses,
|
|
214
|
-
focusList:
|
|
276
|
+
focusList: focusOptions,
|
|
215
277
|
eventsReady,
|
|
216
278
|
blogReady,
|
|
217
279
|
validUntil: Math.floor(Math.min(midnight, nextStart, headAt) / 1000),
|
|
@@ -243,7 +305,9 @@ export function degraded(status: "error" | "signedOut" | "loading", siteName: st
|
|
|
243
305
|
// A FIXED instant, not the clock: a degrade frame has no rows, so nothing in it depends on
|
|
244
306
|
// the time — and the initial state must be deterministic (template contract: no worker's
|
|
245
307
|
// initial state may read the clock).
|
|
246
|
-
|
|
308
|
+
// No timeline: a degrade frame stays what it is until the next read, and frame times would
|
|
309
|
+
// read the build machine's local midnight.
|
|
310
|
+
const base = shape({ bookings: null, requests: null, orders: null }, new Date(0), siteName, buildVerticals(), { timeline: false });
|
|
247
311
|
const message = status === "signedOut" ? COPY.signedOut : status === "error" ? COPY.error : "";
|
|
248
312
|
// validUntil pinned too: shape() derives it from LOCAL midnight, so even at a fixed instant it
|
|
249
313
|
// would vary with the build machine's timezone and change the baked services hash.
|
|
@@ -254,11 +318,80 @@ export function degraded(status: "error" | "signedOut" | "loading", siteName: st
|
|
|
254
318
|
...base, status, message, hideMessage: !message, validUntil: 0,
|
|
255
319
|
...line,
|
|
256
320
|
// Each focus keeps its own header ("Orders"), only the line is the degrade one.
|
|
257
|
-
focuses: Object.fromEntries(base.focusList.map((f) => [f.id,
|
|
258
|
-
{ ...line, glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel, validUntil: 0 }])) as Record<GlanceFocus,
|
|
321
|
+
focuses: Object.fromEntries(base.focusList.map((f): [GlanceFocus, FocusGlance] => [f.id,
|
|
322
|
+
{ ...line, glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel, validUntil: 0, score: 0, frames: [], timelineUntil: 0 }])) as Record<GlanceFocus, FocusGlance>,
|
|
259
323
|
};
|
|
260
324
|
}
|
|
261
325
|
|
|
326
|
+
/** The glance of a frame whose counts were dropped (`ShapeOptions.countsStale`): where a count
|
|
327
|
+
* would be, "Open Owner to refresh". With nothing left by the clock it says only that, never
|
|
328
|
+
* "All clear" — the counts it dropped may still be waiting. */
|
|
329
|
+
function withDropped(fields: GlanceFields, kept: GlanceItem[], dropped: GlanceItem[]): GlanceFields {
|
|
330
|
+
if (!dropped.length) return fields;
|
|
331
|
+
if (!kept.length) return { ...degradeGlance(COPY.error), glanceLabel: fields.glanceLabel };
|
|
332
|
+
const secondary = [...kept.slice(1, 2).map((i) => i.short), COPY.error].filter(Boolean).join(" · ");
|
|
333
|
+
return {
|
|
334
|
+
...fields,
|
|
335
|
+
glanceSecondary: secondary, hideGlanceSecondary: false,
|
|
336
|
+
lockRectSecondary: COPY.error, lockHideSecondary: false,
|
|
337
|
+
// Every surface that carried a count says so, the one-line Lock Screen slot included.
|
|
338
|
+
lockInline: `${kept[0].lockTitle} · ${COPY.error}`,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const GLANCE_KEYS: (keyof GlanceFields)[] = [
|
|
343
|
+
"glanceHeadline", "glanceDetail", "glanceTime", "hideGlanceTime", "glanceSymbol", "glanceSecondary",
|
|
344
|
+
"hideGlanceSecondary", "glanceAllClear", "glanceLabel", "lockInline", "lockCircValue", "lockCircLabel",
|
|
345
|
+
"lockRectTitle", "lockRectDetail", "lockRectSecondary", "lockHideSecondary",
|
|
346
|
+
];
|
|
347
|
+
|
|
348
|
+
function glanceOnly(f: GlanceFields): GlanceFields {
|
|
349
|
+
return Object.fromEntries(GLANCE_KEYS.map((k) => [k, f[k]])) as unknown as GlanceFields;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function sameGlance(a: GlanceFields, b: GlanceFields): boolean {
|
|
353
|
+
return GLANCE_KEYS.every((k) => a[k] === b[k]);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Lay each focus's future frames over its glance now: the state re-shaped at each moment the
|
|
357
|
+
* glance changes by itself (`frameTimes`), keeping only a frame that reads differently from the
|
|
358
|
+
* one before it, at most `MAX_FRAMES` in all (the now frame counts). A focus whose area could not
|
|
359
|
+
* be read reads the same degrade line at every moment, so it gets none. */
|
|
360
|
+
function withTimeline(
|
|
361
|
+
focuses: Record<GlanceFocus, FocusGlance>, sources: Sources, now: Date, siteName: string, provisioned: Verticals,
|
|
362
|
+
): Record<GlanceFocus, FocusGlance> {
|
|
363
|
+
const t0 = now.getTime();
|
|
364
|
+
const later = frameTimes(sources, now).map((t) => ({
|
|
365
|
+
t, state: shape(sources, new Date(t), siteName, provisioned, { countsStale: t - t0 >= COUNTS_FRESH_MS }),
|
|
366
|
+
}));
|
|
367
|
+
return Object.fromEntries(Object.entries(focuses).map(([id, focus]) => {
|
|
368
|
+
const frames: GlanceFrame[] = [];
|
|
369
|
+
let previous: GlanceFields & { score: number } = focus;
|
|
370
|
+
let until = focus.validUntil;
|
|
371
|
+
for (const { t, state } of later) {
|
|
372
|
+
if (frames.length >= MAX_FRAMES - 1) break;
|
|
373
|
+
const at = state.focuses[id as GlanceFocus];
|
|
374
|
+
// A frame that reads the same but matters more (a booking entering its last hour) is kept:
|
|
375
|
+
// its relevance is what lifts the widget to the top of a Smart Stack.
|
|
376
|
+
if (!at) continue;
|
|
377
|
+
if (sameGlance(previous, at) && previous.score === at.score) {
|
|
378
|
+
// Still reads the same at this moment, so what is showing holds for as long as it does here.
|
|
379
|
+
until = Math.max(until, at.validUntil);
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
frames.push({ ...glanceOnly(at), startsAt: Math.floor(t / 1000), score: at.score, durationSeconds: 0 });
|
|
383
|
+
previous = at;
|
|
384
|
+
until = at.validUntil;
|
|
385
|
+
}
|
|
386
|
+
// Each frame holds until the next one starts; the last until it stops being true, or the
|
|
387
|
+
// timeline's 24 h end if that comes first.
|
|
388
|
+
const end = Math.min(Math.floor((t0 + TIMELINE_HOURS * HOUR_MS) / 1000), until);
|
|
389
|
+
const starts = [...frames.map((f) => f.startsAt), end];
|
|
390
|
+
frames.forEach((f, i) => { f.durationSeconds = Math.max(0, starts[i + 1] - f.startsAt); });
|
|
391
|
+
return [id, { ...focus, frames, timelineUntil: frames.length ? end : focus.validUntil }];
|
|
392
|
+
})) as Record<GlanceFocus, FocusGlance>;
|
|
393
|
+
}
|
|
394
|
+
|
|
262
395
|
/** The glance of a frame that could not read the business: its one line in every slot, never
|
|
263
396
|
* "All clear", and never an empty Lock Screen circle. */
|
|
264
397
|
function degradeGlance(message: string): GlanceFields {
|
|
@@ -23,7 +23,7 @@ import { queryEvents } from "@himalaya/wix-events";
|
|
|
23
23
|
import { queryDraftPosts } from "@himalaya/wix-blog";
|
|
24
24
|
import {
|
|
25
25
|
str, hhmm, sameLocalDay, dayLabel, rankItems, glanceFields, bookingItems, orderItems, eventItems, blogItems, shownUntil,
|
|
26
|
-
GLANCE_COPY, focusFields, focusList, type GlanceFields, type GlanceFocus, type GlanceFocusOption,
|
|
26
|
+
GLANCE_COPY, focusFields, focusList, type GlanceFields, type GlanceFocus, type GlanceFocusOption, type GlanceItem,
|
|
27
27
|
} from "./glance.js";
|
|
28
28
|
|
|
29
29
|
export { isSectionUnavailable, isSignedOut, dayLabel };
|
|
@@ -45,9 +45,24 @@ export interface TodayBooking {
|
|
|
45
45
|
|
|
46
46
|
/** The glance ("what matters now", glance.ts) rides on the same state: the existing fields stay
|
|
47
47
|
* for the embedding hosts that bind them (the embedding guide), the glance fields are added. */
|
|
48
|
+
/** One future frame of a focus's timeline: the glance as it will read from `startsAt` (epoch
|
|
49
|
+
* SECONDS), re-ranked at that moment, with the score of what it headlines. */
|
|
50
|
+
export type GlanceFrame = GlanceFields & { startsAt: number; score: number; durationSeconds: number };
|
|
51
|
+
|
|
52
|
+
/** One focus's glance now, and the frames that follow it. */
|
|
53
|
+
export type FocusGlance = GlanceFields & {
|
|
54
|
+
validUntil: number;
|
|
55
|
+
/** The headline's score (0 when nothing is waiting): the widget's Smart Stack relevance. */
|
|
56
|
+
score: number;
|
|
57
|
+
/** Future frames, ascending, at most `MAX_FRAMES - 1` within 24 h; each differs from the one before. */
|
|
58
|
+
frames: GlanceFrame[];
|
|
59
|
+
/** Epoch SECONDS the last frame stops being true; equals `validUntil` without frames. */
|
|
60
|
+
timelineUntil: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
48
63
|
export interface TodayState extends GlanceFields {
|
|
49
64
|
/** The glance for each focus a widget may be set to ("now" + the provisioned areas). */
|
|
50
|
-
focuses: Record<GlanceFocus,
|
|
65
|
+
focuses: Record<GlanceFocus, FocusGlance>;
|
|
51
66
|
/** Those focuses with owner-facing titles, in order — the widget picker's list. */
|
|
52
67
|
focusList: GlanceFocusOption[];
|
|
53
68
|
eventsReady: boolean;
|
|
@@ -150,8 +165,46 @@ function upcoming(rows: Rec[] | null, statuses: string[], now: Date): Rec[] {
|
|
|
150
165
|
.sort((a, b) => Date.parse(startOf(a)) - Date.parse(startOf(b)) || str(a, "id").localeCompare(str(b, "id")));
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
/** A widget timeline is small: WidgetKit budgets its reloads, and a frame is a whole tree. */
|
|
169
|
+
export const MAX_FRAMES = 8;
|
|
170
|
+
export const TIMELINE_HOURS = 24;
|
|
171
|
+
/** A count read now is not vouched for past this: a later frame drops it. */
|
|
172
|
+
export const COUNTS_FRESH_MS = 3 * 60 * 60 * 1000;
|
|
173
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
174
|
+
|
|
175
|
+
interface ShapeOptions {
|
|
176
|
+
/** Compute the future frames (off for the frames themselves). */
|
|
177
|
+
timeline?: boolean;
|
|
178
|
+
/** This frame is far enough from the read that its counts are dropped. */
|
|
179
|
+
countsStale?: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The moments a glance changes on its own, with no new read: a booking enters its last hour
|
|
183
|
+
* and starts, an event enters its last 3 h and starts, a post goes live, the day turns, and the
|
|
184
|
+
* counts stop being fresh. Ascending, after `now`, within 24 h. Only the `MAX_FRAMES` soonest
|
|
185
|
+
* starts of each source: no focus keeps more frames than that, and every moment costs a full
|
|
186
|
+
* `shape`, so a day of 50 bookings must not mean 100 of them on every publish. */
|
|
187
|
+
function frameTimes(sources: Sources, now: Date): number[] {
|
|
188
|
+
const t0 = now.getTime();
|
|
189
|
+
const at = (s: string) => Date.parse(s);
|
|
190
|
+
// A second past a start: an item is still upcoming AT its start (`start >= now`).
|
|
191
|
+
const started = (s: number) => s + 1000;
|
|
192
|
+
const soonest = (starts: number[]) =>
|
|
193
|
+
starts.filter((s) => Number.isFinite(s) && started(s) > t0).sort((a, b) => a - b).slice(0, MAX_FRAMES);
|
|
194
|
+
const times = [
|
|
195
|
+
...soonest((sources.bookings ?? []).map((b) => at(startOf(b)))).flatMap((s) => [s - HOUR_MS, started(s)]),
|
|
196
|
+
...soonest((sources.events ?? []).map((e) => at(str(e, "dateAndTimeSettings.startDate")))).flatMap((s) => [s - 3 * HOUR_MS, started(s)]),
|
|
197
|
+
...soonest((sources.drafts ?? []).map((d) => at(str(d, "scheduledPublishDate")))).map(started),
|
|
198
|
+
new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime(),
|
|
199
|
+
t0 + COUNTS_FRESH_MS,
|
|
200
|
+
];
|
|
201
|
+
return [...new Set(times.filter((t) => Number.isFinite(t) && t > t0 && t <= t0 + TIMELINE_HOURS * HOUR_MS))]
|
|
202
|
+
.sort((a, b) => a - b);
|
|
203
|
+
}
|
|
204
|
+
|
|
153
205
|
/** Pure: shape what was read into the widget state. `now` is injectable for tests. */
|
|
154
|
-
export function shape(sources: Sources, now: Date, siteName: string, provisioned: Verticals = buildVerticals()
|
|
206
|
+
export function shape(sources: Sources, now: Date, siteName: string, provisioned: Verticals = buildVerticals(),
|
|
207
|
+
options: ShapeOptions = { timeline: true }): TodayState {
|
|
155
208
|
const bookingsReady = sources.bookings !== null && sources.requests !== null;
|
|
156
209
|
const ordersReady = sources.orders !== null;
|
|
157
210
|
// Re-filtered even though the queries filter: a server (or a fixture) that returns a
|
|
@@ -176,7 +229,7 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
176
229
|
const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).getTime();
|
|
177
230
|
const nextStart = next ? Date.parse(startOf(next)) : Infinity;
|
|
178
231
|
const nextShown = next ? booking(next, now) : undefined;
|
|
179
|
-
const
|
|
232
|
+
const allRanked = rankItems([
|
|
180
233
|
...(bookingsReady ? bookingItems(
|
|
181
234
|
nextShown && { start: new Date(startOf(next!)), service: nextShown.service, firstName: str(next, "contactDetails.firstName") },
|
|
182
235
|
requestCount, requestsCapped, now, LIMIT) : []),
|
|
@@ -184,14 +237,18 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
184
237
|
...(eventsReady ? eventItems(sources.events ?? [], now) : []),
|
|
185
238
|
...(blogReady ? blogItems(sources.drafts ?? [], now, LIMIT) : []),
|
|
186
239
|
]);
|
|
240
|
+
// A frame far from the read keeps what is true by the clock (a booking, an event, a post) and
|
|
241
|
+
// drops the counts, which may have changed since: it never shows a morning's "3 orders" at night.
|
|
242
|
+
const ranked = options.countsStale ? allRanked.filter((i) => i.count === undefined) : allRanked;
|
|
243
|
+
const dropped = options.countsStale ? allRanked.filter((i) => i.count !== undefined) : [];
|
|
187
244
|
// A shown item stops being true when it starts (a booking or event is no longer "next"), as
|
|
188
245
|
// the next booking already did: the frame's lifetime ends at the first of them.
|
|
189
246
|
const headAt = shownUntil(ranked);
|
|
190
247
|
// Nothing could be read at all: the glance shows the degrade line, never "All clear".
|
|
191
|
-
const glance = nothing ? degradeGlance(COPY.nothing) : glanceFields(ranked, GLANCE_COPY);
|
|
248
|
+
const glance = nothing ? degradeGlance(COPY.nothing) : withDropped(glanceFields(ranked, GLANCE_COPY), ranked, dropped);
|
|
192
249
|
// One glance per focus a widget may be set to (PR 2): "now" is the glance above, an area keeps
|
|
193
250
|
// only its own items. The phone publishes one frame per focus from the same descriptors.
|
|
194
|
-
const
|
|
251
|
+
const focusOptions = focusList(provisioned);
|
|
195
252
|
// Each focus's frame ends when an item IT shows starts — an Events widget headlines an event
|
|
196
253
|
// that may be 4th in "now" — so it carries its own `validUntil`, laid over the state's.
|
|
197
254
|
const ownEnd = (f: GlanceFocus) => f === "now"
|
|
@@ -202,16 +259,21 @@ export function shape(sources: Sources, now: Date, siteName: string, provisioned
|
|
|
202
259
|
const areaReady: Record<GlanceFocus, boolean> = {
|
|
203
260
|
now: !nothing, bookings: bookingsReady, store: ordersReady, events: eventsReady, blog: blogReady,
|
|
204
261
|
};
|
|
205
|
-
const
|
|
262
|
+
const own = (items: GlanceItem[], f: GlanceFocus) => f === "now" ? items : items.filter((i) => i.area === f);
|
|
263
|
+
const nowFocuses = Object.fromEntries(focusOptions.map((f) => [f.id, {
|
|
206
264
|
...(!areaReady[f.id]
|
|
207
265
|
? { ...degradeGlance(COPY.nothing), glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel }
|
|
208
|
-
: focusFields(ranked, f.id, GLANCE_COPY)),
|
|
266
|
+
: withDropped(focusFields(ranked, f.id, GLANCE_COPY), own(ranked, f.id), own(dropped, f.id))),
|
|
209
267
|
validUntil: ownEnd(f.id),
|
|
210
|
-
|
|
268
|
+
score: areaReady[f.id] ? own(ranked, f.id)[0]?.score ?? 0 : 0,
|
|
269
|
+
frames: [] as GlanceFrame[],
|
|
270
|
+
timelineUntil: ownEnd(f.id),
|
|
271
|
+
}])) as Record<GlanceFocus, FocusGlance>;
|
|
272
|
+
const focuses = options.timeline ? withTimeline(nowFocuses, sources, now, siteName, provisioned) : nowFocuses;
|
|
211
273
|
return {
|
|
212
274
|
...glance,
|
|
213
275
|
focuses,
|
|
214
|
-
focusList:
|
|
276
|
+
focusList: focusOptions,
|
|
215
277
|
eventsReady,
|
|
216
278
|
blogReady,
|
|
217
279
|
validUntil: Math.floor(Math.min(midnight, nextStart, headAt) / 1000),
|
|
@@ -243,7 +305,9 @@ export function degraded(status: "error" | "signedOut" | "loading", siteName: st
|
|
|
243
305
|
// A FIXED instant, not the clock: a degrade frame has no rows, so nothing in it depends on
|
|
244
306
|
// the time — and the initial state must be deterministic (template contract: no worker's
|
|
245
307
|
// initial state may read the clock).
|
|
246
|
-
|
|
308
|
+
// No timeline: a degrade frame stays what it is until the next read, and frame times would
|
|
309
|
+
// read the build machine's local midnight.
|
|
310
|
+
const base = shape({ bookings: null, requests: null, orders: null }, new Date(0), siteName, buildVerticals(), { timeline: false });
|
|
247
311
|
const message = status === "signedOut" ? COPY.signedOut : status === "error" ? COPY.error : "";
|
|
248
312
|
// validUntil pinned too: shape() derives it from LOCAL midnight, so even at a fixed instant it
|
|
249
313
|
// would vary with the build machine's timezone and change the baked services hash.
|
|
@@ -254,11 +318,80 @@ export function degraded(status: "error" | "signedOut" | "loading", siteName: st
|
|
|
254
318
|
...base, status, message, hideMessage: !message, validUntil: 0,
|
|
255
319
|
...line,
|
|
256
320
|
// Each focus keeps its own header ("Orders"), only the line is the degrade one.
|
|
257
|
-
focuses: Object.fromEntries(base.focusList.map((f) => [f.id,
|
|
258
|
-
{ ...line, glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel, validUntil: 0 }])) as Record<GlanceFocus,
|
|
321
|
+
focuses: Object.fromEntries(base.focusList.map((f): [GlanceFocus, FocusGlance] => [f.id,
|
|
322
|
+
{ ...line, glanceLabel: focusFields([], f.id, GLANCE_COPY).glanceLabel, validUntil: 0, score: 0, frames: [], timelineUntil: 0 }])) as Record<GlanceFocus, FocusGlance>,
|
|
259
323
|
};
|
|
260
324
|
}
|
|
261
325
|
|
|
326
|
+
/** The glance of a frame whose counts were dropped (`ShapeOptions.countsStale`): where a count
|
|
327
|
+
* would be, "Open Owner to refresh". With nothing left by the clock it says only that, never
|
|
328
|
+
* "All clear" — the counts it dropped may still be waiting. */
|
|
329
|
+
function withDropped(fields: GlanceFields, kept: GlanceItem[], dropped: GlanceItem[]): GlanceFields {
|
|
330
|
+
if (!dropped.length) return fields;
|
|
331
|
+
if (!kept.length) return { ...degradeGlance(COPY.error), glanceLabel: fields.glanceLabel };
|
|
332
|
+
const secondary = [...kept.slice(1, 2).map((i) => i.short), COPY.error].filter(Boolean).join(" · ");
|
|
333
|
+
return {
|
|
334
|
+
...fields,
|
|
335
|
+
glanceSecondary: secondary, hideGlanceSecondary: false,
|
|
336
|
+
lockRectSecondary: COPY.error, lockHideSecondary: false,
|
|
337
|
+
// Every surface that carried a count says so, the one-line Lock Screen slot included.
|
|
338
|
+
lockInline: `${kept[0].lockTitle} · ${COPY.error}`,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const GLANCE_KEYS: (keyof GlanceFields)[] = [
|
|
343
|
+
"glanceHeadline", "glanceDetail", "glanceTime", "hideGlanceTime", "glanceSymbol", "glanceSecondary",
|
|
344
|
+
"hideGlanceSecondary", "glanceAllClear", "glanceLabel", "lockInline", "lockCircValue", "lockCircLabel",
|
|
345
|
+
"lockRectTitle", "lockRectDetail", "lockRectSecondary", "lockHideSecondary",
|
|
346
|
+
];
|
|
347
|
+
|
|
348
|
+
function glanceOnly(f: GlanceFields): GlanceFields {
|
|
349
|
+
return Object.fromEntries(GLANCE_KEYS.map((k) => [k, f[k]])) as unknown as GlanceFields;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function sameGlance(a: GlanceFields, b: GlanceFields): boolean {
|
|
353
|
+
return GLANCE_KEYS.every((k) => a[k] === b[k]);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Lay each focus's future frames over its glance now: the state re-shaped at each moment the
|
|
357
|
+
* glance changes by itself (`frameTimes`), keeping only a frame that reads differently from the
|
|
358
|
+
* one before it, at most `MAX_FRAMES` in all (the now frame counts). A focus whose area could not
|
|
359
|
+
* be read reads the same degrade line at every moment, so it gets none. */
|
|
360
|
+
function withTimeline(
|
|
361
|
+
focuses: Record<GlanceFocus, FocusGlance>, sources: Sources, now: Date, siteName: string, provisioned: Verticals,
|
|
362
|
+
): Record<GlanceFocus, FocusGlance> {
|
|
363
|
+
const t0 = now.getTime();
|
|
364
|
+
const later = frameTimes(sources, now).map((t) => ({
|
|
365
|
+
t, state: shape(sources, new Date(t), siteName, provisioned, { countsStale: t - t0 >= COUNTS_FRESH_MS }),
|
|
366
|
+
}));
|
|
367
|
+
return Object.fromEntries(Object.entries(focuses).map(([id, focus]) => {
|
|
368
|
+
const frames: GlanceFrame[] = [];
|
|
369
|
+
let previous: GlanceFields & { score: number } = focus;
|
|
370
|
+
let until = focus.validUntil;
|
|
371
|
+
for (const { t, state } of later) {
|
|
372
|
+
if (frames.length >= MAX_FRAMES - 1) break;
|
|
373
|
+
const at = state.focuses[id as GlanceFocus];
|
|
374
|
+
// A frame that reads the same but matters more (a booking entering its last hour) is kept:
|
|
375
|
+
// its relevance is what lifts the widget to the top of a Smart Stack.
|
|
376
|
+
if (!at) continue;
|
|
377
|
+
if (sameGlance(previous, at) && previous.score === at.score) {
|
|
378
|
+
// Still reads the same at this moment, so what is showing holds for as long as it does here.
|
|
379
|
+
until = Math.max(until, at.validUntil);
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
frames.push({ ...glanceOnly(at), startsAt: Math.floor(t / 1000), score: at.score, durationSeconds: 0 });
|
|
383
|
+
previous = at;
|
|
384
|
+
until = at.validUntil;
|
|
385
|
+
}
|
|
386
|
+
// Each frame holds until the next one starts; the last until it stops being true, or the
|
|
387
|
+
// timeline's 24 h end if that comes first.
|
|
388
|
+
const end = Math.min(Math.floor((t0 + TIMELINE_HOURS * HOUR_MS) / 1000), until);
|
|
389
|
+
const starts = [...frames.map((f) => f.startsAt), end];
|
|
390
|
+
frames.forEach((f, i) => { f.durationSeconds = Math.max(0, starts[i + 1] - f.startsAt); });
|
|
391
|
+
return [id, { ...focus, frames, timelineUntil: frames.length ? end : focus.validUntil }];
|
|
392
|
+
})) as Record<GlanceFocus, FocusGlance>;
|
|
393
|
+
}
|
|
394
|
+
|
|
262
395
|
/** The glance of a frame that could not read the business: its one line in every slot, never
|
|
263
396
|
* "All clear", and never an empty Lock Screen circle. */
|
|
264
397
|
function degradeGlance(message: string): GlanceFields {
|