Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / store / events / eventsSelectors.ts
blobeda56ad9aa81c370603e723b8eb574673d597411
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) => ({
18         ...event,
19         data: {
20             ...event.data,
21             eventReadResult: getEventReadResult(event.uniqueId),
22             calendarData: visualCalendarsMap.get(event.data.eventData!.CalendarID) || ({} as VisualCalendar),
23         },
24     }));
27 export const visualEventsSelector = createSelector([selectEvents, selectCalendarsWithMembers], (events, calendars) => {
28     return mapEventsWithCalendars(
29         events.filter((event) => !event.isDeleted),
30         calendars
31     );
32 });
34 export const eventsSelector = createSelector([selectEvents, selectCalendarsWithMembers], (events, calendars) => {
35     return mapEventsWithCalendars(events, calendars);
36 });
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);
46             }
47             return acc;
48         }, []);
50         if (tmpEventSaving) {
51             uniqueIds.push(TMP_UNIQUE_ID);
52         }
54         return uniqueIds;
55     }