mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 07:07:16 +00:00
104 lines
4.1 KiB
TypeScript
104 lines
4.1 KiB
TypeScript
import {useQuery} from "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 {uuidv4} from "@firebase/util";
|
|
|
|
export const useGetEvents = () => {
|
|
const {user, profileData} = useAuthContext();
|
|
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
|
|
|
return useQuery({
|
|
queryKey: ["events", user?.uid, isFamilyView],
|
|
queryFn: async () => {
|
|
const db = firestore();
|
|
const userId = user?.uid;
|
|
const familyId = profileData?.familyId;
|
|
|
|
let allEvents = [];
|
|
|
|
if (isFamilyView) {
|
|
// Get public family events
|
|
const publicFamilyEvents = await db.collection("Events")
|
|
.where("familyId", "==", familyId)
|
|
.where("private", "==", false)
|
|
.get();
|
|
|
|
// Get private events where user is creator
|
|
const privateCreatorEvents = await db.collection("Events")
|
|
.where("familyId", "==", familyId)
|
|
.where("private", "==", true)
|
|
.where("creatorId", "==", userId)
|
|
.get();
|
|
|
|
// Get private events where user is attendee
|
|
const privateAttendeeEvents = await db.collection("Events")
|
|
.where("private", "==", true)
|
|
.where("attendees", "array-contains", userId)
|
|
.get();
|
|
|
|
allEvents = [
|
|
...publicFamilyEvents.docs.map(doc => doc.data()),
|
|
...privateCreatorEvents.docs.map(doc => doc.data()),
|
|
...privateAttendeeEvents.docs.map(doc => doc.data())
|
|
];
|
|
} else {
|
|
// Personal view: Only show events where user is creator or attendee
|
|
const [creatorEvents, attendeeEvents] = await Promise.all([
|
|
db.collection("Events")
|
|
.where("creatorId", "==", userId)
|
|
.get(),
|
|
db.collection("Events")
|
|
.where("attendees", "array-contains", userId)
|
|
.get()
|
|
]);
|
|
|
|
allEvents = [
|
|
...creatorEvents.docs.map(doc => doc.data()),
|
|
...attendeeEvents.docs.map(doc => doc.data())
|
|
];
|
|
}
|
|
|
|
// Ensure uniqueness
|
|
const uniqueEventsMap = new Map();
|
|
allEvents.forEach(event => {
|
|
if (event.id) {
|
|
uniqueEventsMap.set(event.id, event);
|
|
} else {
|
|
uniqueEventsMap.set(uuidv4(), event);
|
|
}
|
|
});
|
|
|
|
// Map events with creator colors
|
|
return await Promise.all(
|
|
Array.from(uniqueEventsMap.values()).map(async (event) => {
|
|
const profileSnapshot = await db
|
|
.collection("Profiles")
|
|
.doc(event.creatorId)
|
|
.get();
|
|
|
|
const profileData = profileSnapshot.data();
|
|
const eventColor = profileData?.eventColor || colorMap.pink;
|
|
|
|
return {
|
|
...event,
|
|
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,
|
|
notes: event.notes,
|
|
};
|
|
})
|
|
);
|
|
},
|
|
staleTime: Infinity,
|
|
cacheTime: Infinity,
|
|
keepPreviousData: true,
|
|
});
|
|
}; |