-
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
1 parent
9c56c21
commit f74504b
Showing
1 changed file
with
70 additions
and
29 deletions.
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 |
---|---|---|
@@ -1,50 +1,91 @@ | ||
import { useConnectModal } from '@rainbow-me/rainbowkit' | ||
import { useSearchParams } from 'next/navigation' | ||
import { useEffect, useRef } from 'react' | ||
import { useEffect } from 'react' | ||
import { match } from 'ts-pattern' | ||
import { useAccount } from 'wagmi' | ||
|
||
import { useAbilities } from '@app/hooks/abilities/useAbilities' | ||
import { useNameDetails } from '@app/hooks/useNameDetails' | ||
import { useBasicName } from '@app/hooks/useBasicName' | ||
import { useRouterWithHistory } from '@app/hooks/useRouterWithHistory' | ||
import { useTransactionFlow } from '@app/transaction-flow/TransactionFlowProvider' | ||
import { RegistrationStatus } from '@app/utils/registrationStatus' | ||
import { parseNumericString } from '@app/utils/string' | ||
|
||
type RenewStatus = 'connect-user' | 'display-extend-names' | 'idle' | ||
|
||
export const calculateRenewState = ({ | ||
registrationStatus, | ||
isRegistrationStatusLoading, | ||
renewSeconds, | ||
openConnectModal, | ||
connectModalOpen, | ||
accountStatus, | ||
isAbilitiesLoading, | ||
name, | ||
}: { | ||
registrationStatus?: RegistrationStatus | ||
isRegistrationStatusLoading: boolean | ||
renewSeconds: number | null | ||
connectModalOpen: boolean | ||
openConnectModal: ReturnType<typeof useConnectModal>['openConnectModal'] | ||
accountStatus: ReturnType<typeof useAccount>['status'] | ||
isAbilitiesLoading: boolean | ||
name?: string | ||
}): RenewStatus => { | ||
const isNameRegisteredOrGracePeriod = | ||
registrationStatus === 'registered' || registrationStatus === 'gracePeriod' | ||
const isRenewActive = | ||
!isRegistrationStatusLoading && | ||
!!name && | ||
isNameRegisteredOrGracePeriod && | ||
!!renewSeconds && | ||
!connectModalOpen | ||
|
||
if (isRenewActive && accountStatus === 'disconnected' && !!openConnectModal) return 'connect-user' | ||
if (isRenewActive && accountStatus === 'connected' && !isAbilitiesLoading) | ||
return 'display-extend-names' | ||
return 'idle' | ||
} | ||
|
||
export function useRenew(name: string) { | ||
const { registrationStatus, isLoading } = useNameDetails({ name }) | ||
const router = useRouterWithHistory() | ||
const { registrationStatus, isLoading: isBasicNameLoading } = useBasicName({ name }) | ||
const abilities = useAbilities({ name }) | ||
const searchParams = useSearchParams() | ||
const { isConnected, isDisconnected } = useAccount() | ||
const { usePreparedDataInput } = useTransactionFlow() | ||
const { status } = useAccount() | ||
const { openConnectModal, connectModalOpen } = useConnectModal() | ||
|
||
const { usePreparedDataInput } = useTransactionFlow() | ||
const showExtendNamesInput = usePreparedDataInput('ExtendNames') | ||
|
||
const { data: { canSelfExtend } = {} } = abilities | ||
const isAvailableName = registrationStatus === 'available' | ||
const renewSeconds = parseNumericString(searchParams.get('renew')) | ||
const { data: { canSelfExtend } = {}, isLoading: isAbilitiesLoading } = abilities | ||
|
||
const prevIsConnected = useRef(isConnected) | ||
const renewSeconds = parseNumericString(searchParams.get('renew')) | ||
|
||
const isRenewActive = | ||
(!isConnected || !connectModalOpen) && !!renewSeconds && !isLoading && !isAvailableName | ||
const renewState = calculateRenewState({ | ||
registrationStatus, | ||
isRegistrationStatusLoading: isBasicNameLoading, | ||
renewSeconds, | ||
connectModalOpen, | ||
accountStatus: status, | ||
isAbilitiesLoading, | ||
name, | ||
openConnectModal, | ||
}) | ||
|
||
// http://localhost:3000/anyname.eth?renew=63072000 | ||
useEffect(() => { | ||
if (!isRenewActive) return | ||
|
||
if (isDisconnected && prevIsConnected.current) { | ||
openConnectModal?.() | ||
return | ||
} | ||
|
||
if (isConnected) { | ||
showExtendNamesInput(`extend-names-${name}`, { | ||
names: [name], | ||
isSelf: canSelfExtend, | ||
seconds: renewSeconds, | ||
match(renewState) | ||
.with('connect-user', () => openConnectModal?.()) | ||
.with('display-extend-names', () => { | ||
showExtendNamesInput(`extend-names-${name}`, { | ||
names: [name], | ||
isSelf: canSelfExtend, | ||
seconds: renewSeconds!, | ||
}) | ||
router.replace(`/${name}`) | ||
}) | ||
} | ||
|
||
prevIsConnected.current = !isConnected | ||
|
||
.with('idle', () => {}) | ||
.exhaustive() | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isRenewActive, isConnected, isDisconnected, name, canSelfExtend]) | ||
}, [renewState]) | ||
} |