1 import type { TypedStartListening } from '@reduxjs/toolkit';
2 import type { ProtonDispatch, ProtonThunkArguments } from 'packages/redux-shared-store-types';
4 import { addressesThunk, subscriptionThunk, userSettingsThunk, userThunk } from '@proton/account';
5 import { bootstrapEvent } from '@proton/account/bootstrap/action';
6 import { categoriesThunk, mailSettingsThunk } from '@proton/mail';
7 import { TelemetryMailHeartbeatEvents, TelemetryMeasurementGroups } from '@proton/shared/lib/api/telemetry';
8 import { LABEL_TYPE } from '@proton/shared/lib/constants';
9 import { sendTelemetryReportWithBaseDimensions } from '@proton/shared/lib/helpers/metrics';
11 AUTO_DELETE_SPAM_AND_TRASH_DAYS,
21 } from '@proton/shared/lib/mail/mailSettings';
22 import { PROTON_DEFAULT_THEME_SETTINGS, PROTON_THEMES_MAP } from '@proton/shared/lib/themes/themes';
25 formatBooleanForHeartbeat,
27 saveHeartbeatTimestamp,
29 } from './heartbeatHelper';
30 import type { RequiredState } from './interface';
42 } from './mailHeartbeatHelper';
44 type AppStartListening = TypedStartListening<RequiredState, ProtonDispatch<any>, ProtonThunkArguments>;
46 const mailHeartbeatKey = 'mail-heartbeat-timestamp' as const;
47 export const mailSettingsHeartbeatListener = (startListening: AppStartListening) => {
49 actionCreator: bootstrapEvent,
50 effect: async (_, listenerApi) => {
51 if (!shouldSendHeartBeat(mailHeartbeatKey)) {
55 const [mailSettings, addresses, categories, user, userSettings, subscription] = await Promise.all([
56 listenerApi.dispatch(mailSettingsThunk()),
57 listenerApi.dispatch(addressesThunk()),
58 listenerApi.dispatch(categoriesThunk()),
59 listenerApi.dispatch(userThunk()),
60 listenerApi.dispatch(userSettingsThunk()),
61 listenerApi.dispatch(subscriptionThunk()),
64 const folders = categories.filter((category) => category.Type === LABEL_TYPE.MESSAGE_FOLDER);
65 const labels = categories.filter((category) => category.Type === LABEL_TYPE.MESSAGE_LABEL);
67 const { DarkTheme, LightTheme, Mode } = userSettings.Theme || PROTON_DEFAULT_THEME_SETTINGS;
69 void sendTelemetryReportWithBaseDimensions({
73 api: listenerApi.extra.api,
74 measurementGroup: TelemetryMeasurementGroups.settingsHeartBeat,
75 event: TelemetryMailHeartbeatEvents.mail_heartbeat,
77 lightThemeName: PROTON_THEMES_MAP[LightTheme].label,
78 darkThemeName: PROTON_THEMES_MAP[DarkTheme].label,
79 themeMode: getThemeMode(Mode),
80 sign: formatBooleanForHeartbeat(mailSettings.Sign),
81 keyTransparency: formatBooleanForHeartbeat(mailSettings.KT),
82 shortcuts: formatBooleanForHeartbeat(mailSettings.Shortcuts),
83 promptPin: formatBooleanForHeartbeat(mailSettings.PromptPin),
84 pmSignature: formatBooleanForHeartbeat(mailSettings.PMSignature),
85 stickyLabels: formatBooleanForHeartbeat(mailSettings.StickyLabels),
86 unreadFavicon: formatBooleanForHeartbeat(mailSettings.UnreadFavicon),
87 almostAllMail: formatBooleanForHeartbeat(mailSettings.AlmostAllMail),
88 folderColors: formatBooleanForHeartbeat(mailSettings.EnableFolderColor),
89 attachPublicKey: formatBooleanForHeartbeat(mailSettings.AttachPublicKey),
90 autoSaveContacts: formatBooleanForHeartbeat(mailSettings.AutoSaveContacts),
91 removeImageMetadata: formatBooleanForHeartbeat(mailSettings.RemoveImageMetadata),
92 pmSignatureReferral: formatBooleanForHeartbeat(mailSettings.PMSignatureReferralLink),
93 inheritParentFolderColor: formatBooleanForHeartbeat(mailSettings.InheritParentFolderColor),
94 labelsCount: getArrayLengthRange(labels),
95 foldersCount: getArrayLengthRange(folders),
96 addressCount: getAddressRange(addresses),
97 pageSize: getPageSize(mailSettings.PageSize),
98 fontFace: getFontFace(mailSettings.FontFace),
99 fontSize: getFontSize(mailSettings.FontSize),
100 imageProxy: imageProxy(mailSettings.ImageProxy),
101 pgpScheme: getPGPScheme(mailSettings.PGPScheme),
102 swipeLeft: getSwipeAction(mailSettings.SwipeLeft),
103 swipeRight: getSwipeAction(mailSettings.SwipeRight),
104 delaySendSeconds: getDelaySecond(mailSettings.DelaySendSeconds),
105 nextMessageOnMove: getNextMessageOnMove(mailSettings.NextMessageOnMove),
106 viewMode: mailSettings.ViewMode === VIEW_MODE.GROUP ? 'group' : 'single',
107 viewLayout: mailSettings.ViewLayout === VIEW_LAYOUT.ROW ? 'row' : 'colum',
108 confirmLink: mailSettings.ConfirmLink === CONFIRM_LINK.CONFIRM ? 'true' : 'false',
109 rightToLeft: mailSettings.RightToLeft === DIRECTION.RIGHT_TO_LEFT ? 'true' : 'false',
110 hideRemoteImages: mailSettings.HideRemoteImages === SHOW_IMAGES.HIDE ? 'hide' : 'show',
111 hideEmbeedImages: mailSettings.HideEmbeddedImages === SHOW_IMAGES.HIDE ? 'hide' : 'show',
112 composerMode: mailSettings.ComposerMode === COMPOSER_MODE.MAXIMIZED ? 'maximized' : 'popup',
113 hideSenderImage: mailSettings.HideSenderImages === HIDE_SENDER_IMAGES.HIDE ? 'true' : 'false',
114 spamAction: mailSettings.SpamAction === SPAM_ACTION.JustSpam ? 'just-spam' : 'spam-and-unsub',
116 mailSettings.MessageButtons === MESSAGE_BUTTONS.READ_UNREAD ? 'read-unread' : 'unread-read',
118 mailSettings.AutoDeleteSpamAndTrashDays === AUTO_DELETE_SPAM_AND_TRASH_DAYS.ACTIVE
124 saveHeartbeatTimestamp(mailHeartbeatKey);