1 import { z } from 'zod';
3 import type { Environment } from '@proton/shared/lib/interfaces';
5 import type { ColorScheme, ThemeSetting } from '../themes/themes';
6 import { type DefaultProtocol, zDefaultProtocol } from './DefaultProtocol';
7 import type { AppVersion, DesktopVersion } from './DesktopVersion';
9 export type CHANGE_VIEW_TARGET = 'mail' | 'calendar' | 'account';
10 export type ElectronNotification = {
13 app: CHANGE_VIEW_TARGET;
18 export type ESUserChoice = boolean | null;
20 // This type must be updated in the Electron application as well
21 export type IPCInboxDesktopFeature =
26 | 'LatestVersionCheck'
32 export type IPCInboxGetInfoMessage =
33 | { type: 'theme'; result: ThemeSetting }
34 | { type: 'latestVersion'; result: DesktopVersion | null }
35 | { type: 'installSource'; result: string | null }
36 | { type: 'defaultMailto'; result: DefaultProtocol }
37 | { type: 'colorScheme'; result: ColorScheme }
38 | { type: 'getAllAppVersions'; result: string };
39 export type IPCInboxGetUserInfoMessage = { type: 'esUserChoice'; result: ESUserChoice };
40 export type IPCInboxClientUpdateMessage =
41 | { type: 'updateNotification'; payload: number }
42 | { type: 'userLogin'; payload?: undefined }
43 | { type: 'userLogout'; payload?: undefined }
44 | { type: 'clearAppData'; payload?: undefined }
45 | { type: 'oauthPopupOpened'; payload: 'oauthPopupStarted' | 'oauthPopupFinished' }
46 | { type: 'subscriptionModalOpened'; payload: 'subscriptionModalStarted' | 'subscriptionModalFinished' }
47 | { type: 'openExternal'; payload: string }
48 | { type: 'changeView'; payload: CHANGE_VIEW_TARGET }
49 | { type: 'trialEnd'; payload?: 'trialEnded' | 'resetTrialEnded' | undefined }
50 | { type: 'showNotification'; payload: ElectronNotification }
51 | { type: 'updateLocale'; payload: string }
52 | { type: 'setTheme'; payload: ThemeSetting }
53 | { type: 'earlyAccess'; payload: Environment | undefined }
54 | { type: 'checkDefaultMailtoAndSignal'; payload?: undefined }
55 | { type: 'defaultMailtoTelemetryReported'; payload: number }
56 | { type: 'setESUserChoice'; payload: { userID: string; userChoice: boolean } }
57 | { type: 'storeAppVersion'; payload: AppVersion }
58 | { type: 'triggerCrash'; payload?: undefined };
59 export type IPCInboxClientUpdateMessageType = IPCInboxClientUpdateMessage['type'];
61 export const IPCInboxHostUpdateMessageSchema = z.union([
63 type: z.literal('captureMessage'),
66 level: z.union([z.literal('error'), z.literal('warning')]),
67 tags: z.record(z.union([z.string(), z.number()])),
68 extra: z.record(z.union([z.string(), z.number()])),
72 type: z.literal('defaultMailtoChecked'),
73 payload: zDefaultProtocol,
77 export type IPCInboxHostUpdateMessage = z.infer<typeof IPCInboxHostUpdateMessageSchema>;
78 export type IPCInboxHostUpdateMessageType = IPCInboxHostUpdateMessage['type'];
79 export type IPCInboxHostUpdateMessagePayload = IPCInboxHostUpdateMessage['payload'];
80 export type IPCInboxHostUpdateListener = (payload: IPCInboxHostUpdateMessagePayload) => void;
81 export type IPCInboxHostUpdateListenerRemover = { removeListener: () => void };
83 // THIS NEEDS REFACTOR: inda-refactor-001
84 // The orininal idea for `on` function was that this type will be defined by
85 // generic T extends IPCInboxHostUpdateMessageType, but it makes it messy
86 // because generic doesn't pick one value but also union of all possible types
87 // and therefore payload and callback need to accpet all.
88 export type IPCInboxHostUpdateListenerAdder = (
89 eventType: IPCInboxHostUpdateMessageType,
90 callback: IPCInboxHostUpdateListener
91 ) => IPCInboxHostUpdateListenerRemover;
94 * Electron injects an object in the window object
95 * This object can then be used to communicate from the web app to the desktop app
96 * This type can be used in any file that needs to communicate with the desktop app.
98 * The object can be injected when used in specific clients to avoid adding it globally
100 export type IPCInboxMessageBroker = {
101 hasFeature?: (feature: IPCInboxDesktopFeature) => boolean;
102 getInfo?: <T extends IPCInboxGetInfoMessage['type']>(
104 ) => Extract<IPCInboxGetInfoMessage, { type: T }>['result'];
105 getUserInfo?: <T extends IPCInboxGetUserInfoMessage['type']>(
108 ) => Extract<IPCInboxGetUserInfoMessage, { type: T }>['result'];
109 on?: IPCInboxHostUpdateListenerAdder;
110 send?: <T extends IPCInboxClientUpdateMessageType>(
112 payload: Extract<IPCInboxClientUpdateMessage, { type: T }>['payload']
116 export type PayloadOfHostUpdateType<T extends IPCInboxHostUpdateMessageType> = Extract<
117 IPCInboxHostUpdateMessage,
121 export const END_OF_TRIAL_KEY = 'endOfTrial';
123 export interface InboxDesktopFreeTrialDates {
124 trialStartDate?: Date;
128 export interface InboxDesktopFreeTrialReminders {