Skip to content

Commit ba944aa

Browse files
authored
Bugfix: Fix delayed event display after addEvent/deleteEvent (#3235)
2 parents d052273 + 7fac0ca commit ba944aa

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

apps/db/supabase/migrations/20250707043640_remote_schema.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ alter table "public"."workspace_quiz_attempt_answers" enable row level security;
44

55
alter table "public"."workspace_quiz_attempts" enable row level security;
66

7-
drop extension if exists "pg_trgm";
8-
97
set check_function_bodies = off;
108

119
CREATE OR REPLACE FUNCTION public.get_user_tasks(_board_id uuid)

packages/ui/src/hooks/use-calendar-sync.tsx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ type CalendarCache = {
9292
googleEvents: WorkspaceCalendarEvent[];
9393
dbLastUpdated: number;
9494
googleLastUpdated: number;
95-
isForced: boolean;
9695
};
9796
};
9897

@@ -102,7 +101,6 @@ type CacheUpdate = {
102101
googleEvents?: WorkspaceCalendarEvent[];
103102
dbLastUpdated?: number;
104103
googleLastUpdated?: number;
105-
isForced?: boolean;
106104
};
107105

108106
export const CalendarSyncProvider = ({
@@ -132,6 +130,7 @@ export const CalendarSyncProvider = ({
132130
const [isSyncing, setIsSyncing] = useState(false);
133131
const prevGoogleDataRef = useRef<string>('');
134132
const prevDatesRef = useRef<string>('');
133+
const isForcedRef = useRef<boolean>(false);
135134
const queryClient = useQueryClient();
136135

137136
// Helper to generate cache key from dates
@@ -192,18 +191,15 @@ export const CalendarSyncProvider = ({
192191
googleEvents: [],
193192
dbLastUpdated: 0,
194193
googleLastUpdated: 0,
195-
isForced: false,
196194
};
197195

198196
return {
199197
...prev,
200198
[cacheKey]: {
201-
dbEvents: update.dbEvents || existing.dbEvents,
202-
googleEvents: update.googleEvents || existing.googleEvents,
203-
dbLastUpdated: update.dbLastUpdated || existing.dbLastUpdated,
204-
googleLastUpdated:
205-
update.googleLastUpdated || existing.googleLastUpdated,
206-
isForced: update.isForced || existing.isForced,
199+
dbEvents: update.dbEvents !== undefined ? update.dbEvents : existing.dbEvents,
200+
googleEvents: update.googleEvents !== undefined ? update.googleEvents : existing.googleEvents,
201+
dbLastUpdated: update.dbLastUpdated !== undefined ? update.dbLastUpdated : existing.dbLastUpdated,
202+
googleLastUpdated: update.googleLastUpdated !== undefined ? update.googleLastUpdated : existing.googleLastUpdated,
207203
},
208204
};
209205
});
@@ -224,7 +220,7 @@ export const CalendarSyncProvider = ({
224220
cachedData?.dbEvents &&
225221
cachedData.dbEvents.length > 0 &&
226222
!isCacheStaleEnhanced(cachedData.dbLastUpdated, dates) &&
227-
!cachedData.isForced
223+
!isForcedRef.current
228224
) {
229225
setData(cachedData.dbEvents);
230226
return cachedData.dbEvents;
@@ -251,9 +247,11 @@ export const CalendarSyncProvider = ({
251247
// Update cache with new data and reset isForced flag
252248
updateCache(cacheKey, {
253249
dbEvents: fetchedData,
254-
dbLastUpdated: Date.now(),
255-
isForced: false, // Reset isForced after successful fetch
250+
dbLastUpdated: Date.now()
256251
});
252+
253+
// Reset the ref immediately (synchronous)
254+
isForcedRef.current = false;
257255

258256
setData(fetchedData);
259257
return fetchedData;
@@ -592,11 +590,13 @@ export const CalendarSyncProvider = ({
592590
const isCurrentWeek = includesCurrentWeek(dates);
593591

594592
if (cacheData) {
595-
cacheData.isForced = true;
593+
isForcedRef.current = true;
596594
// For current week, also reset the cache timestamp to force refresh
597595
if (isCurrentWeek) {
598-
cacheData.dbLastUpdated = 0;
599-
cacheData.googleLastUpdated = 0;
596+
updateCache(cacheKey, {
597+
dbLastUpdated: 0,
598+
googleLastUpdated: 0,
599+
});
600600
}
601601
}
602602

@@ -694,7 +694,8 @@ export const CalendarSyncProvider = ({
694694
// If events were deleted, refresh to get updated data
695695
if (deletionPerformed) {
696696
queryClient.invalidateQueries({
697-
queryKey: ['calendarEvents', wsId],
697+
queryKey: ['databaseCalendarEvents', wsId],
698+
exact: false,
698699
});
699700
}
700701
} catch (err) {
@@ -723,17 +724,12 @@ export const CalendarSyncProvider = ({
723724
const cacheKey = getCacheKey(dates);
724725
if (!cacheKey) return null;
725726

726-
727-
// Use updateCache instead of direct mutation
728-
updateCache(cacheKey, {
729-
isForced: true,
730-
dbLastUpdated: 0,
731-
});
727+
isForcedRef.current = true;
732728

733729
queryClient.invalidateQueries({
734730
queryKey: ['databaseCalendarEvents', wsId, getCacheKey(dates)],
735731
});
736-
}, [queryClient, wsId, calendarCache, dates, updateCache]);
732+
}, [queryClient, wsId, dates]);
737733

738734
const eventsWithoutAllDays = useMemo(() => {
739735
// Process events immediately when they change

0 commit comments

Comments
 (0)