1 import { c } from 'ttag';
3 import { CALENDAR_APP_NAME, EVENT_ACTIONS, HOUR } from '../../constants';
4 import type { Calendar, SubscribedCalendar } from '../../interfaces/calendar';
5 import { CALENDAR_SUBSCRIPTION_STATUS } from '../../interfaces/calendar';
7 CalendarSubscriptionEventManager,
8 CalendarSubscriptionEventManagerCreate,
9 CalendarSubscriptionEventManagerDelete,
10 CalendarSubscriptionEventManagerUpdate,
11 } from '../../interfaces/calendar/EventManager';
16 ICS_SIZE_EXCEED_LIMIT,
18 HTTP_REQUEST_FAILED_BAD_REQUEST,
19 HTTP_REQUEST_FAILED_UNAUTHORIZED,
20 HTTP_REQUEST_FAILED_FORBIDDEN,
21 HTTP_REQUEST_FAILED_NOT_FOUND,
22 HTTP_REQUEST_FAILED_GENERIC,
23 HTTP_REQUEST_FAILED_INTERNAL_SERVER_ERROR,
24 HTTP_REQUEST_FAILED_TIMEOUT,
25 INTERNAL_CALENDAR_URL_NOT_FOUND,
26 INTERNAL_CALENDAR_UNDECRYPTABLE,
27 } = CALENDAR_SUBSCRIPTION_STATUS;
29 export const getIsCalendarSubscriptionEventManagerDelete = (
30 event: CalendarSubscriptionEventManager
31 ): event is CalendarSubscriptionEventManagerDelete => {
32 return event.Action === EVENT_ACTIONS.DELETE;
34 export const getIsCalendarSubscriptionEventManagerCreate = (
35 event: CalendarSubscriptionEventManager
36 ): event is CalendarSubscriptionEventManagerCreate => {
37 return event.Action === EVENT_ACTIONS.CREATE;
39 export const getIsCalendarSubscriptionEventManagerUpdate = (
40 event: CalendarSubscriptionEventManager
41 ): event is CalendarSubscriptionEventManagerUpdate => {
42 return event.Action === EVENT_ACTIONS.UPDATE;
45 export const getCalendarHasSubscriptionParameters = (
46 calendar: Calendar | SubscribedCalendar
47 ): calendar is SubscribedCalendar => {
48 return !!(calendar as SubscribedCalendar).SubscriptionParameters;
51 export const getSyncingInfo = (text: string, longText = '') => ({
52 label: c('Calendar status').t`Syncing`,
54 longText: longText ? longText : `${text}.`,
58 export const getNotSyncedInfo = (text: string, longText = '') => ({
59 label: c('Calendar status').t`Not synced`,
61 longText: longText ? longText : `${text}.`,
65 export const getCalendarStatusInfo = (status: CALENDAR_SUBSCRIPTION_STATUS) => {
70 if (status === INVALID_ICS) {
71 return getNotSyncedInfo(c('Calendar subscription not synced error').t`Unsupported calendar format`);
74 if (status === ICS_SIZE_EXCEED_LIMIT) {
75 return getNotSyncedInfo(c('Calendar subscription not synced error').t`Calendar is too big`);
78 if (status === SYNCHRONIZING) {
79 return getSyncingInfo(
80 c('Calendar subscription not synced error').t`Calendar is syncing`,
81 c('Calendar subscription not synced error')
82 .t`Calendar is syncing: it may take several minutes for all of its events to show up.`
88 HTTP_REQUEST_FAILED_BAD_REQUEST,
89 HTTP_REQUEST_FAILED_UNAUTHORIZED,
90 HTTP_REQUEST_FAILED_FORBIDDEN,
91 HTTP_REQUEST_FAILED_NOT_FOUND,
92 INTERNAL_CALENDAR_URL_NOT_FOUND,
95 return getNotSyncedInfo(
96 c('Calendar subscription not synced error').t`Calendar link is not accessible`,
97 c('Calendar subscription not synced error; long version')
98 .t`Calendar link is not accessible from outside the calendar provider's ecosystem.`
103 [HTTP_REQUEST_FAILED_GENERIC, HTTP_REQUEST_FAILED_INTERNAL_SERVER_ERROR, HTTP_REQUEST_FAILED_TIMEOUT].includes(
107 return getNotSyncedInfo(
108 c('Calendar subscription not synced error').t`Calendar link is temporarily inaccessible`,
109 c('Calendar subscription not synced error; long version')
110 .t`Calendar link is temporarily inaccessible. Please verify that the link from the calendar provider is still valid.`
114 if (status === INTERNAL_CALENDAR_UNDECRYPTABLE) {
115 return getNotSyncedInfo(c('Calendar subscription not synced error').t`Calendar could not be decrypted`);
118 return getNotSyncedInfo(c('Calendar subscription not synced error').t`Failed to sync calendar`);
121 export const getCalendarIsNotSyncedInfo = (calendar: SubscribedCalendar) => {
122 const { Status, LastUpdateTime } = calendar.SubscriptionParameters;
124 if (LastUpdateTime === 0) {
125 return getSyncingInfo(
126 c('Calendar subscription not synced error').t`Calendar is syncing`,
127 c('Calendar subscription not synced error')
128 .t`Calendar is syncing: it may take several minutes for all of its events to show up.`
132 if (Date.now() - LastUpdateTime * 1000 > 12 * HOUR) {
133 return getNotSyncedInfo(
134 c('Calendar subscription not synced error').t`More than 12 hours passed since last update`,
135 c('Calendar subscription not synced error; long version')
136 .t`More than 12 hours passed since last update — ${CALENDAR_APP_NAME} will try to update the calendar in a few hours.`
140 return getCalendarStatusInfo(Status);