diff --git a/src/krist/contacts/contactStorage.ts b/src/krist/contacts/contactStorage.ts index c5a481c..3a486f6 100644 --- a/src/krist/contacts/contactStorage.ts +++ b/src/krist/contacts/contactStorage.ts @@ -48,15 +48,20 @@ /** Loads all available contacts from local storage. */ export function loadContacts(): ContactMap { - // Find all `contact2` keys from local storage - const keysToLoad = Object.keys(localStorage) - .map(extractContactKey) - .filter(k => k !== undefined) as [string, string][]; + const contactMap: ContactMap = {}; - const contacts = keysToLoad.map(([key, id]) => parseContact(id, localStorage.getItem(key))); + const lsKeys = Object.keys(localStorage); + for (const lsKey of lsKeys) { + // Find all 'contact2' keys from local storage + const extracted = extractContactKey(lsKey); + if (!extracted) continue; - // Convert to map with contact IDs - const contactMap: ContactMap = contacts.reduce((obj, c) => ({ ...obj, [c.id]: c }), {}); + // Parse the contact from the stored string + const [key, id] = extracted; + const contact = parseContact(id, localStorage.getItem(key)); + + contactMap[contact.id] = contact; + } return contactMap; } diff --git a/src/krist/contacts/utils.ts b/src/krist/contacts/utils.ts index 2942695..517b4ba 100644 --- a/src/krist/contacts/utils.ts +++ b/src/krist/contacts/utils.ts @@ -19,10 +19,18 @@ /** Hook that fetches the contacts from the Redux store. */ export function useContacts(): ContactsHookResponse { const contacts = useSelector((s: RootState) => s.contacts.contacts, shallowEqual); - const contactAddressMap = Object.values(contacts) - .reduce((o, contact) => ({ ...o, [contact.address]: contact }), {}); - const contactAddressList = Object.keys(contactAddressMap); + const contactAddressMap: ContactAddressMap = {}; + const contactAddressList: string[] = []; + + for (const id in contacts) { + const contact = contacts[id]; + const address = contact.address; + + contactAddressMap[address] = contact; + contactAddressList.push(address); + } + const joinedContactAddressList = contactAddressList.join(","); return { diff --git a/src/krist/wallets/utils.ts b/src/krist/wallets/utils.ts index 4f9331e..1c9c56e 100644 --- a/src/krist/wallets/utils.ts +++ b/src/krist/wallets/utils.ts @@ -36,16 +36,25 @@ /** Hook that fetches the wallets from the Redux store. */ export function useWallets(): WalletsHookResponse { const wallets = useSelector((s: RootState) => s.wallets.wallets, shallowEqual); - const walletAddressMap = Object.values(wallets) - .reduce((o, wallet) => ({ ...o, [wallet.address]: wallet }), {}); - // A cheap address list used for deep comparison. It's totally okay to assume - // this list will only change when the addresses will change, as since ES2015, - // object ordering is _basically_ consistent: - // https://stackoverflow.com/a/5525820/1499974 - // https://stackoverflow.com/a/38218582/1499974 - // https://stackoverflow.com/a/23202095/1499974 - const addressList = Object.keys(walletAddressMap); + const walletAddressMap: WalletAddressMap = {}; + const addressList: string[] = []; + + for (const id in wallets) { + const wallet = wallets[id]; + const address = wallet.address; + + walletAddressMap[address] = wallet; + + // A cheap address list used for deep comparison. It's totally okay to + // assume this list will only change when the addresses will change, as + // since ES2015, object ordering is *basically* consistent: + // https://stackoverflow.com/a/5525820/1499974 + // https://stackoverflow.com/a/38218582/1499974 + // https://stackoverflow.com/a/23202095/1499974 + addressList.push(address); + } + const joinedAddressList = addressList.join(","); return { wallets, walletAddressMap, addressList, joinedAddressList }; diff --git a/src/krist/wallets/walletStorage.ts b/src/krist/wallets/walletStorage.ts index 63587ad..077aaf9 100644 --- a/src/krist/wallets/walletStorage.ts +++ b/src/krist/wallets/walletStorage.ts @@ -54,15 +54,20 @@ /** Loads all available wallets from local storage. */ export function loadWallets(): WalletMap { - // Find all `wallet2` keys from local storage - const keysToLoad = Object.keys(localStorage) - .map(extractWalletKey) - .filter(k => k !== undefined) as [string, string][]; + const walletMap: WalletMap = {}; - const wallets = keysToLoad.map(([key, id]) => parseWallet(id, localStorage.getItem(key))); + const lsKeys = Object.keys(localStorage); + for (const lsKey of lsKeys) { + // Find all 'wallet2' keys from local storage + const extracted = extractWalletKey(lsKey); + if (!extracted) continue; - // Convert to map with wallet IDs - const walletMap: WalletMap = wallets.reduce((obj, w) => ({ ...obj, [w.id]: w }), {}); + // Parse the wallet from the stored string + const [key, id] = extracted; + const wallet = parseWallet(id, localStorage.getItem(key)); + + walletMap[wallet.id] = wallet; + } return walletMap; } diff --git a/src/store/reducers/WalletsReducer.ts b/src/store/reducers/WalletsReducer.ts index ee51b51..a82e944 100644 --- a/src/store/reducers/WalletsReducer.ts +++ b/src/store/reducers/WalletsReducer.ts @@ -19,7 +19,12 @@ }; } -function assignNewWalletProperties(state: State, id: string, partialWallet: Partial, allowedKeys?: (keyof Wallet)[]) { +function assignNewWalletProperties( + state: State, + id: string, + partialWallet: Partial, + allowedKeys?: (keyof Wallet)[] +) { // Fetch the old wallet and assign the new properties const { [id]: wallet } = state.wallets; const newWallet = allowedKeys diff --git a/src/utils/index.ts b/src/utils/index.ts index fcf6d6e..0912e20 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -152,3 +152,14 @@ } export const mod = (n: number, m: number): number => ((n % m) + m) % m; + +export function toLookup(arr: string[]): Record { + const out: Record = {}; + if (!arr) return out; + + for (let i = 0; i < arr.length; i++) { + out[arr[i]] = true; + } + + return out; +}