|
| 1 | +import { type FC } from 'react' |
| 2 | +import { useQuery } from '@tanstack/react-query' |
| 3 | +import axios from 'axios' |
| 4 | +import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton' |
| 5 | +import { RoflInstance } from '../../../nexus/generated/api.ts' |
| 6 | +import { useERC8004TokenURI } from '../../../hooks/useERC8004TokenURI.ts' |
| 7 | +import { fromMetadataToAgentId, tokenURIToLink } from '../../../utils/rofl-8004.ts' |
| 8 | +import { CodeDisplay } from '../../../components/CodeDisplay' |
| 9 | + |
| 10 | +type Props = { |
| 11 | + roflInstances: RoflInstance[] |
| 12 | +} |
| 13 | + |
| 14 | +export const AppERC8004: FC<Props> = ({ roflInstances }) => { |
| 15 | + // TODO: Display all instances |
| 16 | + const [instance] = roflInstances |
| 17 | + const tokenId = fromMetadataToAgentId(instance.metadata) |
| 18 | + const { |
| 19 | + isLoading: isLoadingTokenURI, |
| 20 | + data: tokenURI, |
| 21 | + error: tokenURIError, |
| 22 | + } = useERC8004TokenURI(BigInt(tokenId)) |
| 23 | + |
| 24 | + const { |
| 25 | + data: metadata, |
| 26 | + isLoading: isLoadingMetadata, |
| 27 | + error: metadataError, |
| 28 | + } = useQuery({ |
| 29 | + queryKey: ['erc8004-metadata', tokenURI], |
| 30 | + queryFn: async () => { |
| 31 | + if (!tokenURI) throw new Error('No tokenURI available') |
| 32 | + const httpLink = tokenURIToLink(tokenURI) |
| 33 | + const response = await axios.get(httpLink) |
| 34 | + return response.data |
| 35 | + }, |
| 36 | + enabled: !!tokenURI, |
| 37 | + }) |
| 38 | + |
| 39 | + const isLoading = isLoadingTokenURI || isLoadingMetadata |
| 40 | + const error = tokenURIError || metadataError |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="space-y-6"> |
| 44 | + {isLoading && <Skeleton className="w-full h-[400px]" />} |
| 45 | + {!isLoading && ( |
| 46 | + <> |
| 47 | + {metadata && ( |
| 48 | + <div> |
| 49 | + <h3 className="text-lg font-semibold mb-2">ERC-8004 Metadata</h3> |
| 50 | + <CodeDisplay data={JSON.stringify(metadata, null, '\t')} format="json" /> |
| 51 | + </div> |
| 52 | + )} |
| 53 | + {error && <div className="text-red-500">Error loading metadata: {error.message}</div>} |
| 54 | + </> |
| 55 | + )} |
| 56 | + </div> |
| 57 | + ) |
| 58 | +} |
0 commit comments