Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / account / securityCheckup / slice.ts
blobffbfea393a3a180ecf48731133ef5e09df9bbb49
1 import type { PayloadAction } from '@reduxjs/toolkit';
2 import { createSlice } from '@reduxjs/toolkit';
4 import type {
5     BackLink,
6     SecurityCheckupAction,
7     SecurityCheckupSession,
8 } from '@proton/shared/lib/interfaces/securityCheckup';
9 import SecurityCheckupCohort from '@proton/shared/lib/interfaces/securityCheckup/SecurityCheckupCohort';
10 import type SecurityState from '@proton/shared/lib/interfaces/securityCheckup/SecurityState';
12 import getBackLink from './helpers/getBackLink';
13 import getSecurityCheckupRecommendations from './helpers/getSecurityCheckupRecommendations';
14 import getValidSecurityCheckupSession from './helpers/getValidSecurityCheckupSession';
16 const name = 'securityCheckup' as const;
18 interface SecurityCheckupState {
19     securityState: SecurityState;
20     cohort: SecurityCheckupCohort | undefined;
21     session: SecurityCheckupSession | undefined;
22     actions: SecurityCheckupAction[];
23     furtherActions: SecurityCheckupAction[];
24     loading: boolean;
25     backLink?: BackLink;
28 export interface SecurityCheckupReduxState {
29     [name]: SecurityCheckupState;
32 const initialState: SecurityCheckupState = {
33     securityState: {
34         phrase: {
35             isAvailable: false,
36             isSet: false,
37             isOutdated: false,
38         },
39         email: {
40             value: '',
41             isEnabled: false,
42             verified: false,
43         },
44         phone: {
45             value: '',
46             isEnabled: false,
47             verified: false,
48         },
49         deviceRecovery: {
50             isAvailable: false,
51             isEnabled: false,
52         },
53     },
54     loading: true,
55     cohort: undefined,
56     session: undefined,
57     actions: [],
58     furtherActions: [],
61 export const securityCheckupSlice = createSlice({
62     name,
63     initialState,
64     reducers: {
65         setSecurityState: (
66             state,
67             action: PayloadAction<{
68                 securityState: SecurityState;
69             }>
70         ) => {
71             const { securityState } = action.payload;
73             state.securityState = securityState;
75             const { cohort, actions, furtherActions } = getSecurityCheckupRecommendations(securityState);
77             state.cohort = cohort;
78             state.actions = actions;
79             state.furtherActions = furtherActions;
80         },
81         setLoading: (state, action: PayloadAction<{ loading: boolean }>) => {
82             const { loading } = action.payload;
84             state.loading = loading;
85         },
86         setBackLink: (state, action: PayloadAction<{ backHref: string; hostname: string }>) => {
87             const { backHref, hostname } = action.payload;
89             try {
90                 const backLink = getBackLink({
91                     backHref,
92                     hostname,
93                 });
94                 state.backLink = backLink;
95             } catch (error) {
96                 state.backLink = undefined;
97             }
98         },
99         setSession: (state, action: PayloadAction<{ session: SecurityCheckupSession | undefined }>) => {
100             const { session } = action.payload;
102             state.session = getValidSecurityCheckupSession({
103                 currentSession: session,
104                 currentCohort: state.cohort || SecurityCheckupCohort.NO_RECOVERY_METHOD,
105             });
106         },
107         clearSession: (state) => {
108             state.session = undefined;
109         },
110     },
113 export const { setBackLink } = securityCheckupSlice.actions;
115 export const selectSecurityCheckup = (state: { [name]: SecurityCheckupState }) => state[name];