|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { |
| 5 | + SandpackProvider, |
| 6 | + SandpackLayout, |
| 7 | + SandpackCodeEditor, |
| 8 | + SandpackPreview, |
| 9 | + SandpackConsole, |
| 10 | + useActiveCode, |
| 11 | + useSandpack, |
| 12 | +} from '@codesandbox/sandpack-react' |
| 13 | +// sandpackDark is bundled with sandpack-react |
| 14 | +const sandpackDark = 'dark' as const |
| 15 | + |
| 16 | +// Conflux network configs |
| 17 | +const NETWORKS = { |
| 18 | + testnet: { |
| 19 | + label: 'Testnet', |
| 20 | + chainId: 71, |
| 21 | + rpcUrl: 'https://evmtestnet.confluxrpc.com', |
| 22 | + blockExplorer: 'https://evmtestnet.confluxscan.io', |
| 23 | + }, |
| 24 | + local: { |
| 25 | + label: 'Local (localhost:8545)', |
| 26 | + chainId: 2030, |
| 27 | + rpcUrl: 'http://localhost:8545', |
| 28 | + blockExplorer: '', |
| 29 | + }, |
| 30 | +} as const |
| 31 | + |
| 32 | +type Network = keyof typeof NETWORKS |
| 33 | + |
| 34 | +interface PlaygroundProps { |
| 35 | + /** The active code file name, e.g. "index.ts" */ |
| 36 | + file?: string |
| 37 | + /** Record of filename → code content */ |
| 38 | + files?: Record<string, string> |
| 39 | + /** Template: "vanilla-ts" | "nextjs" | "node" */ |
| 40 | + template?: 'vanilla-ts' | 'node' |
| 41 | + /** Show console instead of preview */ |
| 42 | + showConsole?: boolean |
| 43 | + /** Extra npm dependencies beyond the defaults */ |
| 44 | + extraDeps?: Record<string, string> |
| 45 | +} |
| 46 | + |
| 47 | +const DEFAULT_DEPS: Record<string, string> = { |
| 48 | + viem: '^2.0.0', |
| 49 | + '@cfxdevkit/core': 'latest', |
| 50 | + '@cfxdevkit/contracts': 'latest', |
| 51 | +} |
| 52 | + |
| 53 | +function NetworkToggle({ |
| 54 | + network, |
| 55 | + onChange, |
| 56 | +}: { |
| 57 | + network: Network |
| 58 | + onChange: (n: Network) => void |
| 59 | +}) { |
| 60 | + return ( |
| 61 | + <div |
| 62 | + style={{ |
| 63 | + display: 'flex', |
| 64 | + alignItems: 'center', |
| 65 | + gap: '8px', |
| 66 | + padding: '6px 12px', |
| 67 | + borderBottom: '1px solid var(--sp-colors-surface2)', |
| 68 | + background: 'var(--sp-colors-surface1)', |
| 69 | + fontSize: '12px', |
| 70 | + fontFamily: 'var(--sp-font-mono)', |
| 71 | + }} |
| 72 | + > |
| 73 | + <span style={{ color: 'var(--sp-colors-fg-inactive)' }}>network:</span> |
| 74 | + {(Object.keys(NETWORKS) as Network[]).map((key) => ( |
| 75 | + <button |
| 76 | + key={key} |
| 77 | + onClick={() => onChange(key)} |
| 78 | + style={{ |
| 79 | + padding: '2px 10px', |
| 80 | + borderRadius: '4px', |
| 81 | + border: 'none', |
| 82 | + cursor: 'pointer', |
| 83 | + fontFamily: 'inherit', |
| 84 | + fontSize: '11px', |
| 85 | + background: |
| 86 | + network === key |
| 87 | + ? 'var(--sp-colors-accent)' |
| 88 | + : 'var(--sp-colors-surface2)', |
| 89 | + color: |
| 90 | + network === key |
| 91 | + ? '#fff' |
| 92 | + : 'var(--sp-colors-fg-inactive)', |
| 93 | + }} |
| 94 | + > |
| 95 | + {NETWORKS[key].label} |
| 96 | + </button> |
| 97 | + ))} |
| 98 | + <span |
| 99 | + style={{ |
| 100 | + marginLeft: 'auto', |
| 101 | + color: 'var(--sp-colors-fg-inactive)', |
| 102 | + fontSize: '10px', |
| 103 | + }} |
| 104 | + > |
| 105 | + chainId: {NETWORKS[network].chainId} · {NETWORKS[network].rpcUrl} |
| 106 | + </span> |
| 107 | + </div> |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +export function Playground({ |
| 112 | + files = {}, |
| 113 | + template = 'vanilla-ts', |
| 114 | + showConsole = true, |
| 115 | + extraDeps = {}, |
| 116 | + file = 'index.ts', |
| 117 | +}: PlaygroundProps) { |
| 118 | + const [network, setNetwork] = useState<Network>('testnet') |
| 119 | + |
| 120 | + const networkConfig = NETWORKS[network] |
| 121 | + |
| 122 | + // Inject a network-config helper that examples can import |
| 123 | + const networkFile = `// Auto-generated — re-renders when you toggle network above |
| 124 | +export const NETWORK = { |
| 125 | + chainId: ${networkConfig.chainId}, |
| 126 | + rpcUrl: '${networkConfig.rpcUrl}', |
| 127 | + blockExplorer: '${networkConfig.blockExplorer}', |
| 128 | +} as const |
| 129 | +` |
| 130 | + |
| 131 | + const mergedFiles = { |
| 132 | + '/network-config.ts': { code: networkFile, readOnly: true }, |
| 133 | + ...Object.fromEntries( |
| 134 | + Object.entries(files).map(([name, code]) => [ |
| 135 | + name.startsWith('/') ? name : `/${name}`, |
| 136 | + { code }, |
| 137 | + ]) |
| 138 | + ), |
| 139 | + } |
| 140 | + |
| 141 | + return ( |
| 142 | + <div style={{ margin: '1.5rem 0', borderRadius: '8px', overflow: 'hidden' }}> |
| 143 | + <SandpackProvider |
| 144 | + key={network} // remount on network change to reset execution |
| 145 | + template={template} |
| 146 | + theme={sandpackDark} |
| 147 | + files={mergedFiles} |
| 148 | + options={{ |
| 149 | + activeFile: file.startsWith('/') ? file : `/${file}`, |
| 150 | + visibleFiles: Object.keys(mergedFiles), |
| 151 | + recompileMode: 'delayed', |
| 152 | + recompileDelay: 500, |
| 153 | + }} |
| 154 | + customSetup={{ |
| 155 | + dependencies: { |
| 156 | + ...DEFAULT_DEPS, |
| 157 | + ...extraDeps, |
| 158 | + }, |
| 159 | + }} |
| 160 | + > |
| 161 | + <NetworkToggle network={network} onChange={setNetwork} /> |
| 162 | + <SandpackLayout> |
| 163 | + <SandpackCodeEditor |
| 164 | + showLineNumbers |
| 165 | + showInlineErrors |
| 166 | + wrapContent |
| 167 | + style={{ height: 400 }} |
| 168 | + /> |
| 169 | + {showConsole ? ( |
| 170 | + <SandpackConsole style={{ height: 400 }} /> |
| 171 | + ) : ( |
| 172 | + <SandpackPreview style={{ height: 400 }} /> |
| 173 | + )} |
| 174 | + </SandpackLayout> |
| 175 | + </SandpackProvider> |
| 176 | + </div> |
| 177 | + ) |
| 178 | +} |
0 commit comments