Skip to content

Commit

Permalink
fix lazy NFT card load
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 30, 2024
1 parent 11bd1a1 commit 19528fe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,16 @@ const InnerStakingModal = ({

const onDeselectAll = () => setCurrentTokenIds([])

const nfts =
(mode === StakingMode.Stake
? loadingWalletUnstakedNfts
: mode === StakingMode.Unstake
? loadingWalletStakedNfts
: undefined) ??
({ loading: false, errored: true } as LoadingDataWithError<
LazyNftCardInfo[]
>)
const nfts: LoadingDataWithError<LazyNftCardInfo[]> = (mode ===
StakingMode.Stake
? loadingWalletUnstakedNfts
: mode === StakingMode.Unstake
? loadingWalletStakedNfts
: undefined) ?? {
loading: false,
errored: true,
error: new Error('Unexpected failure to load NFTs'),
}

const onSelectAll = () =>
setCurrentTokenIds(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
DaoVotingCw721StakedSelectors,
blockHeightSelector,
contractVersionSelector,
nftCardInfoSelector,
refreshClaimsIdAtom,
refreshWalletBalancesIdAtom,
} from '@dao-dao/state'
Expand All @@ -22,8 +21,9 @@ import {
useCachedLoadingWithError,
useDao,
} from '@dao-dao/stateless'
import { LazyNftCardInfo } from '@dao-dao/types'
import { NftClaim } from '@dao-dao/types/contracts/DaoVotingCw721Staked'
import { claimAvailable } from '@dao-dao/utils'
import { claimAvailable, getNftKey } from '@dao-dao/utils'

import { useWallet } from '../../../../hooks/useWallet'
import { UseStakingInfoOptions, UseStakingInfoResponse } from '../types'
Expand Down Expand Up @@ -130,56 +130,42 @@ export const useStakingInfo = ({
)

// Wallet staked value
const loadingWalletStakedNftsLoadable = useCachedLoading(
const loadingWalletStakedNfts = useCachedLoadingWithError(
fetchWalletStakedValue && walletAddress
? DaoVotingCw721StakedSelectors.stakedNftsSelector({
chainId: dao.chainId,
contractAddress: dao.votingModule.address,
params: [{ address: walletAddress }],
})
: undefined,
undefined
)

const loadingWalletStakedNfts = useCachedLoadingWithError(
!loadingWalletStakedNftsLoadable.loading &&
loadingWalletStakedNftsLoadable.data
? waitForAll(
loadingWalletStakedNftsLoadable.data?.map((tokenId) =>
nftCardInfoSelector({
chainId: dao.chainId,
collection: governanceTokenAddress,
tokenId,
})
)
)
: undefined
(data) =>
data.map(
(tokenId): LazyNftCardInfo => ({
key: getNftKey(dao.chainId, governanceTokenAddress, tokenId),
chainId: dao.chainId,
collectionAddress: governanceTokenAddress,
tokenId,
})
)
)

const loadingWalletUnstakedNftsLoadable = useCachedLoadingWithError(
const loadingWalletUnstakedNfts = useCachedLoadingWithError(
fetchWalletUnstakedNfts && walletAddress && governanceTokenAddress
? CommonNftSelectors.unpaginatedAllTokensForOwnerSelector({
chainId: dao.chainId,
contractAddress: governanceTokenAddress,
owner: walletAddress,
})
: undefined
)

const loadingWalletUnstakedNfts = useCachedLoadingWithError(
!loadingWalletUnstakedNftsLoadable.loading &&
!loadingWalletUnstakedNftsLoadable.errored &&
loadingWalletUnstakedNftsLoadable.data
? waitForAll(
loadingWalletUnstakedNftsLoadable.data?.map((tokenId) =>
nftCardInfoSelector({
chainId: dao.chainId,
collection: governanceTokenAddress,
tokenId,
})
)
)
: undefined
: undefined,
(data) =>
data.map(
(tokenId): LazyNftCardInfo => ({
key: getNftKey(dao.chainId, governanceTokenAddress, tokenId),
chainId: dao.chainId,
collectionAddress: governanceTokenAddress,
tokenId,
})
)
)

return {
Expand Down Expand Up @@ -208,13 +194,13 @@ export const useStakingInfo = ({
data: HugeDecimal.from(loadingTotalStakedValue.data.power),
},
// Wallet staked value
loadingWalletStakedValue: loadingWalletStakedNftsLoadable.loading
loadingWalletStakedValue: loadingWalletStakedNfts.loading
? { loading: true }
: !loadingWalletStakedNftsLoadable.data
: loadingWalletStakedNfts.errored
? undefined
: {
loading: false,
data: HugeDecimal.from(loadingWalletStakedNftsLoadable.data.length),
data: HugeDecimal.from(loadingWalletStakedNfts.data.length),
},
loadingWalletStakedNfts,
loadingWalletUnstakedNfts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
ContractVersion,
Duration,
GenericToken,
LazyNftCardInfo,
LoadingData,
LoadingDataWithError,
NftCardInfo,
} from '@dao-dao/types'
import { NftClaim } from '@dao-dao/types/contracts/DaoVotingCw721Staked'

Expand Down Expand Up @@ -33,8 +33,8 @@ export interface UseStakingInfoResponse {
loadingTotalStakedValue?: LoadingData<HugeDecimal>
// Wallet staked value
loadingWalletStakedValue?: LoadingData<HugeDecimal>
loadingWalletStakedNfts?: LoadingDataWithError<NftCardInfo[]>
loadingWalletUnstakedNfts?: LoadingDataWithError<NftCardInfo[]>
loadingWalletStakedNfts?: LoadingDataWithError<LazyNftCardInfo[]>
loadingWalletUnstakedNfts?: LoadingDataWithError<LazyNftCardInfo[]>
}

export interface UseGovernanceCollectionInfoOptions {
Expand Down

0 comments on commit 19528fe

Please sign in to comment.