@wix/himalaya-cli 0.1854.0 → 0.1856.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/browser-plane/assets.json +5 -5
- package/dist/browser-plane/preview-deferred/{fb500748f6c4768468f2e7f985aa67c38633077c7a98caaa6b500c7b00eea8e9.bin → c1f008be31a41521c6de7c1afad419a1f4dad53cbc2701a14738d5b22f291463.bin} +1 -1
- package/dist/browser-plane/sw.js +10 -10
- package/dist/cli.mjs +598 -312
- package/dist/preview-assets.generated.bin +0 -0
- package/guides/himi-json-output.md +2 -2
- package/guides/one-app-to-himi-app.md +2 -2
- package/package.json +2 -2
- package/templates/bare-fashion/tier5-src/himi-bare-fashion-home/index.ts +5 -2
- package/templates/branded-lite/l10n/ar.json +15 -10
- package/templates/branded-lite/l10n/cs.json +15 -10
- package/templates/branded-lite/l10n/da.json +15 -10
- package/templates/branded-lite/l10n/de.json +15 -10
- package/templates/branded-lite/l10n/el.json +15 -10
- package/templates/branded-lite/l10n/es.json +15 -10
- package/templates/branded-lite/l10n/fi.json +15 -10
- package/templates/branded-lite/l10n/fr.json +15 -10
- package/templates/branded-lite/l10n/he.json +12 -8
- package/templates/branded-lite/l10n/hu.json +15 -10
- package/templates/branded-lite/l10n/id.json +15 -10
- package/templates/branded-lite/l10n/it.json +15 -10
- package/templates/branded-lite/l10n/ja.json +15 -10
- package/templates/branded-lite/l10n/ko.json +15 -10
- package/templates/branded-lite/l10n/lt.json +15 -10
- package/templates/branded-lite/l10n/nl.json +15 -10
- package/templates/branded-lite/l10n/no.json +15 -10
- package/templates/branded-lite/l10n/pl.json +15 -10
- package/templates/branded-lite/l10n/pt.json +15 -10
- package/templates/branded-lite/l10n/ru.json +15 -10
- package/templates/branded-lite/l10n/sv.json +15 -10
- package/templates/branded-lite/l10n/th.json +15 -10
- package/templates/branded-lite/l10n/tr.json +15 -10
- package/templates/branded-lite/l10n/uk.json +15 -10
- package/templates/branded-lite/l10n/vi.json +15 -10
- package/templates/branded-lite/l10n/zh.json +15 -10
- package/templates/branded-lite/recipes/performance-tracker.json +464 -75
- package/templates/branded-lite/tier5-src/_shared/galleries-section.ts +52 -23
- package/templates/branded-lite/tier5-src/_shared/performance-tracker-goal.ts +57 -0
- package/templates/branded-lite/tier5-src/_shared/performance-tracker-section.ts +124 -49
- package/templates/branded-lite/tier5-src/_shared/performance-tracker-weeks.ts +2 -2
|
@@ -2,11 +2,17 @@
|
|
|
2
2
|
// limit -> how many galleries (the source section's `maxGalleries`); `noMore` is false when the site has more,
|
|
3
3
|
// which is when the section's See All button (to the galleries screen) shows.
|
|
4
4
|
// showName -> whether a card shows the gallery's name (the source section's `isShowName`); the accessibility label always has it.
|
|
5
|
-
// The
|
|
5
|
+
// The source reads the One App's own galleries service with the MEMBER's signed instance, never hidden galleries (`withHidden=false`), and
|
|
6
|
+
// drops empty ones; a visitor has no member instance, so the source shows them its "no galleries" state, and so does a failed read. This section
|
|
7
|
+
// does the same, and never falls back to the public Pro Gallery list (`/progallery/v2/galleries`), which has no hidden flag:
|
|
8
|
+
// - a signed-out visitor gets the empty state, without a read;
|
|
9
|
+
// - a member reads that service (`photography-one-app-viewer/galleries`), which a native host reaches;
|
|
10
|
+
// - where it cannot be read (the published web bridge refuses that host) the member gets the empty state too: a hidden gallery must never show.
|
|
6
11
|
// A page worker registers it under a service name; the descriptor binds `$service:<serviceName>.*` (recipes galleries-carousel / galleries-list).
|
|
7
12
|
import { defineService } from "@himalaya/state";
|
|
8
13
|
import { wixMediaUrl } from "@himalaya/wix-media";
|
|
9
14
|
import { copyParam, stashCopy, copyTemplate } from "./himi-copy.js";
|
|
15
|
+
import { memberAccess } from "./member-access.js";
|
|
10
16
|
|
|
11
17
|
export interface GalleriesSectionOptions {
|
|
12
18
|
/** How many galleries to show. */
|
|
@@ -37,7 +43,8 @@ export interface GalleriesSectionState {
|
|
|
37
43
|
noMore: boolean;
|
|
38
44
|
}
|
|
39
45
|
|
|
40
|
-
|
|
46
|
+
/** The One App's own list: what the source section reads (its `API_BASE_URL` + `/galleries`), as a member, without hidden galleries. */
|
|
47
|
+
const ONE_APP_GALLERIES_URL = "https://www.wix.com/_api/photography-one-app-viewer/galleries";
|
|
41
48
|
const DEFAULT_LIMIT = 10;
|
|
42
49
|
/** The API's page maximum, and the most galleries a section shows (the template's galleries screen caps at 30 too). */
|
|
43
50
|
const PAGE_SIZE = 10, MAX_GALLERIES = 30;
|
|
@@ -45,15 +52,15 @@ const PAGE_SIZE = 10, MAX_GALLERIES = 30;
|
|
|
45
52
|
const CARD_IMAGE = { width: 720, height: 540, quality: 85 };
|
|
46
53
|
|
|
47
54
|
type WixApi = { call: (method: string, path: string) => Promise<unknown> };
|
|
48
|
-
interface
|
|
55
|
+
interface RawItem { mediaUrl?: unknown; dataType?: unknown; videoMetadata?: { posters?: Array<{ url?: unknown }> } }
|
|
56
|
+
/** A gallery of the One App's list. */
|
|
57
|
+
interface RawGallery { id?: unknown; name?: unknown; items?: RawItem[]; totalItemsCount?: unknown; isHidden?: unknown }
|
|
49
58
|
|
|
50
|
-
/** A
|
|
59
|
+
/** A gallery's cover: the first item's picture (a video gives its poster). */
|
|
51
60
|
function coverOf(gallery: RawGallery): string {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const media = info as { url?: unknown; id?: unknown };
|
|
56
|
-
return wixMediaUrl(media.url, CARD_IMAGE) || wixMediaUrl(media.id, CARD_IMAGE);
|
|
61
|
+
const item = Array.isArray(gallery.items) ? gallery.items[0] : undefined;
|
|
62
|
+
const poster = item?.dataType === "Video" ? item.videoMetadata?.posters?.find((p) => typeof p?.url === "string" && p.url)?.url : undefined;
|
|
63
|
+
return wixMediaUrl(poster ?? item?.mediaUrl, CARD_IMAGE);
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
function rowOf(gallery: RawGallery, index: number, showName: boolean): GalleryRow {
|
|
@@ -67,21 +74,37 @@ function rowOf(gallery: RawGallery, index: number, showName: boolean): GalleryRo
|
|
|
67
74
|
};
|
|
68
75
|
}
|
|
69
76
|
|
|
70
|
-
/**
|
|
77
|
+
/** A gallery the section may show: the source drops hidden and empty galleries. An unknown count is not "empty". */
|
|
78
|
+
function visible(gallery: RawGallery): boolean {
|
|
79
|
+
const count = gallery.totalItemsCount;
|
|
80
|
+
return typeof gallery.id === "string" && !!gallery.id && gallery.isHidden !== true && !(typeof count === "number" && count <= 0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The most galleries one read looks through while it fills the section: the source's own cap on a list. */
|
|
84
|
+
const MAX_SCANNED = 200;
|
|
85
|
+
|
|
86
|
+
/** One page of the One App's list, as the source section reads it (`withHidden=false`). A native transport may hand the body over bare or wrapped as
|
|
87
|
+
* `{ data }`. The service drops hidden galleries after it pages, so only its `totalGalleriesCount` says where the list ends: a body without one is not read. */
|
|
88
|
+
async function readPage(api: WixApi, offset: number): Promise<{ galleries: RawGallery[]; total: number }> {
|
|
89
|
+
const res = await api.call("GET", `${ONE_APP_GALLERIES_URL}?itemsPerGallery=1&offset=${offset}&limit=${PAGE_SIZE}&withHidden=false&useNew=true`);
|
|
90
|
+
const body = (res as { data?: unknown } | null)?.data ?? res;
|
|
91
|
+
const { galleries, totalGalleriesCount } = (body ?? {}) as { galleries?: unknown; totalGalleriesCount?: unknown };
|
|
92
|
+
if (!Array.isArray(galleries) || typeof totalGalleriesCount !== "number") throw new Error("Malformed One App galleries response");
|
|
93
|
+
return { galleries, total: totalGalleriesCount };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The first `limit` visible galleries, page by page, and whether there are more. A page can hold fewer than `PAGE_SIZE` visible galleries, so paging goes by the offset and the total. */
|
|
71
97
|
async function readGalleries(api: WixApi, limit: number): Promise<{ galleries: RawGallery[]; more: boolean }> {
|
|
72
98
|
const galleries: RawGallery[] = [];
|
|
73
|
-
let total
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
total = res?.totalGalleries;
|
|
81
|
-
if (page.length < size) break;
|
|
99
|
+
let offset = 0, total = Infinity;
|
|
100
|
+
// One more than the section shows, so "more" is known without a second read.
|
|
101
|
+
while (galleries.length <= limit && offset < total && offset < MAX_SCANNED) {
|
|
102
|
+
const page = await readPage(api, offset);
|
|
103
|
+
galleries.push(...page.galleries.filter(visible));
|
|
104
|
+
total = page.total;
|
|
105
|
+
offset += PAGE_SIZE;
|
|
82
106
|
}
|
|
83
|
-
|
|
84
|
-
return { galleries, more: typeof total === "number" ? total > galleries.length : galleries.length >= limit };
|
|
107
|
+
return { galleries: galleries.slice(0, limit), more: galleries.length > limit || (offset < total && offset >= MAX_SCANNED) };
|
|
85
108
|
}
|
|
86
109
|
|
|
87
110
|
export function registerGalleriesSection(serviceName: string, options: GalleriesSectionOptions = {}) {
|
|
@@ -100,10 +123,16 @@ export function registerGalleriesSection(serviceName: string, options: Galleries
|
|
|
100
123
|
load: async (_state, arg) => {
|
|
101
124
|
stashCopy(arg);
|
|
102
125
|
try {
|
|
126
|
+
// The source reads as the member: a visitor has no instance for it and sees its "no galleries" state.
|
|
127
|
+
if (!(await memberAccess()).signedIn) return view("ready", [], false);
|
|
103
128
|
const api = (globalThis as { himalaya?: { wixApi?: WixApi } }).himalaya?.wixApi;
|
|
104
129
|
if (!api) throw new Error("Wix bridge unavailable");
|
|
105
|
-
const { galleries, more } = await readGalleries(api, limit)
|
|
106
|
-
|
|
130
|
+
const { galleries, more } = await readGalleries(api, limit).catch((oneAppError) => {
|
|
131
|
+
// As in the source, a list that cannot be read is its "no galleries" state (and it is never replaced by the public list: that one cannot tell a hidden gallery).
|
|
132
|
+
console.warn("galleries_section: the One App galleries service could not be read", oneAppError);
|
|
133
|
+
return { galleries: [] as RawGallery[], more: false };
|
|
134
|
+
});
|
|
135
|
+
const rows = galleries.slice(0, limit).map((gallery, index) => rowOf(gallery, index, showName));
|
|
107
136
|
return view("ready", rows, more);
|
|
108
137
|
} catch (error) {
|
|
109
138
|
console.warn("galleries_section: the galleries could not be read", error);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The member's own weekly session goal on a performance tracker, as pure rules over a key-value store (no @himalaya import, so a test
|
|
2
|
+
// can load this file alone). The source keeps the goal in a private fitness service that signs its calls with the site's instance, a
|
|
3
|
+
// credential a member's session cannot stand in for, so the goal is kept on this device: under the member's own key, never read for
|
|
4
|
+
// another member, and never shown to a visitor.
|
|
5
|
+
import type { MemberAccess } from "./member-access.js";
|
|
6
|
+
|
|
7
|
+
/** The source's picker offers 1 to 14 sessions a week. */
|
|
8
|
+
export const MIN_MEMBER_GOAL = 1;
|
|
9
|
+
export const MAX_MEMBER_GOAL = 14;
|
|
10
|
+
export const GOAL_CHOICES: readonly number[] = Array.from({ length: MAX_MEMBER_GOAL }, (_, i) => MIN_MEMBER_GOAL + i);
|
|
11
|
+
|
|
12
|
+
/** What the picker lists: 1 to 14, plus the goal in force when the site's default is above that (a default of 20 must show as chosen). */
|
|
13
|
+
export function goalChoices(current: number): number[] {
|
|
14
|
+
return GOAL_CHOICES.includes(current) ? [...GOAL_CHOICES] : [...GOAL_CHOICES, current];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GoalStore { getItem(key: string): string | null; setItem(key: string, value: string): void }
|
|
18
|
+
|
|
19
|
+
const KEY = "himi-performance-tracker:weekly-goal:v1";
|
|
20
|
+
|
|
21
|
+
/** A goal the picker offers (a whole number in [1, 14], or its text), else 0. */
|
|
22
|
+
export function pickableGoal(raw: unknown): number {
|
|
23
|
+
const n = typeof raw === "number" ? raw : typeof raw === "string" && raw.trim() !== "" ? Number(raw) : NaN;
|
|
24
|
+
return Number.isInteger(n) && n >= MIN_MEMBER_GOAL && n <= MAX_MEMBER_GOAL ? n : 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whose goal this is: the member's partition (account + business + email), else their email on a host that has no partition. "" = nobody. */
|
|
28
|
+
export function goalOwner(member: Pick<MemberAccess, "email" | "scope"> | undefined): string {
|
|
29
|
+
return member?.email ? member.scope || member.email : "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const keyOf = (owner: string): string => `${KEY}.${encodeURIComponent(owner)}`;
|
|
33
|
+
|
|
34
|
+
/** The goal this member set, or 0 when they have not (or the store cannot be read). */
|
|
35
|
+
export function readMemberGoal(store: GoalStore | undefined, owner: string): number {
|
|
36
|
+
if (!store || !owner) return 0;
|
|
37
|
+
try { return pickableGoal(store.getItem(keyOf(owner))); } catch { return 0; }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether this device can keep a goal for this member: a probe key is written and read back. A member is offered the picker only when it can. */
|
|
41
|
+
export function canKeepGoal(store: GoalStore | undefined, owner: string): boolean {
|
|
42
|
+
if (!store || !owner) return false;
|
|
43
|
+
try {
|
|
44
|
+
const key = `${keyOf(owner)}:probe`;
|
|
45
|
+
store.setItem(key, "1");
|
|
46
|
+
return store.getItem(key) === "1";
|
|
47
|
+
} catch { return false; }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Keep `goal` for this member. True only when it reads back: a host may swallow a failed write, and a save must never claim success. */
|
|
51
|
+
export function saveMemberGoal(store: GoalStore | undefined, owner: string, goal: number): boolean {
|
|
52
|
+
if (!store || !owner || !pickableGoal(goal)) return false;
|
|
53
|
+
try {
|
|
54
|
+
store.setItem(keyOf(owner), String(goal));
|
|
55
|
+
return readMemberGoal(store, owner) === goal;
|
|
56
|
+
} catch { return false; }
|
|
57
|
+
}
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
// A page's performance-tracker section as a registrable service: the signed-in member's progress against a weekly session goal
|
|
2
|
-
// (this week's range, "N of G sessions this week", a progress ring), their
|
|
2
|
+
// (this week's range, "N of G sessions this week", a progress ring), their weeks of meeting the goal and their sessions in total. The source
|
|
3
3
|
// reads a private fitness service; this reads the same member-scoped bookings as My Bookings (himi_bookings) and counts with the
|
|
4
4
|
// same rule as its "This week" card (`weekly-goal.ts`: a CONFIRMED booking that already took place; Wix attendance is owner-scoped,
|
|
5
5
|
// so a no-show still counts), so a guest or a refused member gets the sign-in state instead of a read.
|
|
6
|
-
// goal -> the weekly goal (the source's `defaultWeeklyGoal
|
|
6
|
+
// goal -> the default weekly goal (the source's `defaultWeeklyGoal`). A member picks their own from 1 to 14 in a sheet with a number wheel, as in
|
|
7
|
+
// the source; it is kept on this device under the member's key (`performance-tracker-goal.ts`), because the source's goal lives in a
|
|
8
|
+
// private service a member's session cannot call. A visitor keeps the default and is asked to sign in to change it,
|
|
7
9
|
// showTotal -> the all-time row (the source's `totalNumberOfSessions.show`).
|
|
8
10
|
// A page worker registers it under a service name; the descriptor binds `$service:<serviceName>.*` (recipe performance-tracker.json).
|
|
9
11
|
import { defineService } from "@himalaya/state";
|
|
10
12
|
import { hostSessionFromGlobal } from "@himalaya/iap";
|
|
11
|
-
import {
|
|
13
|
+
import { copyPlural, stashCopy } from "./himi-copy.js";
|
|
12
14
|
import { formatNumber } from "./engine-intl.js";
|
|
13
15
|
import { fetchMemberBookings } from "./member-bookings.js";
|
|
14
16
|
import { memberAccess, memberAccessDenied, memberChanged } from "./member-access.js";
|
|
15
17
|
import { parseWeeklyGoal, weeklyGoalProgress } from "./weekly-goal.js";
|
|
18
|
+
import { canKeepGoal, goalChoices, goalOwner, pickableGoal, readMemberGoal, saveMemberGoal, type GoalStore } from "./performance-tracker-goal.js";
|
|
16
19
|
import { DEFAULT_WEEKLY_GOAL, MAX_ALL_TIME_SESSIONS, MAX_PAGES, allTimeSessions, sessionsInWeek, streakMayBeLonger, weekRange, weeklyStreak } from "./performance-tracker-weeks.js";
|
|
20
|
+
import type { MemberBookingRow } from "./member-bookings.js";
|
|
17
21
|
|
|
18
22
|
export interface PerformanceTrackerOptions {
|
|
19
23
|
/** The weekly session goal. Not a goal in [1, 21]: the source's default of 2. */
|
|
@@ -22,11 +26,13 @@ export interface PerformanceTrackerOptions {
|
|
|
22
26
|
showTotal?: boolean;
|
|
23
27
|
}
|
|
24
28
|
|
|
29
|
+
export interface GoalOption { id: string; label: string }
|
|
30
|
+
|
|
25
31
|
export interface PerformanceTrackerState {
|
|
26
32
|
status: "loading" | "ready" | "error";
|
|
27
33
|
/** A guest, or a member Wix refused: the section asks to sign in instead of reading. */
|
|
28
34
|
signedOut: boolean;
|
|
29
|
-
/** "Nov 16
|
|
35
|
+
/** "Nov 16 - Nov 22". */
|
|
30
36
|
weekRange: string;
|
|
31
37
|
/** "1 of 2 sessions this week". */
|
|
32
38
|
goalLabel: string;
|
|
@@ -36,8 +42,15 @@ export interface PerformanceTrackerState {
|
|
|
36
42
|
progress: number;
|
|
37
43
|
/** "1/2", inside the ring. */
|
|
38
44
|
ringText: string;
|
|
45
|
+
/** The goal wheel: 1 to 14 (the numbers the wheel shows), the id of the wheel's choice, the id in force, and its label ("2 Sessions"; a visitor reads it as text). */
|
|
46
|
+
goalOptions: GoalOption[]; goalDraft: string; goalValue: string; goalSessions: string;
|
|
47
|
+
/** The goal sheet is open. */
|
|
48
|
+
goalOpen: boolean;
|
|
49
|
+
/** The device cannot keep a goal (or just failed to): the goal row is plain text, with no sheet to open. */
|
|
50
|
+
goalLocked: boolean; goalUnlocked: boolean;
|
|
51
|
+
/** "Weeks of meeting my goal": the count, then its label ("Week of..." for one). */
|
|
39
52
|
streak: string; streakLabel: string;
|
|
40
|
-
/** "500+" past the source's cap. */
|
|
53
|
+
/** "500+" past the source's cap, then "Sessions attended in total". */
|
|
41
54
|
total: string; totalLabel: string;
|
|
42
55
|
notLoading: boolean; notErrored: boolean; notSignedOut: boolean;
|
|
43
56
|
/** Nothing to show yet: loading, failed or signed out. */
|
|
@@ -47,70 +60,132 @@ export interface PerformanceTrackerState {
|
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
export function registerPerformanceTrackerSection(serviceName: string, options: PerformanceTrackerOptions = {}) {
|
|
50
|
-
const
|
|
63
|
+
const defaultGoal = parseWeeklyGoal(options.goal) || DEFAULT_WEEKLY_GOAL;
|
|
51
64
|
const showTotal = options.showTotal !== false;
|
|
52
|
-
const
|
|
65
|
+
const sessionsLabel = (n: number): string => copyPlural("performanceTracker.goalSessions", "{n, plural, one {# Session} other {# Sessions}}", n);
|
|
66
|
+
const weeksLabel = (n: number): string => copyPlural("performanceTracker.weeksMeetingGoal", "{n, plural, one {Week of meeting my goal} other {Weeks of meeting my goal}}", n);
|
|
67
|
+
const attendedLabel = (n: number): string => copyPlural("performanceTracker.sessionsAttended", "{n, plural, one {Session attended in total} other {Sessions attended in total}}", n);
|
|
68
|
+
const blank = {
|
|
69
|
+
goalLabel: "", remainingLabel: "", progress: 0, ringText: "", streak: "", total: "",
|
|
70
|
+
goalOptions: [] as GoalOption[], goalDraft: "", goalValue: "", goalSessions: "", goalOpen: false, goalLocked: false, goalUnlocked: true,
|
|
71
|
+
streakLabel: "", totalLabel: "",
|
|
72
|
+
};
|
|
53
73
|
const view = (status: PerformanceTrackerState["status"], signedOut: boolean, fields: Partial<typeof blank> = {}, now?: number): PerformanceTrackerState => {
|
|
54
74
|
const noContent = !(status === "ready" && !signedOut);
|
|
55
75
|
return {
|
|
56
76
|
// No clock read at registration (a worker boots deterministically): the range is set by the load.
|
|
57
77
|
status, signedOut, weekRange: now === undefined ? "" : weekRange(now), ...blank, ...fields,
|
|
58
|
-
streakLabel: copyParam("performanceTracker.weekStreak", "Week streak"),
|
|
59
|
-
totalLabel: copyParam("performanceTracker.allTimeSessions", "All-time sessions"),
|
|
60
78
|
notLoading: status !== "loading", notErrored: status !== "error", notSignedOut: !signedOut,
|
|
61
79
|
noContent, noTotal: noContent || !showTotal,
|
|
62
80
|
};
|
|
63
81
|
};
|
|
64
82
|
const session = hostSessionFromGlobal();
|
|
83
|
+
const goalStore = (): GoalStore | undefined => {
|
|
84
|
+
const host = globalThis as { himalaya?: { storage?: GoalStore }; localStorage?: GoalStore };
|
|
85
|
+
try { return host.himalaya?.storage ?? host.localStorage; } catch { return undefined; }
|
|
86
|
+
};
|
|
87
|
+
// What the last read found, so choosing a goal repaints this week's numbers without reading the bookings again.
|
|
88
|
+
let found: { rows: MemberBookingRow[]; now: number; unreadRemain: boolean; owner: string; locked: boolean } | undefined;
|
|
89
|
+
const ready = (read: NonNullable<typeof found>, goal: number): PerformanceTrackerState => {
|
|
90
|
+
const { rows, now, unreadRemain } = read;
|
|
91
|
+
const progress = weeklyGoalProgress(sessionsInWeek(rows, now, 0), goal);
|
|
92
|
+
const total = allTimeSessions(rows, now);
|
|
93
|
+
const streak = weeklyStreak(rows, now, goal);
|
|
94
|
+
return view("ready", false, {
|
|
95
|
+
goalLabel: progress.label, remainingLabel: progress.remainingLabel, progress: progress.fraction,
|
|
96
|
+
ringText: `${formatNumber(progress.done)}/${formatNumber(progress.goal)}`,
|
|
97
|
+
goalOptions: goalChoices(progress.goal).map((n) => ({ id: String(n), label: formatNumber(n) })),
|
|
98
|
+
goalDraft: String(progress.goal), goalValue: String(progress.goal), goalSessions: sessionsLabel(progress.goal),
|
|
99
|
+
goalLocked: read.locked, goalUnlocked: !read.locked,
|
|
100
|
+
streak: `${formatNumber(streak)}${streakMayBeLonger(rows, now, goal, streak, unreadRemain) ? "+" : ""}`, streakLabel: weeksLabel(streak),
|
|
101
|
+
total: total > MAX_ALL_TIME_SESSIONS ? `${formatNumber(MAX_ALL_TIME_SESSIONS)}+` : `${formatNumber(total)}${unreadRemain ? "+" : ""}`,
|
|
102
|
+
totalLabel: attendedLabel(Math.min(total, MAX_ALL_TIME_SESSIONS)),
|
|
103
|
+
}, now);
|
|
104
|
+
};
|
|
105
|
+
// A guest, or a member Wix refused, keeps the default goal and is asked to sign in to change it.
|
|
106
|
+
const signedOut = () => view("ready", true, { goalSessions: sessionsLabel(defaultGoal) });
|
|
65
107
|
// Only the newest load may answer: a guest read that finishes after the member signed in must not overwrite the member's progress.
|
|
66
108
|
let sequence = 0;
|
|
67
109
|
const initial = view("loading", false);
|
|
68
110
|
// The member changed: the progress already published is the previous member's, so it goes at once.
|
|
69
111
|
const restart = () => { void service.clear(); void service.load(); };
|
|
70
|
-
|
|
112
|
+
type State = PerformanceTrackerState;
|
|
113
|
+
const load = async (_state: State, arg?: unknown): Promise<State | undefined> => {
|
|
114
|
+
stashCopy(arg);
|
|
115
|
+
const seq = ++sequence;
|
|
116
|
+
let member: Awaited<ReturnType<typeof memberAccess>> | undefined;
|
|
117
|
+
try {
|
|
118
|
+
member = await memberAccess();
|
|
119
|
+
if (seq !== sequence) return undefined;
|
|
120
|
+
if (!member.email) return signedOut();
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
const nowIso = new Date(now).toISOString();
|
|
123
|
+
const read = (cursor: string) => fetchMemberBookings({ query: "", filter: "value:Past", sort: "za", savedIds: [] }, cursor, nowIso, member);
|
|
124
|
+
let page = await read("");
|
|
125
|
+
const rows = [...page.items];
|
|
126
|
+
for (let pages = 1; pages < MAX_PAGES && page.nextCursor; pages++) { page = await read(page.nextCursor); rows.push(...page.items); }
|
|
127
|
+
// Older bookings the page cap left unread: the streak and the all-time total are then "at least", never a silent shortfall.
|
|
128
|
+
const unreadRemain = Boolean(page.nextCursor);
|
|
129
|
+
if (seq !== sequence) return undefined;
|
|
130
|
+
const owner = goalOwner(member);
|
|
131
|
+
found = { rows, now, unreadRemain, owner, locked: !canKeepGoal(goalStore(), owner) };
|
|
132
|
+
return ready(found, readMemberGoal(goalStore(), owner) || defaultGoal);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (seq !== sequence) return undefined;
|
|
135
|
+
// The member signed out (or changed) while the read was out: read again for whoever is signed in now.
|
|
136
|
+
const changed = await memberChanged(member, error);
|
|
137
|
+
// A newer load may have answered while that check was out: restarting then would blank its card and read the bookings again.
|
|
138
|
+
if (seq !== sequence) return undefined;
|
|
139
|
+
if (changed) { restart(); return undefined; }
|
|
140
|
+
// Wix refusing the member is a sign-in state, as on My Bookings; anything else is a failed read.
|
|
141
|
+
if (memberAccessDenied(error)) return signedOut();
|
|
142
|
+
console.warn("performance_tracker_section: the bookings could not be read", error);
|
|
143
|
+
return view("error", false);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const service = defineService<State, {
|
|
147
|
+
load: (state: State, arg?: unknown) => Promise<State | undefined>;
|
|
148
|
+
refresh: (state: State, arg?: unknown) => Promise<State | undefined>;
|
|
149
|
+
openGoal: (state: State) => State;
|
|
150
|
+
closeGoal: (state: State) => State;
|
|
151
|
+
draftGoal: (state: State, arg?: unknown) => State;
|
|
152
|
+
chooseGoal: (state: State) => Promise<State>;
|
|
153
|
+
clear: () => Promise<State>;
|
|
154
|
+
}>(serviceName, {
|
|
71
155
|
state: initial,
|
|
72
156
|
// The progress is one member's: never written to disk and restored for the next person on the device.
|
|
73
|
-
notHydrated: Object.keys(initial) as Array<keyof
|
|
157
|
+
notHydrated: Object.keys(initial) as Array<keyof State>,
|
|
74
158
|
actions: {
|
|
75
|
-
clear: async () => initial,
|
|
76
|
-
load
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (seq !== sequence) return undefined;
|
|
104
|
-
// The member signed out (or changed) while the read was out: read again for whoever is signed in now.
|
|
105
|
-
const changed = await memberChanged(member, error);
|
|
106
|
-
// A newer load may have answered while that check was out: restarting then would blank its card and read the bookings again.
|
|
107
|
-
if (seq !== sequence) return undefined;
|
|
108
|
-
if (changed) { restart(); return undefined; }
|
|
109
|
-
// Wix refusing the member is a sign-in state, as on My Bookings; anything else is a failed read.
|
|
110
|
-
if (memberAccessDenied(error)) return view("ready", true);
|
|
111
|
-
console.warn("performance_tracker_section: the bookings could not be read", error);
|
|
112
|
-
return view("error", false);
|
|
159
|
+
clear: async () => { found = undefined; return initial; },
|
|
160
|
+
load,
|
|
161
|
+
// The "..." menu's Refresh: the same read, for the member who is here now.
|
|
162
|
+
refresh: load,
|
|
163
|
+
// The goal row opens the sheet on the goal in force.
|
|
164
|
+
openGoal: (state) => (state.status === "ready" && !state.signedOut && !state.goalLocked ? { ...state, goalOpen: true, goalDraft: state.goalValue } : state),
|
|
165
|
+
closeGoal: (state) => (state.goalOpen ? { ...state, goalOpen: false } : state),
|
|
166
|
+
// The wheel turned: only a goal the wheel offers is taken.
|
|
167
|
+
draftGoal: (state, arg) => {
|
|
168
|
+
const pick = pickableGoal((arg as { selectedId?: unknown } | undefined)?.selectedId);
|
|
169
|
+
return pick && state.goalOpen ? { ...state, goalDraft: String(pick) } : state;
|
|
170
|
+
},
|
|
171
|
+
// "Select": keep the wheel's choice for this member, then repaint and close. A choice that cannot be kept leaves the goal as it was.
|
|
172
|
+
chooseGoal: async (state) => {
|
|
173
|
+
const choice = pickableGoal(state.goalDraft);
|
|
174
|
+
const read = found;
|
|
175
|
+
if (!choice || !read || read.locked || state.status !== "ready" || state.signedOut) return { ...state, goalOpen: false };
|
|
176
|
+
if (String(choice) === state.goalValue) return { ...state, goalOpen: false };
|
|
177
|
+
const seq = sequence;
|
|
178
|
+
let owner = "";
|
|
179
|
+
try { owner = goalOwner(await memberAccess()); } catch { return { ...state, goalOpen: false }; }
|
|
180
|
+
// The member changed, or a newer load took over, while the identity was read: the choice was not this member's to keep.
|
|
181
|
+
if (seq !== sequence || owner !== read.owner) return { ...state, goalOpen: false };
|
|
182
|
+
if (!saveMemberGoal(goalStore(), owner, choice)) {
|
|
183
|
+
// The goal row turns into plain text: nothing is offered that cannot be kept, and the goal in force is the one shown.
|
|
184
|
+
console.warn("performance_tracker_section: the weekly goal could not be kept on this device");
|
|
185
|
+
read.locked = true;
|
|
186
|
+
return ready(read, readMemberGoal(goalStore(), owner) || defaultGoal);
|
|
113
187
|
}
|
|
188
|
+
return ready(read, choice);
|
|
114
189
|
},
|
|
115
190
|
},
|
|
116
191
|
});
|
|
@@ -62,10 +62,10 @@ export function allTimeSessions(rows: readonly MemberBookingRow[], now: number):
|
|
|
62
62
|
return new Set(rows.filter((row) => isHeldSession(row, now)).map((row) => row.id || `${row.start}|${row.title}`)).size;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/** "Nov 16
|
|
65
|
+
/** "Nov 16 - Nov 22" (the source's `{startDate} - {endDate}`): Monday to Sunday of the week `now` is in. */
|
|
66
66
|
export function weekRange(now: number): string {
|
|
67
67
|
const start = new Date(weekStartMs(now));
|
|
68
68
|
const end = new Date(start);
|
|
69
69
|
end.setDate(end.getDate() + 6);
|
|
70
|
-
return `${formatMonthDay(start)}
|
|
70
|
+
return `${formatMonthDay(start)} - ${formatMonthDay(end)}`;
|
|
71
71
|
}
|