Skip to content

Commit

Permalink
resolve change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
storywithoutend committed Sep 22, 2023
1 parent 9e09eb8 commit 770b34e
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ type Props = {
}

export const ExpirySection = ({ name, details }: Props) => {
console.log(details)
const { t } = useTranslation('profile')
const expiry = useExpiryDetails({ name, details })
const actions = useExpiryActions({ name, expiryDetails: expiry.data })
Expand Down
124 changes: 71 additions & 53 deletions src/transaction-flow/input/EditRoles/hooks/useSimpleSearch.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,90 @@
import { isAddress } from '@ethersproject/address'
import { useCallback } from 'react'
import { useMutation } from 'wagmi'
import { useEffect } from 'react'
import { useMutation, useQueryClient } from 'wagmi'

import { normalise } from '@ensdomains/ensjs/utils/normalise'

import useDebouncedCallback from '@app/hooks/useDebouncedCallback'
import { useEns } from '@app/utils/EnsProvider'
import { useQueryKeys } from '@app/utils/cacheKeyFactory'

type Result = { name?: string; address: string }
type Options = { cache?: boolean }

export const useSimpleSearch = (options: Options = {}) => {
const cache = options.cache ?? true

export const useSimpleSearch = () => {
const { ready, getAddr, getName } = useEns()
const queryClient = useQueryClient()
const queryKey = useQueryKeys()

useEffect(() => {
return () => {
queryClient.removeQueries(queryKey.simpleSearchBase(), { exact: false })
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

const queryByName = useCallback(
async (name: string): Promise<Result | null> => {
try {
const normalisedName = normalise(name)
const record = await getAddr(normalisedName, '60')
const address = typeof record === 'string' ? record : record?.addr
if (!address) throw new Error('No address found')
return {
name: normalisedName,
address,
}
} catch {
return null
const queryByName = async (name: string): Promise<Result | null> => {
try {
const normalisedName = normalise(name)
const record = await getAddr(normalisedName, '60')
const address = typeof record === 'string' ? record : record?.addr
if (!address) throw new Error('No address found')
return {
name: normalisedName,
address,
}
},
[getAddr],
)
} catch {
return null
}
}

const queryByAddress = useCallback(
async (address: string): Promise<Result | null> => {
try {
const name = await getName(address)
return {
name: name?.name,
address,
}
} catch {
return null
const queryByAddress = async (address: string): Promise<Result | null> => {
try {
const name = await getName(address)
return {
name: name?.name,
address,
}
} catch {
return null
}
}

const { mutate, isLoading, ...rest } = useMutation(
async (query: string) => {
if (!ready) throw new Error('ENSJS not ready')
if (query.length < 3) throw new Error('Query too short')
if (cache) {
const cachedData = queryClient.getQueryData<Result[]>(queryKey.simpleSearch(query))
if (cachedData) return cachedData
}
const results = await Promise.allSettled([
queryByName(query),
queryByName(`${query}.eth`),
...(isAddress(query) ? [queryByAddress(query)] : []),
])
const filteredData = results
.filter<PromiseFulfilledResult<Result>>(
(item): item is PromiseFulfilledResult<Result> =>
item.status === 'fulfilled' && !!item.value,
)
.map((item) => item.value)
.reduce((acc, cur) => {
return {
...acc,
[cur.address]: cur,
}
}, {})
return Object.values(filteredData) as Result[]
},
{
onSuccess: (data, variables) => {
queryClient.setQueryData(queryKey.simpleSearch(variables), data)
},
},
[getName],
)

const { mutate, isLoading, ...rest } = useMutation(async (query: string) => {
if (!ready) throw new Error('ENSJS not ready')
if (query.length < 3) throw new Error('Query too short')
const results = await Promise.allSettled([
queryByName(query),
queryByName(`${query}.eth`),
...(isAddress(query) ? [queryByAddress(query)] : []),
])
const filteredData = results
.filter<PromiseFulfilledResult<Result>>(
(item): item is PromiseFulfilledResult<Result> =>
item.status === 'fulfilled' && !!item.value,
)
.map((item) => item.value)
.reduce((acc, cur) => {
return {
...acc,
[cur.address]: cur,
}
}, {})
return Object.values(filteredData) as Result[]
})
const debouncedMutate = useDebouncedCallback(mutate, 500)

return {
Expand Down
46 changes: 9 additions & 37 deletions src/transaction-flow/input/SendName/SendName-flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { useAccountSafely } from '@app/hooks/useAccountSafely'
import { useBasicName } from '@app/hooks/useBasicName'
import { useNameType } from '@app/hooks/useNameType'
import { useResolver } from '@app/hooks/useResolver'
import { makeTransactionItem } from '@app/transaction-flow/transaction'
import { TransactionDialogPassthrough } from '@app/transaction-flow/types'

import { checkCanSend, senderRole } from './utils/checkCanSend'
import { getSendNameTransactions } from './utils/getSendNameTransactions'
import { CannotSendView } from './views/CannotSendView'
import { ConfirmationView } from './views/ConfirmationView'
import { SearchView } from './views/SearchView/SearchView'
Expand Down Expand Up @@ -99,42 +99,14 @@ const SendName = ({ data: { name }, dispatch, onDismiss }: Props) => {
const isOwnerOrManager =
account.address === basic.ownerData?.owner || basic.ownerData?.registrant === account.address

const setEthRecordOnly = transactions.setEthRecord && !transactions.resetProfile
// Anytime you reset the profile you will need to set the eth record as well
const setEthRecordAndResetProfile = transactions.resetProfile

const _transactions = [
setEthRecordOnly
? makeTransactionItem('updateEthAddress', { name, address: recipient })
: null,
setEthRecordAndResetProfile
? makeTransactionItem('resetProfileWithRecords', {
name,
records: {
coinTypes: [{ key: 'ETH', value: recipient }],
},
resolver: resolver.data,
})
: null,

transactions.sendManager && !!abilities.data?.sendNameFunctionCallDetails?.sendManager
? makeTransactionItem(isOwnerOrManager ? 'transferName' : 'transferSubname', {
name,
newOwner: recipient,
sendType: 'sendManager',
contract: abilities.data?.sendNameFunctionCallDetails?.sendManager?.contract,
reclaim: abilities.data?.sendNameFunctionCallDetails?.sendManager?.method === 'reclaim',
})
: null,
transactions.sendOwner && !!abilities.data?.sendNameFunctionCallDetails?.sendOwner
? makeTransactionItem(isOwnerOrManager ? 'transferName' : 'transferSubname', {
name,
newOwner: recipient,
sendType: 'sendOwner',
contract: abilities.data?.sendNameFunctionCallDetails?.sendOwner?.contract,
})
: null,
].filter((transaction) => !!transaction)
const _transactions = getSendNameTransactions({
name,
recipient,
transactions,
isOwnerOrManager,
abilities: abilities.data,
resolverAddress: resolver.data,
})

if (_transactions.length === 0) return

Expand Down
Loading

0 comments on commit 770b34e

Please sign in to comment.