Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / redux-shared-store / settingsHeartbeat / mailSettingsHeartbeatListener.ts
blob5afe6ad51a839fb3529d48a13fb04c0b40a11a11
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';
10 import {
11     AUTO_DELETE_SPAM_AND_TRASH_DAYS,
12     COMPOSER_MODE,
13     CONFIRM_LINK,
14     DIRECTION,
15     HIDE_SENDER_IMAGES,
16     MESSAGE_BUTTONS,
17     SHOW_IMAGES,
18     SPAM_ACTION,
19     VIEW_LAYOUT,
20     VIEW_MODE,
21 } from '@proton/shared/lib/mail/mailSettings';
22 import { PROTON_DEFAULT_THEME_SETTINGS, PROTON_THEMES_MAP } from '@proton/shared/lib/themes/themes';
24 import {
25     formatBooleanForHeartbeat,
26     getThemeMode,
27     saveHeartbeatTimestamp,
28     shouldSendHeartBeat,
29 } from './heartbeatHelper';
30 import type { RequiredState } from './interface';
31 import {
32     getAddressRange,
33     getArrayLengthRange,
34     getDelaySecond,
35     getFontFace,
36     getFontSize,
37     getNextMessageOnMove,
38     getPGPScheme,
39     getPageSize,
40     getSwipeAction,
41     imageProxy,
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) => {
48     startListening({
49         actionCreator: bootstrapEvent,
50         effect: async (_, listenerApi) => {
51             if (!shouldSendHeartBeat(mailHeartbeatKey)) {
52                 return;
53             }
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()),
62             ]);
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({
70                 user,
71                 subscription,
72                 userSettings,
73                 api: listenerApi.extra.api,
74                 measurementGroup: TelemetryMeasurementGroups.settingsHeartBeat,
75                 event: TelemetryMailHeartbeatEvents.mail_heartbeat,
76                 dimensions: {
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',
115                     messageButton:
116                         mailSettings.MessageButtons === MESSAGE_BUTTONS.READ_UNREAD ? 'read-unread' : 'unread-read',
117                     autodeleteTrashDays:
118                         mailSettings.AutoDeleteSpamAndTrashDays === AUTO_DELETE_SPAM_AND_TRASH_DAYS.ACTIVE
119                             ? 'true'
120                             : 'false',
121                 },
122             });
124             saveHeartbeatTimestamp(mailHeartbeatKey);
125         },
126     });