Calendar, syncing rework

This commit is contained in:
Milan Paunovic
2024-11-27 01:37:58 +01:00
parent f2af60111b
commit 95d5e74703
9 changed files with 629 additions and 174 deletions

View File

@ -1,6 +1,6 @@
import React, {useCallback, useEffect, useMemo, useState} from "react";
import {Calendar} from "react-native-big-calendar";
import {ActivityIndicator, StyleSheet, View, ViewStyle} from "react-native";
import {ActivityIndicator, StyleSheet, TouchableOpacity, View, ViewStyle} from "react-native";
import {useGetEvents} from "@/hooks/firebase/useGetEvents";
import {useAtom, useSetAtom} from "jotai";
import {
@ -15,10 +15,12 @@ import {
import {useAuthContext} from "@/contexts/AuthContext";
import {CalendarEvent} from "@/components/pages/calendar/interfaces";
import {Text} from "react-native-ui-lib";
import {addDays, compareAsc, isWithinInterval, subDays} from "date-fns";
import {addDays, compareAsc, format, isWithinInterval, subDays} from "date-fns";
import {useCalSync} from "@/hooks/useCalSync";
import {useSyncEvents} from "@/hooks/useSyncOnScroll";
import {colorMap} from "@/constants/colorMap";
import {useGetFamilyMembers} from "@/hooks/firebase/useGetFamilyMembers";
import CachedImage from "expo-cached-image";
interface EventCalendarProps {
calendarHeight: number;
@ -31,6 +33,342 @@ const getTotalMinutes = () => {
return Math.abs(date.getUTCHours() * 60 + date.getUTCMinutes() - 200);
};
const processEventsForSideBySide = (events: CalendarEvent[]) => {
if (!events) return [];
// Group events by day and time slot
const timeSlots: { [key: string]: CalendarEvent[] } = {};
events.forEach(event => {
const startDate = new Date(event.start);
const endDate = new Date(event.end);
// If it's an all-day event, mark it and add it directly
if (event.allDay) {
const key = `${startDate.toISOString().split('T')[0]}-allday`;
if (!timeSlots[key]) timeSlots[key] = [];
timeSlots[key].push({
...event,
isAllDayEvent: true,
width: 1,
xPos: 0
});
return;
}
// Handle multi-day events
if (startDate.toDateString() !== endDate.toDateString()) {
// Create array of dates between start and end
const dates = [];
let currentDate = new Date(startDate);
while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
// Create segments for each day
dates.forEach((date, index) => {
const isFirstDay = index === 0;
const isLastDay = index === dates.length - 1;
let segmentStart, segmentEnd;
if (isFirstDay) {
// First day: use original start time to end of day
segmentStart = new Date(startDate);
segmentEnd = new Date(date);
segmentEnd.setHours(23, 59, 59);
} else if (isLastDay) {
// Last day: use start of day to original end time
segmentStart = new Date(date);
segmentStart.setHours(0, 0, 0);
segmentEnd = new Date(endDate);
} else {
// Middle days: full day
segmentStart = new Date(date);
segmentStart.setHours(0, 0, 0);
segmentEnd = new Date(date);
segmentEnd.setHours(23, 59, 59);
}
const key = `${segmentStart.toISOString().split('T')[0]}-${segmentStart.getHours()}`;
if (!timeSlots[key]) timeSlots[key] = [];
timeSlots[key].push({
...event,
start: segmentStart,
end: segmentEnd,
isMultiDaySegment: true,
isFirstDay,
isLastDay,
originalStart: startDate,
originalEnd: endDate,
allDay: true // Mark multi-day events as all-day events
});
});
} else {
// Regular event
const key = `${startDate.toISOString().split('T')[0]}-${startDate.getHours()}`;
if (!timeSlots[key]) timeSlots[key] = [];
timeSlots[key].push(event);
}
});
// Process all time slots
return Object.values(timeSlots).flatMap(slotEvents => {
// Sort events by start time
slotEvents.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime());
// Find overlapping events (only for non-all-day events)
return slotEvents.map((event, index) => {
// If it's an all-day or multi-day event, return as is
if (event.allDay || event.isMultiDaySegment) {
return {
...event,
width: 1,
xPos: 0
};
}
// Handle regular events
const overlappingEvents = slotEvents.filter((otherEvent, otherIndex) => {
if (index === otherIndex || otherEvent.allDay || otherEvent.isMultiDaySegment) return false;
const eventStart = new Date(event.start);
const eventEnd = new Date(event.end);
const otherStart = new Date(otherEvent.start);
const otherEnd = new Date(otherEvent.end);
return (eventStart < otherEnd && eventEnd > otherStart);
});
const total = overlappingEvents.length + 1;
const position = index % total;
return {
...event,
width: 1 / total,
xPos: position / total
};
});
});
};
const renderEvent = (event: CalendarEvent & {
width: number;
xPos: number;
isMultiDaySegment?: boolean;
isFirstDay?: boolean;
isLastDay?: boolean;
originalStart?: Date;
originalEnd?: Date;
isAllDayEvent?: boolean;
allDay?: boolean;
eventColor?: string;
attendees?: string[];
creatorId?: string;
pfp?: string;
firstName?: string;
lastName?: string;
notes?: string;
hideHours?: boolean;
}, props: any) => {
const {data: familyMembers} = useGetFamilyMembers();
const attendees = useMemo(() => {
if (!familyMembers || !event.attendees) return event?.creatorId ? [event?.creatorId] : [];
return familyMembers.filter(member => event?.attendees?.includes(member?.uid!));
}, [familyMembers, event.attendees]);
if (event.allDay && !!event.isMultiDaySegment) {
return (
<TouchableOpacity
{...props}
style={[
props.style,
{
width: '100%',
flexDirection: 'row',
alignItems: 'center'
}
]}
>
<Text style={styles.allDayEventText} numberOfLines={1}>
{event.title}
{event.isMultiDaySegment &&
` (${format(new Date(event.start), 'MMM d')} - ${format(new Date(event.end), 'MMM d')})`
}
</Text>
</TouchableOpacity>
);
}
const originalStyle = Array.isArray(props.style) ? props.style[0] : props.style;
console.log('Rendering event:', {
title: event.title,
start: event.start,
end: event.end,
width: event.width,
xPos: event.xPos,
isMultiDaySegment: event.isMultiDaySegment
});
// Ensure we have Date objects
const startDate = event.start instanceof Date ? event.start : new Date(event.start);
const endDate = event.end instanceof Date ? event.end : new Date(event.end);
const hourHeight = props.hourHeight || 60;
const startHour = startDate.getHours();
const startMinutes = startDate.getMinutes();
const topPosition = (startHour + startMinutes / 60) * hourHeight;
const endHour = endDate.getHours();
const endMinutes = endDate.getMinutes();
const duration = (endHour + endMinutes / 60) - (startHour + startMinutes / 60);
const height = duration * hourHeight;
const formatTime = (date: Date) => {
const hours = date.getHours();
const minutes = date.getMinutes();
const ampm = hours >= 12 ? 'pm' : 'am';
const formattedHours = hours % 12 || 12;
const formattedMinutes = minutes.toString().padStart(2, '0');
return `${formattedHours}:${formattedMinutes}${ampm}`;
};
const timeString = event.isMultiDaySegment
? event.isFirstDay
? `${formatTime(startDate)} → 12:00PM`
: event.isLastDay
? `12:00am → ${formatTime(endDate)}`
: 'All day'
: `${formatTime(startDate)} - ${formatTime(endDate)}`;
return (
<TouchableOpacity
{...props}
style={[
originalStyle,
{
position: 'absolute',
width: `${event.width * 100}%`,
left: `${event.xPos * 100}%`,
top: topPosition,
height: height,
zIndex: event.isMultiDaySegment ? 1 : 2,
shadowRadius: 2,
overflow: "hidden"
}
]}
>
<View
style={{
flex: 1,
backgroundColor: event.eventColor,
borderRadius: 4,
padding: 8,
justifyContent: 'space-between'
}}
>
<View>
<Text
style={{
color: 'white',
fontSize: 12,
fontFamily: "PlusJakartaSans_500Medium",
fontWeight: '600',
marginBottom: 4
}}
numberOfLines={1}
>
{event.title}
</Text>
<Text
style={{
color: 'white',
fontSize: 10,
fontFamily: "PlusJakartaSans_500Medium",
opacity: 0.8
}}
>
{timeString}
</Text>
</View>
{/* Attendees Section */}
{attendees?.length > 0 && (
<View style={{flexDirection: 'row', marginTop: 8, height: 27.32}}>
{attendees?.filter(x=>x?.firstName).slice(0, 5).map((attendee, index) => (
<View
key={attendee?.uid}
style={{
position: 'absolute',
left: index * 19,
width: 20,
height: 20,
borderRadius: 50,
borderWidth: 2,
borderColor: '#f2f2f2',
overflow: 'hidden',
backgroundColor: attendee.eventColor || colorMap.pink,
}}
>
{attendee.pfp ? (
<CachedImage
source={{uri: attendee.pfp}}
style={{width: '100%', height: '100%'}}
cacheKey={attendee.pfp}
/>
) : (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text style={{
color: 'white',
fontSize: 12,
fontFamily: "Manrope_600SemiBold",
}}>
{attendee?.firstName?.at(0)}
{attendee?.lastName?.at(0)}
</Text>
</View>
)}
</View>
))}
{attendees.length > 3 && (
<View style={{
position: 'absolute',
left: 3 * 19,
width: 27.32,
height: 27.32,
borderRadius: 50,
borderWidth: 2,
borderColor: '#f2f2f2',
backgroundColor: colorMap.pink,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text style={{
color: 'white',
fontFamily: "Manrope_600SemiBold",
fontSize: 12,
}}>
+{attendees.length - 3}
</Text>
</View>
)}
</View>
)}
</View>
</TouchableOpacity>
);
};
export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
({calendarHeight}) => {
const {data: events, isLoading} = useGetEvents();
@ -233,6 +571,10 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
return renderDate(date);
};
const processedEvents = useMemo(() => {
return processEventsForSideBySide(filteredEvents);
}, [filteredEvents]);
useEffect(() => {
setOffsetMinutes(getTotalMinutes());
}, [events, mode]);
@ -258,12 +600,13 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
bodyContainerStyle={styles.calHeader}
swipeEnabled
mode={mode}
// enableEnrichedEvents={true}
sortedMonthView
// enrichedEventsByDate={enrichedEvents}
events={filteredEvents}
events={processedEvents}
renderEvent={renderEvent}
eventCellStyle={memoizedEventCellStyle}
allDayEventCellStyle={memoizedEventCellStyle}
// enableEnrichedEvents={true}
// enrichedEventsByDate={enrichedEvents}
onPressEvent={handlePressEvent}
weekStartsOn={memoizedWeekStartsOn}
height={calendarHeight}
@ -361,4 +704,16 @@ const styles = StyleSheet.create({
fontSize: 12,
fontFamily: "Manrope_500Medium",
},
eventCell: {
flex: 1,
borderRadius: 4,
padding: 4,
height: '100%',
justifyContent: 'center',
},
eventTitle: {
color: 'white',
fontSize: 12,
fontFamily: "PlusJakartaSans_500Medium",
},
});

View File

@ -39,6 +39,7 @@ import BinIcon from "@/assets/svgs/BinIcon";
import DeleteEventDialog from "./DeleteEventDialog";
import { useDeleteEvent } from "@/hooks/firebase/useDeleteEvent";
import AddPersonIcon from "@/assets/svgs/AddPersonIcon";
import {addHours, startOfHour, startOfMinute} from "date-fns";
const daysOfWeek = [
{ label: "Monday", value: "monday" },
@ -86,7 +87,6 @@ export const ManuallyAddEventModal = () => {
if(allDayAtom === true) setIsAllDay(true);
}, [allDayAtom])
const [startTime, setStartTime] = useState(() => {
const date = initialDate ?? new Date();
if (
@ -104,27 +104,11 @@ export const ManuallyAddEventModal = () => {
const [endTime, setEndTime] = useState(() => {
if (editEvent?.end) {
const date = new Date(editEvent.end);
date.setSeconds(0, 0);
return date;
return new Date(editEvent.end);
}
const baseDate = editEvent?.end ?? initialDate ?? new Date();
const date = new Date(baseDate);
if (
date.getMinutes() > 0 ||
date.getSeconds() > 0 ||
date.getMilliseconds() > 0
) {
date.setHours(date.getHours() + 1);
}
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
date.setHours(date.getHours() + 1);
return date;
return addHours(startOfHour(baseDate), 1);
});
const [startDate, setStartDate] = useState(initialDate ?? new Date());
const [endDate, setEndDate] = useState(
@ -172,27 +156,11 @@ export const ManuallyAddEventModal = () => {
setEndTime(() => {
if (editEvent?.end) {
const date = new Date(editEvent.end);
date.setSeconds(0, 0);
return date;
return startOfMinute(new Date(editEvent.end));
}
const baseDate = editEvent?.end ?? initialDate ?? new Date();
const date = new Date(baseDate);
if (
date.getMinutes() > 0 ||
date.getSeconds() > 0 ||
date.getMilliseconds() > 0
) {
date.setHours(date.getHours() + 1);
}
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
date.setHours(date.getHours() + 1);
return date;
return addHours(startOfHour(baseDate), 1);
});
setStartDate(initialDate ?? new Date());
@ -282,10 +250,10 @@ export const ManuallyAddEventModal = () => {
Alert.alert('Alert', 'Title field cannot be empty');
return false;
}
if (!selectedAttendees || selectedAttendees?.length === 0) {
Alert.alert('Alert', 'Cannot have an event without any attendees');
return false;
}
// if (!selectedAttendees || selectedAttendees?.length === 0) {
// Alert.alert('Alert', 'Cannot have an event without any attendees');
// return false;
// }
return true;
}

View File

@ -21,6 +21,20 @@ import { DeviceType } from "expo-device";
if (Platform.OS === "ios") KeyboardManager.setEnableAutoToolbar(true);
const firebaseAuthErrors: { [key: string]: string } = {
'auth/invalid-email': 'Please enter a valid email address',
'auth/user-disabled': 'This account has been disabled. Please contact support',
'auth/user-not-found': 'No account found with this email address',
'auth/wrong-password': 'Incorrect password. Please try again',
'auth/email-already-in-use': 'An account with this email already exists',
'auth/operation-not-allowed': 'This login method is not enabled. Please contact support',
'auth/weak-password': 'Password should be at least 6 characters',
'auth/invalid-credential': 'Invalid login credentials. Please try again',
'auth/network-request-failed': 'Network error. Please check your internet connection',
'auth/too-many-requests': 'Too many failed login attempts. Please try again later',
'auth/invalid-login-credentials': 'Invalid email or password. Please try again',
};
const SignInPage = () => {
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
@ -52,17 +66,21 @@ const SignInPage = () => {
const router = useRouter();
const handleSignIn = async () => {
try {
await signIn({ email, password });
if (!isError) {
Toast.show({
type: "success",
text1: "Login successful!",
});
} else {
} catch (error: any) {
const errorCode = error?.code || 'unknown-error';
const errorMessage = firebaseAuthErrors[errorCode] || 'An unexpected error occurred. Please try again';
Toast.show({
type: "error",
text1: "Error logging in",
text2: `${error}`,
text2: errorMessage,
});
}
};
@ -139,9 +157,9 @@ const SignInPage = () => {
/>
{isError && (
<Text center style={{ marginBottom: 20 }}>{`${
error?.toString()?.split("]")?.[1]
}`}</Text>
<Text center style={{ marginBottom: 20 }}>
{firebaseAuthErrors[error?.code] || 'An unexpected error occurred. Please try again'}
</Text>
)}
<View row centerH marginB-5 gap-5>

View File

@ -318,23 +318,23 @@ const MyProfile = () => {
)}
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => handleChangeColor(colorMap.gray)}>
<View style={styles.colorBox} backgroundColor={colorMap.gray}>
{selectedColor == colorMap.gray && (
<TouchableOpacity onPress={() => handleChangeColor(colorMap.indigo)}>
<View style={styles.colorBox} backgroundColor={colorMap.indigo}>
{selectedColor == colorMap.indigo && (
<AntDesign name="check" size={30} color="white" />
)}
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => handleChangeColor(colorMap.yellow)}>
<View style={styles.colorBox} backgroundColor={colorMap.yellow}>
{selectedColor == colorMap.yellow && (
<TouchableOpacity onPress={() => handleChangeColor(colorMap.emerald)}>
<View style={styles.colorBox} backgroundColor={colorMap.emerald}>
{selectedColor == colorMap.emerald && (
<AntDesign name="check" size={30} color="white" />
)}
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => handleChangeColor(colorMap.sky)}>
<View style={styles.colorBox} backgroundColor={colorMap.sky}>
{selectedColor == colorMap.sky && (
<TouchableOpacity onPress={() => handleChangeColor(colorMap.violet)}>
<View style={styles.colorBox} backgroundColor={colorMap.violet}>
{selectedColor == colorMap.violet && (
<AntDesign name="check" size={30} color="white" />
)}
</View>

View File

@ -8,5 +8,8 @@ export const colorMap = {
red: '#ff1637',
gray: '#607d8b',
yellow: '#ffc107',
sky: '#2196f3'
sky: '#2196f3',
indigo: '#4F46E5',
emerald: '#059669',
violet: '#7C3AED',
};

View File

@ -23,10 +23,32 @@ async function getPushTokensForFamily(familyId, excludeUserId = null) {
const snapshot = await usersRef.where('familyId', '==', familyId).get();
let pushTokens = [];
console.log('Getting push tokens:', {
familyId,
excludeUserId
});
snapshot.forEach(doc => {
const data = doc.data();
if ((!excludeUserId || data.uid !== excludeUserId) && data.pushToken) {
const userId = doc.id;
console.log('Processing user:', {
docId: userId,
hasToken: !!data.pushToken,
excluded: userId === excludeUserId
});
if (userId !== excludeUserId && data.pushToken) {
console.log('Including token for user:', {
userId,
excludeUserId
});
pushTokens.push(data.pushToken);
} else {
console.log('Excluding token for user:', {
userId,
excludeUserId
});
}
});
@ -194,6 +216,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
const creatorId = eventData.creatorId;
const title = eventData.title || '';
const externalOrigin = eventData.externalOrigin || false;
const eventId = context.params.eventId;
if (!familyId || !creatorId) {
console.error('Missing familyId or creatorId in event data');
@ -214,9 +237,9 @@ exports.sendNotificationOnEventCreation = functions.firestore
creatorId,
externalOrigin: externalOrigin || false,
events: [{
id: context.params.eventId,
id: eventId,
title: title || '',
timestamp: new Date().toISOString()
timestamp: new Date().toISOString() // Using ISO string instead of serverTimestamp
}],
createdAt: admin.firestore.FieldValue.serverTimestamp(),
processed: false,
@ -226,19 +249,50 @@ exports.sendNotificationOnEventCreation = functions.firestore
const existingEvents = batchDoc.data().events || [];
transaction.update(batchRef, {
events: [...existingEvents, {
id: context.params.eventId,
id: eventId,
title: title || '',
timestamp: new Date().toISOString()
timestamp: new Date().toISOString() // Using ISO string instead of serverTimestamp
}]
});
}
});
console.log(`[HOUSEHOLD_UPDATE] Event created - Processing timestamp updates`, {
eventId,
familyId,
eventTitle: title || 'Untitled'
});
const householdsSnapshot = await admin.firestore().collection('Households')
.where('familyId', '==', familyId)
.get();
console.log(`[HOUSEHOLD_UPDATE] Found ${householdsSnapshot.size} households to update for family ${familyId}`);
const householdBatch = admin.firestore().batch();
householdsSnapshot.docs.forEach((doc) => {
console.log(`[HOUSEHOLD_UPDATE] Adding household ${doc.id} to update batch`);
householdBatch.update(doc.ref, {
lastSyncTimestamp: admin.firestore.FieldValue.serverTimestamp()
});
});
const batchStartTime = Date.now();
await householdBatch.commit();
console.log(`[HOUSEHOLD_UPDATE] Batch update completed in ${Date.now() - batchStartTime}ms`, {
familyId,
householdsUpdated: householdsSnapshot.size,
eventId
});
} catch (error) {
console.error('Error adding to event batch:', error);
console.error('Error in event creation handler:', error);
throw error;
}
});
exports.processEventBatches = functions.pubsub
.schedule('every 1 minutes')
.onRun(async (context) => {
@ -256,13 +310,27 @@ exports.processEventBatches = functions.pubsub
const batchData = doc.data();
const {familyId, creatorId, externalOrigin, events} = batchData;
console.log('Processing batch:', {
batchId: doc.id,
creatorId,
familyId
});
try {
const pushTokens = await getPushTokensForFamily(
familyId,
creatorId
);
// Add logging to see what tokens are returned
console.log('Push tokens retrieved:', {
batchId: doc.id,
tokenCount: pushTokens.length,
tokens: pushTokens
});
if (pushTokens.length) {
let notificationMessage;
if (externalOrigin) {
notificationMessage = `Calendar sync completed: ${events.length} ${events.length === 1 ? 'event has' : 'events have'} been added.`;
@ -1654,53 +1722,6 @@ exports.triggerMicrosoftSync = functions.https.onCall(async (data, context) => {
}
});
exports.updateHouseholdTimestampOnEventCreate = functions.firestore
.document('Events/{eventId}')
.onCreate(async (snapshot, context) => {
const eventData = snapshot.data();
const familyId = eventData.familyId;
const eventId = context.params.eventId;
console.log(`[HOUSEHOLD_UPDATE] Event created - Processing timestamp updates`, {
eventId,
familyId,
eventTitle: eventData.title || 'Untitled'
});
try {
const householdsSnapshot = await db.collection('Households')
.where('familyId', '==', familyId)
.get();
console.log(`[HOUSEHOLD_UPDATE] Found ${householdsSnapshot.size} households to update for family ${familyId}`);
const batch = db.batch();
householdsSnapshot.docs.forEach((doc) => {
console.log(`[HOUSEHOLD_UPDATE] Adding household ${doc.id} to update batch`);
batch.update(doc.ref, {
lastUpdateTimestamp: admin.firestore.FieldValue.serverTimestamp()
});
});
const batchStartTime = Date.now();
await batch.commit();
console.log(`[HOUSEHOLD_UPDATE] Batch update completed in ${Date.now() - batchStartTime}ms`, {
familyId,
householdsUpdated: householdsSnapshot.size,
eventId
});
} catch (error) {
console.error(`[HOUSEHOLD_UPDATE] Error updating households for event creation`, {
eventId,
familyId,
error: error.message,
stack: error.stack
});
throw error;
}
});
exports.updateHouseholdTimestampOnEventUpdate = functions.firestore
.document('Events/{eventId}')
.onUpdate(async (change, context) => {
@ -1725,7 +1746,7 @@ exports.updateHouseholdTimestampOnEventUpdate = functions.firestore
householdsSnapshot.docs.forEach((doc) => {
console.log(`[HOUSEHOLD_UPDATE] Adding household ${doc.id} to update batch`);
batch.update(doc.ref, {
lastUpdateTimestamp: admin.firestore.FieldValue.serverTimestamp()
lastSyncTimestamp: admin.firestore.FieldValue.serverTimestamp()
});
});

View File

@ -24,6 +24,7 @@ export const useCreateEvent = () => {
.doc(docId)
.set({
...eventData,
attendees: (eventData.attendees?.length ?? 0) === 0 ?? [currentUser?.uid],
creatorId: currentUser?.uid,
familyId: profileData?.familyId
}, {merge: true});
@ -37,15 +38,12 @@ export const useCreateEvent = () => {
} catch (e) {
console.error(e);
}
},
onSuccess: () => {
queryClients.invalidateQueries("events")
}
})
}
export const useCreateEventsFromProvider = () => {
const { user: currentUser } = useAuthContext();
const {user: currentUser} = useAuthContext();
const queryClient = useQueryClient();
return useMutation({
@ -66,14 +64,14 @@ export const useCreateEventsFromProvider = () => {
// Event doesn't exist, so add it
return firestore()
.collection("Events")
.add({ ...eventData, creatorId: currentUser?.uid });
.add({...eventData, creatorId: currentUser?.uid});
} else {
// Event exists, update it
const docId = snapshot.docs[0].id;
return firestore()
.collection("Events")
.doc(docId)
.set({ ...eventData, creatorId: currentUser?.uid }, { merge: true });
.set({...eventData, creatorId: currentUser?.uid}, {merge: true});
}
});

View File

@ -7,36 +7,95 @@ import {colorMap} from "@/constants/colorMap";
import {uuidv4} from "@firebase/util";
import {useEffect} 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();
useEffect(() => {
if (!profileData?.familyId) return;
if (!profileData?.familyId) {
console.log('[SYNC] No family ID available, skipping listener setup');
return;
}
console.log(`[SYNC] Setting up sync listener for family: ${profileData.familyId}`);
console.log('[SYNC] Setting up sync listener', {
familyId: profileData.familyId,
userId: user?.uid,
isFamilyView
});
const unsubscribe = firestore()
.collection('Households')
.where("familyId", "==", profileData.familyId)
.onSnapshot((snapshot) => {
console.log('[SYNC] Snapshot received', {
empty: snapshot.empty,
size: snapshot.size,
changes: snapshot.docChanges().length
});
snapshot.docChanges().forEach((change) => {
console.log('[SYNC] Processing change', {
type: change.type,
docId: change.doc.id,
newData: change.doc.data()
});
if (change.type === 'modified') {
const data = change.doc.data();
console.log('[SYNC] Modified document data', {
hasLastSyncTimestamp: !!data?.lastSyncTimestamp,
hasLastUpdateTimestamp: !!data?.lastUpdateTimestamp,
allFields: Object.keys(data || {})
});
if (data?.lastSyncTimestamp) {
console.log(`[SYNC] Change detected at ${data.lastSyncTimestamp.toDate()}`);
console.log(`[SYNC] Household ${change.doc.id} triggered refresh`);
console.log('[SYNC] Sync timestamp change detected', {
timestamp: data.lastSyncTimestamp.toDate(),
householdId: change.doc.id,
queryKey: ["events", user?.uid, isFamilyView]
});
console.log('[SYNC] Invalidating queries...');
queryClient.invalidateQueries(["events", user?.uid, isFamilyView]);
console.log('[SYNC] Queries invalidated');
} else {
console.log('[SYNC] Modified document without lastSyncTimestamp', {
householdId: change.doc.id
});
}
}
});
}, (error) => {
console.error('[SYNC] Listener error:', error);
console.error('[SYNC] Listener error:', {
message: error.message,
code: error.code,
stack: error.stack
});
});
console.log('[SYNC] Listener setup complete');
return () => {
console.log('[SYNC] Cleaning up sync listener');
console.log('[SYNC] Cleaning up sync listener', {
familyId: profileData.familyId,
userId: user?.uid
});
unsubscribe();
};
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
@ -53,28 +112,45 @@ export const useGetEvents = () => {
let allEvents = [];
if (isFamilyView) {
const publicFamilyEvents = await db.collection("Events")
const [publicFamilyEvents, privateCreatorEvents, privateAttendeeEvents, userAttendeeEvents, userCreatorEvents] = await Promise.all([
// Public family events
db.collection("Events")
.where("familyId", "==", familyId)
.where("private", "==", false)
.get();
.get(),
const privateCreatorEvents = await db.collection("Events")
// Private events user created
db.collection("Events")
.where("familyId", "==", familyId)
.where("private", "==", true)
.where("creatorId", "==", userId)
.get();
.get(),
const privateAttendeeEvents = await db.collection("Events")
// Private events user is attending
db.collection("Events")
.where("private", "==", true)
.where("attendees", "array-contains", userId)
.get();
.get(),
console.log(`Found ${publicFamilyEvents.size} public events, ${privateCreatorEvents.size} private creator events, ${privateAttendeeEvents.size} private attendee events`);
// All events where user is attendee
db.collection("Events")
.where("attendees", "array-contains", userId)
.get(),
// ALL events where user is creator (regardless of attendees)
db.collection("Events")
.where("creatorId", "==", userId)
.get()
]);
console.log(`Found ${publicFamilyEvents.size} public events, ${privateCreatorEvents.size} private creator events, ${privateAttendeeEvents.size} private attendee events, ${userAttendeeEvents.size} user attendee events, ${userCreatorEvents.size} user creator events`);
allEvents = [
...publicFamilyEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
...privateCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
...privateAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
...privateAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
...userAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
...userCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
];
} else {
const [creatorEvents, attendeeEvents] = await Promise.all([
@ -95,17 +171,29 @@ export const useGetEvents = () => {
}
const uniqueEventsMap = new Map();
const processedHashes = new Set();
allEvents.forEach(event => {
if (event.id) {
uniqueEventsMap.set(event.id, event);
const eventHash = createEventHash(event);
console.log(`Processing ${uniqueEventsMap.size} unique events`);
const processedEvent = {
...event,
id: event.id || uuidv4(),
creatorId: event.creatorId || userId
};
// Only add the event if we haven't seen this hash before
if (!processedHashes.has(eventHash)) {
processedHashes.add(eventHash);
uniqueEventsMap.set(processedEvent.id, processedEvent);
} else {
const newId = uuidv4();
console.log(`Generated new ID for event without ID: ${newId}`);
uniqueEventsMap.set(newId, {...event, id: newId});
console.log(`Duplicate event detected and skipped using hash: ${eventHash}`);
}
});
console.log(`Processing ${uniqueEventsMap.size} unique events`);
console.log(`Processing ${uniqueEventsMap.size} unique events after deduplication`);
const processedEvents = await Promise.all(
Array.from(uniqueEventsMap.values()).map(async (event) => {
@ -132,6 +220,7 @@ export const useGetEvents = () => {
})
);
console.log(`Events processing completed, returning ${processedEvents.length} events`);
return processedEvents;
},

View File

@ -1,11 +1,14 @@
import {useMutation} from "react-query";
import auth from "@react-native-firebase/auth";
import {useRouter} from "expo-router";
export const useSignOut = () => {
const {replace} = useRouter();
return useMutation({
mutationKey: ["signOut"],
mutationFn: async () => {
await auth().signOut()
replace("/(unauth)")
}
});
}