Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / store / selectors / monitor.ts
blobbfffd4f7a5fb256449af38c155a7f51833cbc455
1 import { createSelector } from '@reduxjs/toolkit';
3 import { isActiveMonitored, isExcluded, itemEq } from '@proton/pass/lib/items/item.predicates';
4 import { intoSelectedItem } from '@proton/pass/lib/items/item.utils';
5 import { getDuplicatePasswords, intoAliasMonitorAddress } from '@proton/pass/lib/monitor/monitor.utils';
6 import { AddressType } from '@proton/pass/lib/monitor/types';
7 import type { State } from '@proton/pass/store/types';
8 import type { SelectedItem } from '@proton/pass/types';
9 import { first } from '@proton/pass/utils/array/first';
11 import { selectAliasItems, selectLoginItems } from './items';
13 export const selectMonitorState = (state: State) => state.monitor;
14 export const selectMonitoredLogins = createSelector(selectLoginItems, (items) => items.filter(isActiveMonitored));
15 export const selectExcludedLogins = createSelector(selectLoginItems, (items) => items.filter(isExcluded));
16 export const selectDuplicatePasswords = createSelector(selectMonitoredLogins, getDuplicatePasswords);
17 export const selectExcludedItems = createSelector(selectExcludedLogins, (items) => items.map(intoSelectedItem));
19 export const selectItemReport = (item: SelectedItem) =>
20     createSelector(selectDuplicatePasswords, (duplicates) => ({
21         password: { duplicates: duplicates.find((group) => group.some(itemEq(item))) ?? [] },
22     }));
24 export const selectCustomBreaches = createSelector(selectMonitorState, (monitor) => monitor?.custom);
25 export const selectProtonBreaches = createSelector(selectMonitorState, (monitor) => monitor?.proton);
26 export const selectMonitorPreview = createSelector(selectMonitorState, (monitor) => first(monitor?.preview ?? []));
27 export const selectTotalBreaches = createSelector(selectMonitorState, (monitor) => monitor?.total);
28 export const selectHasCustomDomains = createSelector(selectMonitorState, (monitor) => monitor?.customDomains);
30 export const selectAliasBreaches = createSelector(selectAliasItems, (items) => items.map(intoAliasMonitorAddress));
32 export const selectMonitorSettings = ({ user }: State) => user.monitor;
34 export const selectMonitorSettingByType = (type: AddressType) =>
35     createSelector(selectMonitorSettings, (settings) => {
36         if (!settings) return false;
37         switch (type) {
38             case AddressType.ALIAS:
39                 return settings.Aliases;
40             case AddressType.PROTON:
41                 return settings.ProtonAddress;
42             default:
43                 return true;
44         }
45     });