9
9
Field ,
10
10
Label ,
11
11
} from "@headlessui/react" ;
12
+ import { ArrowPathIcon } from "@heroicons/react/24/outline" ;
12
13
import PythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json" ;
13
14
import PythErrorsAbi from "@pythnetwork/pyth-sdk-solidity/abis/PythErrors.json" ;
14
15
import { ChainIcon } from "connectkit" ;
@@ -32,6 +33,7 @@ import { ParameterInput } from "./parameter-input";
32
33
import { type EvmApiType , RunButton } from "./run-button" ;
33
34
import { useIsMounted } from "../../use-is-mounted" ;
34
35
import { type SupportedLanguage , Code } from "../Code" ;
36
+ import { ErrorTooltip } from "../ErrorTooltip" ;
35
37
import { InlineLink } from "../InlineLink" ;
36
38
import { Select } from "../Select" ;
37
39
@@ -258,9 +260,12 @@ const Example = <ParameterName extends string>({
258
260
setParamValues,
259
261
} : ExampleProps < ParameterName > ) => {
260
262
const config = useConfig ( ) ;
263
+ const [ error , setError ] = useState < string | undefined > ( undefined ) ;
264
+ const [ loading , setLoading ] = useState ( false ) ;
261
265
262
266
const updateValues = useCallback ( ( ) => {
263
267
if ( typeof example . parameters === "function" ) {
268
+ setError ( undefined ) ;
264
269
const address = getContractAddress ( config . state . chainId ) ;
265
270
if ( ! address ) {
266
271
throw new Error (
@@ -272,12 +277,18 @@ const Example = <ParameterName extends string>({
272
277
readContract ( config , { abi, address, functionName, args } ) ,
273
278
} ) ;
274
279
if ( params instanceof Promise ) {
280
+ setLoading ( true ) ;
275
281
params
276
282
. then ( ( paramsResolved ) => {
277
283
setParamValues ( paramsResolved ) ;
278
284
} )
279
285
. catch ( ( ) => {
280
- /* TODO add some UI when this errors */
286
+ setError (
287
+ "An error occurred while fetching data for this example, please try again" ,
288
+ ) ;
289
+ } )
290
+ . finally ( ( ) => {
291
+ setLoading ( false ) ;
281
292
} ) ;
282
293
} else {
283
294
setParamValues ( params ) ;
@@ -289,13 +300,17 @@ const Example = <ParameterName extends string>({
289
300
const Icon = example . icon ;
290
301
291
302
return (
292
- < InlineLink
293
- as = "button"
294
- onClick = { updateValues }
295
- className = "flex flex-row items-center gap-2"
296
- >
297
- { Icon && < Icon className = "h-4" /> }
298
- < span > { example . name } </ span >
299
- </ InlineLink >
303
+ < div className = "flex flex-row items-center gap-2" >
304
+ < InlineLink
305
+ as = "button"
306
+ onClick = { updateValues }
307
+ className = "flex flex-row items-center gap-2"
308
+ >
309
+ { Icon && < Icon className = "h-4" /> }
310
+ < span > { example . name } </ span >
311
+ </ InlineLink >
312
+ { error && < ErrorTooltip className = "size-4" > { error } </ ErrorTooltip > }
313
+ { loading && < ArrowPathIcon className = "size-4 animate-spin" /> }
314
+ </ div >
300
315
) ;
301
316
} ;
0 commit comments