Skip to content

Commit

Permalink
review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Lysak committed Nov 19, 2024
1 parent 6bdf235 commit e6df0a9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/hooks/pages/profile/useRenew.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useConnectModal } from '@rainbow-me/rainbowkit'
import { useSearchParams } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useAccount } from 'wagmi'

import { useAbilities } from '@app/hooks/abilities/useAbilities'
import { useNameDetails } from '@app/hooks/useNameDetails'
import { useRouterWithHistory } from '@app/hooks/useRouterWithHistory'
import { useTransactionFlow } from '@app/transaction-flow/TransactionFlowProvider'
import { parseNumericString } from '@app/utils/string'

export function useRenew(name: string) {
const [opened, setOpened] = useState<boolean>(false)
const router = useRouterWithHistory()

const { registrationStatus, isLoading } = useNameDetails({ name })
const abilities = useAbilities({ name })
const searchParams = useSearchParams()
const { isConnected, isDisconnected } = useAccount()
const { usePreparedDataInput } = useTransactionFlow()
const { openConnectModal } = useConnectModal()
const showExtendNamesInput = usePreparedDataInput('ExtendNames')

const { data: { canSelfExtend } = {} } = abilities
const isAvailableName = registrationStatus === 'available'
const renewSeconds = parseNumericString(searchParams.get('renew'))

const isRenewActive = !opened && !!renewSeconds && !isLoading

useEffect(() => {
if (!isRenewActive) return

if (!isAvailableName && isDisconnected) {
setOpened(true)
openConnectModal?.()
return
}

if (!isAvailableName && isConnected) {
setOpened(true)
showExtendNamesInput(`extend-names-${name}`, {
names: [name],
isSelf: canSelfExtend,
seconds: renewSeconds,
})
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isRenewActive, isAvailableName, isConnected, isDisconnected, name, canSelfExtend])
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const ExtendNames = ({
}: Props) => {
const { t } = useTranslation(['transactionFlow', 'common'])

const [seconds, setSeconds] = useState(Math.max(defaultSeconds, ONE_YEAR))
const [seconds, setSeconds] = useState(defaultSeconds)
const years = secondsToYears(seconds)
const [durationType, setDurationType] = useState<'years' | 'date'>('years')

Expand Down
9 changes: 9 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const parseNumericString = (time: string | null): number | null => {
if (!time) return null

if (typeof +time === 'number' && !Number.isNaN(+time)) {
return +time
}

return null
}

0 comments on commit e6df0a9

Please sign in to comment.