mirror of
https://github.com/urosran/cally.git
synced 2025-07-17 02:25:10 +00:00
Fix stuff
This commit is contained in:
@ -20,55 +20,16 @@ const createEventHash = (event: any): string => {
|
||||
return hash.toString(36);
|
||||
};
|
||||
|
||||
export const useGetEvents = () => {
|
||||
const {user, profileData} = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
const queryClient = useQueryClient();
|
||||
const lastSyncTimestamp = useRef<number>(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 fetchEvents = async (userId: string, familyId: string | undefined, isFamilyView: boolean) => {
|
||||
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)
|
||||
eventsQuery.where("familyId", "==", familyId).where("private", "==", false),
|
||||
eventsQuery.where("creatorId", "==", userId),
|
||||
eventsQuery.where("attendees", "array-contains", userId)
|
||||
];
|
||||
} else {
|
||||
constraints = [
|
||||
@ -77,9 +38,7 @@ export const useGetEvents = () => {
|
||||
];
|
||||
}
|
||||
|
||||
const snapshots = await Promise.all(
|
||||
constraints.map(query => query.get())
|
||||
);
|
||||
const snapshots = await Promise.all(constraints.map(query => query.get()));
|
||||
|
||||
const uniqueEvents = new Map();
|
||||
const processedHashes = new Set();
|
||||
@ -113,27 +72,76 @@ export const useGetEvents = () => {
|
||||
});
|
||||
}
|
||||
|
||||
return Array.from(uniqueEvents.entries()).map(([id, event]) => {
|
||||
const startSeconds = event.startDate.seconds;
|
||||
const endSeconds = event.endDate.seconds;
|
||||
|
||||
return {
|
||||
return Array.from(uniqueEvents.entries()).map(([id, event]) => ({
|
||||
...event,
|
||||
id,
|
||||
start: event.allDay
|
||||
? new Date(new Date(startSeconds * 1000).setHours(0, 0, 0, 0))
|
||||
: new Date(startSeconds * 1000),
|
||||
? new Date(new Date(event.startDate.seconds * 1000).setHours(0, 0, 0, 0))
|
||||
: new Date(event.startDate.seconds * 1000),
|
||||
end: event.allDay
|
||||
? new Date(new Date(endSeconds * 1000).setHours(0, 0, 0, 0))
|
||||
: new Date(endSeconds * 1000),
|
||||
? new Date(new Date(event.endDate.seconds * 1000).setHours(0, 0, 0, 0))
|
||||
: new Date(event.endDate.seconds * 1000),
|
||||
hideHours: event.allDay,
|
||||
eventColor: creatorProfiles.get(event.creatorId) || colorMap.pink,
|
||||
notes: event.notes
|
||||
};
|
||||
}));
|
||||
};
|
||||
|
||||
export const useGetEvents = () => {
|
||||
const { user, profileData } = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
const queryClient = useQueryClient();
|
||||
const lastSyncTimestamp = useRef<number>(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?.uid || !profileData?.familyId) return;
|
||||
|
||||
const prefetchEvents = async () => {
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: ["events", user.uid, false], // Personal events
|
||||
queryFn: () => fetchEvents(user.uid, profileData.familyId, false),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
},
|
||||
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: ["events", user.uid, true], // Family events
|
||||
queryFn: () => fetchEvents(user.uid, profileData.familyId, true),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
};
|
||||
|
||||
prefetchEvents();
|
||||
|
||||
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;
|
||||
// Invalidate both queries
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["events", user.uid]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}, console.error);
|
||||
|
||||
return unsubscribe;
|
||||
}, [profileData?.familyId, user?.uid, queryClient]);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["events", user?.uid, isFamilyView],
|
||||
queryFn: () => fetchEvents(user?.uid!, profileData?.familyId, isFamilyView),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
gcTime: Infinity,
|
||||
placeholderData: (previousData) => previousData,
|
||||
enabled: Boolean(user?.uid),
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user