Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / reducers / popup.ts
bloba0890b8f1307604c2e688a183aa84d09d6e9d91d
1 import type { Action, Reducer } from 'redux';
3 import { popupTabStateGarbageCollect, popupTabStateSave } from '@proton/pass/store/actions/creators/popup';
4 import type { ItemFilters, MaybeNull, SelectedItem, TabId } from '@proton/pass/types';
5 import { objectDelete } from '@proton/pass/utils/object/delete';
6 import { merge } from '@proton/pass/utils/object/merge';
8 export type PopupTabState = {
9     tabId: TabId;
10     domain: MaybeNull<string>;
11     search: MaybeNull<string>;
12     selectedItem: MaybeNull<SelectedItem>;
15 export type PopupState = {
16     tabs: { [tabId: TabId]: PopupTabState };
17     filters: MaybeNull<ItemFilters>;
20 const getInitialState = (): PopupState => ({ tabs: {}, filters: null });
22 const popupReducer: Reducer<PopupState> = (state = getInitialState(), action: Action) => {
23     if (popupTabStateSave.match(action)) {
24         return merge(state, {
25             filters: action.payload.filters ?? state.filters,
26             tabs: {
27                 [action.payload.tabId]: action.payload,
28             },
29         });
30     }
32     if (popupTabStateGarbageCollect.match(action)) {
33         return {
34             ...state,
35             tabs: action.payload.tabIds.reduce<PopupState['tabs']>(
36                 (acc, tabId) => objectDelete(acc, tabId),
37                 state.tabs
38             ),
39         };
40     }
42     return state;
45 export default popupReducer;