diff --git a/frontend/src/routes/+page.server.ts b/frontend/src/routes/+page.server.ts index 5466205..78a29b1 100644 --- a/frontend/src/routes/+page.server.ts +++ b/frontend/src/routes/+page.server.ts @@ -1,4 +1,5 @@ import { getRoutines, getSkinSnapshots } from '$lib/api'; +import type { SkinConditionSnapshot } from '$lib/types'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async () => { @@ -8,7 +9,7 @@ export const load: PageServerLoad = async () => { ]); return { recentRoutines: routines.slice(0, 10), - latestSnapshot: snapshots.at(-1) ?? null + latestSnapshot: getFreshestSnapshot(snapshots) }; }; @@ -17,3 +18,12 @@ function recentDate(daysAgo: number): string { d.setDate(d.getDate() - daysAgo); return d.toISOString().slice(0, 10); } + +function getFreshestSnapshot(snapshots: SkinConditionSnapshot[]): SkinConditionSnapshot | null { + if (!snapshots.length) return null; + return snapshots.reduce((freshest, current) => { + if (current.snapshot_date > freshest.snapshot_date) return current; + if (current.snapshot_date < freshest.snapshot_date) return freshest; + return current.created_at > freshest.created_at ? current : freshest; + }); +}