@@ -17,6 +17,17 @@ interface TokenOperationProps {
1717 refetch : ( ) => void ;
1818}
1919
20+ const normalizeTokenAmount = ( amount : string ) : string | null => {
21+ const value = amount . trim ( ) ;
22+ const normalized = value . startsWith ( "." ) ? `0${ value } ` : value ;
23+
24+ if ( ! / ^ \d + ( \. \d + ) ? $ / . test ( normalized ) ) {
25+ return null ;
26+ }
27+
28+ return / [ 1 - 9 ] / . test ( normalized ) ? normalized : null ;
29+ } ;
30+
2031const TokenOperation = ( {
2132 useMutationHook,
2233 buildRequest,
@@ -30,6 +41,8 @@ const TokenOperation = ({
3041 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
3142 const [ showModal , setShowModal ] = useState ( false ) ;
3243 const [ inputValue , setInputValue ] = useState ( value ) ;
44+ const availableAmount = normalizeTokenAmount ( value ) ;
45+ const normalizedInputValue = normalizeTokenAmount ( inputValue ) ;
3346 const handleTransactionLifecycle = useTransactionLifecycle ( ) ;
3447 const waitForTransactionToComplete = useWaitForTransactionToComplete ( ) ;
3548
@@ -66,16 +79,22 @@ const TokenOperation = ({
6679 } ) ;
6780
6881 const handleSubmit = ( ) => {
82+ if ( ! normalizedInputValue ) {
83+ return toast . error ( "Enter a valid amount" ) ;
84+ }
85+
6986 setIsSubmitting ( true ) ;
7087 umami . track ( title . toLowerCase ( ) . replace ( " " , "_" ) ) ;
7188
72- return mutate ( { variables : { request : buildRequest ( inputValue ) } } ) ;
89+ return mutate ( {
90+ variables : { request : buildRequest ( normalizedInputValue ) }
91+ } ) ;
7392 } ;
7493
7594 return (
7695 < >
7796 < Button
78- disabled = { isSubmitting || inputValue === "0" }
97+ disabled = { isSubmitting || ! availableAmount }
7998 loading = { isSubmitting }
8099 onClick = { ( ) => setShowModal ( true ) }
81100 outline
@@ -99,7 +118,7 @@ const TokenOperation = ({
99118 </ div >
100119 < Button
101120 className = "w-full"
102- disabled = { isSubmitting || ! inputValue || inputValue === "0" }
121+ disabled = { isSubmitting || ! normalizedInputValue }
103122 loading = { isSubmitting }
104123 onClick = { handleSubmit }
105124 size = "lg"
0 commit comments