|
| 1 | +// RunSnippet — a click-to-load "Try it" button for read-only EVM JSON-RPC calls. |
| 2 | +// |
| 3 | +// Sei's public EVM RPC endpoints return `access-control-allow-origin: *`, so a |
| 4 | +// read-only call can be issued straight from the browser with a plain `fetch` |
| 5 | +// POST — no SDK, no proxy. Mintlify's React runtime cannot import npm packages, |
| 6 | +// so this component uses only browser globals (fetch, BigInt). |
| 7 | +// |
| 8 | +// Theming note: Mintlify applies `dark:text-*` utilities (class strategy) but |
| 9 | +// NOT `dark:bg-*` / `dark:border-*` on custom-snippet container <div>s — verified |
| 10 | +// empirically. So surfaces and hairlines use theme-agnostic translucent inline |
| 11 | +// styles (rgba grey), which render correctly over both light and dark backdrops, |
| 12 | +// while text keeps the working `dark:text-*` classes. |
| 13 | +// |
| 14 | +// Usage in MDX: |
| 15 | +// import { RunSnippet } from '/snippets/run-snippet.jsx'; |
| 16 | +// |
| 17 | +// <RunSnippet method="eth_blockNumber" /> |
| 18 | +// <RunSnippet |
| 19 | +// method="eth_getBalance" |
| 20 | +// params={["0x0000000000000000000000000000000000000000", "latest"]} |
| 21 | +// network="testnet" |
| 22 | +// description="Read the SEI balance of an address at the latest block." |
| 23 | +// /> |
| 24 | +export const RunSnippet = (props) => { |
| 25 | + const { |
| 26 | + method = 'eth_blockNumber', |
| 27 | + params = [], |
| 28 | + network = 'testnet', |
| 29 | + endpoint, |
| 30 | + label, |
| 31 | + title, |
| 32 | + description, |
| 33 | + decode = 'auto' |
| 34 | + } = props || {}; |
| 35 | + |
| 36 | + // --- config (kept inside the component: module-level decls crash in Mintlify) --- |
| 37 | + const ENDPOINTS = { |
| 38 | + testnet: 'https://evm-rpc-testnet.sei-apis.com', |
| 39 | + mainnet: 'https://evm-rpc.sei-apis.com' |
| 40 | + }; |
| 41 | + const rpcUrl = endpoint || ENDPOINTS[network] || ENDPOINTS.testnet; |
| 42 | + const networkLabel = network === 'mainnet' ? 'pacific-1 · mainnet' : network === 'testnet' ? 'atlantic-2 · testnet' : network; |
| 43 | + |
| 44 | + const requestBody = { jsonrpc: '2.0', id: 1, method, params }; |
| 45 | + const requestJson = JSON.stringify(requestBody, null, 2); |
| 46 | + |
| 47 | + // --- state --- |
| 48 | + const [phase, setPhase] = useState('idle'); // idle | loading | success | error |
| 49 | + const [result, setResult] = useState(null); |
| 50 | + const [errorMsg, setErrorMsg] = useState(null); |
| 51 | + const [elapsed, setElapsed] = useState(null); |
| 52 | + const [copied, setCopied] = useState(false); |
| 53 | + const [btnHover, setBtnHover] = useState(false); |
| 54 | + |
| 55 | + // --- helpers (nested) --- |
| 56 | + const groupThousands = (s) => s.replace(/\B(?=(\d{3})+(?!\d))/g, ','); |
| 57 | + |
| 58 | + const hexToDecimal = (value) => { |
| 59 | + if (typeof value !== 'string' || !/^0x[0-9a-fA-F]+$/.test(value)) return null; |
| 60 | + try { |
| 61 | + return groupThousands(BigInt(value).toString(10)); |
| 62 | + } catch (e) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const run = async () => { |
| 68 | + setPhase('loading'); |
| 69 | + setErrorMsg(null); |
| 70 | + setResult(null); |
| 71 | + setElapsed(null); |
| 72 | + const startedAt = typeof performance !== 'undefined' ? performance.now() : null; |
| 73 | + try { |
| 74 | + const response = await fetch(rpcUrl, { |
| 75 | + method: 'POST', |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + body: JSON.stringify(requestBody) |
| 78 | + }); |
| 79 | + const data = await response.json(); |
| 80 | + if (startedAt != null && typeof performance !== 'undefined') { |
| 81 | + setElapsed(Math.round(performance.now() - startedAt)); |
| 82 | + } |
| 83 | + if (data && data.error) { |
| 84 | + setErrorMsg(data.error.message || 'RPC returned an error'); |
| 85 | + setPhase('error'); |
| 86 | + return; |
| 87 | + } |
| 88 | + setResult(data ? data.result : undefined); |
| 89 | + setPhase('success'); |
| 90 | + } catch (err) { |
| 91 | + setErrorMsg(err && err.message ? err.message : 'Request failed'); |
| 92 | + setPhase('error'); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + const resultString = result === undefined ? 'undefined' : JSON.stringify(result, null, 2); |
| 97 | + const decoded = decode !== 'off' && typeof result === 'string' ? hexToDecimal(result) : null; |
| 98 | + |
| 99 | + const copyResult = () => { |
| 100 | + if (typeof navigator !== 'undefined' && navigator.clipboard) { |
| 101 | + navigator.clipboard.writeText(resultString); |
| 102 | + setCopied(true); |
| 103 | + setTimeout(() => setCopied(false), 2000); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + // --- icons (nested) --- |
| 108 | + const SpinnerIcon = () => ( |
| 109 | + <svg className='animate-spin h-4 w-4' xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24'> |
| 110 | + <circle className='opacity-25' cx='12' cy='12' r='10' stroke='currentColor' strokeWidth='4' /> |
| 111 | + <path className='opacity-75' fill='currentColor' d='M4 12a8 8 0 0 1 8-8v4a4 4 0 0 0-4 4H4z' /> |
| 112 | + </svg> |
| 113 | + ); |
| 114 | + |
| 115 | + const PlayIcon = () => ( |
| 116 | + <svg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='currentColor'> |
| 117 | + <path d='M8 5v14l11-7z' /> |
| 118 | + </svg> |
| 119 | + ); |
| 120 | + |
| 121 | + // --- theme-agnostic surfaces (see theming note above) --- |
| 122 | + const HAIRLINE = 'rgba(128, 128, 128, 0.25)'; |
| 123 | + const surfaceStyle = { backgroundColor: 'rgba(128, 128, 128, 0.08)' }; |
| 124 | + const monoStyle = { fontFamily: 'var(--sei-font-mono)' }; |
| 125 | + const codeStyle = { backgroundColor: 'rgba(128, 128, 128, 0.05)', fontFamily: 'var(--sei-font-mono)' }; |
| 126 | + |
| 127 | + const cardClass = 'not-prose w-full rounded-lg border overflow-hidden my-4'; |
| 128 | + const headerClass = 'flex items-center justify-between gap-3 px-4 py-2.5 border-b'; |
| 129 | + const labelClass = 'text-xs uppercase tracking-wide text-neutral-500 dark:text-neutral-500'; |
| 130 | + const preClass = 'm-0 px-4 py-3 text-sm overflow-x-auto text-neutral-700 dark:text-neutral-300'; |
| 131 | + |
| 132 | + const buttonStyle = { |
| 133 | + backgroundColor: btnHover ? 'var(--sei-maroon-200)' : 'var(--sei-maroon-100)', |
| 134 | + color: '#ffffff', |
| 135 | + fontFamily: 'var(--sei-font-mono)', |
| 136 | + textTransform: 'uppercase', |
| 137 | + letterSpacing: '0.04em', |
| 138 | + fontSize: '10px', |
| 139 | + opacity: phase === 'loading' ? 0.7 : 1, |
| 140 | + cursor: phase === 'loading' ? 'default' : 'pointer' |
| 141 | + }; |
| 142 | + |
| 143 | + return ( |
| 144 | + <div className={cardClass} style={{ borderColor: HAIRLINE }}> |
| 145 | + <div className={headerClass} style={{ ...surfaceStyle, borderBottomColor: HAIRLINE }}> |
| 146 | + <div className='flex flex-col min-w-0'> |
| 147 | + <span className='text-sm font-medium text-neutral-900 dark:text-white truncate' style={monoStyle}> |
| 148 | + {title || method} |
| 149 | + </span> |
| 150 | + <span className='text-xs text-neutral-500 dark:text-neutral-500'>{networkLabel}</span> |
| 151 | + </div> |
| 152 | + <button |
| 153 | + type='button' |
| 154 | + onClick={run} |
| 155 | + disabled={phase === 'loading'} |
| 156 | + onMouseEnter={() => setBtnHover(true)} |
| 157 | + onMouseLeave={() => setBtnHover(false)} |
| 158 | + className='inline-flex items-center gap-1.5 px-3 py-1.5 shrink-0 transition-colors' |
| 159 | + style={buttonStyle}> |
| 160 | + {phase === 'loading' ? <SpinnerIcon /> : <PlayIcon />} |
| 161 | + {phase === 'loading' ? 'Running…' : label || 'Run'} |
| 162 | + </button> |
| 163 | + </div> |
| 164 | + |
| 165 | + {description ? ( |
| 166 | + <div className='px-4 pt-3 text-sm text-neutral-600 dark:text-neutral-400'>{description}</div> |
| 167 | + ) : null} |
| 168 | + |
| 169 | + <div className='px-4 pt-3 pb-1'> |
| 170 | + <span className={labelClass}>Request</span> |
| 171 | + </div> |
| 172 | + <pre className={preClass} style={codeStyle}> |
| 173 | + {requestJson} |
| 174 | + </pre> |
| 175 | + |
| 176 | + {phase === 'success' ? ( |
| 177 | + <div className='border-t' style={{ borderTopColor: HAIRLINE }}> |
| 178 | + <div className='flex items-center justify-between px-4 pt-3 pb-1'> |
| 179 | + <span className={labelClass}>Response{elapsed != null ? ` · ${elapsed} ms` : ''}</span> |
| 180 | + <button |
| 181 | + type='button' |
| 182 | + onClick={copyResult} |
| 183 | + className='text-xs text-neutral-500 hover:text-neutral-800 dark:text-neutral-400 dark:hover:text-neutral-200 transition-colors'> |
| 184 | + {copied ? 'Copied' : 'Copy'} |
| 185 | + </button> |
| 186 | + </div> |
| 187 | + <pre className={preClass} style={codeStyle}> |
| 188 | + {resultString} |
| 189 | + </pre> |
| 190 | + {decoded ? ( |
| 191 | + <div className='px-4 pb-3 text-xs text-neutral-500 dark:text-neutral-500' style={monoStyle}> |
| 192 | + = {decoded} (decimal) |
| 193 | + </div> |
| 194 | + ) : null} |
| 195 | + </div> |
| 196 | + ) : null} |
| 197 | + |
| 198 | + {phase === 'error' ? ( |
| 199 | + <div className='border-t' style={{ borderTopColor: HAIRLINE }}> |
| 200 | + <div className='px-4 pt-3 pb-1'> |
| 201 | + <span className={labelClass}>Error</span> |
| 202 | + </div> |
| 203 | + <pre className={`${preClass} text-red-600 dark:text-red-400`} style={codeStyle}> |
| 204 | + {errorMsg} |
| 205 | + </pre> |
| 206 | + </div> |
| 207 | + ) : null} |
| 208 | + </div> |
| 209 | + ); |
| 210 | +}; |
0 commit comments