perf(vacay): read the calendar in parallel and keep it on screen

loadAll ran eight requests one after another, and the holidays fetched each calendar and year in turn, behind a full spinner on every visit. Plan and year settings now load together, then the years, then everything for the year at once; the holiday calendars are asked in parallel and laid out in the calendars' order. The spinner shows only for the first load; coming back keeps the calendar and refreshes it.
This commit is contained in:
Maurice
2026-11-29 10:04:14 +01:00
parent 7e3ce20e6b
commit 64697fdb7f
2 changed files with 61 additions and 36 deletions
+50 -36
View File
@@ -429,34 +429,44 @@ export const useVacayStore = create<VacayState>((set, get) => ({
// per calendar year, so each one the window touches is fetched and the result
// clipped back to what the grid actually renders.
const calendarYears = windowCalendarYears(y, settings)
for (const cal of enabledCalendars) {
const { country, subdivision, group } = parseCalendarRegion(cal.region)
for (const cy of calendarYears) {
try {
if ((cal.type ?? 'public_holiday') === 'school_holiday') {
if (!isSchoolHolidayCountrySupported(country) && !manualSchoolRegionId(cal.region)) continue
const data = await api.getSchoolHolidays(cy, country, subdivision, group)
data.forEach((h: VacaySchoolHolidayRaw) => {
if (!h.startDate) return
const name = schoolHolidayName(h)
datesBetween(h.startDate, h.endDate || h.startDate).filter(date => inGridWindow(date, y, settings)).forEach(date => {
pushHolidayMarker(map, date, { name, localName: name, color: cal.color, label: cal.label, type: 'school_holiday' })
})
})
} else {
const data = await api.getHolidays(cy, country)
const hasRegions = data.some((h: VacayHolidayRaw) => h.counties && h.counties.length > 0)
if (hasRegions && !subdivision) continue
data.forEach((h: VacayHolidayRaw) => {
if (!inGridWindow(h.date, y, settings)) return
if (h.global || !h.counties || (subdivision && h.counties.includes(subdivision))) {
pushHolidayMarker(map, h.date, { name: h.name, localName: h.localName, color: cal.color, label: cal.label, type: 'public_holiday' })
}
})
}
} catch { /* API error, skip */ }
// Every calendar and year is asked at once; the answers are then laid out in
// the calendars' order, so which marker comes first does not depend on which
// answer happened to arrive first.
const asks = enabledCalendars.flatMap(cal => calendarYears.map(cy => ({ cal, cy, ...parseCalendarRegion(cal.region) })))
const answers = await Promise.all(asks.map(async ({ cal, cy, country, subdivision, group }) => {
try {
if ((cal.type ?? 'public_holiday') === 'school_holiday') {
if (!isSchoolHolidayCountrySupported(country) && !manualSchoolRegionId(cal.region)) return null
return { school: await api.getSchoolHolidays(cy, country, subdivision, group), publicDays: null }
}
return { school: null, publicDays: await api.getHolidays(cy, country) }
} catch { /* API error, skip */
return null
}
}
}))
answers.forEach((answer, i) => {
if (!answer) return
const { cal, subdivision } = asks[i]
if (answer.school) {
answer.school.forEach((h: VacaySchoolHolidayRaw) => {
if (!h.startDate) return
const name = schoolHolidayName(h)
datesBetween(h.startDate, h.endDate || h.startDate).filter(date => inGridWindow(date, y, settings)).forEach(date => {
pushHolidayMarker(map, date, { name, localName: name, color: cal.color, label: cal.label, type: 'school_holiday' })
})
})
return
}
const data = answer.publicDays ?? []
const hasRegions = data.some((h: VacayHolidayRaw) => h.counties && h.counties.length > 0)
if (hasRegions && !subdivision) return
data.forEach((h: VacayHolidayRaw) => {
if (!inGridWindow(h.date, y, settings)) return
if (h.global || !h.counties || (subdivision && h.counties.includes(subdivision))) {
pushHolidayMarker(map, h.date, { name: h.name, localName: h.localName, color: cal.color, label: cal.label, type: 'public_holiday' })
}
})
})
set({ holidays: map })
},
@@ -533,19 +543,23 @@ export const useVacayStore = create<VacayState>((set, get) => ({
},
loadAll: async (preferredYear?: number) => {
set({ loading: true })
// Only the first load shows the spinner: coming back to Vacay keeps the
// calendar already in the store on screen while it is read again.
if (!get().plan) set({ loading: true })
try {
await get().loadPlan()
// The leave-year window decides which period is current, so it is resolved
// before the year list picks a default (#737).
await get().loadYearSettings()
// before the year list picks a default (#737); the plan beside it.
await Promise.all([get().loadPlan(), get().loadYearSettings()])
await get().loadYears(preferredYear)
const year = get().selectedYear
await get().loadEntries(year)
await get().loadStats(year)
await get().loadHolidays(year)
await get().loadShares()
await get().loadSharedCalendars(year)
// Everything for the year reads at once rather than one request after another.
await Promise.all([
get().loadEntries(year),
get().loadStats(year),
get().loadHolidays(year),
get().loadShares(),
get().loadSharedCalendars(year),
])
} finally {
set({ loading: false })
}
@@ -21,6 +21,17 @@ describe('vacayStore', () => {
expect(state.stats.length).toBeGreaterThan(0);
expect(state.loading).toBe(false);
});
it('shows the spinner only for the first load, not when the plan is already there', async () => {
const first = useVacayStore.getState().loadAll();
expect(useVacayStore.getState().loading).toBe(true);
await first;
const again = useVacayStore.getState().loadAll();
expect(useVacayStore.getState().loading).toBe(false);
await again;
expect(useVacayStore.getState().plan?.id).toBe(1);
});
});
describe('FE-VACAY-002: toggleEntry()', () => {