fix(frontend): pick freshest skin snapshot on dashboard
This commit is contained in:
parent
49c304d06f
commit
921fe3ef61
1 changed files with 11 additions and 1 deletions
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue