1 import { useCallback } from 'react';
3 import type { PayloadAction } from '@reduxjs/toolkit';
4 import { createSlice } from '@reduxjs/toolkit';
6 import { baseUseDispatch, baseUseSelector } from '@proton/react-redux-store';
7 import type { UserSettings } from '@proton/shared/lib/interfaces';
9 const name = 'welcomeFlags' as const;
11 export interface WelcomeFlagsState {
12 hasGenericWelcomeStep: boolean;
13 isWelcomeFlow: boolean;
18 const initialState: WelcomeFlagsState = {
19 hasGenericWelcomeStep: false,
25 export const getWelcomeFlagsValue = (userSettings: Pick<UserSettings, 'WelcomeFlag' | 'Flags'>): WelcomeFlagsState => {
26 const hasProductWelcomeStep = userSettings.WelcomeFlag === 0;
27 const hasGenericWelcomeStep = userSettings.Flags.Welcomed === 0;
28 const isWelcomeFlow = hasGenericWelcomeStep || hasProductWelcomeStep;
30 hasGenericWelcomeStep,
32 isDone: !isWelcomeFlow,
37 const slice = createSlice({
41 initial: (state, action: PayloadAction<Pick<UserSettings, 'WelcomeFlag' | 'Flags'>>) => {
42 return getWelcomeFlagsValue(action.payload);
45 return { ...state, isDone: true };
47 replayed: (state) => {
48 return { ...state, isReplay: false };
50 replaying: (state) => {
51 return { ...state, isReplay: true };
56 export const welcomeFlagsReducer = { [name]: slice.reducer };
57 export const welcomeFlagsActions = slice.actions;
58 export const selectWelcomeFlags = (state: { [name]: WelcomeFlagsState }) => state[name];
60 interface WelcomeFlagsOutput {
61 endReplay: () => void;
63 startReplay: () => void;
64 welcomeFlags: WelcomeFlagsState;
67 export const useWelcomeFlags = (): WelcomeFlagsOutput => {
68 const value = baseUseSelector(selectWelcomeFlags);
69 const dispatch = baseUseDispatch();
70 const setDone = useCallback(() => {
71 dispatch(slice.actions.done());
73 const endReplay = useCallback(() => {
74 dispatch(slice.actions.replayed());
76 const startReplay = useCallback(() => {
77 dispatch(slice.actions.replaying());