diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index b562ec38..a4e1107d 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -47,7 +47,7 @@ export function accountsSynchronizer(): () => void { // A) If account is removed, clean up any associated subscriptions removedAccounts.forEach((account) => { - store.dispatch(accountActions.removeAccountGlobally(account)); + store.dispatch(accountActions.removeAccount(account)); }); // B) Insert or update remaining accounts @@ -57,7 +57,7 @@ export function accountsSynchronizer(): () => void { name: accountName(account), }; - store.dispatch(accountActions.setAccountGlobally(accountData)); + store.dispatch(accountActions.setAccount(accountData)); }); }); @@ -170,12 +170,9 @@ const initApiPromise = (network: NetworkName, networkUrl: string) => store.dispatch( accountActions.setAccount({ - data: { - address: account, - name: accountName(account), - balance: { total, transferrable, locked }, - }, - network, + address: account, + name: accountName(account), + balance: { total, transferrable, locked }, }) ); diff --git a/packages/core/src/store/features/accounts.ts b/packages/core/src/store/features/accounts.ts index 51a76125..9f50dba9 100644 --- a/packages/core/src/store/features/accounts.ts +++ b/packages/core/src/store/features/accounts.ts @@ -1,76 +1,39 @@ -/* eslint-disable sort-keys */ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import merge from 'lodash/merge'; import isEqual from 'lodash-es/isEqual'; -import { AccountData, NetworkName } from '../../types'; -import { initialFeatureState } from '../utils'; +import { AccountData } from '../../types'; -type State = { selected?: string } & Record< - NetworkName, - Record ->; -const initialState: State = initialFeatureState() as Record< - NetworkName, - Record ->; - -type SetAccountPayload = { network: NetworkName; data: AccountData }; -type RemoveAccountPayload = { network: NetworkName; address: string }; -type SelectAccountPayload = string; +type AccountsState = { selected?: string; keys: Record }; +const initialState: AccountsState = { + keys: {}, +} as AccountsState; const accountsSlice = createSlice({ name: 'accounts', initialState, reducers: { - setAccount(state, action: PayloadAction) { - const { data, network } = action.payload; - const prev = state[network][data.address]; + setAccount(state, action: PayloadAction) { + const data = action.payload; + const prev = state.keys[data.address]; const next = merge(prev, data); if (!isEqual(next, prev)) { - state[network][data.address] = next; + state.keys[data.address] = next; } }, - removeAccount(state, action: PayloadAction) { - const { address, network } = action.payload; + removeAccount(state, action: PayloadAction) { + const address = action.payload; - delete state[network][address]; + delete state.keys[address]; if (state.selected === address) { state.selected = undefined; } }, - selectAccount(state, action: PayloadAction) { + selectAccount(state, action: PayloadAction) { state.selected = action.payload; }, - setAccountGlobally(state, action: PayloadAction) { - const data = action.payload; - - Object.keys(NetworkName).forEach((network: string) => { - const prev = state[network as NetworkName][data.address]; - const next = merge(prev, data); - - if (!isEqual(next, prev)) { - state[network as NetworkName][data.address] = next; - } - }); - }, - removeAccountGlobally(state, action: PayloadAction) { - const address = action.payload; - - Object.keys(NetworkName).forEach((network: string) => { - const prev = state[network as NetworkName][address]; - - if (prev) { - delete state[network as NetworkName][address]; - } - }); - - if (state.selected === address) { - state.selected = undefined; - } - }, }, }); diff --git a/packages/core/src/store/selectors.ts b/packages/core/src/store/selectors.ts index be7686e3..247074f9 100644 --- a/packages/core/src/store/selectors.ts +++ b/packages/core/src/store/selectors.ts @@ -65,9 +65,8 @@ export const reversedDidList = createSelector( ); export const accounts = createSelector( - selectedNetwork, (state: RootState) => state.accounts, - (network, accounts) => accounts[network] + (accounts) => accounts.keys ); export const accountsAddresses = createSelector(accounts, (accounts) => diff --git a/packages/core/src/store/utils.ts b/packages/core/src/store/utils.ts deleted file mode 100644 index dbed6577..00000000 --- a/packages/core/src/store/utils.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { NetworkName } from '../types'; - -export function initialFeatureState(): Record { - const state = Object.keys(NetworkName).reduce(function (acc, key) { - acc[key as NetworkName] = {}; - - return acc; - }, {} as Record); - - return state; -}