1 import { c } from 'ttag';
3 import type { FetchedBreaches } from '@proton/components';
4 import { isMonitored } from '@proton/pass/lib/items/item.predicates';
5 import { getAddressId } from '@proton/pass/lib/monitor/monitor.utils';
14 } from '@proton/pass/lib/monitor/types';
15 import { withCache } from '@proton/pass/store/actions/enhancers/cache';
16 import { withNotification } from '@proton/pass/store/actions/enhancers/notification';
18 addCustomAddressRequest,
22 deleteCustomAddressRequest,
23 itemUpdateFlagsRequest,
25 resendCustomAddressCodeRequest,
26 resolveAddressMonitorRequest,
27 sentinelToggleRequest,
28 toggleAddressMonitorRequest,
30 verifyCustomAddressRequest,
31 } from '@proton/pass/store/actions/requests';
32 import { requestActionsFactory } from '@proton/pass/store/request/flow';
33 import type { ItemRevision, SelectedItem } from '@proton/pass/types';
35 BreachCustomEmailGetResponse,
37 UpdateUserMonitorStateRequest,
38 } from '@proton/pass/types/api/pass';
39 import { pipe } from '@proton/pass/utils/fp/pipe';
40 import { UNIX_MINUTE } from '@proton/pass/utils/time/constants';
41 import { getApiError } from '@proton/shared/lib/api/helpers/apiErrorHelper';
42 import { PROTON_SENTINEL_NAME } from '@proton/shared/lib/constants';
43 import type { SETTINGS_PROTON_SENTINEL_STATE } from '@proton/shared/lib/interfaces';
45 export type SentinelState = SETTINGS_PROTON_SENTINEL_STATE;
47 export const sentinelToggle = requestActionsFactory<SentinelState, SentinelState>('monitor::sentinel::toggle')({
48 requestId: sentinelToggleRequest,
54 ? c('Info').t`${PROTON_SENTINEL_NAME} successfully enabled`
55 : c('Info').t`${PROTON_SENTINEL_NAME} successfully disabled`,
56 })({ payload: { value } }),
62 text: c('Error').t`Failed updating ${PROTON_SENTINEL_NAME} setting`,
68 export const monitorToggle = requestActionsFactory<UpdateUserMonitorStateRequest, UpdateUserMonitorStateRequest>(
69 'monitor::all:addresses:toggle'
71 requestId: toggleMonitorRequest,
73 prepare: ({ ProtonAddress, Aliases }) =>
78 text: c('Info').t`Monitoring settings successfully updated`,
80 )({ payload: { ProtonAddress, Aliases } }),
86 text: c('Error').t`Failed to update monitoring settings`,
92 export const getBreaches = requestActionsFactory<void, BreachesGetResponse>('monitor::breaches::get')({
93 requestId: breachesRequest,
94 success: { config: { maxAge: UNIX_MINUTE } },
97 export const getProtonBreach = requestActionsFactory<ProtonAddressID, FetchedBreaches[]>(
98 'monitor::breaches::proton::get'
100 requestId: protonBreachRequest,
101 success: { config: { data: true } },
105 text: c('Error').t`Failed to load breaches for this address`,
108 })({ payload: null }),
112 export const getCustomBreach = requestActionsFactory<CustomAddressID, FetchedBreaches[]>(
113 'monitor::breaches::custom::get'
115 requestId: customBreachRequest,
116 success: { config: { data: true } },
120 text: c('Error').t`Failed to load breaches for this address`,
123 })({ payload: null }),
127 export const getAliasBreach = requestActionsFactory<SelectedItem, FetchedBreaches[]>('monitor::breaches::alias::get')({
128 requestId: ({ shareId, itemId }) => aliasBreachRequest(shareId, itemId),
129 success: { config: { data: true } },
133 text: c('Error').t`Failed to load breaches for this email alias`,
136 })({ payload: null }),
140 export const addCustomAddress = requestActionsFactory<string, BreachCustomEmailGetResponse>(
141 'monitor::breaches::custom::add'
143 requestId: addCustomAddressRequest,
144 success: { config: { data: true } },
148 text: c('Error').t`Failed to add email address`,
151 })({ payload: null }),
155 export const deleteCustomAddress = requestActionsFactory<CustomAddressID, CustomAddressID>(
156 'monitor::breaches::custom::delete'
158 requestId: deleteCustomAddressRequest,
160 prepare: (addressId) =>
162 text: c('Info').t`Email address successfully deleted from monitoring`,
164 })({ payload: addressId }),
169 text: c('Error').t`Failed to delete email address from monitoring`,
172 })({ payload: null }),
176 export const verifyCustomAddress = requestActionsFactory<MonitorVerifyDTO, MonitorAddress<AddressType.CUSTOM>>(
177 'monitor::breaches::custom::verify'
179 requestId: ({ addressId }) => verifyCustomAddressRequest(addressId),
181 config: { data: true },
184 text: c('Error').t`Failed to verify email address`,
187 })({ payload: getApiError(error) }),
191 export const toggleAddressMonitor = requestActionsFactory<MonitorToggleDTO, MonitorAddress>(
192 'monitor::breaches::address::toggle'
194 requestId: (dto) => toggleAddressMonitorRequest(getAddressId(dto)),
196 prepare: (payload) =>
199 text: payload.monitored
200 ? c('Info').t`Email address successfully included in monitoring`
201 : c('Info').t`Email address successfully excluded from monitoring`,
208 text: c('Error').t`Failed updating monitoring status for this email address`,
214 export const resolveAddressMonitor = requestActionsFactory<AddressBreachDTO, AddressBreachDTO>(
215 'monitor::breaches::address::resolve'
217 requestId: (dto) => resolveAddressMonitorRequest(getAddressId(dto)),
219 prepare: (payload) =>
222 text: c('Info').t`All breaches for this address were resolved`,
228 text: c('Error').t`Failed to resolve breaches for this address`,
231 })({ payload: null }),
235 export const setItemFlags = requestActionsFactory<
236 SelectedItem & { SkipHealthCheck: boolean },
237 SelectedItem & { item: ItemRevision }
238 >('monitor::toggle::item')({
239 requestId: ({ shareId, itemId }) => itemUpdateFlagsRequest(shareId, itemId),
241 prepare: ({ shareId, itemId, item }) =>
244 text: isMonitored(item)
245 ? c('Info').t`Item successfully included in monitoring`
246 : c('Info').t`Item successfully excluded from monitoring`,
247 })({ payload: { shareId, itemId, item } }),
253 text: c('Error').t`Failed updating monitor flag of the item`,
259 export const resendVerificationCode = requestActionsFactory<CustomAddressID, boolean>(
260 'monitor::breaches::custom::resend::verification'
262 requestId: resendCustomAddressCodeRequest,
266 text: c('Error').t`Failed to resend verification for custom email`,
269 })({ payload: null }),