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,14 +20,97 @@ const createEventHash = (event: any): string => {
|
|||||||
return hash.toString(36);
|
return hash.toString(36);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchEvents = async (userId: string, familyId: string | undefined, isFamilyView: boolean) => {
|
||||||
|
const db = firestore();
|
||||||
|
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]) => ({
|
||||||
|
...event,
|
||||||
|
id,
|
||||||
|
start: event.allDay
|
||||||
|
? 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(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 = () => {
|
export const useGetEvents = () => {
|
||||||
const {user, profileData} = useAuthContext();
|
const { user, profileData } = useAuthContext();
|
||||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const lastSyncTimestamp = useRef<number>(0);
|
const lastSyncTimestamp = useRef<number>(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!profileData?.familyId) return;
|
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()
|
const unsubscribe = firestore()
|
||||||
.collection('Households')
|
.collection('Households')
|
||||||
@ -40,7 +123,10 @@ export const useGetEvents = () => {
|
|||||||
const newTimestamp = data.lastSyncTimestamp.seconds;
|
const newTimestamp = data.lastSyncTimestamp.seconds;
|
||||||
if (newTimestamp > lastSyncTimestamp.current) {
|
if (newTimestamp > lastSyncTimestamp.current) {
|
||||||
lastSyncTimestamp.current = newTimestamp;
|
lastSyncTimestamp.current = newTimestamp;
|
||||||
queryClient.invalidateQueries({queryKey: ["events", user?.uid, isFamilyView]});
|
// Invalidate both queries
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["events", user.uid]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,92 +134,14 @@ export const useGetEvents = () => {
|
|||||||
}, console.error);
|
}, console.error);
|
||||||
|
|
||||||
return unsubscribe;
|
return unsubscribe;
|
||||||
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
|
}, [profileData?.familyId, user?.uid, queryClient]);
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["events", user?.uid, isFamilyView],
|
queryKey: ["events", user?.uid, isFamilyView],
|
||||||
queryFn: async () => {
|
queryFn: () => fetchEvents(user?.uid!, profileData?.familyId, isFamilyView),
|
||||||
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,
|
staleTime: 5 * 60 * 1000,
|
||||||
gcTime: Infinity,
|
gcTime: Infinity,
|
||||||
placeholderData: (previousData) => previousData,
|
placeholderData: (previousData) => previousData,
|
||||||
|
enabled: Boolean(user?.uid),
|
||||||
});
|
});
|
||||||
};
|
};
|
Reference in New Issue
Block a user