Skip to content

Commit

Permalink
fix: fix contact list preserving after the wallet reset (#887)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ost-ptk and ost-ptk authored Dec 14, 2023
1 parent aaa5122 commit febe2ef
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -139,6 +143,7 @@ import {
} from '@libs/services/validators-service';
import {
contactRemoved,
contactsReseted,
contactUpdated,
newContactAdded
} from '@background/redux/contacts/actions';
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/background/redux/contacts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const contactRemoved = createAction('CONTACT_REMOVED')<string>();

export const contactUpdated =
createAction('CONTACT_UPDATED')<EditContactActionType>();

export const contactsReseted = createAction('CONTACTS_RESETED')();
2 changes: 2 additions & 0 deletions src/background/redux/contacts/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createReducer } from 'typesafe-actions';

import {
contactRemoved,
contactsReseted,
contactUpdated,
newContactAdded
} from '@background/redux/contacts/actions';
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/background/redux/recent-recipient-public-keys/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export const recipientPublicKeyAdded = createAction(
'RECIPIENT_PUBLIC_KEY_ADDED',
(payload: string) => payload
)<string>();

export const recipientPublicKeyReseted = createAction(
'RECIPIENT_PUBLIC_KEY_RESETED'
)();
14 changes: 8 additions & 6 deletions src/background/redux/recent-recipient-public-keys/reducer.ts
Original file line number Diff line number Diff line change
@@ -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])]
);
7 changes: 7 additions & 0 deletions src/background/redux/sagas/onboarding-sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -45,6 +48,10 @@ function* resetVaultSaga(action: ReturnType<typeof resetVault>) {
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);
Expand Down
2 changes: 2 additions & 0 deletions src/background/redux/settings/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export const activeNetworkSettingChanged = createAction(
export const themeModeSettingChanged = createAction(
'THEME_MODE_SETTING_CHANGED'
)<ThemeMode>();

export const vaultSettingsReseted = createAction('VAULT_SETTINGS_RESETED')();
4 changes: 3 additions & 1 deletion src/background/redux/settings/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { NetworkSetting } from '@src/constants';
import {
activeNetworkSettingChanged,
activeTimeoutDurationSettingChanged,
themeModeSettingChanged
themeModeSettingChanged,
vaultSettingsReseted
} from './actions';
import { SettingsState, ThemeMode } from './types';

Expand All @@ -18,6 +19,7 @@ const initialState: SettingsState = {
};

export const reducer = createReducer(initialState)
.handleAction(vaultSettingsReseted, (): SettingsState => initialState)
.handleAction(
activeTimeoutDurationSettingChanged,
(state, { payload }): SettingsState => ({
Expand Down

0 comments on commit febe2ef

Please sign in to comment.