From e6df0a9923f2b27439887ec5aa8f3f44f12b2e23 Mon Sep 17 00:00:00 2001 From: Stanislav Lysak Date: Tue, 19 Nov 2024 10:35:50 +0200 Subject: [PATCH] review updates --- src/hooks/pages/profile/useRenew.ts | 50 +++++++++++++++++++ .../input/ExtendNames/ExtendNames-flow.tsx | 2 +- src/utils/string.ts | 9 ++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/hooks/pages/profile/useRenew.ts create mode 100644 src/utils/string.ts diff --git a/ src/hooks/pages/profile/useRenew.ts b/ src/hooks/pages/profile/useRenew.ts new file mode 100644 index 000000000..8b6e17f7f --- /dev/null +++ b/ src/hooks/pages/profile/useRenew.ts @@ -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(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]) +} diff --git a/src/transaction-flow/input/ExtendNames/ExtendNames-flow.tsx b/src/transaction-flow/input/ExtendNames/ExtendNames-flow.tsx index 7f374ef1e..22d2d61b2 100644 --- a/src/transaction-flow/input/ExtendNames/ExtendNames-flow.tsx +++ b/src/transaction-flow/input/ExtendNames/ExtendNames-flow.tsx @@ -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') diff --git a/src/utils/string.ts b/src/utils/string.ts new file mode 100644 index 000000000..c2467d224 --- /dev/null +++ b/src/utils/string.ts @@ -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 +}