1 import { createSelector } from '@reduxjs/toolkit';
3 import { selectCalendarsWithMembers } from '@proton/calendar';
4 import { getVisualCalendars } from '@proton/shared/lib/calendar/calendar';
5 import { TMP_UNIQUE_ID } from '@proton/shared/lib/calendar/constants';
6 import type { CalendarWithOwnMembers, VisualCalendar } from '@proton/shared/lib/interfaces/calendar';
8 import type { CalendarState } from '../store';
9 import { getEventReadResult } from './eventsCache';
10 import { type CalendarViewEventStore, eventsSliceName } from './eventsSlice';
12 const selectEvents = (state: CalendarState) => state[eventsSliceName].events;
13 const selectIsTmpEventSaving = (state: CalendarState) => state[eventsSliceName].isTmpEventSaving;
15 const mapEventsWithCalendars = (events: CalendarViewEventStore[], calendars?: CalendarWithOwnMembers[]) => {
16 const visualCalendarsMap = new Map(getVisualCalendars(calendars || []).map((calendar) => [calendar.ID, calendar]));
17 return events.map((event) => ({
21 eventReadResult: getEventReadResult(event.uniqueId),
22 calendarData: visualCalendarsMap.get(event.data.eventData!.CalendarID) || ({} as VisualCalendar),
27 export const visualEventsSelector = createSelector([selectEvents, selectCalendarsWithMembers], (events, calendars) => {
28 return mapEventsWithCalendars(
29 events.filter((event) => !event.isDeleted),
34 export const eventsSelector = createSelector([selectEvents, selectCalendarsWithMembers], (events, calendars) => {
35 return mapEventsWithCalendars(events, calendars);
38 export const isTmpEventSavingSelector = createSelector([selectIsTmpEventSaving], (tmpEventSaving) => tmpEventSaving);
40 export const pendingUniqueIdsSelector = createSelector(
41 [selectIsTmpEventSaving, selectEvents],
42 (tmpEventSaving, events) => {
43 const uniqueIds = events.reduce<string[]>((acc, event) => {
44 if (event.isDeleting || event.isSaving) {
45 acc.push(event.uniqueId);
51 uniqueIds.push(TMP_UNIQUE_ID);