mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 16:34:54 +00:00
Calendar, syncing rework
This commit is contained in:
@ -7,36 +7,95 @@ import {colorMap} from "@/constants/colorMap";
|
||||
import {uuidv4} from "@firebase/util";
|
||||
import {useEffect} from "react";
|
||||
|
||||
const createEventHash = (event: any): string => {
|
||||
const str = `${event.startDate?.seconds || ''}-${event.endDate?.seconds || ''}-${
|
||||
event.title || ''
|
||||
}-${event.location || ''}-${event.allDay ? 'true' : 'false'}`;
|
||||
|
||||
let hash = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = str.charCodeAt(i);
|
||||
hash = ((hash << 5) - hash) + char;
|
||||
hash = hash & hash;
|
||||
}
|
||||
return hash.toString(36);
|
||||
};
|
||||
|
||||
|
||||
export const useGetEvents = () => {
|
||||
const {user, profileData} = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
useEffect(() => {
|
||||
if (!profileData?.familyId) return;
|
||||
if (!profileData?.familyId) {
|
||||
console.log('[SYNC] No family ID available, skipping listener setup');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[SYNC] Setting up sync listener for family: ${profileData.familyId}`);
|
||||
console.log('[SYNC] Setting up sync listener', {
|
||||
familyId: profileData.familyId,
|
||||
userId: user?.uid,
|
||||
isFamilyView
|
||||
});
|
||||
|
||||
const unsubscribe = firestore()
|
||||
.collection('Households')
|
||||
.where("familyId", "==", profileData.familyId)
|
||||
.onSnapshot((snapshot) => {
|
||||
console.log('[SYNC] Snapshot received', {
|
||||
empty: snapshot.empty,
|
||||
size: snapshot.size,
|
||||
changes: snapshot.docChanges().length
|
||||
});
|
||||
|
||||
snapshot.docChanges().forEach((change) => {
|
||||
console.log('[SYNC] Processing change', {
|
||||
type: change.type,
|
||||
docId: change.doc.id,
|
||||
newData: change.doc.data()
|
||||
});
|
||||
|
||||
if (change.type === 'modified') {
|
||||
const data = change.doc.data();
|
||||
console.log('[SYNC] Modified document data', {
|
||||
hasLastSyncTimestamp: !!data?.lastSyncTimestamp,
|
||||
hasLastUpdateTimestamp: !!data?.lastUpdateTimestamp,
|
||||
allFields: Object.keys(data || {})
|
||||
});
|
||||
|
||||
if (data?.lastSyncTimestamp) {
|
||||
console.log(`[SYNC] Change detected at ${data.lastSyncTimestamp.toDate()}`);
|
||||
console.log(`[SYNC] Household ${change.doc.id} triggered refresh`);
|
||||
console.log('[SYNC] Sync timestamp change detected', {
|
||||
timestamp: data.lastSyncTimestamp.toDate(),
|
||||
householdId: change.doc.id,
|
||||
queryKey: ["events", user?.uid, isFamilyView]
|
||||
});
|
||||
|
||||
console.log('[SYNC] Invalidating queries...');
|
||||
queryClient.invalidateQueries(["events", user?.uid, isFamilyView]);
|
||||
console.log('[SYNC] Queries invalidated');
|
||||
} else {
|
||||
console.log('[SYNC] Modified document without lastSyncTimestamp', {
|
||||
householdId: change.doc.id
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}, (error) => {
|
||||
console.error('[SYNC] Listener error:', error);
|
||||
console.error('[SYNC] Listener error:', {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
stack: error.stack
|
||||
});
|
||||
});
|
||||
|
||||
console.log('[SYNC] Listener setup complete');
|
||||
|
||||
return () => {
|
||||
console.log('[SYNC] Cleaning up sync listener');
|
||||
console.log('[SYNC] Cleaning up sync listener', {
|
||||
familyId: profileData.familyId,
|
||||
userId: user?.uid
|
||||
});
|
||||
unsubscribe();
|
||||
};
|
||||
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
|
||||
@ -53,28 +112,45 @@ export const useGetEvents = () => {
|
||||
let allEvents = [];
|
||||
|
||||
if (isFamilyView) {
|
||||
const publicFamilyEvents = await db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", false)
|
||||
.get();
|
||||
const [publicFamilyEvents, privateCreatorEvents, privateAttendeeEvents, userAttendeeEvents, userCreatorEvents] = await Promise.all([
|
||||
// Public family events
|
||||
db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", false)
|
||||
.get(),
|
||||
|
||||
const privateCreatorEvents = await db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", true)
|
||||
.where("creatorId", "==", userId)
|
||||
.get();
|
||||
// Private events user created
|
||||
db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", true)
|
||||
.where("creatorId", "==", userId)
|
||||
.get(),
|
||||
|
||||
const privateAttendeeEvents = await db.collection("Events")
|
||||
.where("private", "==", true)
|
||||
.where("attendees", "array-contains", userId)
|
||||
.get();
|
||||
// Private events user is attending
|
||||
db.collection("Events")
|
||||
.where("private", "==", true)
|
||||
.where("attendees", "array-contains", userId)
|
||||
.get(),
|
||||
|
||||
console.log(`Found ${publicFamilyEvents.size} public events, ${privateCreatorEvents.size} private creator events, ${privateAttendeeEvents.size} private attendee events`);
|
||||
// All events where user is attendee
|
||||
db.collection("Events")
|
||||
.where("attendees", "array-contains", userId)
|
||||
.get(),
|
||||
|
||||
// ALL events where user is creator (regardless of attendees)
|
||||
db.collection("Events")
|
||||
.where("creatorId", "==", userId)
|
||||
.get()
|
||||
]);
|
||||
|
||||
console.log(`Found ${publicFamilyEvents.size} public events, ${privateCreatorEvents.size} private creator events, ${privateAttendeeEvents.size} private attendee events, ${userAttendeeEvents.size} user attendee events, ${userCreatorEvents.size} user creator events`);
|
||||
|
||||
allEvents = [
|
||||
...publicFamilyEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...privateCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...privateAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||
...privateAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...userAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...userCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||
];
|
||||
} else {
|
||||
const [creatorEvents, attendeeEvents] = await Promise.all([
|
||||
@ -95,17 +171,29 @@ export const useGetEvents = () => {
|
||||
}
|
||||
|
||||
const uniqueEventsMap = new Map();
|
||||
const processedHashes = new Set();
|
||||
|
||||
allEvents.forEach(event => {
|
||||
if (event.id) {
|
||||
uniqueEventsMap.set(event.id, event);
|
||||
const eventHash = createEventHash(event);
|
||||
|
||||
console.log(`Processing ${uniqueEventsMap.size} unique events`);
|
||||
|
||||
const processedEvent = {
|
||||
...event,
|
||||
id: event.id || uuidv4(),
|
||||
creatorId: event.creatorId || userId
|
||||
};
|
||||
|
||||
// Only add the event if we haven't seen this hash before
|
||||
if (!processedHashes.has(eventHash)) {
|
||||
processedHashes.add(eventHash);
|
||||
uniqueEventsMap.set(processedEvent.id, processedEvent);
|
||||
} else {
|
||||
const newId = uuidv4();
|
||||
console.log(`Generated new ID for event without ID: ${newId}`);
|
||||
uniqueEventsMap.set(newId, {...event, id: newId});
|
||||
console.log(`Duplicate event detected and skipped using hash: ${eventHash}`);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Processing ${uniqueEventsMap.size} unique events`);
|
||||
console.log(`Processing ${uniqueEventsMap.size} unique events after deduplication`);
|
||||
|
||||
const processedEvents = await Promise.all(
|
||||
Array.from(uniqueEventsMap.values()).map(async (event) => {
|
||||
@ -132,6 +220,7 @@ export const useGetEvents = () => {
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
console.log(`Events processing completed, returning ${processedEvents.length} events`);
|
||||
return processedEvents;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user