mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 17:47:08 +00:00
Fix stuff
This commit is contained in:
@ -20,14 +20,97 @@ const createEventHash = (event: any): string => {
|
||||
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 = () => {
|
||||
const {user, profileData} = useAuthContext();
|
||||
const { user, profileData } = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
const queryClient = useQueryClient();
|
||||
const lastSyncTimestamp = useRef<number>(0);
|
||||
|
||||
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()
|
||||
.collection('Households')
|
||||
@ -40,7 +123,10 @@ export const useGetEvents = () => {
|
||||
const newTimestamp = data.lastSyncTimestamp.seconds;
|
||||
if (newTimestamp > lastSyncTimestamp.current) {
|
||||
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);
|
||||
|
||||
return unsubscribe;
|
||||
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
|
||||
}, [profileData?.familyId, user?.uid, 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
|
||||
};
|
||||
});
|
||||
},
|
||||
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