From 64697fdb7f490a2b576611756b76c4168799fd50 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 29 Nov 2026 10:04:14 +0100 Subject: [PATCH] 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. --- client/src/store/vacayStore.ts | 86 ++++++++++++--------- client/tests/unit/stores/vacayStore.test.ts | 11 +++ 2 files changed, 61 insertions(+), 36 deletions(-) diff --git a/client/src/store/vacayStore.ts b/client/src/store/vacayStore.ts index c8fd15682..561dd893c 100644 --- a/client/src/store/vacayStore.ts +++ b/client/src/store/vacayStore.ts @@ -429,34 +429,44 @@ export const useVacayStore = create((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((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 }) } diff --git a/client/tests/unit/stores/vacayStore.test.ts b/client/tests/unit/stores/vacayStore.test.ts index dd1a1b492..857744543 100644 --- a/client/tests/unit/stores/vacayStore.test.ts +++ b/client/tests/unit/stores/vacayStore.test.ts @@ -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()', () => {