Skip to content

Commit

Permalink
fix: remove network specific account states
Browse files Browse the repository at this point in the history
  • Loading branch information
F-OBrien authored and adamdossa committed Jan 2, 2024
1 parent 4b29316 commit 2969fb1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 71 deletions.
13 changes: 5 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +57,7 @@ export function accountsSynchronizer(): () => void {
name: accountName(account),
};

store.dispatch(accountActions.setAccountGlobally(accountData));
store.dispatch(accountActions.setAccount(accountData));
});
});

Expand Down Expand Up @@ -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 },
})
);

Expand Down
63 changes: 13 additions & 50 deletions packages/core/src/store/features/accounts.ts
Original file line number Diff line number Diff line change
@@ -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<string, AccountData>
>;
const initialState: State = initialFeatureState() as Record<
NetworkName,
Record<string, AccountData>
>;

type SetAccountPayload = { network: NetworkName; data: AccountData };
type RemoveAccountPayload = { network: NetworkName; address: string };
type SelectAccountPayload = string;
type AccountsState = { selected?: string; keys: Record<string, AccountData> };
const initialState: AccountsState = {
keys: {},
} as AccountsState;

const accountsSlice = createSlice({
name: 'accounts',
initialState,
reducers: {
setAccount(state, action: PayloadAction<SetAccountPayload>) {
const { data, network } = action.payload;
const prev = state[network][data.address];
setAccount(state, action: PayloadAction<AccountData>) {
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<RemoveAccountPayload>) {
const { address, network } = action.payload;
removeAccount(state, action: PayloadAction<string>) {
const address = action.payload;

delete state[network][address];
delete state.keys[address];

if (state.selected === address) {
state.selected = undefined;
}
},
selectAccount(state, action: PayloadAction<SelectAccountPayload>) {
selectAccount(state, action: PayloadAction<string>) {
state.selected = action.payload;
},
setAccountGlobally(state, action: PayloadAction<AccountData>) {
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<string>) {
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;
}
},
},
});

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
11 changes: 0 additions & 11 deletions packages/core/src/store/utils.ts

This file was deleted.

0 comments on commit 2969fb1

Please sign in to comment.