Preserve unchanged habit renders and memoize calendar geometry
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { HabitService } from "../habits/service";
|
||||
import type { Schedule } from "../habits/contracts";
|
||||
|
||||
export type TodayHabit = ReturnType<HabitService["day"]> & { calendarRevision?: number };
|
||||
export type TodayHabit = ReturnType<HabitService["day"]>;
|
||||
export type TodayResponse = {
|
||||
date: string;
|
||||
timezone: string;
|
||||
@@ -46,3 +46,15 @@ export function formatTrackingDate(date: string) {
|
||||
timeZone: "UTC",
|
||||
});
|
||||
}
|
||||
|
||||
/** Preserve unchanged row references when a background refresh returns new JSON objects. */
|
||||
export function reconcileToday(current: TodayResponse | null, next: TodayResponse): TodayResponse {
|
||||
if (!current || current.date !== next.date || current.timezone !== next.timezone) return next;
|
||||
const previous = new Map(current.habits.map(habit => [habit.habitId, habit]));
|
||||
const habits = next.habits.map(habit => {
|
||||
const old = previous.get(habit.habitId);
|
||||
return old && JSON.stringify(old) === JSON.stringify(habit) ? old : habit;
|
||||
});
|
||||
if (current.due === next.due && current.completed === next.completed && habits.length === current.habits.length && habits.every((habit, index) => habit === current.habits[index])) return current;
|
||||
return { ...next, habits };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user