Skip to content

Commit

Permalink
fix: update collections on account change (#2407)
Browse files Browse the repository at this point in the history
Co-authored-by: Jean Ribeiro <[email protected]>
  • Loading branch information
nicole-obrien and jeeanribeiro authored May 2, 2024
1 parent 7f1f284 commit d835238
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Text type="body2" truncate>{collection.name}</Text>
</nft-name>
<nft-pills class="flex flex-row items-center gap-2">
<NetworkAvatar networkId={collection.nfts[0].networkId} size="sm" showTooltip />
<NetworkAvatar networkId={collection.nfts[0]?.networkId} size="sm" showTooltip />
<AssetPillsForNft nft={collection.nfts[0]} />
<Pill compact color="brand">{localize('general.nfts', { count: collection.nfts.length })}</Pill>
</nft-pills>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Collections> = writable({})

Expand Down Expand Up @@ -50,12 +50,27 @@ async function updateCollections(nfts: Nft[]): Promise<void> {
}

selectedAccountNfts.subscribe((nfts) => {
void updateCollections(nfts)
void updateCollections(nfts.filter((nft) => nft.isSpendable))
})

export const selectedAccountCollections: Readable<Collections> = 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<string> = writable('')

0 comments on commit d835238

Please sign in to comment.