From d8352385bc0539ff54387d493075714ae464158f Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Thu, 2 May 2024 13:10:31 +0100 Subject: [PATCH] fix: update collections on account change (#2407) Co-authored-by: Jean Ribeiro --- .../components/CollectionsGalleryItem.svelte | 2 +- .../selected-account-collections.store.ts | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/desktop/views/dashboard/collectibles/components/CollectionsGalleryItem.svelte b/packages/desktop/views/dashboard/collectibles/components/CollectionsGalleryItem.svelte index f3e5fa3cf0..3b7ae4a33a 100644 --- a/packages/desktop/views/dashboard/collectibles/components/CollectionsGalleryItem.svelte +++ b/packages/desktop/views/dashboard/collectibles/components/CollectionsGalleryItem.svelte @@ -33,7 +33,7 @@ {collection.name} - + {localize('general.nfts', { count: collection.nfts.length })} diff --git a/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts b/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts index e45446b556..68105a4dc9 100644 --- a/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts +++ b/packages/shared/src/lib/core/nfts/stores/selected-account-collections.store.ts @@ -3,7 +3,7 @@ import { NftStandard } from '../enums' import { Nft } from '../interfaces' import { Collections } from '../types' import { getCollectionFromNft } from '../utils' -import { selectedAccountNfts } from './selected-account-nfts.store' +import { ownedNfts, selectedAccountNfts } from './selected-account-nfts.store' export const collectionsStore: Writable = writable({}) @@ -50,12 +50,27 @@ async function updateCollections(nfts: Nft[]): Promise { } selectedAccountNfts.subscribe((nfts) => { - void updateCollections(nfts) + void updateCollections(nfts.filter((nft) => nft.isSpendable)) }) export const selectedAccountCollections: Readable = derived( - collectionsStore, - ($collectionsStore) => $collectionsStore + [collectionsStore, ownedNfts], + ([$collectionsStore, $ownedNfts]) => { + const accountCollections: Collections = {} + for (const collectionId in $collectionsStore) { + const collection = $collectionsStore[collectionId] + const nftsForAccount = collection.nfts.filter((nft) => + $ownedNfts.some((ownedNft) => ownedNft.id === nft.id) + ) + if (nftsForAccount.length > 0) { + // @ts-expect-error - ignore type error because we are filtering the existing nfts for the account + accountCollections[collectionId] = { ...collection, nfts: nftsForAccount } + } else { + delete accountCollections[collectionId] + } + } + return accountCollections + } ) export const collectionsSearchTerm: Writable = writable('')