Open History to see the last commit
1import { ChevronDown, ChevronRight, Copy, Check } from 'lucide-react'
2import { useState, useEffect, useRef } from 'react'
3import hljs from 'highlight.js/lib/core'
4import typescript from 'highlight.js/lib/languages/typescript'
5import 'highlight.js/styles/github-dark.css'
6
7// Register TypeScript language
8hljs.registerLanguage('typescript', typescript)
9
10// Highlight external_* function calls after hljs processing
11function highlightExternalCalls(html: string): string {
12 // Match external_functionName patterns (with word boundaries)
13 return html.replace(
14 /\b(external_\w+)\b/g,
15 '<span class="external-fn">$1</span>',
16 )
17}
18
19interface CodeBlockProps {
20 code: string
21 language?: string
22 title?: string
23 status?: 'pending' | 'running' | 'success' | 'error'
24 collapsible?: boolean
25}
26
27export default function CodeBlock({
28 code,
29 language = 'typescript',
30 title,
31 status,
32 collapsible = true,
33}: CodeBlockProps) {
34 // Track if user has manually toggled
35 const [userControlled, setUserControlled] = useState(false)
36 const [isCollapsed, setIsCollapsed] = useState(false)
37 const [copied, setCopied] = useState(false)
38 const prevStatusRef = useRef(status)
39
40 // Auto-collapse when status changes from running to success/error (with delay)
41 useEffect(() => {
42 let timeoutId: ReturnType<typeof setTimeout> | null = null
43
44 if (!userControlled) {
45 const wasRunning =
46 prevStatusRef.current === 'running' ||
47 prevStatusRef.current === 'pending'
48 const isComplete = status === 'success' || status === 'error'
49
50 if (wasRunning && isComplete) {
51 // Delay auto-collapse by 3 seconds so user can see the result
52 timeoutId = setTimeout(() => {
53 setIsCollapsed(true)
54 }, 3000)
55 } else if (status === 'running' || status === 'pending') {
56 setIsCollapsed(false)
57 }
58 }
59 prevStatusRef.current = status
60
61 return () => {
62 if (timeoutId) clearTimeout(timeoutId)
63 }
64 }, [status, userControlled])
65
66 const handleToggle = () => {
67 setUserControlled(true)
68 setIsCollapsed(!isCollapsed)
69 }
70
71 const highlighted = highlightExternalCalls(
72 hljs.highlight(code, { language }).value,
73 )
74
75 const handleCopy = async () => {
76 await navigator.clipboard.writeText(code)
77 setCopied(true)
78 setTimeout(() => setCopied(false), 2000)
79 }
80
81 const statusColors = {
82 pending: 'bg-gray-800 border-gray-600',
83 running: 'bg-blue-900/30 border-blue-700',
84 success: 'bg-green-900/30 border-green-700',
85 error: 'bg-red-900/30 border-red-700',
86 }
87
88 const statusLabels = {
89 pending: 'Pending',
90 running: 'Running...',
91 success: 'Executed',
92 error: 'Error',
93 }
94
95 return (
96 <div
97 className={`rounded-lg border overflow-hidden ${status ? statusColors[status] : 'border-gray-700'}`}
98 >
99 <div className="flex items-center justify-between px-3 py-2 bg-gray-800 text-gray-200 text-sm">
100 <div className="flex items-center gap-2">
101 {collapsible && (
102 <button
103 onClick={handleToggle}
104 className="p-1 hover:bg-gray-700 rounded transition-colors"
105 >
106 {isCollapsed ? (
107 <ChevronRight size={16} />
108 ) : (
109 <ChevronDown size={16} />
110 )}
111 </button>
112 )}
113 <span className="font-mono">{title || 'Generated Code'}</span>
114 {status && (
115 <span
116 className={`text-xs px-2 py-0.5 rounded-full ${
117 status === 'running'
118 ? 'bg-blue-600 animate-pulse'
119 : status === 'success'
120 ? 'bg-green-600'
121 : status === 'error'
122 ? 'bg-red-600'
123 : 'bg-gray-600'
124 }`}
125 >
126 {statusLabels[status]}
127 </span>
128 )}
129 </div>
130 <button
131 onClick={handleCopy}
132 className="p-1 hover:bg-gray-700 rounded flex items-center gap-1 transition-colors"
133 title="Copy code"
134 >
135 {copied ? <Check size={16} /> : <Copy size={16} />}
136 </button>
137 </div>
138
139 {!isCollapsed && (
140 <pre className="p-4 overflow-x-auto bg-gray-900 text-sm">
141 <code
142 className="language-typescript"
143 dangerouslySetInnerHTML={{ __html: highlighted }}
144 />
145 </pre>
146 )}
147 </div>
148 )
149}