Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useInboxDesktopBadgeCount.ts
blob20b11ffc5a5d69e2b18d12ca552902e8a9b009a6
1 import { useEffect } from 'react';
3 import { useConversationCounts } from '@proton/mail/counts/conversationCounts';
4 import { useMessageCounts } from '@proton/mail/counts/messageCounts';
5 import { useMailSettings } from '@proton/mail/mailSettings/hooks';
6 import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants';
7 import { invokeInboxDesktopIPC } from '@proton/shared/lib/desktop/ipcHelpers';
8 import { isElectronMail } from '@proton/shared/lib/helpers/desktop';
9 import { captureMessage } from '@proton/shared/lib/helpers/sentry';
10 import { VIEW_MODE } from '@proton/shared/lib/mail/mailSettings';
12 const useInboxDesktopBadgeCount = () => {
13     const [mailSettings] = useMailSettings();
14     const [conversationCounts] = useConversationCounts();
15     const [messageCounts] = useMessageCounts();
16     const counts = mailSettings?.ViewMode === VIEW_MODE.GROUP ? conversationCounts : messageCounts;
18     // Updates the notification badge on the desktop app icon depending on the unread count
19     useEffect(() => {
20         if (!isElectronMail) {
21             return;
22         }
24         const inboxConvCount = counts?.find(({ LabelID }) => LabelID === MAILBOX_LABEL_IDS.INBOX);
25         let payload = inboxConvCount?.Unread;
27         if (payload === undefined) {
28             captureMessage('Invalid undefined unread count', {
29                 level: 'error',
30                 extra: { inboxConvCount, payload },
31             });
33             payload = 0;
34         } else if (payload < 0) {
35             captureMessage('Invalid negative unread count', {
36                 level: 'error',
37                 extra: { inboxConvCount, payload },
38             });
40             payload = 0;
41         }
43         invokeInboxDesktopIPC({
44             type: 'updateNotification',
45             payload,
46         });
47     }, [counts]);
50 export default useInboxDesktopBadgeCount;