1
- import { thirdwebClient } from "@/constants/client" ;
2
1
import { AdminOnly } from "@3rdweb-sdk/react/components/roles/admin-only" ;
3
2
import { Flex , FormControl } from "@chakra-ui/react" ;
4
3
import { zodResolver } from "@hookform/resolvers/zod" ;
5
- import {
6
- usePrimarySaleRecipient ,
7
- useUpdatePrimarySaleRecipient ,
8
- } from "@thirdweb-dev/react" ;
9
- import {
10
- CommonPrimarySaleSchema ,
11
- type ValidContractInstance ,
12
- } from "@thirdweb-dev/sdk" ;
13
4
import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton" ;
14
5
import { TransactionButton } from "components/buttons/TransactionButton" ;
6
+ import { AddressOrEnsSchema } from "constants/schemas" ;
15
7
import { SolidityInput } from "contract-ui/components/solidity-inputs" ;
16
8
import { useTrack } from "hooks/analytics/useTrack" ;
17
9
import { useTxNotifications } from "hooks/useTxNotifications" ;
18
- import { useV5DashboardChain } from "lib/v5-adapter" ;
19
10
import { useForm } from "react-hook-form" ;
20
- import { getContract } from "thirdweb" ;
21
- import { useActiveAccount } from "thirdweb/react" ;
11
+ import { toast } from "sonner" ;
12
+ import { type ThirdwebContract , ZERO_ADDRESS } from "thirdweb" ;
13
+ import {
14
+ primarySaleRecipient ,
15
+ setPrimarySaleRecipient ,
16
+ } from "thirdweb/extensions/common" ;
17
+ import {
18
+ useActiveAccount ,
19
+ useReadContract ,
20
+ useSendAndConfirmTransaction ,
21
+ } from "thirdweb/react" ;
22
22
import {
23
23
Card ,
24
24
FormErrorMessage ,
25
25
FormLabel ,
26
26
Heading ,
27
27
Text ,
28
28
} from "tw-components" ;
29
- import type { z } from "zod" ;
29
+ import { z } from "zod" ;
30
30
import { SettingDetectedState } from "./detected-state" ;
31
31
32
- export const SettingsPrimarySale = <
33
- TContract extends ValidContractInstance | undefined ,
34
- > ( {
32
+ const CommonPrimarySaleSchema = z . object ( {
33
+ primary_sale_recipient : AddressOrEnsSchema . default ( ZERO_ADDRESS ) ,
34
+ } ) ;
35
+
36
+ export const SettingsPrimarySale = ( {
35
37
contract,
36
38
detectedState,
37
39
} : {
38
- contract : TContract ;
40
+ contract : ThirdwebContract ;
39
41
detectedState : ExtensionDetectedState ;
40
42
} ) => {
41
43
const address = useActiveAccount ( ) ?. address ;
42
44
const trackEvent = useTrack ( ) ;
43
- const query = usePrimarySaleRecipient ( contract ) ;
44
- const mutation = useUpdatePrimarySaleRecipient ( contract ) ;
45
+ const query = useReadContract ( primarySaleRecipient , {
46
+ contract,
47
+ } ) ;
48
+ const mutation = useSendAndConfirmTransaction ( ) ;
45
49
46
50
const transformedQueryData = {
47
51
primary_sale_recipient : query . data ,
@@ -53,20 +57,10 @@ export const SettingsPrimarySale = <
53
57
values : transformedQueryData ,
54
58
} ) ;
55
59
56
- const chain = useV5DashboardChain ( contract ?. chainId ) ;
57
- const contractV5 =
58
- contract && chain
59
- ? getContract ( {
60
- address : contract . getAddress ( ) ,
61
- chain : chain ,
62
- client : thirdwebClient ,
63
- } )
64
- : null ;
65
-
66
60
const { onSuccess, onError } = useTxNotifications (
67
61
"Primary sale address updated" ,
68
62
"Error updating primary sale address" ,
69
- contractV5 ,
63
+ contract ,
70
64
) ;
71
65
72
66
return (
@@ -80,15 +74,25 @@ export const SettingsPrimarySale = <
80
74
action : "set-primary-sale" ,
81
75
label : "attempt" ,
82
76
} ) ;
77
+ const saleRecipient = d . primary_sale_recipient ;
78
+ if ( ! saleRecipient ) {
79
+ return toast . error (
80
+ "Please enter a valid primary sale recipient address" ,
81
+ ) ;
82
+ }
83
+ const transaction = setPrimarySaleRecipient ( {
84
+ contract,
85
+ saleRecipient,
86
+ } ) ;
83
87
// if we switch back to mutateAsync then *need* to catch errors
84
- mutation . mutate ( d . primary_sale_recipient as string , {
85
- onSuccess : ( _data , variables ) => {
88
+ mutation . mutate ( transaction , {
89
+ onSuccess : ( ) => {
86
90
trackEvent ( {
87
91
category : "settings" ,
88
92
action : "set-primary-sale" ,
89
93
label : "success" ,
90
94
} ) ;
91
- form . reset ( { primary_sale_recipient : variables } ) ;
95
+ form . reset ( { primary_sale_recipient : saleRecipient } ) ;
92
96
onSuccess ( ) ;
93
97
} ,
94
98
onError : ( error ) => {
@@ -112,15 +116,15 @@ export const SettingsPrimarySale = <
112
116
</ Text >
113
117
< Flex gap = { 4 } direction = { { base : "column" , md : "row" } } >
114
118
< FormControl
115
- isDisabled = { mutation . isLoading || ! address }
119
+ isDisabled = { mutation . isPending || ! address }
116
120
isInvalid = {
117
121
! ! form . getFieldState ( "primary_sale_recipient" , form . formState )
118
122
. error
119
123
}
120
124
>
121
125
< FormLabel > Recipient Address</ FormLabel >
122
126
< SolidityInput
123
- isDisabled = { mutation . isLoading || ! address }
127
+ isDisabled = { mutation . isPending || ! address }
124
128
solidityType = "address"
125
129
formContext = { form }
126
130
variant = "filled"
@@ -135,24 +139,22 @@ export const SettingsPrimarySale = <
135
139
</ FormControl >
136
140
</ Flex >
137
141
</ Flex >
138
- { contractV5 && (
139
- < AdminOnly contract = { contractV5 } >
140
- < TransactionButton
141
- colorScheme = "primary"
142
- transactionCount = { 1 }
143
- isDisabled = { query . isLoading || ! form . formState . isDirty }
144
- type = "submit"
145
- isLoading = { mutation . isLoading }
146
- loadingText = "Saving..."
147
- size = "md"
148
- borderRadius = "xl"
149
- borderTopLeftRadius = "0"
150
- borderTopRightRadius = "0"
151
- >
152
- Update Primary Sale Settings
153
- </ TransactionButton >
154
- </ AdminOnly >
155
- ) }
142
+ < AdminOnly contract = { contract } >
143
+ < TransactionButton
144
+ colorScheme = "primary"
145
+ transactionCount = { 1 }
146
+ isDisabled = { query . isLoading || ! form . formState . isDirty }
147
+ type = "submit"
148
+ isLoading = { mutation . isPending }
149
+ loadingText = "Saving..."
150
+ size = "md"
151
+ borderRadius = "xl"
152
+ borderTopLeftRadius = "0"
153
+ borderTopRightRadius = "0"
154
+ >
155
+ Update Primary Sale Settings
156
+ </ TransactionButton >
157
+ </ AdminOnly >
156
158
</ Flex >
157
159
</ Card >
158
160
) ;
0 commit comments