@@ -15,6 +15,8 @@ import { isUndefined } from "utils/index";
1515import { wrapWithToast } from "utils/wrapWithToast" ;
1616
1717import { EnsureChain } from "components/EnsureChain" ;
18+ import { ErrorButtonMessage } from "components/ErrorButtonMessage" ;
19+ import ClosedCircleIcon from "components/StyledIcons/ClosedCircleIcon" ;
1820
1921const Container = styled . div `
2022 display: flex;
@@ -46,6 +48,7 @@ const StyledButton = styled(Button)`
4648const StyledLabel = styled . label `
4749 align-self: flex-start;
4850` ;
51+
4952const useNeedFund = ( ) => {
5053 const { loserSideCountdown } = useCountdownContext ( ) ;
5154 const { fundedChoices, winningChoice } = useFundingContext ( ) ;
@@ -59,20 +62,24 @@ const useNeedFund = () => {
5962 return needFund ;
6063} ;
6164
62- const useFundAppeal = ( parsedAmount ) => {
65+ const useFundAppeal = ( parsedAmount , insufficientBalance ) => {
6366 const { id } = useParams ( ) ;
6467 const { selectedOption } = useSelectedOptionContext ( ) ;
65- const { data : fundAppealConfig , isError } = useSimulateDisputeKitClassicFundAppeal ( {
68+ const {
69+ data : fundAppealConfig ,
70+ isLoading,
71+ isError,
72+ } = useSimulateDisputeKitClassicFundAppeal ( {
6673 query : {
67- enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) ,
74+ enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) && ! insufficientBalance ,
6875 } ,
6976 args : [ BigInt ( id ?? 0 ) , BigInt ( selectedOption ?? 0 ) ] ,
7077 value : parsedAmount ,
7178 } ) ;
7279
7380 const { writeContractAsync : fundAppeal } = useWriteDisputeKitClassicFundAppeal ( ) ;
7481
75- return { fundAppeal, fundAppealConfig, isError } ;
82+ return { fundAppeal, fundAppealConfig, isLoading , isError } ;
7683} ;
7784
7885interface IFund {
@@ -98,12 +105,15 @@ const Fund: React.FC<IFund> = ({ amount, setAmount, setIsOpen }) => {
98105
99106 const parsedAmount = useParsedAmount ( debouncedAmount as `${number } `) ;
100107
101- const { fundAppealConfig, fundAppeal, isError } = useFundAppeal ( parsedAmount ) ;
108+ const insufficientBalance = useMemo ( ( ) => {
109+ return balance && balance . value < parsedAmount ;
110+ } , [ balance , parsedAmount ] ) ;
111+
112+ const { fundAppealConfig, fundAppeal, isLoading, isError } = useFundAppeal ( parsedAmount , insufficientBalance ) ;
102113
103114 const isFundDisabled = useMemo (
104- ( ) =>
105- isDisconnected || isSending || ! balance || parsedAmount > balance . value || Number ( parsedAmount ) <= 0 || isError ,
106- [ isDisconnected , isSending , balance , parsedAmount , isError ]
115+ ( ) => isDisconnected || isSending || ! balance || insufficientBalance || Number ( parsedAmount ) <= 0 || isError ,
116+ [ isDisconnected , isSending , balance , insufficientBalance , parsedAmount , isError ]
107117 ) ;
108118
109119 return needFund ? (
@@ -118,28 +128,33 @@ const Fund: React.FC<IFund> = ({ amount, setAmount, setIsOpen }) => {
118128 placeholder = "Amount to fund"
119129 />
120130 < EnsureChain >
121- < StyledButton
122- disabled = { isFundDisabled }
123- isLoading = { isSending }
124- text = { isDisconnected ? "Connect to Fund" : "Fund" }
125- onClick = { ( ) => {
126- if ( fundAppeal && fundAppealConfig && publicClient ) {
127- setIsSending ( true ) ;
128- wrapWithToast ( async ( ) => await fundAppeal ( fundAppealConfig . request ) , publicClient )
129- . then ( ( res ) => {
130- res . status && setIsOpen ( true ) ;
131- } )
132- . finally ( ( ) => {
133- setIsSending ( false ) ;
134- } ) ;
135- }
136- } }
137- />
131+ < div >
132+ < StyledButton
133+ disabled = { isFundDisabled }
134+ isLoading = { ( isSending || isLoading ) && ! insufficientBalance }
135+ text = { isDisconnected ? "Connect to Fund" : "Fund" }
136+ onClick = { ( ) => {
137+ if ( fundAppeal && fundAppealConfig && publicClient ) {
138+ setIsSending ( true ) ;
139+ wrapWithToast ( async ( ) => await fundAppeal ( fundAppealConfig . request ) , publicClient )
140+ . then ( ( res ) => {
141+ res . status && setIsOpen ( true ) ;
142+ } )
143+ . finally ( ( ) => {
144+ setIsSending ( false ) ;
145+ } ) ;
146+ }
147+ } }
148+ />
149+ { insufficientBalance && (
150+ < ErrorButtonMessage >
151+ < ClosedCircleIcon /> Insufficient balance
152+ </ ErrorButtonMessage >
153+ ) }
154+ </ div >
138155 </ EnsureChain >
139156 </ Container >
140- ) : (
141- < > </ >
142- ) ;
157+ ) : null ;
143158} ;
144159
145160export default Fund ;
0 commit comments