-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-core.mjs
More file actions
695 lines (598 loc) · 21.5 KB
/
app-core.mjs
File metadata and controls
695 lines (598 loc) · 21.5 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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
export const SCHEMA_VERSION = 2;
export const DEFAULT_BABY_NAME = "איתן אור";
export const RECENT_ENTRY_WINDOW_DAYS = 45;
export const COLLECTION_CONFIG = {
dailyEntries: {
field: "time",
sort: (a, b) => safeDateMs(b.time) - safeDateMs(a.time),
},
tastingEntries: {
field: "createdAt",
sort: (a, b) => safeDateMs(b.createdAt) - safeDateMs(a.createdAt),
},
milestones: {
field: "date",
sort: (a, b) => safeDateMs(a.date) - safeDateMs(b.date),
},
growthEntries: {
field: "date",
sort: (a, b) => safeDateMs(b.date) - safeDateMs(a.date),
},
medicalEntries: {
field: "date",
sort: (a, b) => safeDateMs(b.date) - safeDateMs(a.date),
},
pumpingEntries: {
field: "time",
sort: (a, b) => safeDateMs(b.time) - safeDateMs(a.time),
},
};
const TAB_FAB_CONFIG = Object.freeze({
home: Object.freeze({
visible: true,
label: "הוסף אירוע",
hint: "לחץ + להוספת אירוע מפורט",
action: "entry",
buttonText: "+",
}),
milestones: Object.freeze({
visible: true,
label: "הוסף אבן דרך",
hint: "לחץ + להוספת אבן דרך חדשה",
action: "milestone",
buttonText: "+",
}),
tastings: Object.freeze({
visible: true,
label: "הוסף טעימה",
hint: "לחץ + להוספת טעימה חדשה",
action: "entry",
buttonText: "+",
}),
health: Object.freeze({
visible: true,
label: "פתח תפריט בריאות",
hint: "לחץ + להוספת אירוע בריאות",
action: "health-menu",
buttonText: "+",
}),
});
export function createEmptyState() {
return {
babyName: DEFAULT_BABY_NAME,
dailyEntries: [],
tastingEntries: [],
milestones: [],
growthEntries: [],
medicalEntries: [],
pumpingEntries: [],
updatedAt: null,
schemaVersion: SCHEMA_VERSION,
};
}
export function getFabStateForTab(tabId) {
const config = TAB_FAB_CONFIG[tabId];
if (!config) {
return {
visible: false,
label: "",
hint: "",
action: "none",
buttonText: "+",
};
}
return { ...config };
}
export function safeDateMs(value, fallback = 0) {
const ms = new Date(value).getTime();
return Number.isFinite(ms) ? ms : fallback;
}
export function isValidDateValue(value) {
return Number.isFinite(safeDateMs(value, Number.NaN));
}
export function sanitizeMeta(input) {
return {
babyName: typeof input?.babyName === "string" && input.babyName.trim()
? input.babyName.trim()
: DEFAULT_BABY_NAME,
updatedAt: isValidDateValue(input?.updatedAt) ? new Date(input.updatedAt).toISOString() : null,
schemaVersion: Number.isInteger(input?.schemaVersion) ? input.schemaVersion : 1,
};
}
export function toUtcIsoString(value, fallback = new Date()) {
if (value instanceof Date) {
return new Date(value.getTime()).toISOString();
}
if (typeof value === "string" && value) {
const parsed = new Date(value);
if (Number.isFinite(parsed.getTime())) return parsed.toISOString();
}
return new Date(fallback).toISOString();
}
export function toLocalDateTime(date) {
const offsetMs = date.getTimezoneOffset() * 60_000;
return new Date(date.getTime() - offsetMs).toISOString().slice(0, 16);
}
export function toDateInputValue(date) {
return toLocalDateTime(date).slice(0, 10);
}
export function getDayKey(value) {
const d = new Date(value);
if (!Number.isFinite(d.getTime())) return "";
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
}
function normalizeNumber(value, { min = null, max = null, integer = false } = {}) {
if (value === "" || value == null) return null;
const num = Number(value);
if (!Number.isFinite(num)) return null;
if (min != null && num < min) return null;
if (max != null && num > max) return null;
return integer ? Math.trunc(num) : num;
}
function normalizeString(value, fallback = "") {
return typeof value === "string" ? value.trim() : fallback;
}
function normalizeWho(value) {
return normalizeString(value, "") || "mom";
}
function normalizeDeletedAt(input) {
if (input?.deletedAt && isValidDateValue(input.deletedAt)) {
return new Date(input.deletedAt).toISOString();
}
if (input?.isDeleted) {
return input?.updatedAt && isValidDateValue(input.updatedAt)
? new Date(input.updatedAt).toISOString()
: new Date().toISOString();
}
return null;
}
function normalizeRecordCommon(input, nowIso, deviceId) {
return {
id: typeof input?.id === "string" && input.id.trim() ? input.id.trim() : crypto.randomUUID(),
createdAt: isValidDateValue(input?.createdAt)
? new Date(input.createdAt).toISOString()
: nowIso,
updatedAt: isValidDateValue(input?.updatedAt)
? new Date(input.updatedAt).toISOString()
: nowIso,
deletedAt: normalizeDeletedAt(input),
sourceDeviceId: normalizeString(input?.sourceDeviceId, "") || deviceId,
};
}
export function normalizeDailyEntry(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const timeIso = toUtcIsoString(input?.time || input?.createdAt || now, now);
const rating = normalizeNumber(input?.rating, { min: 1, max: 5, integer: true });
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
type: normalizeString(input?.type, "note") || "note",
time: timeIso,
timeMs: safeDateMs(timeIso),
dayKey: getDayKey(timeIso),
details: normalizeString(input?.details),
who: normalizeWho(input?.who),
mlAmount: normalizeNumber(input?.mlAmount, { min: 0, max: 1000 }),
rating,
};
}
export function normalizeMilestone(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const date = normalizeDateOnly(input?.date, now);
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
type: normalizeString(input?.type, "roll") || "roll",
date,
dateMs: safeDateMs(`${date}T12:00:00Z`),
notes: normalizeString(input?.notes),
who: normalizeWho(input?.who),
};
}
export function normalizeTastingEntry(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const createdAtIso = toUtcIsoString(input?.createdAt || input?.updatedAt || now, now);
const rating = normalizeNumber(input?.rating, { min: 1, max: 5, integer: true });
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
createdAt: createdAtIso,
updatedAt: isValidDateValue(input?.updatedAt) ? new Date(input.updatedAt).toISOString() : nowIso,
dayKey: normalizeDateOnly(input?.dayKey, new Date(createdAtIso)),
details: normalizeString(input?.details),
who: normalizeWho(input?.who),
rating,
};
}
export function normalizeGrowthEntry(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const date = normalizeDateOnly(input?.date, now);
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
date,
dateMs: safeDateMs(`${date}T12:00:00Z`),
weight: normalizeNumber(input?.weight, { min: 0, max: 30 }),
height: normalizeNumber(input?.height, { min: 0, max: 120 }),
head: normalizeNumber(input?.head, { min: 0, max: 60 }),
who: normalizeWho(input?.who),
};
}
export function normalizeMedicalEntry(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const date = normalizeDateOnly(input?.date, now);
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
date,
dateMs: safeDateMs(`${date}T12:00:00Z`),
type: normalizeString(input?.type, "note") || "note",
temp: normalizeNumber(input?.temp, { min: 30, max: 45 }),
details: normalizeString(input?.details),
who: normalizeWho(input?.who),
};
}
export function normalizePumpingEntry(input, options = {}) {
const now = options.now instanceof Date ? options.now : new Date();
const nowIso = now.toISOString();
const timeIso = toUtcIsoString(input?.time || input?.createdAt || now, now);
return {
...normalizeRecordCommon(input, nowIso, options.deviceId || "unknown-device"),
time: timeIso,
timeMs: safeDateMs(timeIso),
dayKey: getDayKey(timeIso),
amount: normalizeNumber(input?.amount, { min: 0, max: 5000 }) ?? 0,
location: normalizeString(input?.location),
who: normalizeWho(input?.who),
};
}
export function normalizeDateOnly(value, fallbackDate = new Date()) {
if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value)) return value;
return toDateInputValue(fallbackDate);
}
export function normalizeCollectionItem(collectionName, input, options = {}) {
if (collectionName === "dailyEntries") return normalizeDailyEntry(input, options);
if (collectionName === "tastingEntries") return normalizeTastingEntry(input, options);
if (collectionName === "milestones") return normalizeMilestone(input, options);
if (collectionName === "growthEntries") return normalizeGrowthEntry(input, options);
if (collectionName === "medicalEntries") return normalizeMedicalEntry(input, options);
if (collectionName === "pumpingEntries") return normalizePumpingEntry(input, options);
return input;
}
export function normalizeCollectionState(collectionName, items, options = {}) {
const config = COLLECTION_CONFIG[collectionName];
if (!config) return [];
return (Array.isArray(items) ? items : [])
.map((item) => normalizeCollectionItem(collectionName, item, options))
.filter(Boolean)
.sort(config.sort);
}
export function prepareMetaPatch(input) {
return {
babyName: typeof input?.babyName === "string" && input.babyName.trim()
? input.babyName.trim()
: DEFAULT_BABY_NAME,
updatedAt: toUtcIsoString(input?.updatedAt || new Date()),
schemaVersion: SCHEMA_VERSION,
};
}
export function getVisibleEntries(items) {
return (Array.isArray(items) ? items : []).filter((item) => !item.deletedAt);
}
export function getRecentEntries(items, now = new Date(), days = RECENT_ENTRY_WINDOW_DAYS) {
const cutoff = now.getTime() - days * 86_400_000;
return getVisibleEntries(items).filter((item) => {
const value = item.time || item.date;
return safeDateMs(value) >= cutoff;
});
}
export function buildTimelineSegments(entries, dateKey, options = {}) {
const dayKey = dateKey || getDayKey(options.now || new Date());
const dayStart = safeDateMs(`${dayKey}T00:00:00`);
if (!Number.isFinite(dayStart)) {
return { sleepBlocks: [], awakeBlocks: [], segments: [], isToday: false, trackEndTime: Date.now() };
}
const dayEndTime = dayStart + 24 * 60 * 60 * 1000 - 1;
const nowMs = options.nowMs ?? Date.now();
const isToday = dayKey === getDayKey(options.now || new Date(nowMs));
const trackEndTime = isToday ? Math.min(nowMs, dayEndTime) : dayEndTime;
const normalizedEntries = getVisibleEntries(entries)
.filter((entry) => entry.type === "sleep" || entry.type === "wake")
.slice()
.sort((a, b) => safeDateMs(a.time) - safeDateMs(b.time));
const sleepBlocks = [];
let activeSleepStart = null;
for (const entry of normalizedEntries) {
const entryMs = safeDateMs(entry.time, Number.NaN);
if (!Number.isFinite(entryMs)) continue;
if (entry.type === "sleep") {
activeSleepStart = entryMs;
continue;
}
if (entry.type === "wake" && activeSleepStart != null && entryMs > activeSleepStart) {
sleepBlocks.push({
start: Math.max(activeSleepStart, dayStart),
end: Math.min(entryMs, trackEndTime),
ongoing: false,
});
activeSleepStart = null;
}
}
if (activeSleepStart != null && activeSleepStart <= trackEndTime) {
sleepBlocks.push({
start: Math.max(activeSleepStart, dayStart),
end: trackEndTime,
ongoing: isToday,
});
}
const filteredSleepBlocks = sleepBlocks
.filter((block) => Number.isFinite(block.start) && Number.isFinite(block.end) && block.end > block.start)
.sort((a, b) => a.start - b.start);
const awakeBlocks = [];
let cursor = dayStart;
for (const block of filteredSleepBlocks) {
if (block.start > cursor) {
awakeBlocks.push({ start: cursor, end: block.start, ongoing: false });
}
cursor = Math.max(cursor, block.end);
}
if (cursor < trackEndTime) {
awakeBlocks.push({ start: cursor, end: trackEndTime, ongoing: isToday });
}
const segments = [
...awakeBlocks.map((block) => ({ ...block, kind: "awake" })),
...filteredSleepBlocks.map((block) => ({ ...block, kind: "sleep" })),
].sort((a, b) => a.start - b.start);
return { sleepBlocks: filteredSleepBlocks, awakeBlocks, segments, isToday, trackEndTime };
}
export function calcAge(birthStr, toStr) {
const birth = new Date(`${birthStr}T00:00:00`);
const to = new Date(`${toStr}T00:00:00`);
if (!Number.isFinite(birth.getTime()) || !Number.isFinite(to.getTime()) || to < birth) return "";
let years = to.getFullYear() - birth.getFullYear();
let months = to.getMonth() - birth.getMonth();
let days = to.getDate() - birth.getDate();
if (days < 0) {
months--;
days += new Date(to.getFullYear(), to.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
const totalDays = Math.floor((to - birth) / 86_400_000);
const weeks = Math.floor(days / 7);
const heYears = (n) => n === 1 ? "שנה" : n === 2 ? "שנתיים" : `${n} שנים`;
const heMonths = (n) => n === 1 ? "חודש" : n === 2 ? "חודשיים" : `${n} חודשים`;
const heWeeks = (n) => n === 1 ? "שבוע" : n === 2 ? "שבועיים" : `${n} שבועות`;
const heDays = (n) => n === 1 ? "יום" : n === 2 ? "יומיים" : `${n} ימים`;
const and = (a, b) => b ? `${a} ו${/^\d/.test(b) ? "-" : ""}${b}` : a;
if (totalDays < 14) return heDays(Math.max(1, totalDays));
if (totalDays < 30) {
const w = Math.floor(totalDays / 7);
const d = totalDays % 7;
return d > 0 ? and(heWeeks(w), heDays(d)) : heWeeks(w);
}
if (years === 0) {
return weeks > 0 ? and(heMonths(months), heWeeks(weeks)) : heMonths(months);
}
return months > 0 ? and(heYears(years), heMonths(months)) : heYears(years);
}
export function formatDateHeader(dayKey, now = new Date()) {
const todayKey = getDayKey(now);
const yesterdayKey = getDayKey(new Date(now.getTime() - 86_400_000));
if (dayKey === todayKey) return "היום";
if (dayKey === yesterdayKey) return "אתמול";
const d = new Date(`${dayKey}T12:00:00`);
if (!Number.isFinite(d.getTime())) return dayKey;
return new Intl.DateTimeFormat("he-IL", {
weekday: "long",
day: "numeric",
month: "long",
}).format(d);
}
export function formatDuration(ms) {
const totalMinutes = Math.max(1, Math.round(ms / 60_000));
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
if (hours > 0 && minutes > 0) return `${hours} שע׳ ו-${minutes} דק׳`;
if (hours > 0) return `${hours} שע׳`;
return `${minutes} דק׳`;
}
export function getMealClockState(entry, now = Date.now(), targetMs = 3 * 60 * 60 * 1000) {
if (!entry?.time) {
return {
hasMeal: false,
elapsedMs: 0,
remainingMs: targetMs,
progressRatio: 0,
status: "empty",
targetMs,
};
}
const elapsedMs = Math.max(0, now - safeDateMs(entry.time, now));
const remainingMs = Math.max(0, targetMs - elapsedMs);
const progressRatio = Math.min(1, elapsedMs / targetMs);
let status = "fresh";
if (elapsedMs >= targetMs) {
status = "due";
} else if (remainingMs <= 30 * 60 * 1000) {
status = "approaching";
}
return {
hasMeal: true,
elapsedMs,
remainingMs,
progressRatio,
status,
targetMs,
};
}
export function getAwakeWindowState(entries, now = Date.now()) {
const visibleEntries = getVisibleEntries(entries);
const lastWake = visibleEntries.find((entry) => entry.type === "wake");
const lastSleep = visibleEntries.find((entry) => entry.type === "sleep");
if (!lastWake) {
return {
hasAwakeWindow: false,
durationMs: 0,
status: "unknown",
context: "",
};
}
const wakeMs = safeDateMs(lastWake.time, Number.NaN);
if (!Number.isFinite(wakeMs)) {
return {
hasAwakeWindow: false,
durationMs: 0,
status: "unknown",
context: "",
};
}
const sleepMs = lastSleep ? safeDateMs(lastSleep.time, Number.NaN) : Number.NaN;
if (!Number.isFinite(sleepMs) || wakeMs > sleepMs) {
return {
hasAwakeWindow: true,
durationMs: Math.max(0, now - wakeMs),
status: "awake",
context: "מאז שהתעורר",
};
}
return {
hasAwakeWindow: true,
durationMs: Math.max(0, sleepMs - wakeMs),
status: "asleep",
context: "לפני שנרדם",
};
}
export function getRestClockState(entries, now = Date.now()) {
const visibleEntries = getVisibleEntries(entries);
const lastWake = visibleEntries.find((entry) => entry.type === "wake");
const lastSleep = visibleEntries.find((entry) => entry.type === "sleep");
if (!lastWake && !lastSleep) {
return {
hasState: false,
status: "unknown",
title: "☀️ זמן ערות",
durationMs: 0,
context: "",
};
}
const wakeMs = lastWake ? safeDateMs(lastWake.time, Number.NaN) : Number.NaN;
const sleepMs = lastSleep ? safeDateMs(lastSleep.time, Number.NaN) : Number.NaN;
if (!Number.isFinite(sleepMs) || (Number.isFinite(wakeMs) && wakeMs > sleepMs)) {
return {
hasState: Number.isFinite(wakeMs),
status: "awake",
title: "☀️ זמן ערות",
durationMs: Number.isFinite(wakeMs) ? Math.max(0, now - wakeMs) : 0,
context: "מאז שהתעורר",
};
}
return {
hasState: Number.isFinite(sleepMs),
status: "asleep",
title: "🌙 זמן שינה",
durationMs: Number.isFinite(sleepMs) ? Math.max(0, now - sleepMs) : 0,
context: "מאז שנרדם",
};
}
export function calcSleepDuration(entries) {
const visibleEntries = getVisibleEntries(entries);
const lastSleep = visibleEntries.find((entry) => entry.type === "sleep");
if (!lastSleep) return "לא ידוע";
const sleepTime = safeDateMs(lastSleep.time, Number.NaN);
if (!Number.isFinite(sleepTime)) return "לא ידוע";
const firstWakeAfterSleep = visibleEntries
.slice()
.reverse()
.find((entry) => entry.type === "wake" && safeDateMs(entry.time) > sleepTime);
if (!firstWakeAfterSleep) return "עדיין ישן";
return formatDuration(safeDateMs(firstWakeAfterSleep.time) - sleepTime);
}
export function getFeedReminderState(entries, now = Date.now()) {
const lastMealEntry = getVisibleEntries(entries).find((entry) => entry.type === "meal");
if (!lastMealEntry) return { visible: false, elapsedMs: 0, entry: null };
if (lastMealEntry.who === "mom") return { visible: false, elapsedMs: 0, entry: lastMealEntry };
const elapsedMs = now - safeDateMs(lastMealEntry.time, now);
const thresholdMs = (2 * 60 + 45) * 60 * 1000;
return {
visible: elapsedMs >= thresholdMs,
elapsedMs,
entry: lastMealEntry,
thresholdMs,
};
}
export function summarizeHistoryDay(entries) {
const visibleEntries = getVisibleEntries(entries);
const mealCount = visibleEntries.filter((entry) => entry.type === "meal").length;
const sleepEventsCount = visibleEntries.filter((entry) => entry.type === "sleep" || entry.type === "wake").length;
const diaperCount = visibleEntries.filter((entry) => entry.type === "poop" || entry.type === "pee").length;
const healthCount = visibleEntries.filter((entry) => entry.type === "medication").length;
return {
mealCount,
sleepEventsCount,
diaperCount,
healthCount,
};
}
export function coalesceHistoryEntries(entries) {
const visibleEntries = getVisibleEntries(entries).slice();
const result = [];
const usedIds = new Set();
for (const entry of visibleEntries) {
if (usedIds.has(entry.id)) continue;
if (entry.type === "poop") {
const peer = visibleEntries.find((candidate) =>
!usedIds.has(candidate.id)
&& candidate.id !== entry.id
&& candidate.type === "pee"
&& candidate.who === entry.who
&& candidate.time === entry.time
);
if (peer) {
usedIds.add(entry.id);
usedIds.add(peer.id);
result.push({
...entry,
id: `${entry.id}__${peer.id}`,
type: "poop-pee",
groupedEntryIds: [entry.id, peer.id],
groupedEntries: [entry, peer],
});
continue;
}
}
if (entry.type === "pee") {
const peer = visibleEntries.find((candidate) =>
!usedIds.has(candidate.id)
&& candidate.id !== entry.id
&& candidate.type === "poop"
&& candidate.who === entry.who
&& candidate.time === entry.time
);
if (peer) continue;
}
usedIds.add(entry.id);
result.push(entry);
}
return result.sort((a, b) => safeDateMs(b.time) - safeDateMs(a.time));
}
export function getHistoryEmptyState(filterKey, dateLabel) {
const labels = {
all: `אין אירועים להצגה עבור ${dateLabel}.`,
sleep: `אין אירועי שינה עבור ${dateLabel}.`,
food: `אין אירועי אוכל עבור ${dateLabel}.`,
diaper: `אין אירועי חיתולים עבור ${dateLabel}.`,
health: `אין אירועי בריאות עבור ${dateLabel}.`,
};
return labels[filterKey] || labels.all;
}
export function createDiagnosticsEntry(type, message, details = {}) {
return {
type,
message,
details,
at: new Date().toISOString(),
};
}