Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update collections on account change #2407

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('')
Loading