@composable-os/pack-programs 0.1.0-beta.11 → 0.1.0-beta.13
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.d.ts +4 -1
- package/dist/authoring.js +65 -5
- package/dist/blocks/programs-builder/program-builder.d.ts +12 -3
- package/dist/blocks/programs-builder/program-builder.js +16 -3
- package/dist/blocks/programs-class-today/class-today.d.ts +1 -1
- package/dist/blocks/programs-coach-client/coach-client.d.ts +1 -1
- package/dist/blocks/programs-history/history.d.ts +11 -0
- package/dist/blocks/programs-history/history.js +84 -0
- package/dist/blocks/programs-native-class-today/class-today-screen.js +3 -2
- package/dist/blocks/programs-native-coach-client/coach-client-screen.js +2 -2
- package/dist/blocks/programs-native-history/history-screen.d.ts +11 -0
- package/dist/blocks/programs-native-history/history-screen.js +98 -0
- package/dist/blocks/programs-native-progress/progress-screen.js +4 -4
- package/dist/blocks/programs-native-session/session-screen.d.ts +6 -2
- package/dist/blocks/programs-native-session/session-screen.js +214 -88
- package/dist/blocks/programs-native-today/today-screen.d.ts +1 -1
- package/dist/blocks/programs-native-today/today-screen.js +22 -3
- package/dist/blocks/programs-session/session-view.d.ts +7 -3
- package/dist/blocks/programs-session/session-view.js +139 -81
- package/dist/blocks/programs-today/today.d.ts +1 -1
- package/dist/client/index.d.ts +367 -12
- package/dist/client/index.js +393 -12
- package/dist/client/logging.d.ts +289 -0
- package/dist/client/logging.js +558 -0
- package/dist/index.d.ts +152 -6
- package/dist/index.js +141 -6
- package/dist/layout.d.ts +3 -1
- package/dist/layout.js +10 -2
- package/dist/tools.d.ts +25 -2
- package/dist/tools.js +269 -37
- package/package.json +3 -3
package/dist/client/index.js
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
// `user.me` says the account holds, and the host still decides); the dates, targets and last
|
|
11
11
|
// numbers are the tools'. The editing functions below only move parts of a layout around.
|
|
12
12
|
import { packHooks } from "@composable-os/client";
|
|
13
|
+
import { createSessionLog, logExercisesOf, unsaved, } from "./logging.js";
|
|
14
|
+
export * from "./logging.js";
|
|
13
15
|
export const PROGRAM = "programs.program";
|
|
14
16
|
export const EXERCISE = "programs.exercise";
|
|
15
17
|
export const SESSION = "programs.session";
|
|
@@ -68,6 +70,218 @@ export function numberFrom(text) {
|
|
|
68
70
|
const value = Number(text.replace(",", "."));
|
|
69
71
|
return Number.isFinite(value) ? value : undefined;
|
|
70
72
|
}
|
|
73
|
+
/** Sessions a page of the history lists. Their ids go in one `in` filter on the sets. */
|
|
74
|
+
export const PAST_SESSIONS_PAGE = 20;
|
|
75
|
+
/** The most pages of sets read for one page of sessions (200 a page). */
|
|
76
|
+
const PAST_SETS_CALLS = 5;
|
|
77
|
+
/**
|
|
78
|
+
* The sets of a session for one person, in the plan's order, each with what they logged. An item
|
|
79
|
+
* for everyone has every exercise's sets; an item with levels the level they logged at (or its
|
|
80
|
+
* first level when they logged none of it); alternatives the one they did (or the first, when
|
|
81
|
+
* they did none). A set logged beyond the plan's sets follows its exercise's, with no target.
|
|
82
|
+
* Warm-ups are not logged and not listed. An item with levels and exercises for everyone has
|
|
83
|
+
* everyone's first, then the level's. A session with no plan (one imported from before) has the
|
|
84
|
+
* sets logged on it, as they were, with no targets.
|
|
85
|
+
*/
|
|
86
|
+
export function sessionSets(plan, logged) {
|
|
87
|
+
const out = [];
|
|
88
|
+
if (!plan) {
|
|
89
|
+
for (const l of logged) {
|
|
90
|
+
out.push({
|
|
91
|
+
block: "",
|
|
92
|
+
item: l.item ?? "",
|
|
93
|
+
exercise: l.exercise ?? "",
|
|
94
|
+
level: l.level,
|
|
95
|
+
setNumber: l.setNumber ?? 1,
|
|
96
|
+
target: null,
|
|
97
|
+
logged: l,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return out.sort((a, b) => a.item.localeCompare(b.item) ||
|
|
101
|
+
a.exercise.localeCompare(b.exercise) ||
|
|
102
|
+
a.setNumber - b.setNumber);
|
|
103
|
+
}
|
|
104
|
+
for (const block of plan.blocks) {
|
|
105
|
+
for (const item of block.items) {
|
|
106
|
+
const mine = logged.filter((l) => l.item === item.label);
|
|
107
|
+
let level = null;
|
|
108
|
+
let list;
|
|
109
|
+
if (item.kind === "all")
|
|
110
|
+
list = item.exercises;
|
|
111
|
+
else {
|
|
112
|
+
const takenLevel = mine.find((l) => l.level !== null)?.level;
|
|
113
|
+
const chosen = item.levels.find((l) => l.level === takenLevel) ?? item.levels[0];
|
|
114
|
+
if (!chosen)
|
|
115
|
+
continue;
|
|
116
|
+
level = chosen.level;
|
|
117
|
+
list = levelExercises(chosen);
|
|
118
|
+
if (chosen.mode === "alternatives") {
|
|
119
|
+
const did = list.filter((p) => mine.some((l) => l.exercise === p.exercise));
|
|
120
|
+
list = did.length ? did : list.slice(0, 1);
|
|
121
|
+
}
|
|
122
|
+
list = [...(item.exercises ?? []), ...list];
|
|
123
|
+
}
|
|
124
|
+
for (const p of list) {
|
|
125
|
+
const sets = mine.filter((l) => l.exercise === p.exercise);
|
|
126
|
+
const most = Math.max(p.sets.length, ...sets.map((l) => l.setNumber ?? 0));
|
|
127
|
+
for (let n = 1; n <= most; n += 1) {
|
|
128
|
+
out.push({
|
|
129
|
+
block: block.label,
|
|
130
|
+
item: item.label,
|
|
131
|
+
exercise: p.exercise,
|
|
132
|
+
level,
|
|
133
|
+
setNumber: n,
|
|
134
|
+
target: p.sets[n - 1] ?? null,
|
|
135
|
+
logged: sets.find((l) => l.setNumber === n),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
/** How many sets a session's plan asks of one person, given what they logged (`sessionSets`). */
|
|
144
|
+
export function plannedSets(plan, logged) {
|
|
145
|
+
return sessionSets(plan, logged).filter((s) => s.target !== null).length;
|
|
146
|
+
}
|
|
147
|
+
/** Past sessions from their records and the sets logged on them (any order). */
|
|
148
|
+
export function pastSessionsOf(sessions, sets) {
|
|
149
|
+
const bySession = new Map();
|
|
150
|
+
for (const set of sets) {
|
|
151
|
+
const session = str(set.values.session);
|
|
152
|
+
if (!session)
|
|
153
|
+
continue;
|
|
154
|
+
const list = bySession.get(session) ?? [];
|
|
155
|
+
list.push({
|
|
156
|
+
item: str(set.values.item),
|
|
157
|
+
exercise: str(set.values.exercise),
|
|
158
|
+
level: typeof set.values.level === "number" ? set.values.level : null,
|
|
159
|
+
setNumber: typeof set.values.setNumber === "number" ? set.values.setNumber : null,
|
|
160
|
+
});
|
|
161
|
+
bySession.set(session, list);
|
|
162
|
+
}
|
|
163
|
+
return sessions.map((s) => {
|
|
164
|
+
const logged = bySession.get(s.id) ?? [];
|
|
165
|
+
return {
|
|
166
|
+
id: s.id,
|
|
167
|
+
date: str(s.values.date),
|
|
168
|
+
name: str(s.values.name),
|
|
169
|
+
programTitle: str(s.values.programTitle),
|
|
170
|
+
focus: str(s.values.focus),
|
|
171
|
+
state: str(s.values.state),
|
|
172
|
+
setsDone: logged.length,
|
|
173
|
+
setsPlanned: plannedSets(s.values.plan, logged),
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const str = (value) => value === null || value === undefined ? null : String(value);
|
|
178
|
+
/**
|
|
179
|
+
* One page of a person's past sessions (their own, dated `until` or before), newest first, each
|
|
180
|
+
* with its sets logged and planned. A member reads their own through the seam (`my.list`: never a
|
|
181
|
+
* class's sessions, never another person's sets); a coach names the client (`record.list`). Two
|
|
182
|
+
* reads for a page of sessions, and a few more only for a page with very many sets.
|
|
183
|
+
*/
|
|
184
|
+
export async function loadPastSessions(read, options) {
|
|
185
|
+
const person = options.personId ? [{ field: "person", op: "eq", value: options.personId }] : [];
|
|
186
|
+
const page = await read({
|
|
187
|
+
object: SESSION,
|
|
188
|
+
filter: {
|
|
189
|
+
and: [
|
|
190
|
+
// Their own: assigned to them, or imported from before (no kind: it was never planned).
|
|
191
|
+
{
|
|
192
|
+
or: [
|
|
193
|
+
{ field: "kind", op: "eq", value: "personal" },
|
|
194
|
+
{ field: "source", op: "eq", value: "import" },
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
{ field: "date", op: "lte", value: options.until },
|
|
198
|
+
...person,
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
sort: { field: "date", direction: "desc" },
|
|
202
|
+
limit: PAST_SESSIONS_PAGE,
|
|
203
|
+
...(options.cursor ? { cursor: options.cursor } : {}),
|
|
204
|
+
});
|
|
205
|
+
const ids = page.items.map((s) => s.id);
|
|
206
|
+
const sets = [];
|
|
207
|
+
let next = null;
|
|
208
|
+
for (let calls = 0; ids.length && calls < PAST_SETS_CALLS; calls += 1) {
|
|
209
|
+
const found = await read({
|
|
210
|
+
object: LOG,
|
|
211
|
+
filter: {
|
|
212
|
+
and: [{ field: "session", op: "in", value: ids }, ...person],
|
|
213
|
+
},
|
|
214
|
+
sort: { field: "date", direction: "desc" },
|
|
215
|
+
limit: 200,
|
|
216
|
+
...(next ? { cursor: next } : {}),
|
|
217
|
+
});
|
|
218
|
+
sets.push(...found.items);
|
|
219
|
+
next = found.nextCursor;
|
|
220
|
+
if (!next)
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
return { sessions: pastSessionsOf(page.items, sets), nextCursor: page.nextCursor };
|
|
224
|
+
}
|
|
225
|
+
/** What was done in a logged set, in words: "8 reps @ 20 kg · RPE 7". */
|
|
226
|
+
export function describeDone(set) {
|
|
227
|
+
const parts = [];
|
|
228
|
+
if (set.actual.reps !== null)
|
|
229
|
+
parts.push(`${set.actual.reps} reps`);
|
|
230
|
+
if (set.actual.seconds !== null)
|
|
231
|
+
parts.push(`${set.actual.seconds} s`);
|
|
232
|
+
if (set.actual.load !== null) {
|
|
233
|
+
parts[0] = parts[0] ? `${parts[0]} @ ${set.actual.load} kg` : `${set.actual.load} kg`;
|
|
234
|
+
}
|
|
235
|
+
if (set.rpe !== null)
|
|
236
|
+
parts.push(`RPE ${set.rpe}`);
|
|
237
|
+
return parts.join(" · ") || "Logged";
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* A correction to a logged set from what was typed: only what changed, as `x.programs.logUpdate`
|
|
241
|
+
* takes it (the numbers done, effort, the note), or null when nothing did. An emptied note clears
|
|
242
|
+
* it; an emptied number is left as it was. The target, the exercise and who recorded it are not
|
|
243
|
+
* the person's to change; the host keeps them and bounds the numbers.
|
|
244
|
+
*/
|
|
245
|
+
export function correctionOf(set, typed, timed) {
|
|
246
|
+
const count = numberFrom(typed.count);
|
|
247
|
+
const load = numberFrom(typed.load);
|
|
248
|
+
const rpe = numberFrom(typed.rpe);
|
|
249
|
+
const note = typed.note.trim() ? typed.note.trim() : null;
|
|
250
|
+
const actual = {};
|
|
251
|
+
if (count !== undefined && count !== (timed ? set.actual.seconds : set.actual.reps)) {
|
|
252
|
+
if (timed)
|
|
253
|
+
actual.seconds = count;
|
|
254
|
+
else
|
|
255
|
+
actual.reps = count;
|
|
256
|
+
}
|
|
257
|
+
if (load !== undefined && load !== set.actual.load)
|
|
258
|
+
actual.load = load;
|
|
259
|
+
const out = {
|
|
260
|
+
logId: set.id,
|
|
261
|
+
...(Object.keys(actual).length ? { actual } : {}),
|
|
262
|
+
...(rpe !== undefined && rpe !== set.rpe ? { rpe } : {}),
|
|
263
|
+
...(note !== (set.note ?? null) ? { note } : {}),
|
|
264
|
+
};
|
|
265
|
+
return Object.keys(out).length > 1 ? out : null;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* The hook a history block lists past sessions with: a coach's client's when `personId` is given
|
|
269
|
+
* (the staff client's `useClientPastSessions`), else the member's own (`usePastSessions`). A
|
|
270
|
+
* block's hooks and person are fixed for its life, so the hook it calls never changes between
|
|
271
|
+
* renders: `const usePage = pastSessionsHook(programs, personId); const page = usePage({})`.
|
|
272
|
+
*/
|
|
273
|
+
export function pastSessionsHook(programs, personId) {
|
|
274
|
+
if (personId) {
|
|
275
|
+
const hook = programs.useClientPastSessions;
|
|
276
|
+
if (!hook)
|
|
277
|
+
throw new Error("A client's history needs the staff client's useClientPastSessions");
|
|
278
|
+
return (options) => hook(personId, options);
|
|
279
|
+
}
|
|
280
|
+
const hook = programs.usePastSessions;
|
|
281
|
+
if (!hook)
|
|
282
|
+
throw new Error("A member's history needs the customer client's usePastSessions");
|
|
283
|
+
return hook;
|
|
284
|
+
}
|
|
71
285
|
// --- Faults from the host, where they are ---------------------------------------------------------
|
|
72
286
|
/**
|
|
73
287
|
* The faults the host reported for a layout, by path: `weeks.0.sessions.1.day` → "At most 7". The
|
|
@@ -153,11 +367,11 @@ function itemAt(layout, at) {
|
|
|
153
367
|
throw new Error("No item there");
|
|
154
368
|
return item;
|
|
155
369
|
}
|
|
156
|
-
/** The prescription at `at`: an "all" item's `exercise`, or a level's. */
|
|
370
|
+
/** The prescription at `at`: an "all" item's `exercise`, everyone's beside levels, or a level's. */
|
|
157
371
|
export function prescriptionAt(layout, at) {
|
|
158
372
|
const item = itemAt(layout, at);
|
|
159
|
-
if (item.kind === "all") {
|
|
160
|
-
const p = item.exercises[at.exercise ?? 0];
|
|
373
|
+
if (item.kind === "all" || at.everyone) {
|
|
374
|
+
const p = (item.kind === "all" ? item.exercises : (item.exercises ?? []))[at.exercise ?? 0];
|
|
161
375
|
if (!p)
|
|
162
376
|
throw new Error("No exercise there");
|
|
163
377
|
return p;
|
|
@@ -333,11 +547,12 @@ export const layoutEdits = {
|
|
|
333
547
|
};
|
|
334
548
|
}
|
|
335
549
|
else if (kind === "all" && item.kind === "levels") {
|
|
550
|
+
// Everyone's exercises stay everyone's; with none, level 1's become everyone's.
|
|
336
551
|
const first = item.levels[0];
|
|
337
552
|
b.items[index] = {
|
|
338
553
|
kind: "all",
|
|
339
554
|
label: item.label,
|
|
340
|
-
exercises: first ? levelExercises(first) : [],
|
|
555
|
+
exercises: item.exercises?.length ? item.exercises : first ? levelExercises(first) : [],
|
|
341
556
|
...(item.notes ? { notes: item.notes } : {}),
|
|
342
557
|
};
|
|
343
558
|
}
|
|
@@ -385,7 +600,7 @@ export const layoutEdits = {
|
|
|
385
600
|
? { mode, level: level.level, exercise: exercises[0] }
|
|
386
601
|
: { mode, level: level.level, exercises };
|
|
387
602
|
}),
|
|
388
|
-
/** Another exercise beside the ones at `at` (an "all" item's, or a level's). */
|
|
603
|
+
/** Another exercise beside the ones at `at` (an "all" item's, everyone's, or a level's). */
|
|
389
604
|
addExercise: (layout, at, exercise, measure) => edit(layout, (l) => {
|
|
390
605
|
const item = itemAt(l, at);
|
|
391
606
|
const p = newPrescription(exercise, measure);
|
|
@@ -393,6 +608,10 @@ export const layoutEdits = {
|
|
|
393
608
|
item.exercises.push(p);
|
|
394
609
|
return;
|
|
395
610
|
}
|
|
611
|
+
if (at.everyone) {
|
|
612
|
+
item.exercises = [...(item.exercises ?? []), p];
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
396
615
|
const index = at.level ?? -1;
|
|
397
616
|
const level = item.levels[index];
|
|
398
617
|
if (!level)
|
|
@@ -409,6 +628,14 @@ export const layoutEdits = {
|
|
|
409
628
|
item.exercises.splice(index, 1);
|
|
410
629
|
return;
|
|
411
630
|
}
|
|
631
|
+
if (at.everyone) {
|
|
632
|
+
const rest = (item.exercises ?? []).filter((_, i) => i !== index);
|
|
633
|
+
if (rest.length)
|
|
634
|
+
item.exercises = rest;
|
|
635
|
+
else
|
|
636
|
+
delete item.exercises;
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
412
639
|
const li = at.level ?? -1;
|
|
413
640
|
const level = item.levels[li];
|
|
414
641
|
if (!level || level.mode === "one")
|
|
@@ -514,7 +741,9 @@ export function exercisesIn(layout) {
|
|
|
514
741
|
ids.add(p.exercise);
|
|
515
742
|
for (const b of s.blocks) {
|
|
516
743
|
for (const i of b.items) {
|
|
517
|
-
const all = i.kind === "all"
|
|
744
|
+
const all = i.kind === "all"
|
|
745
|
+
? i.exercises
|
|
746
|
+
: [...(i.exercises ?? []), ...i.levels.flatMap(levelExercises)];
|
|
518
747
|
for (const p of all)
|
|
519
748
|
ids.add(p.exercise);
|
|
520
749
|
}
|
|
@@ -538,11 +767,16 @@ export const PROGRAMS_HOOKS_CALL = {
|
|
|
538
767
|
useLogSet: ["x.programs.logAdd"],
|
|
539
768
|
useUpdateSet: ["x.programs.logUpdate"],
|
|
540
769
|
useMarkSession: ["x.programs.sessionComplete"],
|
|
770
|
+
useSessionLog: ["x.programs.sessionGet", "x.programs.logAdd", "x.programs.logRemove"],
|
|
771
|
+
useUnsavedSets: [],
|
|
541
772
|
useHistory: ["x.programs.history"],
|
|
542
773
|
useProgress: ["x.programs.progress"],
|
|
543
774
|
useClientSessions: ["record.list"],
|
|
775
|
+
usePastSessions: ["my.list"],
|
|
776
|
+
useClientPastSessions: ["record.list"],
|
|
544
777
|
usePeople: ["person.list"],
|
|
545
|
-
|
|
778
|
+
// Staff read them with record.list, a member with my.list (every signed-in member reads them).
|
|
779
|
+
useClassTypes: { anyOf: [["record.list"], ["my.list"]] },
|
|
546
780
|
useBank: ["record.list"],
|
|
547
781
|
useExercises: ["record.list"],
|
|
548
782
|
useCreateExercise: ["record.create"],
|
|
@@ -565,11 +799,17 @@ export const PROGRAMS_HOOKS_CALL = {
|
|
|
565
799
|
* not compile.
|
|
566
800
|
*/
|
|
567
801
|
export function createProgramsHooks(react, client) {
|
|
568
|
-
return packHooks(client, PROGRAMS_HOOKS_CALL, (api) => allProgramsHooks(react, api));
|
|
802
|
+
return packHooks(client, PROGRAMS_HOOKS_CALL, (api, holds) => allProgramsHooks(react, api, holds));
|
|
569
803
|
}
|
|
570
|
-
function allProgramsHooks(react, api) {
|
|
804
|
+
function allProgramsHooks(react, api, holds = () => true) {
|
|
571
805
|
const { useQuery, useMutation, useState } = react;
|
|
572
806
|
const useEffect = react.useEffect ?? (() => undefined);
|
|
807
|
+
const logs = sessionLogs(api);
|
|
808
|
+
const needsEffect = () => {
|
|
809
|
+
if (!react.useEffect) {
|
|
810
|
+
throw new Error("Logging sets needs the app's useEffect: pass it to createProgramsHooks");
|
|
811
|
+
}
|
|
812
|
+
};
|
|
573
813
|
return {
|
|
574
814
|
// A member's (or, for staff, a named client's) training.
|
|
575
815
|
useToday(input = {}) {
|
|
@@ -590,6 +830,39 @@ function allProgramsHooks(react, api) {
|
|
|
590
830
|
useMarkSession() {
|
|
591
831
|
return useMutation(api.mutation("x.programs.sessionComplete"));
|
|
592
832
|
},
|
|
833
|
+
/**
|
|
834
|
+
* Logging a session set by set (`./logging.ts`): the session from the host, and its log, a
|
|
835
|
+
* queue that shows every tick and key press at once and saves them in order, one at a time,
|
|
836
|
+
* retrying what fails. The log outlives the screen: leaving it drops nothing, and `flush`
|
|
837
|
+
* sends what waits (the app going to the background, finishing). A coach names the client.
|
|
838
|
+
*/
|
|
839
|
+
useSessionLog(input) {
|
|
840
|
+
const detail = useQuery(api.query("x.programs.sessionGet", input));
|
|
841
|
+
const log = logs.of(input.sessionId, input.personId);
|
|
842
|
+
needsEffect();
|
|
843
|
+
// Re-rendered on every change of the log, whoever made it.
|
|
844
|
+
const [, setSeen] = useState(0);
|
|
845
|
+
useEffect(() => log.subscribe(() => setSeen((n) => n + 1)), [log]);
|
|
846
|
+
const data = detail.data;
|
|
847
|
+
useEffect(() => {
|
|
848
|
+
if (!data)
|
|
849
|
+
return undefined;
|
|
850
|
+
log.dispatch({
|
|
851
|
+
type: "load",
|
|
852
|
+
exercises: logExercisesOf(data.session.plan, data.exercises),
|
|
853
|
+
logged: data.logged,
|
|
854
|
+
});
|
|
855
|
+
return undefined;
|
|
856
|
+
}, [data, log]);
|
|
857
|
+
return { detail, log, state: log.getState() };
|
|
858
|
+
},
|
|
859
|
+
/** The sets not yet saved in every session's log, and a flush of them all (Today, the app). */
|
|
860
|
+
useUnsavedSets() {
|
|
861
|
+
needsEffect();
|
|
862
|
+
const [, setSeen] = useState(0);
|
|
863
|
+
useEffect(() => logs.subscribe(() => setSeen((n) => n + 1)), []);
|
|
864
|
+
return { count: logs.unsaved(), failure: logs.failure(), flush: logs.flush };
|
|
865
|
+
},
|
|
593
866
|
useHistory(input) {
|
|
594
867
|
return useQuery(api.query("x.programs.history", input));
|
|
595
868
|
},
|
|
@@ -605,6 +878,40 @@ function allProgramsHooks(react, api) {
|
|
|
605
878
|
limit: 100,
|
|
606
879
|
}));
|
|
607
880
|
},
|
|
881
|
+
/**
|
|
882
|
+
* A member's own past sessions, newest first (`loadPastSessions`): dated `until` (the
|
|
883
|
+
* person's local date; default today, UTC) or before. Pass a page's `nextCursor` for older.
|
|
884
|
+
*/
|
|
885
|
+
usePastSessions(options = {}) {
|
|
886
|
+
const until = options.until ?? new Date().toISOString().slice(0, 10);
|
|
887
|
+
return useQuery({
|
|
888
|
+
// The client's own key for the page of sessions (under my.list, and under a screen's
|
|
889
|
+
// scope where the app gives one), so what makes those stale or forgets them does here too.
|
|
890
|
+
queryKey: [
|
|
891
|
+
...api.query("my.list", { object: SESSION }).queryKey,
|
|
892
|
+
{ past: true, until, cursor: options.cursor ?? null },
|
|
893
|
+
],
|
|
894
|
+
queryFn: () => loadPastSessions((input) => api.query("my.list", input).queryFn(), {
|
|
895
|
+
until,
|
|
896
|
+
...(options.cursor ? { cursor: options.cursor } : {}),
|
|
897
|
+
}),
|
|
898
|
+
});
|
|
899
|
+
},
|
|
900
|
+
/** A client's past sessions, newest first (staff): as `usePastSessions`, naming the client. */
|
|
901
|
+
useClientPastSessions(personId, options = {}) {
|
|
902
|
+
const until = options.until ?? new Date().toISOString().slice(0, 10);
|
|
903
|
+
return useQuery({
|
|
904
|
+
queryKey: [
|
|
905
|
+
...api.query("record.list", { object: SESSION }).queryKey,
|
|
906
|
+
{ past: true, personId, until, cursor: options.cursor ?? null },
|
|
907
|
+
],
|
|
908
|
+
queryFn: () => loadPastSessions((input) => api.query("record.list", input).queryFn(), {
|
|
909
|
+
personId,
|
|
910
|
+
until,
|
|
911
|
+
...(options.cursor ? { cursor: options.cursor } : {}),
|
|
912
|
+
}),
|
|
913
|
+
});
|
|
914
|
+
},
|
|
608
915
|
/** People whose name contains `search` (staff): whom to assign a program to. */
|
|
609
916
|
usePeople(search) {
|
|
610
917
|
return useQuery({
|
|
@@ -615,12 +922,16 @@ function allProgramsHooks(react, api) {
|
|
|
615
922
|
enabled: search.trim().length > 1,
|
|
616
923
|
});
|
|
617
924
|
},
|
|
618
|
-
/**
|
|
925
|
+
/**
|
|
926
|
+
* The class types, for a member choosing their class (`my.list`) or a coach assigning one
|
|
927
|
+
* (`record.list`): whichever read the client holds, staff's first.
|
|
928
|
+
*/
|
|
619
929
|
useClassTypes() {
|
|
620
|
-
|
|
930
|
+
const input = {
|
|
621
931
|
object: "programs.classtype",
|
|
622
932
|
sort: { field: "order", direction: "asc" },
|
|
623
|
-
}
|
|
933
|
+
};
|
|
934
|
+
return useQuery(api.query(holds("record.list") ? "record.list" : "my.list", input));
|
|
624
935
|
},
|
|
625
936
|
// The bank.
|
|
626
937
|
useBank(search) {
|
|
@@ -777,3 +1088,73 @@ function allProgramsHooks(react, api) {
|
|
|
777
1088
|
},
|
|
778
1089
|
};
|
|
779
1090
|
}
|
|
1091
|
+
/**
|
|
1092
|
+
* The session logs of one hooks object, by session and person: they live as long as the app, not
|
|
1093
|
+
* the screen, so a save still out when the screen is left lands.
|
|
1094
|
+
*/
|
|
1095
|
+
function sessionLogs(api) {
|
|
1096
|
+
const logs = new Map();
|
|
1097
|
+
const listeners = new Set();
|
|
1098
|
+
const notify = () => {
|
|
1099
|
+
for (const listener of [...listeners])
|
|
1100
|
+
listener();
|
|
1101
|
+
};
|
|
1102
|
+
const add = api.mutation("x.programs.logAdd").mutationFn;
|
|
1103
|
+
const remove = api.mutation("x.programs.logRemove").mutationFn;
|
|
1104
|
+
const all = () => [...logs.values()];
|
|
1105
|
+
return {
|
|
1106
|
+
of(sessionId, personId) {
|
|
1107
|
+
const key = `${sessionId}|${personId ?? ""}`;
|
|
1108
|
+
const found = logs.get(key);
|
|
1109
|
+
if (found)
|
|
1110
|
+
return found;
|
|
1111
|
+
const who = personId ? { personId } : {};
|
|
1112
|
+
// One input object per save and revision, so a retry is sent with the same idempotency key.
|
|
1113
|
+
const inputs = new WeakMap();
|
|
1114
|
+
const send = (op) => {
|
|
1115
|
+
if (op.kind === "remove") {
|
|
1116
|
+
const input = inputs.get(op) ?? {
|
|
1117
|
+
sessionId,
|
|
1118
|
+
item: op.item,
|
|
1119
|
+
exerciseId: op.exercise,
|
|
1120
|
+
setNumber: op.setNumber,
|
|
1121
|
+
...who,
|
|
1122
|
+
};
|
|
1123
|
+
inputs.set(op, input);
|
|
1124
|
+
return remove(input);
|
|
1125
|
+
}
|
|
1126
|
+
const input = inputs.get(op) ?? {
|
|
1127
|
+
sessionId,
|
|
1128
|
+
item: op.item,
|
|
1129
|
+
exerciseId: op.exercise,
|
|
1130
|
+
...(op.level !== null ? { level: op.level } : {}),
|
|
1131
|
+
setNumber: op.setNumber,
|
|
1132
|
+
actual: op.actual,
|
|
1133
|
+
...(op.rpe !== null ? { rpe: op.rpe } : {}),
|
|
1134
|
+
...who,
|
|
1135
|
+
};
|
|
1136
|
+
inputs.set(op, input);
|
|
1137
|
+
return add(input);
|
|
1138
|
+
};
|
|
1139
|
+
const log = createSessionLog({ send });
|
|
1140
|
+
log.subscribe(notify);
|
|
1141
|
+
logs.set(key, log);
|
|
1142
|
+
return log;
|
|
1143
|
+
},
|
|
1144
|
+
subscribe(listener) {
|
|
1145
|
+
listeners.add(listener);
|
|
1146
|
+
return () => {
|
|
1147
|
+
listeners.delete(listener);
|
|
1148
|
+
};
|
|
1149
|
+
},
|
|
1150
|
+
unsaved: () => all().reduce((n, log) => n + unsavedOf(log), 0),
|
|
1151
|
+
failure: () => all()
|
|
1152
|
+
.find((log) => log.getState().failure)
|
|
1153
|
+
?.getState().failure ?? null,
|
|
1154
|
+
async flush(timeoutMs) {
|
|
1155
|
+
const results = await Promise.all(all().map((log) => log.flush(timeoutMs)));
|
|
1156
|
+
return results.every(Boolean);
|
|
1157
|
+
},
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
const unsavedOf = (log) => unsaved(log.getState());
|