|
1 | 1 | import { type PayloadAction } from '@reduxjs/toolkit'; |
2 | 2 |
|
3 | | -import { type AnyAction, createSliceWithExtraDeps } from '@suite-common/redux-utils'; |
| 3 | +import { createSliceWithExtraDeps, createWeakMapSelector } from '@suite-common/redux-utils'; |
4 | 4 | import { accountsActions } from '@suite-common/wallet-core'; |
5 | 5 | import { type AccountKey, type ReceiveInfo } from '@suite-common/wallet-types'; |
6 | 6 |
|
@@ -33,12 +33,6 @@ type SetCurrentFreshAddressPayload = { |
33 | 33 | currentFreshAddress?: CurrentFreshAddress; |
34 | 34 | }; |
35 | 35 |
|
36 | | -type PersistedReceiveAccountState = { |
37 | | - touchedAddresses?: ReceiveInfo[]; |
38 | | - revealedAddresses?: ReceiveInfo[]; |
39 | | - currentFreshAddress?: CurrentFreshAddress; |
40 | | -}; |
41 | | - |
42 | 36 | export const receiveInitialState: ReceiveState = { |
43 | 37 | accounts: {}, |
44 | 38 | }; |
@@ -103,44 +97,54 @@ export const receiveSlice = createSliceWithExtraDeps({ |
103 | 97 | }); |
104 | 98 | }) |
105 | 99 | .addCase(extra.actionTypes.storageLoad, (state, action) => { |
106 | | - const actionWithPayload = action as AnyAction; |
107 | | - |
108 | | - state.accounts = ( |
109 | | - (actionWithPayload.payload?.receive ?? []) as { |
110 | | - key: string; |
111 | | - value: PersistedReceiveAccountState; |
112 | | - }[] |
113 | | - ).reduce<ReceiveState['accounts']>((accounts, { key, value }) => { |
114 | | - const touchedAddresses = |
115 | | - value.touchedAddresses ?? value.revealedAddresses ?? []; |
116 | | - |
117 | | - accounts[key as AccountKey] = { |
118 | | - touchedAddresses: touchedAddresses.map(({ path, address }) => ({ |
119 | | - path, |
120 | | - address, |
121 | | - })), |
122 | | - currentFreshAddress: value.currentFreshAddress, |
123 | | - }; |
124 | | - |
125 | | - return accounts; |
126 | | - }, {}); |
| 100 | + const entries = action.payload?.receive as |
| 101 | + | { |
| 102 | + key: string; |
| 103 | + value: ReceiveAccountState; |
| 104 | + }[] |
| 105 | + | undefined; |
| 106 | + |
| 107 | + state.accounts = |
| 108 | + entries?.reduce<ReceiveState['accounts']>((accounts, { key, value }) => { |
| 109 | + accounts[key as AccountKey] = { |
| 110 | + touchedAddresses: value.touchedAddresses.map(({ path, address }) => ({ |
| 111 | + path, |
| 112 | + address, |
| 113 | + })), |
| 114 | + currentFreshAddress: value.currentFreshAddress, |
| 115 | + }; |
| 116 | + |
| 117 | + return accounts; |
| 118 | + }, {}) ?? {}; |
127 | 119 | }); |
128 | 120 | }, |
129 | 121 | }); |
130 | 122 |
|
131 | | -const selectReceiveAccountState = (state: ReceiveRootState, accountKey?: AccountKey) => { |
132 | | - if (!accountKey) { |
133 | | - return emptyReceiveAccountState; |
134 | | - } |
| 123 | +const createMemoizedSelector = createWeakMapSelector.withTypes<ReceiveRootState>(); |
135 | 124 |
|
136 | | - return state.receive.accounts[accountKey] ?? emptyReceiveAccountState; |
137 | | -}; |
| 125 | +const selectReceiveAccountState = createMemoizedSelector( |
| 126 | + [ |
| 127 | + (state: ReceiveRootState) => state.receive.accounts, |
| 128 | + (_state: ReceiveRootState, accountKey?: AccountKey) => accountKey, |
| 129 | + ], |
| 130 | + (accounts, accountKey) => { |
| 131 | + if (!accountKey) { |
| 132 | + return emptyReceiveAccountState; |
| 133 | + } |
| 134 | + |
| 135 | + return accounts[accountKey] ?? emptyReceiveAccountState; |
| 136 | + }, |
| 137 | +); |
138 | 138 |
|
139 | | -export const selectTouchedAddresses = (state: ReceiveRootState, accountKey?: AccountKey) => |
140 | | - selectReceiveAccountState(state, accountKey).touchedAddresses; |
| 139 | +export const selectTouchedAddresses = createMemoizedSelector( |
| 140 | + [selectReceiveAccountState], |
| 141 | + accountState => accountState.touchedAddresses, |
| 142 | +); |
141 | 143 |
|
142 | | -export const selectCurrentFreshAddress = (state: ReceiveRootState, accountKey?: AccountKey) => |
143 | | - selectReceiveAccountState(state, accountKey).currentFreshAddress; |
| 144 | +export const selectCurrentFreshAddress = createMemoizedSelector( |
| 145 | + [selectReceiveAccountState], |
| 146 | + accountState => accountState.currentFreshAddress, |
| 147 | +); |
144 | 148 |
|
145 | 149 | export const receiveActions = receiveSlice.actions; |
146 | 150 | export const prepareReceiveReducer = receiveSlice.prepareReducer; |
0 commit comments