Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

webcontainer-preview.tsx

253 lines9.0 KB

1import { useContext, useEffect, useRef, useState } from 'react'
2import { useStore } from 'zustand'
3import { ChevronDown, ChevronUp } from 'lucide-react'
4​
5import type { SetupStep } from '../hooks/use-webcontainer-store'
6import { WebContainerContext } from './web-container-provider'
7​
8export function WebContainerPreview() {
9 const containerStore = useContext(WebContainerContext)
10 if (!containerStore) {
11 throw new Error('WebContainerContext not found')
12 }
13​
14 const webContainer = useStore(containerStore, (state) => state.webContainer)
15 const setupStep = useStore(containerStore, (state) => state.setupStep)
16 const statusMessage = useStore(containerStore, (state) => state.statusMessage)
17 const terminalOutput = useStore(
18 containerStore,
19 (state) => state.terminalOutput,
20 )
21 const error = useStore(containerStore, (state) => state.error)
22 const previewUrl = useStore(containerStore, (state) => state.previewUrl)
23 const startDevServer = useStore(
24 containerStore,
25 (state) => state.startDevServer,
26 )
27 const setTerminalOutput = useStore(
28 containerStore,
29 (state) => state.setTerminalOutput,
30 )
31​
32 const [isTerminalOpen, setIsTerminalOpen] = useState(false)
33​
34 // Auto-scroll terminal to bottom when new output arrives
35 const terminalRef = useRef<HTMLDivElement>(null)
36 useEffect(() => {
37 if (terminalRef.current) {
38 terminalRef.current.scrollTop = terminalRef.current.scrollHeight
39 }
40 }, [terminalOutput])
41​
42 const getStepIcon = (step: SetupStep) => {
43 switch (step) {
44 case 'mounting':
45 return '📁'
46 case 'installing':
47 return '📦'
48 case 'starting':
49 return '🚀'
50 case 'ready':
51 return '✅'
52 case 'error':
53 return '❌'
54 }
55 }
56​
57 const getStepColor = (step: SetupStep) => {
58 switch (step) {
59 case 'error':
60 return 'text-red-500'
61 case 'ready':
62 return 'text-green-500'
63 default:
64 return 'text-blue-500'
65 }
66 }
67​
68 // Show progress dialog during setup (similar to "Creating Your Application")
69 if (
70 !webContainer ||
71 setupStep === 'error' ||
72 setupStep !== 'ready' ||
73 !previewUrl
74 ) {
75 return (
76 <div className="flex items-center justify-center h-full bg-gray-50 dark:bg-gray-900">
77 <div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl p-8 max-w-2xl w-full mx-4 border-2 border-blue-500">
78 <h2 className="text-2xl font-bold mb-6 text-center">
79 {setupStep === 'error' ? 'Setup Failed' : 'Preparing Preview'}
80 </h2>
81​
82 {setupStep === 'error' ? (
83 <div className="text-center">
84 <div className="text-4xl mb-4">❌</div>
85 <div className="text-lg font-medium text-red-600 mb-2">
86 An error occurred
87 </div>
88 <div className="text-sm text-gray-600 dark:text-gray-400 mb-4">
89 {error}
90 </div>
91 <button
92 onClick={startDevServer}
93 className="px-4 py-2 text-sm bg-orange-500 text-white rounded hover:bg-orange-600 transition-colors"
94 >
95 🔄 Retry
96 </button>
97 </div>
98 ) : (
99 <>
100 {/* Progress Steps */}
101 <div className="space-y-4 mb-6">
102 <div className="flex items-center gap-3">
103 <div
104 className={`text-2xl ${setupStep === 'mounting' ? 'animate-pulse' : ''}`}
105 >
106 {getStepIcon('mounting')}
107 </div>
108 <div
109 className={`flex-1 ${getStepColor(setupStep === 'mounting' ? 'mounting' : setupStep === 'installing' || setupStep === 'starting' || setupStep === 'ready' ? 'ready' : 'mounting')}`}
110 >
111 Mount Files
112 </div>
113 {(setupStep === 'installing' ||
114 setupStep === 'starting' ||
115 setupStep === 'ready') &&
116 '✓'}
117 </div>
118 <div className="flex items-center gap-3">
119 <div
120 className={`text-2xl ${setupStep === 'installing' ? 'animate-spin' : ''}`}
121 >
122 {getStepIcon('installing')}
123 </div>
124 <div
125 className={`flex-1 ${getStepColor(setupStep === 'installing' ? 'installing' : setupStep === 'starting' || setupStep === 'ready' ? 'ready' : 'mounting')}`}
126 >
127 Install Dependencies
128 </div>
129 {(setupStep === 'starting' || setupStep === 'ready') && '✓'}
130 </div>
131 <div className="flex items-center gap-3">
132 <div
133 className={`text-2xl ${setupStep === 'starting' ? 'animate-bounce' : ''}`}
134 >
135 {getStepIcon('starting')}
136 </div>
137 <div
138 className={`flex-1 ${getStepColor(setupStep === 'starting' ? 'starting' : setupStep === 'ready' ? 'ready' : 'mounting')}`}
139 >
140 Start Server
141 </div>
142 {setupStep === 'ready' && '✓'}
143 </div>
144 </div>
145​
146 {/* Current status */}
147 <div className="text-center text-sm text-gray-600 dark:text-gray-400">
148 {statusMessage || 'Preparing your application...'}
149 </div>
150 </>
151 )}
152 </div>
153 </div>
154 )
155 }
156​
157 // Show the running application with collapsible terminal
158 return (
159 <div className="flex flex-col h-full">
160 {/* iframe with the running app */}
161 <div className="flex-1">
162 {previewUrl ? (
163 <iframe
164 src={previewUrl}
165 className="w-full h-full border-0"
166 title="Application Preview"
167 onLoad={() => console.log('Iframe loaded successfully')}
168 onError={(e) => console.error('Iframe load error:', e)}
169 />
170 ) : (
171 <div className="flex items-center justify-center h-full bg-gray-100 dark:bg-gray-800">
172 <div className="text-center">
173 <div className="text-2xl mb-2">🔄</div>
174 <div className="text-sm text-gray-600 dark:text-gray-400">
175 Preview not available
176 </div>
177 </div>
178 </div>
179 )}
180 </div>
181​
182 {/* Collapsible Terminal output */}
183 <div className="border-t border-gray-200 dark:border-gray-700 bg-black text-green-400 flex flex-col flex-shrink-0">
184 <div
185 className="p-2 border-b border-gray-700 bg-gray-900 flex items-center justify-between cursor-pointer hover:bg-gray-800 transition-colors"
186 onClick={() => setIsTerminalOpen(!isTerminalOpen)}
187 >
188 <div className="flex items-center gap-2 flex-1">
189 <button className="text-gray-400 hover:text-gray-200">
190 {isTerminalOpen ? (
191 <ChevronDown className="w-4 h-4" />
192 ) : (
193 <ChevronUp className="w-4 h-4" />
194 )}
195 </button>
196 <div className="text-xs font-medium text-gray-300">
197 Terminal Output
198 </div>
199 {setupStep === 'ready' && previewUrl && (
200 <div className="text-xs text-green-500">● Server Running</div>
201 )}
202 </div>
203 <div
204 className="flex items-center gap-2"
205 onClick={(e) => e.stopPropagation()}
206 >
207 {previewUrl && (
208 <button
209 onClick={() => window.open(previewUrl, '_blank')}
210 className="px-2 py-1 text-xs bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
211 >
212 Open in New Tab
213 </button>
214 )}
215 <button
216 onClick={startDevServer}
217 disabled={!webContainer}
218 className="px-2 py-1 text-xs bg-orange-500 text-white rounded hover:bg-orange-600 transition-colors disabled:opacity-50"
219 >
220 🔄 Restart
221 </button>
222 <button
223 onClick={() => setTerminalOutput([])}
224 className="text-xs text-gray-400 hover:text-gray-200 transition-colors"
225 >
226 Clear
227 </button>
228 </div>
229 </div>
230 {isTerminalOpen && (
231 <div
232 ref={terminalRef}
233 className="font-mono text-xs p-2 overflow-y-auto overflow-x-hidden"
234 style={{ maxHeight: '200px' }}
235 >
236 {terminalOutput.length > 0 ? (
237 terminalOutput.map((line, index) => (
238 <div
239 key={index}
240 className="mb-1 leading-tight whitespace-pre-wrap break-words"
241 >
242 {line}
243 </div>
244 ))
245 ) : (
246 <div className="text-gray-500">No output yet...</div>
247 )}
248 </div>
249 )}
250 </div>
251 </div>
252 )