|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useCallback, useEffect, useRef, useState } from "react" |
| 4 | +import type { VM } from "@stackblitz/sdk" |
| 5 | +import { StackBlitzPlayground } from "../../components/playground/StackBlitzPlayground" |
| 6 | + |
| 7 | +const encodeCode = (code: string) => btoa(encodeURIComponent(code)) |
| 8 | +const decodeCode = (encoded: string) => { |
| 9 | + try { |
| 10 | + return decodeURIComponent(atob(encoded)) |
| 11 | + } catch { |
| 12 | + return null |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +const defaultCode = `import { Core } from "@evolution-sdk/evolution" |
| 17 | +
|
| 18 | +const bech32 = "addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp" |
| 19 | +
|
| 20 | +// Parse from Bech32 |
| 21 | +const address = Core.Address.fromBech32(bech32) |
| 22 | +
|
| 23 | +console.log("Address:", address) |
| 24 | +console.log("Network ID:", address.networkId) |
| 25 | +console.log("Payment credential:", address.paymentCredential) |
| 26 | +console.log("Has staking:", address.stakingCredential !== undefined) |
| 27 | +
|
| 28 | +// Check if it's an enterprise address |
| 29 | +console.log("Is enterprise:", Core.Address.isEnterprise(address))` |
| 30 | + |
| 31 | +export default function PlaygroundPage() { |
| 32 | + const [code, setCode] = useState<string | undefined>() |
| 33 | + const [copied, setCopied] = useState(false) |
| 34 | + const [vmReady, setVmReady] = useState(false) |
| 35 | + const vmRef = useRef<VM | null>(null) |
| 36 | + |
| 37 | + // Load code from URL on mount |
| 38 | + useEffect(() => { |
| 39 | + if (typeof window === 'undefined') return |
| 40 | + const params = new URLSearchParams(window.location.search) |
| 41 | + const encodedCode = params.get('code') |
| 42 | + if (encodedCode) { |
| 43 | + const decoded = decodeCode(encodedCode) |
| 44 | + if (decoded) { |
| 45 | + setCode(decoded) |
| 46 | + } |
| 47 | + } |
| 48 | + }, []) |
| 49 | + |
| 50 | + const shareCode = async () => { |
| 51 | + if (typeof window === 'undefined') return |
| 52 | + |
| 53 | + try { |
| 54 | + let currentCode = code || defaultCode |
| 55 | + |
| 56 | + if (vmRef.current) { |
| 57 | + try { |
| 58 | + const files = await vmRef.current.getFsSnapshot() |
| 59 | + if (files?.['index.ts']) { |
| 60 | + currentCode = files['index.ts'] |
| 61 | + } |
| 62 | + } catch (vmError) { |
| 63 | + console.warn('Could not read from VM:', vmError) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const encoded = encodeCode(currentCode) |
| 68 | + const url = new URL(window.location.href) |
| 69 | + url.searchParams.set('code', encoded) |
| 70 | + |
| 71 | + window.history.pushState({}, '', url.toString()) |
| 72 | + await navigator.clipboard.writeText(url.toString()) |
| 73 | + |
| 74 | + setCopied(true) |
| 75 | + setTimeout(() => setCopied(false), 2000) |
| 76 | + } catch (error) { |
| 77 | + console.error('Failed to share code:', error) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const handleVmReady = useCallback((vm: VM) => { |
| 82 | + vmRef.current = vm |
| 83 | + setVmReady(true) |
| 84 | + }, []) |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="h-screen flex flex-col"> |
| 88 | + <header className="flex-shrink-0 p-4 border-b border-fd-border bg-fd-background"> |
| 89 | + <div className="flex items-center justify-between"> |
| 90 | + <div> |
| 91 | + <h1 className="text-2xl font-bold mb-1">Evolution SDK Playground</h1> |
| 92 | + <p className="text-sm text-muted-foreground"> |
| 93 | + Full Node.js environment in your browser powered by StackBlitz WebContainers |
| 94 | + </p> |
| 95 | + <p className="text-xs text-blue-600 dark:text-blue-400 mt-1"> |
| 96 | + 💡 Save changes: <code className="bg-fd-muted px-1 rounded font-mono">Cmd+S</code> / <code className="bg-fd-muted px-1 rounded font-mono">Ctrl+S</code>, then run: <code className="bg-fd-muted px-1 rounded font-mono">npm start</code> |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + <button |
| 100 | + onClick={shareCode} |
| 101 | + className="inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium h-9 px-6 py-2 bg-zinc-700 text-white hover:bg-zinc-600 active:bg-zinc-500 transition-all cursor-pointer shadow-sm hover:shadow" |
| 102 | + title={vmReady ? "Share your current code" : "Loading playground..."} |
| 103 | + > |
| 104 | + {copied ? ( |
| 105 | + <> |
| 106 | + <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 107 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /> |
| 108 | + </svg> |
| 109 | + Copied! |
| 110 | + </> |
| 111 | + ) : ( |
| 112 | + <> |
| 113 | + <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 114 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z" /> |
| 115 | + </svg> |
| 116 | + Share Code |
| 117 | + </> |
| 118 | + )} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + </header> |
| 122 | + <div className="flex-1 overflow-hidden"> |
| 123 | + <StackBlitzPlayground |
| 124 | + key={code || 'default'} |
| 125 | + initialCode={code} |
| 126 | + onVmReady={handleVmReady} |
| 127 | + /> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + ) |
| 131 | +} |
| 132 | + |
0 commit comments