Skip to content

Commit

Permalink
[AIRMOB-291] Fix Watchlist items disappear
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidHaji-zada committed Sep 25, 2023
1 parent 52ee80d commit fde0405
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/contexts/AllAddresses/AllAddresses.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createContextSelector } from '@helpers/createContextSelector';
import { NotificationService } from '@lib';
import { DeviceEventEmitter } from 'react-native';
import { EVENTS } from '@constants/events';
import { ArrayUtils } from '@utils/array';

const AllAddressesContext = () => {
const [allAddresses, setAllAddresses] = useState<ExplorerAccount[]>([]);
Expand Down Expand Up @@ -128,7 +129,10 @@ const AllAddressesContext = () => {
setLoading(true);
const addresses = ((await Cache.getItem(CacheKey.AllAddresses)) ||
[]) as CacheableAccount[];
const populatedAddresses = await populateAddresses(addresses);
const currentAddresses = allAddresses.map(ExplorerAccount.toCacheable);
const populatedAddresses = await populateAddresses(
ArrayUtils.mergeArrays('address', addresses, currentAddresses)
);
setAllAddresses(populatedAddresses);
reducer({ type: 'set', payload: populatedAddresses });
setLoading(false);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useCachePurifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export const useCachePurifier = () => {
);
await Cache.setItem(CacheKey.AddressLists, cacheableLists);

// filter unique addresses
// watchlisted accounts
const knownAddresses: CacheableAccount[] = allAddresses
.filter((account) => account.isOnWatchlist)
.map((account) => ExplorerAccount.toCacheable(account));

// add accounts from groups
for (const list of listsOfAddressGroup) {
for (const account of list.accounts) {
if (knownAddresses.indexOfItem(account, 'address') === -1) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function mergeArrays<T>(compareKey: keyof T, ...arrays: T[][]) {
const result: T[] = [];
for (let i = 0; i < arrays.length; i++) {
const curArray = arrays[i];
result.push(
...curArray.filter((item) => result.indexOfItem(item, compareKey) === -1)
);
}
return result;
}

export const ArrayUtils = { mergeArrays };

0 comments on commit fde0405

Please sign in to comment.