Skip to content

Commit

Permalink
Refactor useTokenTransfers function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Jan 24, 2024
1 parent 9f7b896 commit a451b67
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const AccountTokenTransfersCard: FC<AccountDetailsContext> = ({ scope, ad

const AccountTokenTransfers: FC<AccountDetailsContext> = ({ scope, address, account }) => {
const { t } = useTranslation()
const { isLoading, isFetched, results } = useTokenTransfers(scope, address)
const { isLoading, isFetched, results } = useTokenTransfers(scope, { address })

const transfers = results.data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TokenTransfers } from '../../components/Tokens/TokenTransfers'
import { LinkableDiv } from '../../components/PageLayout/LinkableDiv'
import { NftDashboardContext } from './'
import { CardEmptyState } from '../AccountDetailsPage/CardEmptyState'
import { useTokenTransfers } from '../TokenDashboardPage/hook'
import { useNFTInstanceTransfers } from '../TokenDashboardPage/hook'

export const nftTokenTransfersContainerId = 'nftTokenTransfers'

Expand All @@ -32,7 +32,10 @@ export const NFTTokenTransfersCard: FC<NftDashboardContext> = props => {

const NFTTokenTransfersView: FC<NftDashboardContext> = ({ scope, address, instanceId }) => {
const { t } = useTranslation()
const { isLoading, isFetched, results } = useTokenTransfers(scope, address, instanceId)
const { isLoading, isFetched, results } = useNFTInstanceTransfers(scope, {
contract_address: address,
nft_id: instanceId,
})
const transfers = results.data

return (
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/TokenDashboardPage/TokenTransfersCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const TokenTransfersCard: FC<TokenDashboardContext> = ({ scope, address }
const TokenTransfersView: FC<TokenDashboardContext> = ({ scope, address }) => {
const { t } = useTranslation()

const { isLoading: areTransfersLoading, isFetched, results } = useTokenTransfers(scope, address)
const { isLoading: areTransfersLoading, isFetched, results } = useTokenTransfers(scope, { address })

const { isLoading: isTokenLoading } = useTokenInfo(scope, address)

Expand Down
29 changes: 16 additions & 13 deletions src/app/pages/TokenDashboardPage/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useGetRuntimeAccountsAddressNfts,
useGetRuntimeEvmTokensAddressNftsId,
RuntimeEventType,
GetRuntimeEventsParams,
} from '../../../oasis-nexus/api'
import { AppErrors } from '../../../types/errors'
import { SearchScope } from '../../../types/searchScope'
Expand Down Expand Up @@ -36,7 +37,20 @@ export const useTokenInfo = (scope: SearchScope, address: string, enabled = true
}
}

export const useTokenTransfers = (scope: SearchScope, address: string, nftInstanceId?: string) => {
export const useTokenTransfers = (scope: SearchScope, params: { address: string }) => {
const oasisAddress = useTransformToOasisAddress(params.address)
return _useTokenTransfers(scope, { rel: oasisAddress! })
}

export const useNFTInstanceTransfers = (
scope: SearchScope,
params: { nft_id: string; contract_address: string },
) => {
const oasisAddress = useTransformToOasisAddress(params.contract_address)
return _useTokenTransfers(scope, { nft_id: params.nft_id, contract_address: oasisAddress! })
}

export const _useTokenTransfers = (scope: SearchScope, params: GetRuntimeEventsParams) => {
const { network, layer } = scope
const pagination = useComprehensiveSearchParamsPagination<RuntimeEvent, RuntimeEventList>({
paramName: 'page',
Expand All @@ -47,17 +61,6 @@ export const useTokenTransfers = (scope: SearchScope, address: string, nftInstan
// Loading transactions on the consensus layer is not supported yet.
// We should use useGetConsensusTransactions()
}

const oasisAddress = useTransformToOasisAddress(address)
const params = nftInstanceId
? {
nft_id: nftInstanceId,
contract_address: oasisAddress!,
}
: {
rel: oasisAddress!,
}

const query = useGetRuntimeEvents(
network,
layer, // This is OK since consensus has been handled separately
Expand All @@ -70,7 +73,7 @@ export const useTokenTransfers = (scope: SearchScope, address: string, nftInstan
},
{
query: {
enabled: !!oasisAddress,
enabled: !!params.rel || (!!params.contract_address && !!params.nft_id),
},
},
)
Expand Down

0 comments on commit a451b67

Please sign in to comment.