Skip to content

Commit

Permalink
Refactor queryOptions type in wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Aug 9, 2024
1 parent bc66fc4 commit 92c9bce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/app/data/oasis-account-names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { useQuery } from '@tanstack/react-query'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import {
Layer,
useGetConsensusAccountsAddresses,
Expand Down Expand Up @@ -65,20 +65,19 @@ const getOasisAccountsMetadata = async (network: Network, layer: Layer): Promise
export const useOasisAccountsMetadata = (
network: Network,
layer: Layer,
queryOptions: { enabled: boolean; useErrorBoundary?: boolean },
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
) => {
return useQuery(['oasisAccounts', network, layer], () => getOasisAccountsMetadata(network, layer), {
enabled: queryOptions.enabled,
staleTime: Infinity,
useErrorBoundary: queryOptions.useErrorBoundary,
...queryOptions,
})
}

export const useOasisAccountMetadata = (
network: Network,
layer: Layer,
address: string,
queryOptions: { enabled: boolean; useErrorBoundary?: boolean },
queryOptions: UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountMetadataInfo => {
const { isLoading, isError, error, data: allData } = useOasisAccountsMetadata(network, layer, queryOptions)
if (isError) {
Expand All @@ -95,14 +94,14 @@ export const useSearchForOasisAccountsByName = (
network: Network,
layer: Layer,
nameFragment: string,
queryOptions: { enabled: boolean },
queryOptions: { enabled: boolean } & UseQueryOptions<AccountData, unknown, AccountData, string[]>,
): AccountNameSearchResults => {
const {
isLoading: isMetadataLoading,
isError: isMetadataError,
error: metadataError,
data: namedAccounts,
} = useOasisAccountsMetadata(network, layer, { ...queryOptions, useErrorBoundary: false })
} = useOasisAccountsMetadata(network, layer, { useErrorBoundary: false, ...queryOptions })
if (isMetadataError) {
console.log('Failed to load Oasis account metadata', metadataError)
}
Expand Down
30 changes: 19 additions & 11 deletions src/app/data/pontusx-account-names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { useQuery } from '@tanstack/react-query'
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import {
AccountMetadata,
AccountMap,
Expand All @@ -14,7 +14,12 @@ import { getOasisAddress } from '../utils/helpers'

const DATA_SOURCE_URL = 'https://raw.githubusercontent.com/deltaDAO/mvg-portal/main/pontusxAddresses.json'

const getPontusXAccountsMetadata = async () => {
type PontusXAccountsMetadata = {
map: AccountMap
list: AccountMetadata[]
}

const getPontusXAccountsMetadata = async (): Promise<PontusXAccountsMetadata> => {
const response = await axios.get(DATA_SOURCE_URL)
if (response.status !== 200) throw new Error("Couldn't load names")
if (!response.data) throw new Error("Couldn't load names")
Expand All @@ -34,20 +39,18 @@ const getPontusXAccountsMetadata = async () => {
}
}

export const usePontusXAccountsMetadata = (queryOptions: {
enabled: boolean
useErrorBoundary?: boolean
}) => {
export const usePontusXAccountsMetadata = (
queryOptions: UseQueryOptions<PontusXAccountsMetadata, unknown, PontusXAccountsMetadata, string[]>,
) => {
return useQuery(['pontusXNames'], getPontusXAccountsMetadata, {
enabled: queryOptions.enabled,
staleTime: Infinity,
useErrorBoundary: queryOptions.useErrorBoundary,
...queryOptions,
})
}

export const usePontusXAccountMetadata = (
address: string,
queryOptions: { enabled: boolean; useErrorBoundary?: boolean },
queryOptions: UseQueryOptions<PontusXAccountsMetadata, unknown, PontusXAccountsMetadata, string[]>,
): AccountMetadataInfo => {
const { isLoading, isError, error, data: allData } = usePontusXAccountsMetadata(queryOptions)
if (isError) {
Expand All @@ -63,14 +66,19 @@ export const usePontusXAccountMetadata = (
export const useSearchForPontusXAccountsByName = (
network: Network,
nameFragment: string,
queryOptions: { enabled: boolean },
queryOptions: { enabled: boolean } & UseQueryOptions<
PontusXAccountsMetadata,
unknown,
PontusXAccountsMetadata,
string[]
>,
): AccountNameSearchRuntimeResults => {
const {
isLoading: isMetadataLoading,
isError: isMetadataError,
error: metadataError,
data: namedAccounts,
} = usePontusXAccountsMetadata({ ...queryOptions, useErrorBoundary: false })
} = usePontusXAccountsMetadata({ useErrorBoundary: false, ...queryOptions })
if (isMetadataError) {
console.log('Failed to load Pontus-X account names', metadataError)
}
Expand Down

0 comments on commit 92c9bce

Please sign in to comment.