33
44'use client' ;
55
6+ import { Warning } from '@iota/apps-ui-icons' ;
67import {
78 Button ,
89 ButtonType ,
@@ -13,6 +14,9 @@ import {
1314 DialogPosition ,
1415 DisplayStats ,
1516 Header ,
17+ InfoBox ,
18+ InfoBoxStyle ,
19+ InfoBoxType ,
1620 LoadingIndicator ,
1721 Panel ,
1822 Select ,
@@ -23,8 +27,7 @@ import { normalizeIotaName } from '@iota/iota-names-sdk';
2327import { useMutation , useQueryClient } from '@tanstack/react-query' ;
2428import { useState } from 'react' ;
2529
26- import { NameUpdate , queryKey , useUpdateNameTransaction } from '@/hooks' ;
27- import { useBalance } from '@/hooks/useBalance' ;
30+ import { NameUpdate , queryKey , useBalanceValidation , useUpdateNameTransaction } from '@/hooks' ;
2831import { useCoreConfig } from '@/hooks/useCoreConfig' ;
2932import { useNameRecord } from '@/hooks/useNameRecord' ;
3033import {
@@ -86,11 +89,14 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
8689 updates : updates ,
8790 } ) ;
8891
92+ const { data : balanceValidation , error : balanceValidationError } = useBalanceValidation (
93+ updateNameData ?. builtTx ?? null ,
94+ price ,
95+ ) ;
96+
8997 const { mutateAsync : signAndExecuteTransaction , isPending : isSendingTransaction } =
9098 useSignAndExecuteTransaction ( ) ;
9199
92- const { data : coinBalance , error : coinBalanceError } = useBalance ( account ?. address ?? '' ) ;
93-
94100 const {
95101 mutateAsync : handlePurchase ,
96102 error : purchaseError ,
@@ -130,19 +136,17 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
130136 } ) )
131137 : [ ] ;
132138
133- const totalBalance = Number ( coinBalance ?. totalBalance ) || 0 ;
134- const totalGas = Number ( updateNameData ?. gasSummary ?. totalGas ) || 0 ;
135- const totalPrice = nameRecordData ?. type === 'available' ? nameRecordData . price + totalGas : 0 ;
136- const hasBalance = totalBalance > totalPrice ;
137-
138139 const hasEnoughGas =
139140 ! updateNameError ?. message . includes ( NOT_ENOUGH_BALANCE_ID ) &&
140141 ! updateNameError ?. message . includes ( GAS_BALANCE_TOO_LOW_ID ) ;
141142
142143 const canPay =
143- isConnected && hasEnoughGas && hasBalance && nameRecordData ?. type === 'available' ;
144+ isConnected &&
145+ hasEnoughGas &&
146+ balanceValidation ?. hasBalance &&
147+ nameRecordData ?. type === 'available' ;
144148
145- const hasErrors = updateNameError || coinBalanceError || purchaseError ;
149+ const hasErrors = updateNameError || balanceValidationError || purchaseError ;
146150
147151 const isLoading = isNameRecordLoading || isUpdateNameLoading || isSigning ;
148152
@@ -186,12 +190,63 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
186190 </ div >
187191 </ Panel >
188192 < div className = "flex flex-row gap-x-sm w-full" >
189- < DisplayStats label = "Registration Expires" value = { expirationDate } />
190- < DisplayStats
191- label = "Total Due"
192- value = { formatNanosToIota ( totalPrice ) }
193- />
193+ { ! isLoading &&
194+ balanceValidation &&
195+ typeof balanceValidation ?. totalPrice === 'number' &&
196+ balanceValidation . totalPrice > 0 && (
197+ < >
198+ < DisplayStats
199+ label = "Registration Expires"
200+ value = { expirationDate }
201+ />
202+ < DisplayStats
203+ label = "Total Due"
204+ value = { formatNanosToIota (
205+ balanceValidation . totalPrice ,
206+ ) }
207+ />
208+ </ >
209+ ) }
194210 </ div >
211+
212+ { ! hasEnoughGas && (
213+ < InfoBox
214+ title = "Error"
215+ supportingText = {
216+ GAS_BUDGET_ERROR_MESSAGES [ GAS_BALANCE_TOO_LOW_ID ]
217+ }
218+ icon = { < Warning /> }
219+ type = { InfoBoxType . Error }
220+ style = { InfoBoxStyle . Default }
221+ />
222+ ) }
223+ { purchaseError && (
224+ < InfoBox
225+ title = "Error"
226+ supportingText = { purchaseError . message }
227+ icon = { < Warning /> }
228+ type = { InfoBoxType . Error }
229+ style = { InfoBoxStyle . Default }
230+ />
231+ ) }
232+ { nameRecordError && (
233+ < InfoBox
234+ title = "Error"
235+ supportingText = { nameRecordError . message }
236+ icon = { < Warning /> }
237+ type = { InfoBoxType . Error }
238+ style = { InfoBoxStyle . Default }
239+ />
240+ ) }
241+ { hasEnoughGas && updateNameError && (
242+ < InfoBox
243+ title = "Error"
244+ supportingText = { updateNameError . message }
245+ icon = { < Warning /> }
246+ type = { InfoBoxType . Error }
247+ style = { InfoBoxStyle . Default }
248+ />
249+ ) }
195250 < div className = "flex w-full flex-row gap-x-xs mt-xs" >
196251 < Button
197252 type = { ButtonType . Secondary }
@@ -209,26 +264,6 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
209264 />
210265 </ div >
211266 </ div >
212- { ! hasEnoughGas && (
213- < div className = "text-center text-red-400 text-sm" >
214- { GAS_BUDGET_ERROR_MESSAGES [ GAS_BALANCE_TOO_LOW_ID ] }
215- </ div >
216- ) }
217- { purchaseError && (
218- < div className = "text-center text-red-400 text-sm" >
219- { purchaseError . message }
220- </ div >
221- ) }
222- { nameRecordError && (
223- < div className = "text-center text-red-400 text-sm" >
224- { nameRecordError . message }
225- </ div >
226- ) }
227- { hasEnoughGas && updateNameError && (
228- < div className = "text-center text-red-400 text-sm" >
229- { updateNameError . message }
230- </ div >
231- ) }
232267 </ div >
233268 </ DialogBody >
234269 </ DialogContent >
0 commit comments