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

feat: ensjs-react #85

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
added hooks to ensjs-react
  • Loading branch information
TateB committed Dec 12, 2022
commit bd5cd9fc8001f654e2c11afc435e29cc7c1a6db5
5 changes: 5 additions & 0 deletions packages/ensjs-react/package.json
Original file line number Diff line number Diff line change
@@ -22,5 +22,10 @@
"ethers": ">=5.6.1",
"react": ">=16",
"wagmi": ">=0.8.10"
},
"dependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/providers": "^5.6.2",
"dataloader": "^2.1.0"
}
}
16 changes: 16 additions & 0 deletions packages/ensjs-react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export { default as useAddr } from './useAddr'
export { default as useAvailable } from './useAvailable'
export { default as useContentHash } from './useContentHash'
export { default as useDNSOwner } from './useDNSOwner'
export { default as useExpiry } from './useExpiry'
export { default as useHistory } from './useHistory'
export { default as useNames } from './useNames'
export { default as useOwner } from './useOwner'
export { default as usePrice } from './usePrice'
export { default as usePrimary } from './usePrimary'
export { default as useProfile } from './useProfile'
export { default as useResolver } from './useResolver'
export { default as useSubnames } from './useSubnames'
export { default as useText } from './useText'
export { default as useValidate } from './useValidate'
export { default as useWrapperData } from './useWrapperData'
32 changes: 32 additions & 0 deletions packages/ensjs-react/src/hooks/useAddr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PublicENS, QueryConfig } from '../types'
import { useEns } from '../utils/EnsProvider'

import { useCachedQuery } from './useCachedQuery'

type Args = {
name: string | null | undefined
coinType: Parameters<PublicENS['getAddr']>[1]
} & QueryConfig<ReturnType<PublicENS['getAddr']>, Error>

const useAddr = ({
name,
coinType,
onError,
onSettled,
onSuccess,
enabled = true,
}: Args) => {
const { ready, getAddr } = useEns()
return useCachedQuery(
['getAddr', { name, coinType }],
() => getAddr(name!, coinType),
{
enabled: Boolean(enabled && ready && coinType && name),
onError,
onSettled,
onSuccess,
},
)
}

export default useAddr
26 changes: 26 additions & 0 deletions packages/ensjs-react/src/hooks/useAvailable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PublicENS, QueryConfig } from '../types'
import { useEns } from '../utils/EnsProvider'

import { useCachedQuery } from './useCachedQuery'

type Args = {
name: string | null | undefined
} & QueryConfig<ReturnType<PublicENS['getAvailable']>, Error>

const useAvailable = ({
name,
onError,
onSettled,
onSuccess,
enabled = true,
}: Args) => {
const { ready, getAvailable } = useEns()
return useCachedQuery(['useAvailable', name], () => getAvailable(name!), {
enabled: Boolean(enabled && ready && name),
onError,
onSettled,
onSuccess,
})
}

export default useAvailable
120 changes: 0 additions & 120 deletions packages/ensjs-react/src/hooks/useBasicName.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/ensjs-react/src/hooks/useBlockTimestamp.ts

This file was deleted.

68 changes: 68 additions & 0 deletions packages/ensjs-react/src/hooks/useCachedQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
QueryFunction,
QueryKey,
QueryObserverResult,
UseQueryOptions,
} from '@tanstack/react-query'
import { useQuery } from 'wagmi'

export type UseQueryResult<TData, TError> = Pick<
QueryObserverResult<TData, TError>,
| 'data'
| 'error'
| 'fetchStatus'
| 'isError'
| 'isFetched'
| 'isFetchedAfterMount'
| 'isFetching'
| 'isLoading'
| 'isRefetching'
| 'isSuccess'
| 'refetch'
> & {
isCachedData: boolean
isIdle: boolean
status: 'idle' | 'loading' | 'success' | 'error'
internal: Pick<
QueryObserverResult,
| 'dataUpdatedAt'
| 'errorUpdatedAt'
| 'failureCount'
| 'isLoadingError'
| 'isPaused'
| 'isPlaceholderData'
| 'isPreviousData'
| 'isRefetchError'
| 'isStale'
| 'remove'
>
}
export type DefinedUseQueryResult<TData = unknown, TError = unknown> = Omit<
UseQueryResult<TData, TError>,
'data'
> & {
data: TData
}

export function useCachedQuery<
TQueryFnData,
TError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options?: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError> {
const query = useQuery<TQueryFnData, TError, TData, TQueryKey>(
queryKey,
queryFn,
options as any,
)
const { isFetched, isFetchedAfterMount, status } = query

return {
...query,
isCachedData: status === 'success' && isFetched && !isFetchedAfterMount,
}
}
11 changes: 0 additions & 11 deletions packages/ensjs-react/src/hooks/useChainId.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/ensjs-react/src/hooks/useChainName.ts

This file was deleted.

26 changes: 26 additions & 0 deletions packages/ensjs-react/src/hooks/useContentHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PublicENS, QueryConfig } from '../types'
import { useEns } from '../utils/EnsProvider'

import { useCachedQuery } from './useCachedQuery'

type Args = {
name: string | null | undefined
} & QueryConfig<ReturnType<PublicENS['getContentHash']>, Error>

const useContentHash = ({
name,
onError,
onSettled,
onSuccess,
enabled = true,
}: Args) => {
const { ready, getContentHash } = useEns()
return useCachedQuery(['getContentHash', name], () => getContentHash(name!), {
enabled: Boolean(enabled && ready && name),
onError,
onSettled,
onSuccess,
})
}

export default useContentHash
13 changes: 0 additions & 13 deletions packages/ensjs-react/src/hooks/useContractAddress.ts

This file was deleted.

Loading