Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / store / reducers / pw-history.ts
blob7dffea3d0e67b9a12478a19aaa8f6d8f86720018
1 import type { Reducer } from 'redux';
3 import { MAX_PASSWORD_HISTORY_RETENTION_WEEKS } from '@proton/pass/constants';
4 import {
5     passwordDelete,
6     passwordHistoryClear,
7     passwordHistoryGarbageCollect,
8     passwordSave,
9 } from '@proton/pass/store/actions/creators/password';
10 import type { MaybeNull } from '@proton/pass/types';
11 import { UNIX_WEEK } from '@proton/pass/utils/time/constants';
12 import { getEpoch } from '@proton/pass/utils/time/epoch';
14 export type PasswordHistoryEntry = {
15     id: string;
16     value: string;
17     origin: MaybeNull<string>;
18     createTime: number;
21 export type PasswordItem = Omit<PasswordHistoryEntry, 'createTime' | 'id'>;
23 export type PasswordHistoryState = PasswordHistoryEntry[];
25 const reducer: Reducer<PasswordHistoryState> = (state = [], action) => {
26     if (passwordSave.match(action)) return [action.payload, ...state];
27     if (passwordDelete.match(action)) return state.filter(({ id }) => id !== action.payload.id);
28     if (passwordHistoryClear.match(action)) return [];
30     if (passwordHistoryGarbageCollect.match(action)) {
31         const limit = getEpoch() - UNIX_WEEK * MAX_PASSWORD_HISTORY_RETENTION_WEEKS;
32         return state.filter(({ createTime }) => createTime > limit);
33     }
35     return state;
38 export default reducer;