From 31a89356fe6aa3fae6cfe25d4556aae927eb355e Mon Sep 17 00:00:00 2001 From: Daniel Volz Date: Tue, 3 Feb 2026 05:57:11 +0100 Subject: [PATCH] fix: prevent crash when takenBy is not an array (#92) - Add Array.isArray() checks before calling .map() on dose.takenBy - Fixes TypeError: dose.takenBy.map is not a function - Affects AppContext missedPastDoseIds calculation - Affects SchedulePage dose ID generation (3 locations) This hotfix prevents the app from crashing when dose.takenBy is null, undefined, or any non-array value. --- frontend/src/context/AppContext.tsx | 3 ++- frontend/src/pages/SchedulePage.tsx | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/frontend/src/context/AppContext.tsx b/frontend/src/context/AppContext.tsx index c346c77..04a7be8 100644 --- a/frontend/src/context/AppContext.tsx +++ b/frontend/src/context/AppContext.tsx @@ -480,7 +480,8 @@ export function AppProvider({ children }: { children: React.ReactNode }) { return []; } - return (dose.takenBy || []).length > 0 ? dose.takenBy.map((p: string) => `${dose.id}-${p}`) : [dose.id]; + const takenByArray = Array.isArray(dose.takenBy) ? dose.takenBy : []; + return takenByArray.length > 0 ? takenByArray.map((p: string) => `${dose.id}-${p}`) : [dose.id]; }); }) ); diff --git a/frontend/src/pages/SchedulePage.tsx b/frontend/src/pages/SchedulePage.tsx index 3aa5a08..12001dc 100644 --- a/frontend/src/pages/SchedulePage.tsx +++ b/frontend/src/pages/SchedulePage.tsx @@ -126,9 +126,10 @@ export function SchedulePage() { {showPastDays && pastDays.map((day) => { const allDoseIds = day.meds.flatMap((item) => - item.doses.flatMap((d) => - (d.takenBy || []).length > 0 ? d.takenBy.map((p) => `${d.id}-${p}`) : [d.id] - ) + item.doses.flatMap((d) => { + const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : []; + return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id]; + }) ); const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id)); const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length; @@ -171,9 +172,10 @@ export function SchedulePage() { const med = meds.find((m) => m.name === item.medName); const medCov = coverageByMed[item.medName]; const isEmpty = medCov ? medCov.medsLeft <= 0 : false; - const itemDoseIds = item.doses.flatMap((d) => - (d.takenBy || []).length > 0 ? d.takenBy.map((p) => `${d.id}-${p}`) : [d.id] - ); + const itemDoseIds = item.doses.flatMap((d) => { + const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : []; + return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id]; + }); const allTaken = itemDoseIds.every((id) => takenDoses.has(id)); return (
@@ -275,9 +277,10 @@ export function SchedulePage() { : medCoverage ? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, settings) : null; - const itemDoseIds = item.doses.flatMap((d) => - (d.takenBy || []).length > 0 ? d.takenBy.map((p) => `${d.id}-${p}`) : [d.id] - ); + const itemDoseIds = item.doses.flatMap((d) => { + const takenByArray = Array.isArray(d.takenBy) ? d.takenBy : []; + return takenByArray.length > 0 ? takenByArray.map((p) => `${d.id}-${p}`) : [d.id]; + }); const allTaken = itemDoseIds.every((id) => takenDoses.has(id)); return (