perf: load calendar events instantly, sync CalDAV in background

Split loadEvents into two functions: loadEvents (instant DB read) and
syncAndReload (background CalDAV sync + reload). Events now appear
immediately when switching to the Calendar tab instead of waiting for
the CalDAV sync to complete.
This commit is contained in:
2026-02-09 18:37:14 +01:00
parent b94b5f5ed8
commit 0e406e4dca
2 changed files with 17 additions and 14 deletions

View File

@@ -85,15 +85,9 @@ const Calendar = () => {
const { events, setEvents, deleteEvent } = useEventsStore();
// Sync CalDAV then load events for current view
// Load events from local DB (fast, no network sync)
const loadEvents = useCallback(async () => {
try {
try {
await CaldavConfigService.sync();
} catch {
// No CalDAV config or sync failed — not critical
}
// Calculate first visible day (up to 6 days before month start)
const firstOfMonth = new Date(currentYear, monthIndex, 1);
const dayOfWeek = firstOfMonth.getDay();
@@ -119,16 +113,25 @@ const Calendar = () => {
}
}, [monthIndex, currentYear, setEvents]);
// Load events when tab gains focus or month/year changes
// NOTE: Wrapper needed because loadEvents is async (returns Promise)
// and useFocusEffect expects a sync function (optionally returning cleanup)
// Sync CalDAV in background, then reload events
const syncAndReload = useCallback(async () => {
try {
await CaldavConfigService.sync();
await loadEvents();
} catch {
// Sync failed — not critical
}
}, [loadEvents]);
// Load events instantly on focus, then sync in background periodically
useFocusEffect(
useCallback(() => {
loadEvents();
syncAndReload();
const interval = setInterval(loadEvents, 10_000);
const interval = setInterval(syncAndReload, 10_000);
return () => clearInterval(interval);
}, [loadEvents]),
}, [loadEvents, syncAndReload]),
);
// Re-open overlay after back navigation from editEvent