-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stanislav Lysak
committed
Nov 19, 2024
1 parent
6bdf235
commit e6df0a9
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |