|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { type RawCode } from 'codehike/code'; |
| 4 | +import { CheckCircle2, Loader2, Play, XCircle } from 'lucide-react'; |
| 5 | +import { useState } from 'react'; |
| 6 | +import { cn } from '@/lib/utils'; |
| 7 | +import { ClientDocsKitCode } from '@openpkg-ts/ui/docskit'; |
| 8 | +import { CollapsiblePanel } from '@openpkg-ts/ui/docskit'; |
| 9 | + |
| 10 | +export interface RunnableSnippetProps { |
| 11 | + /** Code to display */ |
| 12 | + code: string; |
| 13 | + /** Language for syntax highlighting */ |
| 14 | + language?: string; |
| 15 | + /** Optional title */ |
| 16 | + title?: string; |
| 17 | + /** For demo: mock execution state */ |
| 18 | + mockState?: 'idle' | 'running' | 'success' | 'error'; |
| 19 | + /** For demo: mock output data */ |
| 20 | + mockOutput?: string; |
| 21 | + /** Custom className */ |
| 22 | + className?: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Interactive code block with run button and output display. |
| 27 | + * Currently uses mock states; will be wired to sandbox execution in Phase 2. |
| 28 | + */ |
| 29 | +export function RunnableSnippet({ |
| 30 | + code, |
| 31 | + language = 'typescript', |
| 32 | + title, |
| 33 | + mockState = 'idle', |
| 34 | + mockOutput, |
| 35 | + className, |
| 36 | +}: RunnableSnippetProps): React.ReactNode { |
| 37 | + const [state, setState] = useState<'idle' | 'running' | 'success' | 'error'>(mockState); |
| 38 | + const [output, setOutput] = useState<string | null>(mockOutput ?? null); |
| 39 | + const [duration, setDuration] = useState<number>(0); |
| 40 | + |
| 41 | + const handleRun = () => { |
| 42 | + setState('running'); |
| 43 | + setOutput(null); |
| 44 | + |
| 45 | + // Mock execution - will be replaced with real sandbox in Phase 2 |
| 46 | + const startTime = Date.now(); |
| 47 | + setTimeout(() => { |
| 48 | + const elapsed = Date.now() - startTime; |
| 49 | + setDuration(elapsed); |
| 50 | + |
| 51 | + if (mockState === 'error') { |
| 52 | + setState('error'); |
| 53 | + setOutput(mockOutput ?? 'Error: Execution failed'); |
| 54 | + } else { |
| 55 | + setState('success'); |
| 56 | + setOutput( |
| 57 | + mockOutput ?? JSON.stringify({ result: 'success', timestamp: new Date().toISOString() }, null, 2), |
| 58 | + ); |
| 59 | + } |
| 60 | + }, 1200); |
| 61 | + }; |
| 62 | + |
| 63 | + const codeblock: RawCode = { |
| 64 | + value: code, |
| 65 | + lang: language, |
| 66 | + meta: title, |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className={cn('relative', className)}> |
| 71 | + {/* Code display with run button */} |
| 72 | + <div className="relative group"> |
| 73 | + <ClientDocsKitCode codeblock={codeblock} /> |
| 74 | + |
| 75 | + {/* Run button - positioned like CopyButton */} |
| 76 | + <button |
| 77 | + type="button" |
| 78 | + onClick={handleRun} |
| 79 | + disabled={state === 'running'} |
| 80 | + className={cn( |
| 81 | + 'absolute right-3 top-3 z-10', |
| 82 | + 'size-8 flex items-center justify-center', |
| 83 | + 'rounded border border-openpkg-code-border bg-openpkg-code-bg', |
| 84 | + 'text-openpkg-code-text-inactive', |
| 85 | + 'cursor-pointer transition-opacity duration-200', |
| 86 | + 'opacity-0 group-hover:opacity-100', |
| 87 | + state === 'running' && 'cursor-not-allowed opacity-100', |
| 88 | + )} |
| 89 | + aria-label="Run code" |
| 90 | + > |
| 91 | + {state === 'running' ? ( |
| 92 | + <Loader2 size={16} className="animate-spin" /> |
| 93 | + ) : ( |
| 94 | + <Play size={16} className="fill-current" /> |
| 95 | + )} |
| 96 | + </button> |
| 97 | + </div> |
| 98 | + |
| 99 | + {/* Output panel - shown after execution */} |
| 100 | + {output && ( |
| 101 | + <div className="-mt-4"> |
| 102 | + <CollapsiblePanel |
| 103 | + title={`${state === 'success' ? '✓ Success' : '✕ Error'} | ${duration}ms`} |
| 104 | + defaultExpanded={true} |
| 105 | + > |
| 106 | + <pre className="p-4 m-0 text-xs font-mono overflow-auto max-h-[400px]">{output}</pre> |
| 107 | + </CollapsiblePanel> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments