|
| 1 | +import type { Metadata } from "next"; |
| 2 | +import { createPublicClient, type Hash, http } from "viem"; |
| 3 | +import { mainnet } from "viem/chains"; |
| 4 | + |
| 5 | +interface BlockLayoutProps { |
| 6 | + children: React.ReactNode; |
| 7 | + params: Promise<{ hash: string }>; |
| 8 | +} |
| 9 | + |
| 10 | +const RPC_URL = process.env.TIPS_UI_RPC_URL || "http://localhost:8545"; |
| 11 | + |
| 12 | +const client = createPublicClient({ |
| 13 | + chain: mainnet, |
| 14 | + transport: http(RPC_URL), |
| 15 | +}); |
| 16 | + |
| 17 | +function blockLabel(value: string): string { |
| 18 | + if (/^0x[0-9a-f]+$/i.test(value) && value.length < 66) { |
| 19 | + try { |
| 20 | + return `#${BigInt(value).toLocaleString()}`; |
| 21 | + } catch { |
| 22 | + return value; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if (/^[0-9]+$/.test(value)) { |
| 27 | + return `#${Number(value).toLocaleString()}`; |
| 28 | + } |
| 29 | + |
| 30 | + return `${value.slice(0, 10)}...${value.slice(-8)}`; |
| 31 | +} |
| 32 | + |
| 33 | +async function blockNumberLabel(value: string): Promise<string> { |
| 34 | + try { |
| 35 | + const block = /^0x[0-9a-f]{64}$/i.test(value) |
| 36 | + ? await client.getBlock({ blockHash: value as Hash }) |
| 37 | + : await client.getBlock({ blockNumber: BigInt(value) }); |
| 38 | + return `#${block.number.toLocaleString()}`; |
| 39 | + } catch { |
| 40 | + return blockLabel(value); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export async function generateMetadata({ |
| 45 | + params, |
| 46 | +}: BlockLayoutProps): Promise<Metadata> { |
| 47 | + const { hash } = await params; |
| 48 | + const label = await blockNumberLabel(hash); |
| 49 | + |
| 50 | + return { |
| 51 | + title: `Block ${label}`, |
| 52 | + description: `Inspect transactions, execution data, and event history for Base block ${label}.`, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +export default function BlockLayout({ children }: BlockLayoutProps) { |
| 57 | + return children; |
| 58 | +} |
0 commit comments