import type { Meta, StoryObj } from '@storybook/react' import { CodeBlock, InlineCode } from './code-block' const meta: Meta = { title: 'Primitives/CodeBlock', component: CodeBlock, parameters: { layout: 'centered', backgrounds: { default: 'dark' } }, } export default meta type Story = StoryObj const sandboxClientCode = `import { SandboxClient } from '@tangle/sandbox-sdk' const client = new SandboxClient({ apiKey: process.env.TANGLE_API_KEY, region: 'us-east-1', }) const session = await client.sessions.create({ image: 'node:20-alpine', resources: { cpu: 2, memoryMb: 512 }, timeout: 300, }) const result = await session.exec('node -e "console.log(process.version)"') console.log(result.stdout) // v20.11.0 await session.terminate()` const hookCode = `import { useState, useCallback } from 'react' type Status = 'idle' | 'running' | 'success' | 'error' export function useSandboxSession(sessionId: string) { const [status, setStatus] = useState('idle') const [output, setOutput] = useState([]) const exec = useCallback(async (command: string) => { setStatus('running') try { const res = await fetch(\`/api/sessions/\${sessionId}/exec\`, { method: 'POST', body: JSON.stringify({ command }), }) const data = await res.json() setOutput((prev) => [...prev, data.stdout]) setStatus('success') } catch (err) { setStatus('error') } }, [sessionId]) return { status, output, exec } }` export const Default: Story = { args: { code: sandboxClientCode, language: 'typescript', }, } export const WithLineNumbers: Story = { name: 'With Line Numbers', args: { code: sandboxClientCode, language: 'typescript', showLineNumbers: true, }, } export const ShortSnippet: Story = { name: 'Short Snippet', args: { code: `const session = await client.sessions.create({ image: 'node:20' })`, language: 'typescript', showLineNumbers: false, }, } export const HookExample: Story = { name: 'Hook Example', args: { code: hookCode, language: 'typescript', showLineNumbers: true, }, } export const NoCopyButton: Story = { name: 'No Copy Button', args: { code: `$ curl -X POST https://api.tangle.network/v1/sessions \\\n -H "Authorization: Bearer $TANGLE_API_KEY" \\\n -d '{"image": "node:20-alpine"}'`, showLineNumbers: false, }, } export const InlineCodeExample: Story = { name: 'Inline Code', render: () => (

Use client.sessions.create() to provision a new sandbox session. Pass resources.memoryMb to set memory limits.

The exec() method returns a{' '} Promise{''} with{' '} stdout, stderr, and{' '} exitCode.

), }