|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { StyledTd } from "./Table"; |
| 3 | +import { Spinner } from "./Spinner"; |
| 4 | +const fetchLazerPriceIdMetadata = async () => { |
| 5 | + const response = await fetch( |
| 6 | + "https://pyth-lazer-staging.dourolabs.app/history/v1/symbols" |
| 7 | + ); |
| 8 | + const data = await response.json(); |
| 9 | + return data; |
| 10 | +}; |
| 11 | + |
| 12 | +type LazerPriceIdMetadata = { |
| 13 | + asset_type: string; |
| 14 | + description: string; |
| 15 | + exponent: number; |
| 16 | + name: string; |
| 17 | + pyth_lazer_id: number; |
| 18 | + symbol: string; |
| 19 | +}; |
| 20 | + |
| 21 | +enum LazerPriceIdStateType { |
| 22 | + NotLoaded, |
| 23 | + Loading, |
| 24 | + Loaded, |
| 25 | + Error, |
| 26 | +} |
| 27 | + |
| 28 | +const LazerPriceIdState = { |
| 29 | + NotLoaded: () => ({ type: LazerPriceIdStateType.NotLoaded as const }), |
| 30 | + Loading: () => ({ type: LazerPriceIdStateType.Loading as const }), |
| 31 | + Loaded: (priceFeeds: LazerPriceIdMetadata[]) => ({ |
| 32 | + type: LazerPriceIdStateType.Loaded as const, |
| 33 | + priceFeeds, |
| 34 | + }), |
| 35 | + Error: (error: unknown) => ({ |
| 36 | + type: LazerPriceIdStateType.Error as const, |
| 37 | + error, |
| 38 | + }), |
| 39 | +}; |
| 40 | + |
| 41 | +type LazerPriceIdState = ReturnType< |
| 42 | + typeof LazerPriceIdState[keyof typeof LazerPriceIdState] |
| 43 | +>; |
| 44 | + |
| 45 | +const useLazerPriceIdState = () => { |
| 46 | + const [state, setState] = useState<LazerPriceIdState>( |
| 47 | + LazerPriceIdState.NotLoaded() |
| 48 | + ); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + setState(LazerPriceIdState.Loading()); |
| 52 | + fetchLazerPriceIdMetadata() |
| 53 | + .then((priceFeeds) => setState(LazerPriceIdState.Loaded(priceFeeds))) |
| 54 | + .catch((error) => setState(LazerPriceIdState.Error(error))); |
| 55 | + }, []); |
| 56 | + |
| 57 | + return state; |
| 58 | +}; |
| 59 | + |
| 60 | +export function LazerPriceIdTable() { |
| 61 | + const lazerPriceIdState = useLazerPriceIdState(); |
| 62 | + |
| 63 | + switch (lazerPriceIdState.type) { |
| 64 | + case LazerPriceIdStateType.NotLoaded: |
| 65 | + return <div>Loading...</div>; |
| 66 | + case LazerPriceIdStateType.Loading: |
| 67 | + return <Spinner />; |
| 68 | + case LazerPriceIdStateType.Loaded: |
| 69 | + return ( |
| 70 | + <table> |
| 71 | + <thead> |
| 72 | + <tr> |
| 73 | + <th>Asset Type</th> |
| 74 | + <th>Description</th> |
| 75 | + <th>Name</th> |
| 76 | + <th>Symbol</th> |
| 77 | + <th>Pyth Lazer Id</th> |
| 78 | + <th>Exponent</th> |
| 79 | + </tr> |
| 80 | + </thead> |
| 81 | + <tbody> |
| 82 | + {lazerPriceIdState.priceFeeds.map((priceFeed) => ( |
| 83 | + <tr key={priceFeed.symbol}> |
| 84 | + <StyledTd>{priceFeed.asset_type}</StyledTd> |
| 85 | + <StyledTd>{priceFeed.description}</StyledTd> |
| 86 | + <StyledTd>{priceFeed.name}</StyledTd> |
| 87 | + <StyledTd>{priceFeed.symbol}</StyledTd> |
| 88 | + <StyledTd>{priceFeed.pyth_lazer_id}</StyledTd> |
| 89 | + <StyledTd>{priceFeed.exponent}</StyledTd> |
| 90 | + </tr> |
| 91 | + ))} |
| 92 | + </tbody> |
| 93 | + </table> |
| 94 | + ); |
| 95 | + case LazerPriceIdStateType.Error: |
| 96 | + return <div>Error</div>; |
| 97 | + } |
| 98 | +} |
0 commit comments