forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkout-session-viewer.html
More file actions
662 lines (549 loc) · 24.9 KB
/
workout-session-viewer.html
File metadata and controls
662 lines (549 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Workout Session Viewer</title>
<link rel="stylesheet" href="styles.css">
<style>
:root {
--card-padding: clamp(1rem, 3vw, 1.5rem);
}
body {
max-width: 1100px;
margin: 0 auto;
padding: clamp(1.5rem, 3vw, 2.5rem) clamp(1rem, 3vw, 2rem) clamp(3rem, 5vw, 4rem);
}
header h1 {
margin-bottom: 0.35rem;
}
header .lead {
margin-top: 0;
}
main {
display: grid;
gap: 1.25rem;
}
.surface {
padding: var(--card-padding);
}
.stack {
display: grid;
gap: 0.75rem;
}
.controls {
display: flex;
align-items: center;
gap: 0.5rem;
flex-wrap: wrap;
}
.status-text {
color: var(--text-muted);
font-size: 0.95rem;
}
label + select,
label + input {
width: 100%;
}
.panel-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.nav-buttons {
display: inline-flex;
gap: 0.35rem;
}
.record-meta {
display: flex;
flex-wrap: wrap;
gap: 0.75rem 1.25rem;
align-items: center;
}
.chip {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
border-radius: 999px;
border: 1px solid var(--border);
background: var(--surface-3);
font-size: 0.92rem;
}
.exercise-card {
border: 1px solid var(--border);
border-radius: 12px;
background: var(--surface-2);
padding: 1rem;
display: grid;
gap: 0.65rem;
}
.exercise-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 0.75rem;
}
.exercise-name {
font-size: 1.05rem;
font-weight: 700;
margin: 0;
}
.set-grid {
display: grid;
gap: 0.5rem;
}
.set-row {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 0.4rem;
align-items: center;
}
.set-label {
font-weight: 700;
}
.delta {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 2px 8px;
border-radius: 999px;
background: var(--surface-3);
font-weight: 700;
font-size: 0.92rem;
}
.delta.positive {
color: var(--gr);
}
.delta.negative {
color: var(--re);
}
.delta.neutral {
color: var(--text-muted);
}
.notes {
margin: 0;
color: var(--text-muted);
}
.empty-state {
padding: 0.85rem 1rem;
border-radius: 10px;
border: 1px dashed var(--border);
background: var(--surface-2);
}
.comparison-controls {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
align-items: center;
}
.workout-links {
border: 1px solid var(--border);
background: var(--surface-1);
}
.link-grid {
display: grid;
gap: 0.5rem;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.link-card {
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: 0.4rem;
padding: 0.65rem 0.8rem;
border: 1px solid var(--border);
border-radius: 10px;
background: var(--surface-2);
text-decoration: none;
font-weight: 600;
}
.link-card:hover { border-color: var(--accent); }
@media (max-width: 720px) {
.panel-header {
flex-direction: column;
align-items: flex-start;
}
.record-meta {
flex-direction: column;
align-items: flex-start;
}
}
</style>
</head>
<body>
<header class="page-header">
<a class="site-link" href="https://tools.mathspp.com/" aria-label="Back to tools.mathspp.com">← tools.mathspp.com</a>
<h1>Workout Session Viewer</h1>
<p class="lead">Browse past sessions for a template, move through them one by one, and compare sets side by side.</p>
</header>
<main>
<section class="surface stack">
<h2 style="margin: 0;">API configuration</h2>
<p class="status-text">Set the API base URL and bearer token once in <a href="workout-settings.html">Workout settings</a>. Stored values are reused automatically.</p>
</section>
<section class="surface stack">
<div class="controls" style="justify-content: space-between; align-items: center; gap: 1rem;">
<div class="stack" style="gap: 0.35rem;">
<h2 style="margin: 0;">Select template</h2>
<p class="status-text" id="template-status">Choose a workout template to view its history.</p>
</div>
<button type="button" id="refresh-templates" class="secondary">Refresh templates</button>
</div>
<label class="sr-only" for="template-select">Workout template</label>
<select id="template-select" aria-label="Workout template">
<option value="" disabled selected>Pick a template…</option>
</select>
</section>
<section class="surface stack" id="history-panel" hidden>
<div class="panel-header">
<div class="stack" style="gap: 0.25rem;">
<h2 style="margin: 0;">Template history</h2>
<p class="status-text" id="record-status">Loading records…</p>
</div>
<div class="nav-buttons" aria-label="Record navigation">
<button type="button" id="previous-record" class="secondary" aria-label="Previous record">←</button>
<button type="button" id="next-record" class="secondary" aria-label="Next record">→</button>
</div>
</div>
<div class="comparison-controls">
<div class="stack" style="flex: 1; min-width: 240px;">
<label for="compare-select">Compare with</label>
<select id="compare-select" aria-label="Comparison record">
<option value="">No comparison</option>
</select>
</div>
<div class="status-text" id="comparison-hint" aria-live="polite"></div>
</div>
<div id="record-container" class="stack"></div>
</section>
<section class="surface stack workout-links" aria-label="Workout tools navigation">
<h2 style="margin: 0;">Workout tools</h2>
<p class="status-text">Quick links to the rest of the workout suite.</p>
<div class="link-grid">
<a class="link-card" href="workout-settings.html">Workout settings<span>→</span></a>
<a class="link-card" href="workout-template-manager.html">Workout template manager<span>→</span></a>
<a class="link-card" href="workout-session-logger.html">Workout session logger<span>→</span></a>
<a class="link-card" href="workout-session-viewer.html">Workout session viewer<span>→</span></a>
<a class="link-card" href="workout-exercise-manager.html">Workout exercise manager<span>→</span></a>
<a class="link-card" href="workout-exercise-record.html">Workout exercise records<span>→</span></a>
</div>
</section>
</main>
<script>
(() => {
const STORAGE_KEYS = {
token: 'workout-api-token',
base: 'workout-api-base',
};
const state = {
templates: [],
sessions: [],
currentIndex: 0,
comparisonId: '',
};
const elements = {
refreshTemplates: document.getElementById('refresh-templates'),
templateSelect: document.getElementById('template-select'),
templateStatus: document.getElementById('template-status'),
historyPanel: document.getElementById('history-panel'),
recordStatus: document.getElementById('record-status'),
recordContainer: document.getElementById('record-container'),
prevRecord: document.getElementById('previous-record'),
nextRecord: document.getElementById('next-record'),
compareSelect: document.getElementById('compare-select'),
comparisonHint: document.getElementById('comparison-hint'),
};
function getConfig() {
const base = (localStorage.getItem(STORAGE_KEYS.base) || '').trim();
const token = (localStorage.getItem(STORAGE_KEYS.token) || '').trim();
if (!base || !token) {
throw new Error('Set the API base URL and bearer token in Workout settings first.');
}
return { base, token };
}
async function callApi(path, options = {}) {
const { base, token } = getConfig();
const headers = new Headers(options.headers || {});
headers.set('Content-Type', 'application/json');
headers.set('Authorization', `Bearer ${token}`);
const url = `${base.replace(/\/$/, '')}${path}`;
const res = await fetch(url, {
...options,
headers,
});
let data = null;
try {
data = await res.json();
} catch (error) {
// handled by status check below
}
if (res.status === 401) {
throw new Error('Unauthorized – check the token.');
}
if (!res.ok) {
const message = data?.error?.message || data?.error || data?.message || 'Request failed';
throw new Error(message);
}
return data;
}
function formatDate(value) {
if (!value) return 'Unknown date';
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
return value;
}
return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
}
function formatTimestamp(value) {
if (!value) return 'Unknown time';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return date.toLocaleString(undefined, { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
}
function formatWeight(value) {
return Number.isFinite(value) ? `${value} kg` : '—';
}
function formatReps(value) {
return Number.isFinite(value) ? `${value} reps` : '—';
}
function formatReserve(value) {
if (!Number.isFinite(value)) return '';
return `${value} RIR`;
}
function prettifyName(name) {
if (!name) return 'Exercise';
return name.replace(/[_-]+/g, ' ').replace(/\b\w/g, (m) => m.toUpperCase());
}
function diffDisplay(current, previous, unit) {
const wrapper = document.createElement('span');
wrapper.className = 'delta neutral';
wrapper.textContent = '—';
if (!Number.isFinite(current) || !Number.isFinite(previous)) {
return wrapper;
}
const diff = current - previous;
const indicator = diff > 0 ? '▲' : diff < 0 ? '▼' : '—';
const absValue = Math.abs(diff);
wrapper.textContent = `${indicator} ${absValue}${unit}`;
if (diff > 0) {
wrapper.className = 'delta positive';
} else if (diff < 0) {
wrapper.className = 'delta negative';
}
if (diff === 0) {
wrapper.textContent = '—';
}
return wrapper;
}
function buildComparisonMap(session) {
const map = new Map();
if (!session) return map;
(session.exercise_blocks || []).forEach((block, index) => {
const key = `${block.exercise_name || 'exercise'}:${index}`;
map.set(key, block.sets || []);
});
return map;
}
function renderExercise(block, index, comparisonMap) {
const card = document.createElement('article');
card.className = 'exercise-card';
const header = document.createElement('div');
header.className = 'exercise-header';
const name = document.createElement('p');
name.className = 'exercise-name';
name.textContent = prettifyName(block.exercise_name) || 'Exercise';
header.appendChild(name);
if (Number.isFinite(block.rpe_reserve)) {
const reserve = document.createElement('span');
reserve.className = 'chip';
reserve.textContent = `Reserve: ${formatReserve(block.rpe_reserve)}`;
header.appendChild(reserve);
}
card.appendChild(header);
const setGrid = document.createElement('div');
setGrid.className = 'set-grid';
const comparisonSets = comparisonMap.get(`${block.exercise_name || 'exercise'}:${index}`) || [];
(block.sets || []).forEach((set, setIndex) => {
const row = document.createElement('div');
row.className = 'set-row';
const label = document.createElement('div');
label.className = 'set-label';
label.textContent = `Set ${setIndex + 1}`;
row.appendChild(label);
const weightCell = document.createElement('div');
weightCell.innerHTML = `<strong>Weight:</strong> ${formatWeight(set.weight)}`;
const weightComparison = diffDisplay(set.weight, comparisonSets[setIndex]?.weight, 'kg');
weightComparison.setAttribute('aria-label', 'Weight difference');
weightCell.appendChild(document.createTextNode(' '));
weightCell.appendChild(weightComparison);
row.appendChild(weightCell);
const repsCell = document.createElement('div');
repsCell.innerHTML = `<strong>Reps:</strong> ${formatReps(set.reps)}`;
const repsComparison = diffDisplay(set.reps, comparisonSets[setIndex]?.reps, '');
repsComparison.setAttribute('aria-label', 'Reps difference');
repsCell.appendChild(document.createTextNode(' '));
repsCell.appendChild(repsComparison);
row.appendChild(repsCell);
if (set.notes) {
const notes = document.createElement('div');
notes.className = 'notes';
notes.textContent = set.notes;
row.appendChild(notes);
}
setGrid.appendChild(row);
});
if (block.notes) {
const notes = document.createElement('p');
notes.className = 'notes';
notes.textContent = block.notes;
card.appendChild(notes);
}
card.appendChild(setGrid);
return card;
}
function renderSession(session) {
elements.recordContainer.innerHTML = '';
if (!session) {
elements.recordContainer.innerHTML = '<div class="empty-state">No sessions to show yet.</div>';
return;
}
const comparisonSession = state.sessions.find((entry) => entry.id === state.comparisonId) || null;
const comparisonMap = buildComparisonMap(comparisonSession);
const meta = document.createElement('div');
meta.className = 'record-meta';
const sessionDate = document.createElement('span');
sessionDate.className = 'chip';
sessionDate.textContent = `Date: ${formatDate(session.date)}`;
meta.appendChild(sessionDate);
const createdChip = document.createElement('span');
createdChip.className = 'chip';
createdChip.textContent = `Logged: ${formatTimestamp(session.created_at)}`;
meta.appendChild(createdChip);
const countChip = document.createElement('span');
countChip.className = 'chip';
countChip.textContent = `Record ${state.currentIndex + 1} of ${state.sessions.length}`;
meta.appendChild(countChip);
elements.recordContainer.appendChild(meta);
if (session.notes) {
const notes = document.createElement('p');
notes.className = 'notes';
notes.textContent = `Session notes: ${session.notes}`;
elements.recordContainer.appendChild(notes);
}
(session.exercise_blocks || []).forEach((block, index) => {
elements.recordContainer.appendChild(renderExercise(block, index, comparisonMap));
});
if (state.comparisonId) {
elements.comparisonHint.textContent = 'Showing differences against the selected record.';
} else {
elements.comparisonHint.textContent = 'Select another session to compare weights and reps.';
}
}
function updateComparisonOptions() {
elements.compareSelect.innerHTML = '<option value="">No comparison</option>';
state.sessions.forEach((session, idx) => {
if (idx === state.currentIndex) return;
const option = document.createElement('option');
option.value = session.id;
option.textContent = `${formatDate(session.date)} • ${formatTimestamp(session.created_at)}`;
elements.compareSelect.appendChild(option);
});
elements.compareSelect.value = state.comparisonId;
}
function handleNavigation(direction) {
const nextIndex = state.currentIndex + direction;
if (nextIndex < 0 || nextIndex >= state.sessions.length) return;
state.currentIndex = nextIndex;
state.comparisonId = '';
updateComparisonOptions();
renderSession(state.sessions[state.currentIndex]);
elements.prevRecord.disabled = state.currentIndex === state.sessions.length - 1;
elements.nextRecord.disabled = state.currentIndex === 0;
elements.recordStatus.textContent = `Showing ${formatDate(state.sessions[state.currentIndex].date)}`;
}
async function refreshTemplates() {
elements.templateStatus.textContent = 'Loading templates…';
elements.templateSelect.disabled = true;
elements.templateSelect.innerHTML = '<option value="" disabled selected>Pick a template…</option>';
try {
const data = await callApi('/templates');
state.templates = Array.isArray(data?.templates) ? data.templates : [];
if (state.templates.length === 0) {
elements.templateStatus.textContent = 'No templates found.';
return;
}
state.templates
.sort((a, b) => a.localeCompare(b))
.forEach((template) => {
const option = document.createElement('option');
option.value = template;
option.textContent = template;
elements.templateSelect.appendChild(option);
});
elements.templateStatus.textContent = 'Templates loaded.';
} catch (error) {
elements.templateStatus.textContent = error.message;
} finally {
elements.templateSelect.disabled = false;
}
}
async function loadSessionsForTemplate(id) {
elements.recordContainer.innerHTML = '';
elements.recordStatus.textContent = 'Loading records…';
elements.historyPanel.hidden = false;
elements.prevRecord.disabled = true;
elements.nextRecord.disabled = true;
elements.compareSelect.innerHTML = '<option value="">No comparison</option>';
state.sessions = [];
state.currentIndex = 0;
state.comparisonId = '';
try {
const data = await callApi(`/templates/${encodeURIComponent(id)}/sessions?limit=500&offset=0`);
state.sessions = Array.isArray(data?.sessions) ? data.sessions : [];
if (state.sessions.length === 0) {
elements.recordStatus.textContent = 'No records found for this template yet.';
elements.recordContainer.innerHTML = '<div class="empty-state">Log a workout to see it here.</div>';
return;
}
state.currentIndex = 0;
elements.prevRecord.disabled = state.sessions.length <= 1;
elements.nextRecord.disabled = true;
updateComparisonOptions();
renderSession(state.sessions[state.currentIndex]);
elements.recordStatus.textContent = `Showing ${formatDate(state.sessions[state.currentIndex].date)}`;
} catch (error) {
elements.recordStatus.textContent = error.message;
elements.recordContainer.innerHTML = '<div class="empty-state">Could not load records.</div>';
}
}
function init() {
elements.refreshTemplates.addEventListener('click', refreshTemplates);
elements.templateSelect.addEventListener('change', (event) => {
const id = event.target.value;
if (!id) return;
elements.templateStatus.textContent = `Viewing logs for ${id}.`;
loadSessionsForTemplate(id);
});
elements.prevRecord.addEventListener('click', () => handleNavigation(1));
elements.nextRecord.addEventListener('click', () => handleNavigation(-1));
elements.compareSelect.addEventListener('change', (event) => {
state.comparisonId = event.target.value;
renderSession(state.sessions[state.currentIndex]);
if (state.comparisonId) {
elements.comparisonHint.textContent = 'Showing differences against the selected record.';
} else {
elements.comparisonHint.textContent = 'Select another session to compare weights and reps.';
}
});
refreshTemplates();
}
document.addEventListener('DOMContentLoaded', init);
})();
</script>
<footer class="page-footer">
<p>Built with ❤️, 🤖, and 🐍, by <a href="https://mathspp.com/">Rodrigo Girão Serrão</a></p>
</footer>
</body>
</html>