11"use client" ;
22
3- import { AllowListRecord } from "@/allowlists/actions/ getAllowListRecordsForAddressByClaimed" ;
3+ import { AllowListRecord } from "@/allowlists/getAllowListRecordsForAddressByClaimed" ;
44import { Button } from "../ui/button" ;
5- import { useHypercertClient } from "@/hooks/use-hypercert-client" ;
6- import { waitForTransactionReceipt } from "viem/actions" ;
7- import { useAccount , useSwitchChain , useWalletClient } from "wagmi" ;
8- import { useRouter } from "next/navigation" ;
5+ import { useAccount , useSwitchChain } from "wagmi" ;
96import { Row } from "@tanstack/react-table" ;
10- import { useStepProcessDialogContext } from "../global/step-process-dialog" ;
11- import { createExtraContent } from "../global/extra-content" ;
12- import { revalidatePathServerAction } from "@/app/actions/revalidatePathServerAction" ;
137import { useState } from "react" ;
14- import { getAddress } from "viem" ;
8+ import { useAccountStore } from "@/lib/account-store" ;
9+ import { useClaimHypercert } from "@/hypercerts/hooks/useClaimHypercert" ;
1510
1611interface UnclaimedHypercertClaimButtonProps {
1712 allowListRecord : Row < AllowListRecord > ;
@@ -20,102 +15,31 @@ interface UnclaimedHypercertClaimButtonProps {
2015export default function UnclaimedHypercertClaimButton ( {
2116 allowListRecord,
2217} : UnclaimedHypercertClaimButtonProps ) {
23- const { client } = useHypercertClient ( ) ;
24- const { data : walletClient } = useWalletClient ( ) ;
25- const account = useAccount ( ) ;
26- const { refresh } = useRouter ( ) ;
18+ const { address, chain : currentChain } = useAccount ( ) ;
19+ const { selectedAccount } = useAccountStore ( ) ;
2720 const [ isLoading , setIsLoading ] = useState ( false ) ;
28- const { setDialogStep, setSteps, setOpen, setTitle, setExtraContent } =
29- useStepProcessDialogContext ( ) ;
3021 const { switchChain } = useSwitchChain ( ) ;
31- const router = useRouter ( ) ;
32-
3322 const selectedHypercert = allowListRecord . original ;
3423 const hypercertChainId = selectedHypercert ?. hypercert_id ?. split ( "-" ) [ 0 ] ;
24+ const activeAddress = selectedAccount ?. address || ( address as `0x${string } `) ;
25+ const { mutateAsync : claimHypercert } = useClaimHypercert ( ) ;
3526
36- const refreshData = async ( address : string ) => {
37- await revalidatePathServerAction ( [
38- `/profile/${ address } ` ,
39- `/profile/${ address } ?tab` ,
40- `/profile/${ address } ?tab=hypercerts-claimable` ,
41- `/profile/${ address } ?tab=hypercerts-owned` ,
42- `/hypercerts/${ selectedHypercert ?. hypercert_id } ` ,
43- ] ) . then ( ( ) => {
44- setTimeout ( ( ) => {
45- // refresh after 5 seconds
46- router . refresh ( ) ;
47- // push to the profile page with the hypercerts-claimable tab
48- // because revalidatePath will revalidate on the next page visit.
49- router . push ( `/profile/${ address } ?tab=hypercerts-claimable` ) ;
50- } , 5000 ) ;
51- } ) ;
52- } ;
53-
54- const claimHypercert = async ( ) => {
27+ const handleClaim = async ( ) => {
5528 setIsLoading ( true ) ;
56- setOpen ( true ) ;
57- setSteps ( [
58- { id : "preparing" , description : "Preparing to claim fraction..." } ,
59- { id : "claiming" , description : "Claiming fraction on-chain..." } ,
60- { id : "confirming" , description : "Waiting for on-chain confirmation" } ,
61- { id : "route" , description : "Creating your new fraction's link..." } ,
62- { id : "done" , description : "Claiming complete!" } ,
63- ] ) ;
64-
65- setTitle ( "Claim fraction from Allowlist" ) ;
66- if ( ! client ) {
67- throw new Error ( "No client found" ) ;
68- }
69-
70- if ( ! walletClient ) {
71- throw new Error ( "No wallet client found" ) ;
72- }
73-
74- if ( ! account ) {
75- throw new Error ( "No address found" ) ;
76- }
77-
78- if (
79- ! selectedHypercert ?. units ||
80- ! selectedHypercert ?. proof ||
81- ! selectedHypercert ?. token_id
82- ) {
83- throw new Error ( "Invalid allow list record" ) ;
84- }
85- await setDialogStep ( "preparing, active" ) ;
86-
8729 try {
88- await setDialogStep ( "claiming" , "active" ) ;
89- const tx = await client . mintClaimFractionFromAllowlist (
90- BigInt ( selectedHypercert ?. token_id ) ,
91- BigInt ( selectedHypercert ?. units ) ,
92- selectedHypercert ?. proof as `0x${string } `[ ] ,
93- undefined ,
94- ) ;
95-
96- if ( ! tx ) {
97- await setDialogStep ( "claiming" , "error" ) ;
98- throw new Error ( "Failed to claim fraction" ) ;
30+ if (
31+ ! selectedHypercert . token_id ||
32+ ! selectedHypercert . units ||
33+ ! selectedHypercert . proof
34+ ) {
35+ throw new Error ( "Invalid allow list record" ) ;
9936 }
10037
101- await setDialogStep ( "confirming" , "active" ) ;
102- const receipt = await waitForTransactionReceipt ( walletClient , {
103- hash : tx ,
38+ await claimHypercert ( {
39+ tokenId : BigInt ( selectedHypercert . token_id ) ,
40+ units : BigInt ( selectedHypercert . units ) ,
41+ proof : selectedHypercert . proof as `0x${string } `[ ] ,
10442 } ) ;
105-
106- if ( receipt . status == "success" ) {
107- await setDialogStep ( "route" , "active" ) ;
108- const extraContent = createExtraContent ( {
109- receipt : receipt ,
110- hypercertId : selectedHypercert ?. hypercert_id ! ,
111- chain : account . chain ! ,
112- } ) ;
113- setExtraContent ( extraContent ) ;
114- await setDialogStep ( "done" , "completed" ) ;
115- await refreshData ( getAddress ( account . address ! ) ) ;
116- } else if ( receipt . status == "reverted" ) {
117- await setDialogStep ( "confirming" , "error" , "Transaction reverted" ) ;
118- }
11943 } catch ( error ) {
12044 console . error ( error ) ;
12145 } finally {
@@ -126,23 +50,23 @@ export default function UnclaimedHypercertClaimButton({
12650 return (
12751 < Button
12852 variant = {
129- hypercertChainId === account . chainId ?. toString ( ) ? "default" : "outline"
53+ hypercertChainId === currentChain ?. id ?. toString ( )
54+ ? "default"
55+ : "outline"
13056 }
13157 size = { "sm" }
13258 onClick = { ( ) => {
133- if ( hypercertChainId === account . chainId ?. toString ( ) ) {
134- claimHypercert ( ) ;
59+ if ( hypercertChainId === currentChain ?. id ?. toString ( ) ) {
60+ handleClaim ( ) ;
13561 } else {
13662 switchChain ( {
13763 chainId : Number ( hypercertChainId ) ,
13864 } ) ;
13965 }
14066 } }
141- disabled = {
142- selectedHypercert ?. user_address !== account . address || isLoading
143- }
67+ disabled = { selectedHypercert ?. user_address !== activeAddress || isLoading }
14468 >
145- { hypercertChainId === account . chainId ?. toString ( )
69+ { hypercertChainId === currentChain ?. id ?. toString ( )
14670 ? "Claim"
14771 : `Switch chain` }
14872 </ Button >
0 commit comments