import {useQuery, useQueryClient} from "@tanstack/react-query"; import firestore from "@react-native-firebase/firestore"; import {useAuthContext} from "@/contexts/AuthContext"; import {useAtomValue} from "jotai"; import {isFamilyViewAtom} from "@/components/pages/calendar/atoms"; import {colorMap} from "@/constants/colorMap"; import {useEffect, useRef} 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(); const lastSyncTimestamp = useRef(0); useEffect(() => { if (!profileData?.familyId) return; const unsubscribe = firestore() .collection('Households') .where("familyId", "==", profileData.familyId) .onSnapshot((snapshot) => { snapshot.docChanges().forEach((change) => { if (change.type === 'modified') { const data = change.doc.data(); if (data?.lastSyncTimestamp) { const newTimestamp = data.lastSyncTimestamp.seconds; if (newTimestamp > lastSyncTimestamp.current) { lastSyncTimestamp.current = newTimestamp; queryClient.invalidateQueries({queryKey: ["events", user?.uid, isFamilyView]}); } } } }); }, console.error); return unsubscribe; }, [profileData?.familyId, user?.uid, isFamilyView, queryClient]); return useQuery({ queryKey: ["events", user?.uid, isFamilyView], queryFn: async () => { const db = firestore(); const userId = user?.uid; const familyId = profileData?.familyId; const eventsQuery = db.collection("Events"); let constraints = []; if (isFamilyView) { constraints = [ eventsQuery .where("familyId", "==", familyId) .where("private", "==", false), eventsQuery .where("creatorId", "==", userId), eventsQuery .where("attendees", "array-contains", userId) ]; } else { constraints = [ eventsQuery.where("creatorId", "==", userId), eventsQuery.where("attendees", "array-contains", userId) ]; } const snapshots = await Promise.all( constraints.map(query => query.get()) ); const uniqueEvents = new Map(); const processedHashes = new Set(); const creatorIds = new Set(); snapshots.forEach(snapshot => { snapshot.docs.forEach(doc => { const event = doc.data(); const hash = createEventHash(event); if (!processedHashes.has(hash)) { processedHashes.add(hash); creatorIds.add(event.creatorId); uniqueEvents.set(doc.id, event); } }); }); const creatorIdsArray = Array.from(creatorIds); const creatorProfiles = new Map(); for (let i = 0; i < creatorIdsArray.length; i += 10) { const chunk = creatorIdsArray.slice(i, i + 10); const profilesSnapshot = await db .collection("Profiles") .where(firestore.FieldPath.documentId(), "in", chunk) .get(); profilesSnapshot.docs.forEach(doc => { creatorProfiles.set(doc.id, doc.data()?.eventColor || colorMap.pink); }); } return Array.from(uniqueEvents.entries()).map(([id, event]) => { const startSeconds = event.startDate.seconds; const endSeconds = event.endDate.seconds; return { ...event, id, start: event.allDay ? new Date(new Date(startSeconds * 1000).setHours(0, 0, 0, 0)) : new Date(startSeconds * 1000), end: event.allDay ? new Date(new Date(endSeconds * 1000).setHours(0, 0, 0, 0)) : new Date(endSeconds * 1000), hideHours: event.allDay, eventColor: creatorProfiles.get(event.creatorId) || colorMap.pink, notes: event.notes }; }); }, staleTime: 5 * 60 * 1000, gcTime: Infinity, placeholderData: (previousData) => previousData, }); };