Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / wallet / store / slices / hideAmounts.ts
blobfc234ea33f94887abd46a0123487b71745bf7044
1 import { createAction, createSlice } from '@reduxjs/toolkit';
3 import { type ModelState, getInitialModelState } from '@proton/account';
4 import { getItem, setItem } from '@proton/shared/lib/helpers/storage';
6 const name = 'hide_amounts' as const;
8 export interface HideAmountsState {
9     [name]: ModelState<boolean>;
12 type SliceState = HideAmountsState[typeof name];
13 type Model = NonNullable<SliceState['value']>;
15 export const selectHideAmounts = (state: HideAmountsState) => state[name];
17 const HIDE_AMOUNTS_STORAGE_KEY = 'pw-hide-amounts';
18 const initStorageItem = getItem(HIDE_AMOUNTS_STORAGE_KEY);
20 const initialState = getInitialModelState<Model>(initStorageItem === 'hidden');
22 export const toggleHideAmounts = createAction('toggle-hide-amount');
24 const slice = createSlice({
25     name,
26     initialState,
27     reducers: {},
28     extraReducers: (builder) => {
29         builder.addCase(toggleHideAmounts, (state) => {
30             if (state) {
31                 state.value = !state.value;
32                 setItem(HIDE_AMOUNTS_STORAGE_KEY, state.value ? 'hidden' : 'visible');
33             }
34         });
35     },
36 });
38 export const hideAmountsReducer = { [name]: slice.reducer };