1 import { useGetAddresses } from '@proton/account/addresses/hooks';
2 import { useGetCalendarBootstrap } from '@proton/calendar/calendarBootstrap/hooks';
3 import CalendarSelect from '@proton/components/components/calendarSelect/CalendarSelect';
4 import type { SelectTwoProps } from '@proton/components/components/selectTwo/SelectTwo';
5 import { useLoading } from '@proton/hooks';
6 import { notificationsToModel } from '@proton/shared/lib/calendar/alarms/notificationsToModel';
7 import type { CalendarMember, EventModel } from '@proton/shared/lib/interfaces/calendar';
9 import { getIsAvailableCalendar } from '../../../helpers/event';
10 import { getInitialMemberModel, getOrganizerAndSelfAddressModel } from '../eventForm/state';
12 export interface Props extends Omit<SelectTwoProps<string>, 'children'> {
14 setModel: (value: EventModel) => void;
15 isCreateEvent: boolean;
17 isColorPerEventEnabled: boolean;
20 const CreateEventCalendarSelect = ({
25 isColorPerEventEnabled,
28 const [loading, withLoading] = useLoading();
29 const getCalendarBootstrap = useGetCalendarBootstrap();
30 const getAddresses = useGetAddresses();
35 calendar: { id: calendarID },
38 const options = calendars
40 ({ value: id, isOwned, isWritable }) =>
42 getIsAvailableCalendar({
43 isOwnedCalendar: isOwned,
44 isCalendarWritable: isWritable,
45 isInvitation: !!organizer,
48 .map(({ value, text, color, permissions, isOwned, isSubscribed, isWritable, isUnknown }) => ({
58 const { name } = options.find(({ id }) => id === calendarID) || options[0];
62 <div className="py-2 flex w-full">
63 <span className="text-ellipsis" title={name}>
70 const handleChangeCalendar = async (newId: string) => {
71 const { color, permissions, isOwned, isSubscribed, isWritable, isUnknown } =
72 options.find(({ id }) => id === newId) || options[0];
74 // grab members and default settings for the new calendar
78 DefaultEventDuration: defaultEventDuration,
79 DefaultPartDayNotifications,
80 DefaultFullDayNotifications,
82 } = await getCalendarBootstrap(newId);
83 const addresses = await getAddresses();
85 const [Member = { ID: '', Email: '' } as CalendarMember] = Members;
86 const memberEmail = Member.Email;
87 const address = addresses.find(({ Email }) => Email === memberEmail);
88 if (!memberEmail || !address) {
89 throw new Error('Address does not exist');
91 const newDefaultPartDayNotifications = notificationsToModel(DefaultPartDayNotifications, false);
92 const newDefaultFullDayNotifications = notificationsToModel(DefaultFullDayNotifications, true);
94 const partDayNotifications = model.hasPartDayDefaultNotifications
95 ? newDefaultPartDayNotifications
96 : model.partDayNotifications;
98 const fullDayNotifications = model.hasFullDayDefaultNotifications
99 ? newDefaultFullDayNotifications
100 : model.fullDayNotifications;
104 calendar: { id: newId, color, permissions, isOwned, isSubscribed, isWritable, isUnknown },
105 ...getInitialMemberModel(addresses, Members, Member, address),
106 ...getOrganizerAndSelfAddressModel({
107 attendees: model.attendees,
109 addressID: address.ID,
110 isAttendee: model.isAttendee,
112 defaultEventDuration,
113 partDayNotifications,
114 fullDayNotifications,
120 calendarID={calendarID}
121 displayColor={isColorPerEventEnabled ? false : options.length > 1}
123 onChange={({ value }) => withLoading(handleChangeCalendar(value))}
130 export default CreateEventCalendarSelect;