mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
Merge remote-tracking branch 'origin/main' into dev
This commit is contained in:
@ -1,10 +1,29 @@
|
||||
import {ProfileType} from "@/contexts/AuthContext";
|
||||
export type ProfileType = 'parent' | 'child';
|
||||
|
||||
export interface User {
|
||||
uid: string;
|
||||
email: string | null;
|
||||
export interface CalendarAccount {
|
||||
accessToken: string;
|
||||
refreshToken?: string;
|
||||
resourceId?: string;
|
||||
email?: string;
|
||||
expiresAt?: Date;
|
||||
}
|
||||
|
||||
export interface GoogleAccount extends CalendarAccount {
|
||||
scope?: string;
|
||||
}
|
||||
|
||||
export interface MicrosoftAccount extends CalendarAccount {
|
||||
subscriptionId?: string;
|
||||
}
|
||||
|
||||
export interface AppleAccount extends CalendarAccount {
|
||||
identityToken?: string;
|
||||
}
|
||||
|
||||
export type CalendarAccounts = {
|
||||
[email: string]: GoogleAccount | MicrosoftAccount | AppleAccount;
|
||||
};
|
||||
|
||||
export interface UserProfile {
|
||||
userType: ProfileType;
|
||||
firstName: string;
|
||||
@ -21,23 +40,7 @@ export interface UserProfile {
|
||||
eventColor?: string | null;
|
||||
timeZone?: string | null;
|
||||
firstDayOfWeek?: string | null;
|
||||
googleAccounts?: Object;
|
||||
microsoftAccounts?: Object;
|
||||
appleAccounts?: Object;
|
||||
}
|
||||
|
||||
export interface ParentProfile extends UserProfile {
|
||||
userType: ProfileType.PARENT;
|
||||
childrenIds: string[];
|
||||
}
|
||||
|
||||
export interface ChildProfile extends UserProfile {
|
||||
userType: ProfileType.CHILD;
|
||||
birthday: Date;
|
||||
parentId: string;
|
||||
}
|
||||
|
||||
export interface CaregiverProfile extends UserProfile {
|
||||
userType: ProfileType.CAREGIVER;
|
||||
contact: string;
|
||||
}
|
||||
googleAccounts?: { [email: string]: GoogleAccount };
|
||||
microsoftAccounts?: { [email: string]: MicrosoftAccount };
|
||||
appleAccounts?: { [email: string]: AppleAccount };
|
||||
}
|
||||
@ -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});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
37
hooks/firebase/useDeleteNotification.ts
Normal file
37
hooks/firebase/useDeleteNotification.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import {useMutation, useQueryClient} from "react-query";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {Notification} from "@/hooks/firebase/useGetNotifications";
|
||||
|
||||
export const useDeleteNotification = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const {user} = useAuthContext();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
await firestore()
|
||||
.collection("Notifications")
|
||||
.doc(id)
|
||||
.delete();
|
||||
},
|
||||
onMutate: async (deletedId) => {
|
||||
await queryClient.cancelQueries(["notifications", user?.uid]);
|
||||
|
||||
const previousNotifications = queryClient.getQueryData<Notification[]>(["notifications", user?.uid]);
|
||||
|
||||
queryClient.setQueryData<Notification[]>(["notifications", user?.uid], (old) =>
|
||||
old?.filter((notification) => notification?.id! !== deletedId) ?? []
|
||||
);
|
||||
|
||||
return {previousNotifications};
|
||||
},
|
||||
onError: (_err, _deletedId, context) => {
|
||||
if (context?.previousNotifications) {
|
||||
queryClient.setQueryData(["notifications", user?.uid], context.previousNotifications);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(["notifications", user?.uid]);
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -1,80 +1,202 @@
|
||||
import { useQuery } from "react-query";
|
||||
import {useQuery, useQueryClient} 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 {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";
|
||||
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 {user, profileData} = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
useEffect(() => {
|
||||
if (!profileData?.familyId) {
|
||||
console.log('[SYNC] No family ID available, skipping listener setup');
|
||||
return;
|
||||
}
|
||||
|
||||
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] 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:', {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
stack: error.stack
|
||||
});
|
||||
});
|
||||
|
||||
console.log('[SYNC] Listener setup complete');
|
||||
|
||||
return () => {
|
||||
console.log('[SYNC] Cleaning up sync listener', {
|
||||
familyId: profileData.familyId,
|
||||
userId: user?.uid
|
||||
});
|
||||
unsubscribe();
|
||||
};
|
||||
}, [profileData?.familyId, user?.uid, isFamilyView, queryClient]);
|
||||
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["events", user?.uid, isFamilyView],
|
||||
queryFn: async () => {
|
||||
console.log(`Fetching events - Family View: ${isFamilyView}, User: ${user?.uid}`);
|
||||
|
||||
const db = firestore();
|
||||
const userId = user?.uid;
|
||||
const familyId = profileData?.familyId;
|
||||
|
||||
let allEvents = [];
|
||||
|
||||
// If family view is active, include family, creator, and attendee events
|
||||
if (isFamilyView) {
|
||||
const familyQuery = db.collection("Events").where("familyId", "==", familyId);
|
||||
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
|
||||
const [publicFamilyEvents, privateCreatorEvents, privateAttendeeEvents, userAttendeeEvents, userCreatorEvents] = await Promise.all([
|
||||
// Public family events
|
||||
db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", false)
|
||||
.get(),
|
||||
|
||||
const [familySnapshot, attendeeSnapshot] = await Promise.all([
|
||||
familyQuery.get(),
|
||||
attendeeQuery.get(),
|
||||
// Private events user created
|
||||
db.collection("Events")
|
||||
.where("familyId", "==", familyId)
|
||||
.where("private", "==", true)
|
||||
.where("creatorId", "==", userId)
|
||||
.get(),
|
||||
|
||||
// Private events user is attending
|
||||
db.collection("Events")
|
||||
.where("private", "==", true)
|
||||
.where("attendees", "array-contains", userId)
|
||||
.get(),
|
||||
|
||||
// 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()
|
||||
]);
|
||||
|
||||
// Collect all events
|
||||
const familyEvents = familySnapshot.docs.map(doc => doc.data());
|
||||
const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data());
|
||||
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`);
|
||||
|
||||
// console.log("Family events not in creator query: ", familyEvents.filter(event => !creatorEvents.some(creatorEvent => creatorEvent.id === event.id)));
|
||||
|
||||
|
||||
allEvents = [...familyEvents, ...attendeeEvents];
|
||||
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})),
|
||||
...userAttendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...userCreatorEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||
];
|
||||
} else {
|
||||
// Only include creator and attendee events when family view is off
|
||||
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
|
||||
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
|
||||
|
||||
const [creatorSnapshot, attendeeSnapshot] = await Promise.all([
|
||||
creatorQuery.get(),
|
||||
attendeeQuery.get(),
|
||||
const [creatorEvents, attendeeEvents] = await Promise.all([
|
||||
db.collection("Events")
|
||||
.where("creatorId", "==", userId)
|
||||
.get(),
|
||||
db.collection("Events")
|
||||
.where("attendees", "array-contains", userId)
|
||||
.get()
|
||||
]);
|
||||
|
||||
const creatorEvents = creatorSnapshot.docs.map(doc => doc.data());
|
||||
const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data());
|
||||
console.log(`Found ${creatorEvents.size} creator events, ${attendeeEvents.size} attendee events`);
|
||||
|
||||
allEvents = [...creatorEvents, ...attendeeEvents];
|
||||
allEvents = [
|
||||
...creatorEvents.docs.map(doc => ({...doc.data(), id: doc.id})),
|
||||
...attendeeEvents.docs.map(doc => ({...doc.data(), id: doc.id}))
|
||||
];
|
||||
}
|
||||
|
||||
// Use a Map to ensure uniqueness only for events with IDs
|
||||
const uniqueEventsMap = new Map();
|
||||
const processedHashes = new Set();
|
||||
|
||||
allEvents.forEach(event => {
|
||||
if (event.id) {
|
||||
uniqueEventsMap.set(event.id, event); // Ensure uniqueness for events with IDs
|
||||
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 {
|
||||
uniqueEventsMap.set(uuidv4(), event); // Generate a temp key for events without ID
|
||||
console.log(`Duplicate event detected and skipped using hash: ${eventHash}`);
|
||||
}
|
||||
});
|
||||
const uniqueEvents = Array.from(uniqueEventsMap.values());
|
||||
|
||||
// Filter out private events unless the user is the creator
|
||||
const filteredEvents = uniqueEvents.filter(event => {
|
||||
if (event.private) {
|
||||
return event.creatorId === userId;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
console.log(`Processing ${uniqueEventsMap.size} unique events after deduplication`);
|
||||
|
||||
// Attach event colors and return the final list of events
|
||||
return await Promise.all(
|
||||
filteredEvents.map(async (event) => {
|
||||
const processedEvents = await Promise.all(
|
||||
Array.from(uniqueEventsMap.values()).map(async (event) => {
|
||||
const profileSnapshot = await db
|
||||
.collection("Profiles")
|
||||
.doc(event.creatorId)
|
||||
@ -85,19 +207,28 @@ export const useGetEvents = () => {
|
||||
|
||||
return {
|
||||
...event,
|
||||
id: event.id || Math.random().toString(36).slice(2, 9), // Generate temp ID if missing
|
||||
title: event.title,
|
||||
start: new Date(event.startDate.seconds * 1000),
|
||||
end: new Date(event.endDate.seconds * 1000),
|
||||
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,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
console.log(`Events processing completed, returning ${processedEvents.length} events`);
|
||||
return processedEvents;
|
||||
},
|
||||
staleTime: Infinity,
|
||||
cacheTime: Infinity,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
cacheTime: 30 * 60 * 1000,
|
||||
keepPreviousData: true,
|
||||
onError: (error) => {
|
||||
console.error('Error fetching events:', error);
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,11 +1,35 @@
|
||||
import {useQuery} from "react-query";
|
||||
import { useQuery } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
|
||||
interface FirestoreTimestamp {
|
||||
seconds: number;
|
||||
nanoseconds: number;
|
||||
}
|
||||
|
||||
interface NotificationFirestore {
|
||||
creatorId: string;
|
||||
familyId: string;
|
||||
content: string;
|
||||
eventId: string;
|
||||
timestamp: FirestoreTimestamp;
|
||||
date?: FirestoreTimestamp;
|
||||
}
|
||||
|
||||
export interface Notification {
|
||||
id: string;
|
||||
creatorId: string;
|
||||
familyId: string;
|
||||
content: string;
|
||||
eventId: string;
|
||||
timestamp: Date;
|
||||
date?: Date;
|
||||
}
|
||||
|
||||
export const useGetNotifications = () => {
|
||||
const { user, profileData } = useAuthContext();
|
||||
|
||||
return useQuery({
|
||||
return useQuery<Notification[], Error>({
|
||||
queryKey: ["notifications", user?.uid],
|
||||
queryFn: async () => {
|
||||
const snapshot = await firestore()
|
||||
@ -14,16 +38,17 @@ export const useGetNotifications = () => {
|
||||
.get();
|
||||
|
||||
return snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
const data = doc.data() as NotificationFirestore;
|
||||
|
||||
return {...data, timestamp: new Date(data.timestamp.seconds * 1000 + data.timestamp.nanoseconds / 1e6)} as {
|
||||
creatorId: string,
|
||||
familyId: string,
|
||||
content: string,
|
||||
eventId: string,
|
||||
timestamp: Date,
|
||||
return {
|
||||
id: doc.id,
|
||||
...data,
|
||||
timestamp: new Date(data.timestamp.seconds * 1000 + data.timestamp.nanoseconds / 1e6),
|
||||
date: data.date ? new Date(data.date.seconds * 1000 + data.date.nanoseconds / 1e6) : undefined
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
},
|
||||
refetchOnWindowFocus: true,
|
||||
staleTime: 60000,
|
||||
});
|
||||
};
|
||||
@ -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)")
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -2,7 +2,7 @@ import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useEffect} from "react";
|
||||
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
||||
import {useFetchAndSaveGoogleEvents} from "@/hooks/useFetchAndSaveGoogleEvents";
|
||||
import {useFetchAndSaveOutlookEvents} from "@/hooks/useFetchAndSaveOutlookEvents";
|
||||
import {useFetchAndSaveMicrosoftEvents} from "@/hooks/useFetchAndSaveOutlookEvents";
|
||||
import {useFetchAndSaveAppleEvents} from "@/hooks/useFetchAndSaveAppleEvents";
|
||||
import * as WebBrowser from "expo-web-browser";
|
||||
import * as Google from "expo-auth-session/providers/google";
|
||||
@ -10,14 +10,12 @@ import * as AuthSession from "expo-auth-session";
|
||||
import * as AppleAuthentication from "expo-apple-authentication";
|
||||
import * as Notifications from 'expo-notifications';
|
||||
import {useQueryClient} from "react-query";
|
||||
import {AppleAccount, GoogleAccount, MicrosoftAccount} from "@/hooks/firebase/types/profileTypes";
|
||||
|
||||
const googleConfig = {
|
||||
androidClientId:
|
||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
iosClientId:
|
||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
webClientId:
|
||||
"406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
androidClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
iosClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
webClientId: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
scopes: [
|
||||
"email",
|
||||
"profile",
|
||||
@ -39,18 +37,32 @@ const microsoftConfig = {
|
||||
"Calendars.ReadWrite",
|
||||
"User.Read",
|
||||
],
|
||||
authorizationEndpoint:
|
||||
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
||||
authorizationEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
||||
tokenEndpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
|
||||
};
|
||||
|
||||
interface SyncResponse {
|
||||
success: boolean;
|
||||
eventCount?: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface CalendarSyncResult {
|
||||
data: {
|
||||
success: boolean;
|
||||
eventCount: number;
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export const useCalSync = () => {
|
||||
const {profileData} = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const {mutateAsync: updateUserData} = useUpdateUserData();
|
||||
const {mutateAsync: fetchAndSaveGoogleEvents, isLoading: isSyncingGoogle} = useFetchAndSaveGoogleEvents();
|
||||
const {mutateAsync: fetchAndSaveOutlookEvents, isLoading: isSyncingOutlook} = useFetchAndSaveOutlookEvents();
|
||||
const {mutateAsync: fetchAndSaveOutlookEvents, isLoading: isSyncingOutlook} = useFetchAndSaveMicrosoftEvents();
|
||||
const {mutateAsync: fetchAndSaveAppleEvents, isLoading: isSyncingApple} = useFetchAndSaveAppleEvents();
|
||||
|
||||
WebBrowser.maybeCompleteAuthSession();
|
||||
@ -72,134 +84,106 @@ export const useCalSync = () => {
|
||||
}
|
||||
);
|
||||
|
||||
console.log(response)
|
||||
const userInfo = await userInfoResponse.json();
|
||||
const googleMail = userInfo.email;
|
||||
|
||||
let googleAccounts = profileData?.googleAccounts || {};
|
||||
const updatedGoogleAccounts = {
|
||||
...googleAccounts,
|
||||
[googleMail]: {accessToken, refreshToken},
|
||||
const googleAccount: GoogleAccount = {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
email: googleMail,
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||
scope: googleConfig.scopes.join(' ')
|
||||
};
|
||||
|
||||
console.log({refreshToken})
|
||||
|
||||
await updateUserData({
|
||||
newUserData: {googleAccounts: updatedGoogleAccounts},
|
||||
newUserData: {
|
||||
googleAccounts: {
|
||||
...profileData?.googleAccounts,
|
||||
[googleMail]: googleAccount
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
await fetchAndSaveGoogleEvents({
|
||||
token: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
email: googleMail,
|
||||
});
|
||||
await fetchAndSaveGoogleEvents({email: googleMail});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error during Google sign-in:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const handleMicrosoftSignIn = async () => {
|
||||
try {
|
||||
console.log("Starting Microsoft sign-in...");
|
||||
|
||||
const authRequest = new AuthSession.AuthRequest({
|
||||
clientId: microsoftConfig.clientId,
|
||||
scopes: microsoftConfig.scopes,
|
||||
redirectUri: microsoftConfig.redirectUri,
|
||||
responseType: AuthSession.ResponseType.Code,
|
||||
usePKCE: true, // Enable PKCE
|
||||
usePKCE: true,
|
||||
});
|
||||
|
||||
console.log("Auth request created:", authRequest);
|
||||
|
||||
const authResult = await authRequest.promptAsync({
|
||||
authorizationEndpoint: microsoftConfig.authorizationEndpoint,
|
||||
});
|
||||
|
||||
console.log("Auth result:", authResult);
|
||||
|
||||
if (authResult.type === "success" && authResult.params?.code) {
|
||||
const code = authResult.params.code;
|
||||
console.log("Authorization code received:", code);
|
||||
|
||||
// Exchange authorization code for tokens
|
||||
const tokenResponse = await fetch(microsoftConfig.tokenEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: `client_id=${
|
||||
microsoftConfig.clientId
|
||||
}&redirect_uri=${encodeURIComponent(
|
||||
body: `client_id=${microsoftConfig.clientId}&redirect_uri=${encodeURIComponent(
|
||||
microsoftConfig.redirectUri
|
||||
)}&grant_type=authorization_code&code=${code}&code_verifier=${
|
||||
authRequest.codeVerifier
|
||||
}&scope=${encodeURIComponent(
|
||||
"https://graph.microsoft.com/Calendars.ReadWrite offline_access User.Read"
|
||||
)}`,
|
||||
}&scope=${encodeURIComponent(microsoftConfig.scopes.join(' '))}`,
|
||||
});
|
||||
|
||||
console.log("Token response status:", tokenResponse.status);
|
||||
|
||||
if (!tokenResponse.ok) {
|
||||
const errorText = await tokenResponse.text();
|
||||
console.error("Token exchange failed:", errorText);
|
||||
return;
|
||||
throw new Error(await tokenResponse.text());
|
||||
}
|
||||
|
||||
const tokenData = await tokenResponse.json();
|
||||
console.log("Token data received:", tokenData);
|
||||
|
||||
if (tokenData?.access_token) {
|
||||
console.log("Access token received, fetching user info...");
|
||||
|
||||
// Fetch user info from Microsoft Graph API to get the email
|
||||
const userInfoResponse = await fetch(
|
||||
"https://graph.microsoft.com/v1.0/me",
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokenData.access_token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const userInfo = await userInfoResponse.json();
|
||||
console.log("User info received:", userInfo);
|
||||
|
||||
if (userInfo.error) {
|
||||
console.error("Error fetching user info:", userInfo.error);
|
||||
} else {
|
||||
const outlookMail = userInfo.mail || userInfo.userPrincipalName;
|
||||
|
||||
let microsoftAccounts = profileData?.microsoftAccounts;
|
||||
const updatedMicrosoftAccounts = microsoftAccounts
|
||||
? {...microsoftAccounts, [outlookMail]: tokenData.access_token}
|
||||
: {[outlookMail]: tokenData.access_token};
|
||||
|
||||
await updateUserData({
|
||||
newUserData: {microsoftAccounts: updatedMicrosoftAccounts},
|
||||
});
|
||||
|
||||
await fetchAndSaveOutlookEvents(
|
||||
tokenData.access_token,
|
||||
outlookMail
|
||||
);
|
||||
console.log("User data updated successfully.");
|
||||
const userInfoResponse = await fetch(
|
||||
"https://graph.microsoft.com/v1.0/me",
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokenData.access_token}`,
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.warn("Authentication was not successful:", authResult);
|
||||
);
|
||||
|
||||
const userInfo = await userInfoResponse.json();
|
||||
const outlookMail = userInfo.mail || userInfo.userPrincipalName;
|
||||
|
||||
const microsoftAccount: MicrosoftAccount = {
|
||||
accessToken: tokenData.access_token,
|
||||
refreshToken: tokenData.refresh_token,
|
||||
email: outlookMail,
|
||||
expiresAt: new Date(Date.now() + tokenData.expires_in * 1000),
|
||||
};
|
||||
|
||||
await updateUserData({
|
||||
newUserData: {
|
||||
microsoftAccounts: {
|
||||
...profileData?.microsoftAccounts,
|
||||
[outlookMail]: microsoftAccount
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
await fetchAndSaveOutlookEvents({email: outlookMail});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error during Microsoft sign-in:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const handleAppleSignIn = async () => {
|
||||
try {
|
||||
console.log("Starting Apple Sign-in...");
|
||||
|
||||
const credential = await AppleAuthentication.signInAsync({
|
||||
requestedScopes: [
|
||||
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
||||
@ -207,117 +191,124 @@ export const useCalSync = () => {
|
||||
],
|
||||
});
|
||||
|
||||
console.log("Apple sign-in result:", credential);
|
||||
|
||||
alert(JSON.stringify(credential))
|
||||
|
||||
const appleToken = credential.identityToken;
|
||||
const appleMail = credential.email!;
|
||||
|
||||
|
||||
if (appleToken) {
|
||||
console.log("Apple ID token received. Fetch user info if needed...");
|
||||
const appleAccount: AppleAccount = {
|
||||
accessToken: appleToken,
|
||||
email: appleMail,
|
||||
identityToken: credential.identityToken!,
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000)
|
||||
};
|
||||
|
||||
let appleAcounts = profileData?.appleAccounts;
|
||||
const updatedAppleAccounts = appleAcounts
|
||||
? {...appleAcounts, [appleMail]: appleToken}
|
||||
: {[appleMail]: appleToken};
|
||||
const updatedAppleAccounts = {
|
||||
...profileData?.appleAccounts,
|
||||
[appleMail]: appleAccount
|
||||
};
|
||||
|
||||
await updateUserData({
|
||||
newUserData: {appleAccounts: updatedAppleAccounts},
|
||||
});
|
||||
|
||||
console.log("User data updated with Apple ID token.");
|
||||
await fetchAndSaveAppleEvents({token: appleToken, email: appleMail!});
|
||||
} else {
|
||||
console.warn(
|
||||
"Apple authentication was not successful or email was hidden."
|
||||
);
|
||||
await fetchAndSaveAppleEvents({email: appleMail});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error during Apple Sign-in:", error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const resyncAllCalendars = async (): Promise<void> => {
|
||||
try {
|
||||
const syncPromises: Promise<void>[] = [];
|
||||
const results: SyncResponse[] = [];
|
||||
|
||||
if (profileData?.googleAccounts) {
|
||||
console.log(profileData.googleAccounts)
|
||||
for (const [email, emailAcc] of Object.entries(profileData.googleAccounts)) {
|
||||
if(emailAcc?.accessToken) {
|
||||
syncPromises.push(fetchAndSaveGoogleEvents({ token: emailAcc?.accessToken, refreshToken: emailAcc?.refreshToken, email }));
|
||||
for (const email of Object.keys(profileData.googleAccounts)) {
|
||||
try {
|
||||
const result = await fetchAndSaveGoogleEvents({email});
|
||||
results.push({
|
||||
success: result.success,
|
||||
eventCount: result.eventCount
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error(`Failed to sync Google calendar ${email}:`, error);
|
||||
results.push({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (profileData?.microsoftAccounts) {
|
||||
for (const [email, accessToken] of Object.entries(profileData.microsoftAccounts)) {
|
||||
syncPromises.push(fetchAndSaveOutlookEvents(accessToken, email));
|
||||
for (const email of Object.keys(profileData.microsoftAccounts)) {
|
||||
try {
|
||||
const result = await fetchAndSaveOutlookEvents({email});
|
||||
results.push({
|
||||
success: result.success,
|
||||
eventCount: result.eventCount
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error(`Failed to sync Microsoft calendar ${email}:`, error);
|
||||
results.push({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (profileData?.appleAccounts) {
|
||||
for (const [email, token] of Object.entries(profileData.appleAccounts)) {
|
||||
syncPromises.push(fetchAndSaveAppleEvents({ token, email }));
|
||||
for (const email of Object.keys(profileData.appleAccounts)) {
|
||||
try {
|
||||
const result = await fetchAndSaveAppleEvents({email});
|
||||
results.push({
|
||||
success: true,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error(`Failed to sync Apple calendar ${email}:`, error);
|
||||
results.push({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(syncPromises);
|
||||
console.log("All calendars have been resynced.");
|
||||
const successCount = results.filter(r => r.success).length;
|
||||
const failCount = results.filter(r => !r.success).length;
|
||||
const totalEvents = results.reduce((sum, r) => sum + (r.eventCount || 0), 0);
|
||||
|
||||
if (failCount > 0) {
|
||||
console.error(`${failCount} calendar syncs failed, ${successCount} succeeded`);
|
||||
results.filter(r => !r.success).forEach(r => {
|
||||
console.error('Sync failed:', r.error);
|
||||
});
|
||||
} else if (successCount > 0) {
|
||||
console.log(`Successfully synced ${successCount} calendars with ${totalEvents} total events`);
|
||||
} else {
|
||||
console.log("No calendars to sync");
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error resyncing calendars:", error);
|
||||
console.error("Error in resyncAllCalendars:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
let isConnectedToGoogle = false;
|
||||
if (profileData?.googleAccounts) {
|
||||
Object.values(profileData?.googleAccounts).forEach((item) => {
|
||||
if (item !== null) {
|
||||
isConnectedToGoogle = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let isConnectedToMicrosoft = false;
|
||||
const microsoftAccounts = profileData?.microsoftAccounts;
|
||||
if (microsoftAccounts) {
|
||||
Object.values(profileData?.microsoftAccounts).forEach((item) => {
|
||||
if (item !== null) {
|
||||
isConnectedToMicrosoft = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let isConnectedToApple = false;
|
||||
if (profileData?.appleAccounts) {
|
||||
Object.values(profileData?.appleAccounts).forEach((item) => {
|
||||
if (item !== null) {
|
||||
isConnectedToApple = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const isConnectedToGoogle = Object.values(profileData?.googleAccounts || {}).some(account => !!account);
|
||||
const isConnectedToMicrosoft = Object.values(profileData?.microsoftAccounts || {}).some(account => !!account);
|
||||
const isConnectedToApple = Object.values(profileData?.appleAccounts || {}).some(account => !!account);
|
||||
|
||||
useEffect(() => {
|
||||
const handleNotification = async (notification: Notifications.Notification) => {
|
||||
const eventId = notification?.request?.content?.data?.eventId;
|
||||
|
||||
// await resyncAllCalendars();
|
||||
queryClient.invalidateQueries(["events"]);
|
||||
};
|
||||
|
||||
const sub = Notifications.addNotificationReceivedListener(handleNotification);
|
||||
|
||||
return () => sub.remove();
|
||||
}, []);
|
||||
|
||||
|
||||
return {
|
||||
handleAppleSignIn,
|
||||
handleMicrosoftSignIn,
|
||||
@ -334,5 +325,5 @@ export const useCalSync = () => {
|
||||
isSyncingApple,
|
||||
resyncAllCalendars,
|
||||
isSyncing: isSyncingApple || isSyncingOutlook || isSyncingGoogle
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
@ -1,107 +1,37 @@
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import { fetchGoogleCalendarEvents } from "@/calendar-integration/google-calendar-utils";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { useCreateEventsFromProvider } from "@/hooks/firebase/useCreateEvent";
|
||||
import { useClearTokens } from "@/hooks/firebase/useClearTokens";
|
||||
import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData";
|
||||
import {useMutation, useQueryClient} from "react-query";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import functions from "@react-native-firebase/functions";
|
||||
|
||||
interface SyncResponse {
|
||||
success: boolean;
|
||||
eventCount: number;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export const useFetchAndSaveGoogleEvents = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { profileData } = useAuthContext();
|
||||
const { mutateAsync: createEventsFromProvider } = useCreateEventsFromProvider();
|
||||
const { mutateAsync: clearToken } = useClearTokens();
|
||||
const { mutateAsync: updateUserData } = useUpdateUserData();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["fetchAndSaveGoogleEvents", "sync"],
|
||||
mutationFn: async ({ token, email, date, refreshToken }: { token?: string; refreshToken?: string; email?: string; date?: Date }) => {
|
||||
const baseDate = date || new Date();
|
||||
const timeMin = new Date(baseDate.setMonth(baseDate.getMonth() - 1)).toISOString().slice(0, -5) + "Z";
|
||||
const timeMax = new Date(baseDate.setMonth(baseDate.getMonth() + 2)).toISOString().slice(0, -5) + "Z";
|
||||
mutationFn: async ({ email }: { email?: string }) => {
|
||||
if (!email || !profileData?.googleAccounts?.[email]) {
|
||||
throw new Error("No valid Google account found");
|
||||
}
|
||||
|
||||
console.log("Token: ", token);
|
||||
try {
|
||||
const response = await functions()
|
||||
.httpsCallable('triggerGoogleSync')({ email });
|
||||
|
||||
const tryFetchEvents = async (isRetry = false) => {
|
||||
try {
|
||||
const response = await fetchGoogleCalendarEvents(
|
||||
token,
|
||||
email,
|
||||
profileData?.familyId,
|
||||
timeMin,
|
||||
timeMax
|
||||
);
|
||||
|
||||
if (!response.success) {
|
||||
await clearToken({ email: email!, provider: "google" });
|
||||
return; // Stop refetching if clearing the token
|
||||
}
|
||||
|
||||
console.log("Google Calendar events fetched:", response);
|
||||
|
||||
const items = response?.googleEvents?.map((item) => {
|
||||
if (item.allDay) {
|
||||
item.startDate = new Date(item.startDate.setHours(0, 0, 0, 0));
|
||||
item.endDate = item.startDate;
|
||||
}
|
||||
return item;
|
||||
}) || [];
|
||||
|
||||
await createEventsFromProvider(items);
|
||||
} catch (error) {
|
||||
console.error("Error fetching Google Calendar events:", error);
|
||||
|
||||
if (!isRetry) {
|
||||
const refreshedToken = await handleRefreshToken(email, refreshToken);
|
||||
if (refreshedToken) {
|
||||
await updateUserData({
|
||||
newUserData: {
|
||||
googleAccounts: {
|
||||
...profileData.googleAccounts,
|
||||
[email!]: { ...profileData.googleAccounts[email!], accessToken: refreshedToken },
|
||||
},
|
||||
},
|
||||
});
|
||||
return tryFetchEvents(true); // Retry once after refreshing
|
||||
} else {
|
||||
await clearToken({ email: email!, provider: "google" });
|
||||
console.error(`Token refresh failed; token cleared for ${email}`);
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
console.error(`Retry failed after refreshing token for user ${profileData?.email}:`, error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return tryFetchEvents();
|
||||
return response.data as SyncResponse;
|
||||
} catch (error: any) {
|
||||
console.error("Error initiating Google Calendar sync:", error);
|
||||
throw new Error(error.details?.message || error.message || "Failed to sync calendar");
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries(["events"]);
|
||||
},
|
||||
console.log(`Successfully synced ${data.eventCount} events`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
async function handleRefreshToken(email: string, refreshToken: string) {
|
||||
if (!refreshToken) return null;
|
||||
|
||||
try {
|
||||
const response = await fetch('https://oauth2.googleapis.com/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refreshToken,
|
||||
client_id: "406146460310-2u67ab2nbhu23trp8auho1fq4om29fc0.apps.googleusercontent.com",
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
} catch (error) {
|
||||
console.error("Error refreshing Google token:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1,41 +1,144 @@
|
||||
import {useMutation, useQueryClient} from "react-query";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useCreateEventsFromProvider} from "@/hooks/firebase/useCreateEvent";
|
||||
import {fetchMicrosoftCalendarEvents} from "@/calendar-integration/microsoft-calendar-utils";
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { useSetUserData } from "@/hooks/firebase/useSetUserData";
|
||||
import functions from '@react-native-firebase/functions';
|
||||
import * as AuthSession from 'expo-auth-session';
|
||||
|
||||
export const useFetchAndSaveOutlookEvents = () => {
|
||||
const queryClient = useQueryClient()
|
||||
const {profileData} = useAuthContext();
|
||||
const {mutateAsync: createEventsFromProvider} = useCreateEventsFromProvider();
|
||||
interface SyncResponse {
|
||||
success: boolean;
|
||||
eventCount: number;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
return useMutation({
|
||||
interface SyncError extends Error {
|
||||
code?: string;
|
||||
details?: {
|
||||
requiresReauth?: boolean;
|
||||
message?: string;
|
||||
};
|
||||
}
|
||||
|
||||
const microsoftConfig = {
|
||||
clientId: "13c79071-1066-40a9-9f71-b8c4b138b4af",
|
||||
scopes: [
|
||||
"openid",
|
||||
"profile",
|
||||
"email",
|
||||
"offline_access",
|
||||
"Calendars.ReadWrite",
|
||||
"User.Read",
|
||||
],
|
||||
redirectUri: AuthSession.makeRedirectUri({path: "settings"})
|
||||
};
|
||||
|
||||
export const useFetchAndSaveMicrosoftEvents = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { profileData } = useAuthContext();
|
||||
const { mutateAsync: setUserData } = useSetUserData();
|
||||
|
||||
const handleReauth = async (email: string) => {
|
||||
try {
|
||||
const authRequest = new AuthSession.AuthRequest({
|
||||
clientId: microsoftConfig.clientId,
|
||||
scopes: microsoftConfig.scopes,
|
||||
redirectUri: microsoftConfig.redirectUri,
|
||||
responseType: AuthSession.ResponseType.Code,
|
||||
usePKCE: true,
|
||||
});
|
||||
|
||||
const result = await authRequest.promptAsync({
|
||||
authorizationEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
||||
});
|
||||
|
||||
if (result.type === 'success' && result.params?.code) {
|
||||
const tokenResponse = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: microsoftConfig.clientId,
|
||||
scope: microsoftConfig.scopes.join(' '),
|
||||
code: result.params.code,
|
||||
redirect_uri: microsoftConfig.redirectUri,
|
||||
grant_type: 'authorization_code',
|
||||
code_verifier: authRequest.codeVerifier || '',
|
||||
}),
|
||||
});
|
||||
|
||||
const tokens = await tokenResponse.json();
|
||||
|
||||
await setUserData({
|
||||
newUserData: {
|
||||
microsoftAccounts: {
|
||||
...profileData?.microsoftAccounts,
|
||||
[email]: {
|
||||
accessToken: tokens.access_token,
|
||||
refreshToken: tokens.refresh_token,
|
||||
email,
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error('Microsoft reauth error:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
return useMutation<SyncResponse, SyncError, { email?: string }>({
|
||||
mutationKey: ["fetchAndSaveOutlookEvents", "sync"],
|
||||
mutationFn: async ({token, email, date}: { token?: string; email?: string, date?: Date }) => {
|
||||
const baseDate = date || new Date();
|
||||
const timeMin = new Date(new Date(baseDate).setFullYear(new Date(baseDate).getMonth() - 1));
|
||||
const timeMax = new Date(new Date(baseDate).setFullYear(new Date(baseDate).getMonth() + 1));
|
||||
mutationFn: async ({ email }: { email?: string }) => {
|
||||
if (!email) {
|
||||
throw new Error("Email is required");
|
||||
}
|
||||
|
||||
console.log("Token: ", token ?? profileData?.microsoftToken);
|
||||
if (!profileData?.microsoftAccounts?.[email]) {
|
||||
throw new Error("No valid Microsoft account found");
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetchMicrosoftCalendarEvents(
|
||||
token ?? profileData?.microsoftToken,
|
||||
email ?? profileData?.outlookMail,
|
||||
profileData?.familyId,
|
||||
timeMin.toISOString().slice(0, -5) + "Z",
|
||||
timeMax.toISOString().slice(0, -5) + "Z"
|
||||
);
|
||||
const response = await functions()
|
||||
.httpsCallable('triggerMicrosoftSync')({ email });
|
||||
|
||||
return response.data as SyncResponse;
|
||||
} catch (error: any) {
|
||||
console.error("Microsoft sync error:", error);
|
||||
|
||||
// Check if we need to reauthenticate
|
||||
if (error.details?.requiresReauth ||
|
||||
error.code === 'functions/failed-precondition' ||
|
||||
error.code === 'functions/unauthenticated') {
|
||||
|
||||
console.log('Attempting Microsoft reauth...');
|
||||
const reauthSuccessful = await handleReauth(email);
|
||||
|
||||
if (reauthSuccessful) {
|
||||
// Retry the sync with new tokens
|
||||
console.log('Retrying sync after reauth...');
|
||||
const retryResponse = await functions()
|
||||
.httpsCallable('triggerMicrosoftSync')({ email });
|
||||
return retryResponse.data as SyncResponse;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(response);
|
||||
const items = response ?? [];
|
||||
await createEventsFromProvider(items);
|
||||
} catch (error) {
|
||||
console.error("Error fetching and saving Outlook events: ", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries(["events"])
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries(["events"]);
|
||||
console.log(`Successfully synced ${data.eventCount} Microsoft events`);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Microsoft sync failed:', {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
details: error.details
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -3,10 +3,16 @@ import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { useFetchAndSaveGoogleEvents } from "./useFetchAndSaveGoogleEvents";
|
||||
import { useFetchAndSaveAppleEvents } from "./useFetchAndSaveAppleEvents";
|
||||
import { useFetchAndSaveOutlookEvents } from "./useFetchAndSaveOutlookEvents";
|
||||
import { useFetchAndSaveMicrosoftEvents } from "./useFetchAndSaveOutlookEvents";
|
||||
import { selectedDateAtom } from "@/components/pages/calendar/atoms";
|
||||
import { addDays, subDays, isBefore, isAfter, format } from "date-fns";
|
||||
|
||||
interface SyncResponse {
|
||||
success: boolean;
|
||||
eventCount?: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const useSyncEvents = () => {
|
||||
const { profileData } = useAuthContext();
|
||||
const selectedDate = useAtomValue(selectedDateAtom);
|
||||
@ -15,12 +21,18 @@ export const useSyncEvents = () => {
|
||||
const [lowerBoundDate, setLowerBoundDate] = useState<Date>(subDays(selectedDate, 6 * 30));
|
||||
const [upperBoundDate, setUpperBoundDate] = useState<Date>(addDays(selectedDate, 6 * 30));
|
||||
const [isSyncing, setIsSyncing] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
const [syncStats, setSyncStats] = useState<{
|
||||
total: number;
|
||||
success: number;
|
||||
failed: number;
|
||||
events: number;
|
||||
}>({ total: 0, success: 0, failed: 0, events: 0 });
|
||||
|
||||
const syncedRanges = useState<Set<string>>(new Set())[0];
|
||||
|
||||
const { mutateAsync: fetchAndSaveGoogleEvents } = useFetchAndSaveGoogleEvents();
|
||||
const { mutateAsync: fetchAndSaveOutlookEvents } = useFetchAndSaveOutlookEvents();
|
||||
const { mutateAsync: fetchAndSaveOutlookEvents } = useFetchAndSaveMicrosoftEvents();
|
||||
const { mutateAsync: fetchAndSaveAppleEvents } = useFetchAndSaveAppleEvents();
|
||||
|
||||
const generateRangeKey = (startDate: Date, endDate: Date) => {
|
||||
@ -41,26 +53,71 @@ export const useSyncEvents = () => {
|
||||
}
|
||||
|
||||
if (isBefore(selectedDate, lowerBoundDate) || isAfter(selectedDate, upperBoundDate)) {
|
||||
const results: SyncResponse[] = [];
|
||||
const stats = { total: 0, success: 0, failed: 0, events: 0 };
|
||||
|
||||
try {
|
||||
const googleEvents = Object.entries(profileData?.googleAccounts || {}).map(([email, { accessToken }]) =>
|
||||
fetchAndSaveGoogleEvents({ token: accessToken, email, date: selectedDate })
|
||||
);
|
||||
if (profileData?.googleAccounts) {
|
||||
for (const [email] of Object.entries(profileData.googleAccounts)) {
|
||||
try {
|
||||
stats.total++;
|
||||
const result = await fetchAndSaveGoogleEvents({ email }) as SyncResponse;
|
||||
if (result.success) {
|
||||
stats.success++;
|
||||
stats.events += result.eventCount || 0;
|
||||
} else {
|
||||
stats.failed++;
|
||||
}
|
||||
results.push(result);
|
||||
} catch (err) {
|
||||
stats.failed++;
|
||||
console.error(`Failed to sync Google calendar for ${email}:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const outlookEvents = Object.entries(profileData?.microsoftAccounts || {}).map(([email, token]) =>
|
||||
fetchAndSaveOutlookEvents({ token, email, date: selectedDate })
|
||||
);
|
||||
if (profileData?.microsoftAccounts) {
|
||||
for (const [email] of Object.entries(profileData.microsoftAccounts)) {
|
||||
try {
|
||||
stats.total++;
|
||||
const result = await fetchAndSaveOutlookEvents({ email });
|
||||
if (result.success) {
|
||||
stats.success++;
|
||||
stats.events += result.eventCount || 0;
|
||||
} else {
|
||||
stats.failed++;
|
||||
}
|
||||
results.push(result);
|
||||
} catch (err) {
|
||||
stats.failed++;
|
||||
console.error(`Failed to sync Microsoft calendar for ${email}:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const appleEvents = Object.entries(profileData?.appleAccounts || {}).map(([email, token]) =>
|
||||
fetchAndSaveAppleEvents({ token, email, date: selectedDate })
|
||||
);
|
||||
|
||||
await Promise.all([...googleEvents, ...outlookEvents, ...appleEvents]);
|
||||
if (profileData?.appleAccounts) {
|
||||
for (const [email] of Object.entries(profileData.appleAccounts)) {
|
||||
try {
|
||||
stats.total++;
|
||||
const result = await fetchAndSaveAppleEvents({ email });
|
||||
} catch (err) {
|
||||
stats.failed++;
|
||||
console.error(`Failed to sync Apple calendar for ${email}:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setSyncStats(stats);
|
||||
setLastSyncDate(selectedDate);
|
||||
setLowerBoundDate(newLowerBound);
|
||||
setUpperBoundDate(newUpperBound);
|
||||
syncedRanges.add(rangeKey);
|
||||
} catch (err) {
|
||||
|
||||
if (stats.failed > 0) {
|
||||
throw new Error(`Failed to sync ${stats.failed} calendars`);
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
console.error("Error syncing events:", err);
|
||||
setError(err);
|
||||
} finally {
|
||||
@ -69,7 +126,16 @@ export const useSyncEvents = () => {
|
||||
} else {
|
||||
setIsSyncing(false);
|
||||
}
|
||||
}, [selectedDate, lowerBoundDate, upperBoundDate, profileData, fetchAndSaveGoogleEvents, fetchAndSaveOutlookEvents, fetchAndSaveAppleEvents, syncedRanges]);
|
||||
}, [
|
||||
selectedDate,
|
||||
lowerBoundDate,
|
||||
upperBoundDate,
|
||||
profileData,
|
||||
fetchAndSaveGoogleEvents,
|
||||
fetchAndSaveOutlookEvents,
|
||||
fetchAndSaveAppleEvents,
|
||||
syncedRanges
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
syncEvents();
|
||||
@ -81,5 +147,6 @@ export const useSyncEvents = () => {
|
||||
lastSyncDate,
|
||||
lowerBoundDate,
|
||||
upperBoundDate,
|
||||
syncStats,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user