diff --git a/packages/features/calendars/DestinationCalendarSelector.tsx b/packages/features/calendars/DestinationCalendarSelector.tsx
index db63778a01..d2fb3570fd 100644
--- a/packages/features/calendars/DestinationCalendarSelector.tsx
+++ b/packages/features/calendars/DestinationCalendarSelector.tsx
@@ -119,7 +119,7 @@ const DestinationCalendarSelector = ({
// Get primary calendar, which is shown in the placeholder since this is the calendar that will
// be used when no destination calendar is selected.
- const primaryCalendar = query.data.connectedCalendars.filter((cal) => Boolean(cal.primary))[0];
+ const primaryCalendar = query.data.destinationCalendarEmail;
return (
@@ -130,8 +130,7 @@ const DestinationCalendarSelector = ({
`${t("select_destination_calendar")}`
) : (
- {t("default_calendar_selected")}{" "}
- {primaryCalendar?.primary?.externalId && `(${primaryCalendar?.primary?.externalId})`}
+ {t("default_calendar_selected")} {primaryCalendar && `(${primaryCalendar})`}
)
}
diff --git a/packages/trpc/server/routers/viewer.tsx b/packages/trpc/server/routers/viewer.tsx
index 23fac562dd..e3d8d299cf 100644
--- a/packages/trpc/server/routers/viewer.tsx
+++ b/packages/trpc/server/routers/viewer.tsx
@@ -322,6 +322,9 @@ const loggedInViewerRouter = router({
// get all the connected integrations' calendars (from third party)
const connectedCalendars = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
+ // store email of the destination calendar to display
+ let destinationCalendarEmail = null;
+
if (connectedCalendars.length === 0) {
/* As there are no connected calendars, delete the destination calendar if it exists */
if (user.destinationCalendar) {
@@ -335,7 +338,7 @@ const loggedInViewerRouter = router({
There are connected calendars, but no destination calendar
So create a default destination calendar with the first primary connected calendar
*/
- const { integration = "", externalId = "", credentialId } = connectedCalendars[0].primary ?? {};
+ const { integration = "", externalId = "", credentialId, email } = connectedCalendars[0].primary ?? {};
user.destinationCalendar = await ctx.prisma.destinationCalendar.create({
data: {
userId: user.id,
@@ -344,6 +347,7 @@ const loggedInViewerRouter = router({
credentialId,
},
});
+ destinationCalendarEmail = email ?? user.destinationCalendar?.externalId;
} else {
/* There are connected calendars and a destination calendar */
@@ -354,6 +358,7 @@ const loggedInViewerRouter = router({
cal.externalId === user.destinationCalendar?.externalId &&
cal.integration === user.destinationCalendar?.integration
);
+
if (!destinationCal) {
// If destinationCalendar is out of date, update it with the first primary connected calendar
const { integration = "", externalId = "" } = connectedCalendars[0].primary ?? {};
@@ -365,11 +370,13 @@ const loggedInViewerRouter = router({
},
});
}
+ destinationCalendarEmail = destinationCal?.email ?? user.destinationCalendar?.externalId;
}
return {
connectedCalendars,
destinationCalendar: user.destinationCalendar,
+ destinationCalendarEmail,
};
}),
setDestinationCalendar: authedProcedure