From febe2ef2f543d61ea3ddad7209e37f9918f0a4b3 Mon Sep 17 00:00:00 2001 From: Ostap Piatkovskyi <44294945+ost-ptk@users.noreply.github.com> Date: Thu, 14 Dec 2023 13:40:23 +0200 Subject: [PATCH] fix: fix contact list preserving after the wallet reset (#887) * added contact book to the wallet * added contact sort and fixed the issue with updating contact * merged recent recipient with contacts * list height fix * added reset actions for vault settings, contacts and recent recipient --------- Co-authored-by: ost-ptk --- src/background/index.ts | 10 +++++++++- src/background/redux/contacts/actions.ts | 2 ++ src/background/redux/contacts/reducer.ts | 2 ++ .../redux/recent-recipient-public-keys/actions.ts | 4 ++++ .../redux/recent-recipient-public-keys/reducer.ts | 14 ++++++++------ src/background/redux/sagas/onboarding-sagas.ts | 7 +++++++ src/background/redux/settings/actions.ts | 2 ++ src/background/redux/settings/reducer.ts | 4 +++- 8 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index b6f6db073..e62ac41ce 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -96,6 +96,7 @@ import { lastActivityTimeRefreshed } from './redux/last-activity-time/actions'; import { activeNetworkSettingChanged, activeTimeoutDurationSettingChanged, + vaultSettingsReseted, themeModeSettingChanged } from './redux/settings/actions'; import { activeOriginChanged } from './redux/active-origin/actions'; @@ -109,7 +110,10 @@ import { SiteNotConnectedError, WalletLockedError } from '@src/content/sdk-errors'; -import { recipientPublicKeyAdded } from './redux/recent-recipient-public-keys/actions'; +import { + recipientPublicKeyAdded, + recipientPublicKeyReseted +} from './redux/recent-recipient-public-keys/actions'; import { accountCasperActivityChanged, accountInfoReset, @@ -139,6 +143,7 @@ import { } from '@libs/services/validators-service'; import { contactRemoved, + contactsReseted, contactUpdated, newContactAdded } from '@background/redux/contacts/actions'; @@ -521,6 +526,7 @@ browser.runtime.onMessage.addListener( case getType(activeAccountChanged): case getType(activeTimeoutDurationSettingChanged): case getType(activeNetworkSettingChanged): + case getType(vaultSettingsReseted): case getType(themeModeSettingChanged): case getType(lastActivityTimeRefreshed): case getType(siteConnected): @@ -542,6 +548,7 @@ browser.runtime.onMessage.addListener( case getType(loginRetryCountIncremented): case getType(loginRetryLockoutTimeSet): case getType(recipientPublicKeyAdded): + case getType(recipientPublicKeyReseted): case getType(accountBalanceChanged): case getType(accountCurrencyRateChanged): case getType(accountCasperActivityChanged): @@ -566,6 +573,7 @@ browser.runtime.onMessage.addListener( case getType(contactRemoved): case getType(contactEditingPermissionChanged): case getType(contactUpdated): + case getType(contactsReseted): store.dispatch(action); return sendResponse(undefined); diff --git a/src/background/redux/contacts/actions.ts b/src/background/redux/contacts/actions.ts index 9049c237b..ae0a7ea33 100644 --- a/src/background/redux/contacts/actions.ts +++ b/src/background/redux/contacts/actions.ts @@ -11,3 +11,5 @@ export const contactRemoved = createAction('CONTACT_REMOVED')(); export const contactUpdated = createAction('CONTACT_UPDATED')(); + +export const contactsReseted = createAction('CONTACTS_RESETED')(); diff --git a/src/background/redux/contacts/reducer.ts b/src/background/redux/contacts/reducer.ts index 5f6f1c009..213a4e420 100644 --- a/src/background/redux/contacts/reducer.ts +++ b/src/background/redux/contacts/reducer.ts @@ -2,6 +2,7 @@ import { createReducer } from 'typesafe-actions'; import { contactRemoved, + contactsReseted, contactUpdated, newContactAdded } from '@background/redux/contacts/actions'; @@ -10,6 +11,7 @@ import { ContactsState } from '@background/redux/contacts/types'; const initialState = { contacts: [], lastModified: null } as ContactsState; export const reducer = createReducer(initialState) + .handleAction(contactsReseted, () => initialState) .handleAction(newContactAdded, (state, action) => { const sortedContacts = [...state.contacts, action.payload].sort((a, b) => a.name.localeCompare(b.name) diff --git a/src/background/redux/recent-recipient-public-keys/actions.ts b/src/background/redux/recent-recipient-public-keys/actions.ts index 72b9f25f0..51f412225 100644 --- a/src/background/redux/recent-recipient-public-keys/actions.ts +++ b/src/background/redux/recent-recipient-public-keys/actions.ts @@ -4,3 +4,7 @@ export const recipientPublicKeyAdded = createAction( 'RECIPIENT_PUBLIC_KEY_ADDED', (payload: string) => payload )(); + +export const recipientPublicKeyReseted = createAction( + 'RECIPIENT_PUBLIC_KEY_RESETED' +)(); diff --git a/src/background/redux/recent-recipient-public-keys/reducer.ts b/src/background/redux/recent-recipient-public-keys/reducer.ts index 3c6335a1d..ef9fc9d76 100644 --- a/src/background/redux/recent-recipient-public-keys/reducer.ts +++ b/src/background/redux/recent-recipient-public-keys/reducer.ts @@ -1,12 +1,14 @@ import { createReducer } from 'typesafe-actions'; import { RecentRecipientPublicKeysState } from './types'; -import { recipientPublicKeyAdded } from './actions'; +import { recipientPublicKeyAdded, recipientPublicKeyReseted } from './actions'; const initialState = [] as RecentRecipientPublicKeysState; -export const reducer = createReducer(initialState).handleAction( - recipientPublicKeyAdded, - // This is a hack to make sure the most recent recipient is always at the top of the list. - (state, action) => [...new Set([action.payload, ...state])] -); +export const reducer = createReducer(initialState) + .handleAction(recipientPublicKeyReseted, () => initialState) + .handleAction( + recipientPublicKeyAdded, + // This is a hack to make sure the most recent recipient is always at the top of the list. + (state, action) => [...new Set([action.payload, ...state])] + ); diff --git a/src/background/redux/sagas/onboarding-sagas.ts b/src/background/redux/sagas/onboarding-sagas.ts index 0e48c1d04..713426adb 100644 --- a/src/background/redux/sagas/onboarding-sagas.ts +++ b/src/background/redux/sagas/onboarding-sagas.ts @@ -27,6 +27,9 @@ import { initKeys, initVault, resetVault } from './actions'; import { keysReseted, keysUpdated } from '../keys/actions'; import { vaultCipherReseted } from '../vault-cipher/actions'; import { loginRetryCountReseted } from '../login-retry-count/actions'; +import { recipientPublicKeyReseted } from '@background/redux/recent-recipient-public-keys/actions'; +import { contactsReseted } from '@background/redux/contacts/actions'; +import { vaultSettingsReseted } from '@background/redux/settings/actions'; export function* onboardingSagas() { yield takeLatest(getType(resetVault), resetVaultSaga); @@ -45,6 +48,10 @@ function* resetVaultSaga(action: ReturnType) { yield put(sessionReseted()); yield put(deploysReseted()); yield put(loginRetryCountReseted()); + yield put(recipientPublicKeyReseted()); + yield put(contactsReseted()); + yield put(vaultSettingsReseted()); + browser.storage.local.clear(); } catch (err) { console.error(err); diff --git a/src/background/redux/settings/actions.ts b/src/background/redux/settings/actions.ts index d83eb04a6..21e664517 100644 --- a/src/background/redux/settings/actions.ts +++ b/src/background/redux/settings/actions.ts @@ -15,3 +15,5 @@ export const activeNetworkSettingChanged = createAction( export const themeModeSettingChanged = createAction( 'THEME_MODE_SETTING_CHANGED' )(); + +export const vaultSettingsReseted = createAction('VAULT_SETTINGS_RESETED')(); diff --git a/src/background/redux/settings/reducer.ts b/src/background/redux/settings/reducer.ts index 910c9baca..fbe6f2a25 100644 --- a/src/background/redux/settings/reducer.ts +++ b/src/background/redux/settings/reducer.ts @@ -6,7 +6,8 @@ import { NetworkSetting } from '@src/constants'; import { activeNetworkSettingChanged, activeTimeoutDurationSettingChanged, - themeModeSettingChanged + themeModeSettingChanged, + vaultSettingsReseted } from './actions'; import { SettingsState, ThemeMode } from './types'; @@ -18,6 +19,7 @@ const initialState: SettingsState = { }; export const reducer = createReducer(initialState) + .handleAction(vaultSettingsReseted, (): SettingsState => initialState) .handleAction( activeTimeoutDurationSettingChanged, (state, { payload }): SettingsState => ({