forked from Gerome-Elassaad/CodingIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.tsx
More file actions
292 lines (284 loc) · 10.7 KB
/
Copy pathpreview.tsx
File metadata and controls
292 lines (284 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { FragmentCode } from './fragment-code'
import { FragmentPreview } from './fragment-preview'
import { FragmentTerminal } from './fragment-terminal'
import { FragmentInterpreter } from './fragment-interpreter'
import { CodeEditor } from './code-editor'
import { SandboxFileTree } from './sandbox-file-tree'
import { IDE } from './ide'
import { Button } from '@/components/ui/button'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
import { FragmentSchema } from '@/lib/schema'
import { ExecutionResult } from '@/lib/types'
import { DeepPartial } from 'ai'
import { ChevronsRight, LoaderCircle, Terminal, Code, FileCode, FolderTree, Folder } from 'lucide-react'
import { Dispatch, SetStateAction, useState } from 'react'
export function Preview({
teamID,
accessToken,
selectedTab,
onSelectedTabChange,
isChatLoading,
isPreviewLoading,
fragment,
result,
onClose,
code,
selectedFile,
onSelectFile,
onSave,
executeCode,
}: {
teamID: string | undefined
accessToken: string | undefined
selectedTab: 'code' | 'fragment' | 'terminal' | 'interpreter' | 'editor' | 'files' | 'ide'
onSelectedTabChange: Dispatch<SetStateAction<'code' | 'fragment' | 'terminal' | 'interpreter' | 'editor' | 'files' | 'ide'>>
isChatLoading: boolean
isPreviewLoading: boolean
fragment?: DeepPartial<FragmentSchema>
result?: ExecutionResult
onClose: () => void
code?: string
selectedFile?: { path: string; content: string } | null
onSelectFile?: (file: { path: string; content: string }) => void
onSave?: (path: string, content: string) => Promise<void>
executeCode?: (code: string) => Promise<any>
}) {
const [isRefreshingFiles, setIsRefreshingFiles] = useState(false)
async function handleSelectSandboxFile(path: string) {
if (!result?.sbxId) return
try {
const response = await fetch(`/api/sandbox/${result.sbxId}/files/content?path=${encodeURIComponent(path)}`)
const data = await response.json()
if (response.ok && data.content !== undefined) {
// Update the selected file in the parent component
if (onSelectFile) {
onSelectFile({ path: data.path, content: data.content })
}
// Switch to editor tab
onSelectedTabChange('editor')
}
} catch (error) {
console.error('Error loading sandbox file:', error)
}
}
async function handleRefreshFiles() {
if (!result?.sbxId) return
setIsRefreshingFiles(true)
try {
const response = await fetch(`/api/sandbox/${result.sbxId}/files`)
const data = await response.json()
if (response.ok && data.files) {
// Files refreshed - this would need to update result.files in parent
console.log('Files refreshed:', data.files)
}
} catch (error) {
console.error('Error refreshing files:', error)
} finally {
setIsRefreshingFiles(false)
}
}
return (
<div className="absolute md:relative z-10 top-0 left-0 shadow-2xl md:rounded-tl-3xl md:rounded-bl-3xl md:border-l md:border-y bg-popover h-full w-full overflow-auto">
<Tabs
value={selectedTab}
onValueChange={(value) => {
console.log('Tab changed to:', value)
onSelectedTabChange(value as 'code' | 'fragment' | 'terminal' | 'interpreter' | 'editor' | 'files' | 'ide')
}}
className="h-full flex flex-col items-start justify-start"
>
<div className="w-full p-2 grid grid-cols-3 items-center border-b relative z-10">
<TooltipProvider>
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="text-muted-foreground"
onClick={onClose}
>
<ChevronsRight className="h-5 w-5" />
</Button>
</TooltipTrigger>
<TooltipContent>Close sidebar</TooltipContent>
</Tooltip>
</TooltipProvider>
<div className="flex justify-center relative z-20">
<TabsList className="px-1 py-0 border h-8 relative z-30">
<TabsTrigger
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="code"
>
{isChatLoading && (
<LoaderCircle
strokeWidth={3}
className="h-3 w-3 animate-spin"
/>
)}
<Code className="h-3 w-3" />
Code
</TabsTrigger>
<TabsTrigger
disabled={!result}
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="fragment"
>
Preview
{isPreviewLoading && (
<LoaderCircle
strokeWidth={3}
className="h-3 w-3 animate-spin"
/>
)}
</TabsTrigger>
<TabsTrigger
disabled={!result}
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="terminal"
>
<Terminal className="h-3 w-3" />
Terminal
</TabsTrigger>
<TabsTrigger
disabled={!result || result.template !== 'code-interpreter-v1'}
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="interpreter"
>
<Code className="h-3 w-3" />
Interpreter
</TabsTrigger>
<TabsTrigger
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="editor"
>
<FileCode className="h-3 w-3" />
Editor
</TabsTrigger>
<TabsTrigger
disabled={!result || !result.files || result.files.length === 0}
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="files"
>
<FolderTree className="h-3 w-3" />
Files
</TabsTrigger>
<TabsTrigger
className="font-normal text-xs py-1 px-2 gap-1 flex items-center"
value="ide"
>
<Folder className="h-3 w-3" />
IDE
</TabsTrigger>
</TabsList>
</div>
<div className="flex items-center justify-end gap-2">
{/* Add any additional buttons here */}
</div>
</div>
<div className="overflow-y-auto w-full h-full">
<TabsContent value="code" className="h-full">
{fragment?.code ? (
<FragmentCode
files={[
{
name: fragment?.file_path || 'code.txt',
content: fragment?.code || '',
},
]}
/>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
No code to display
</div>
)}
</TabsContent>
<TabsContent value="fragment" className="h-full">
{result ? (
<FragmentPreview
result={result as ExecutionResult}
code={code || fragment?.code || ''}
executeCode={executeCode || (async () => {})}
/>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
Preview will appear here once the code is executed
</div>
)}
</TabsContent>
<TabsContent value="terminal" className="h-full">
{result ? (
<FragmentTerminal
teamID={teamID}
accessToken={accessToken}
result={result as ExecutionResult}
/>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
Terminal access will appear here once the sandbox is created
</div>
)}
</TabsContent>
<TabsContent value="interpreter" className="h-full">
{result && result.template === 'code-interpreter-v1' ? (
<FragmentInterpreter
result={result}
code={code || fragment?.code || ''}
executeCode={executeCode || (async () => {})}
/>
) : result ? (
<div className="flex items-center justify-center h-full text-muted-foreground">
Interpreter only available for code-interpreter-v1 template
</div>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
Interpreter will appear here once the sandbox is created
</div>
)}
</TabsContent>
<TabsContent value="editor" className="h-full">
{selectedFile && onSave ? (
<CodeEditor
code={selectedFile.content}
lang={selectedFile.path.split('.').pop() || 'txt'}
onChange={(value) => {
if (value !== undefined) {
onSave(selectedFile.path, value)
}
}}
/>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
Select a file from the file tree to edit
</div>
)}
</TabsContent>
<TabsContent value="files" className="h-full">
{result && result.files && result.files.length > 0 ? (
<SandboxFileTree
files={result.files}
onSelectFile={handleSelectSandboxFile}
onRefresh={handleRefreshFiles}
isLoading={isRefreshingFiles}
/>
) : (
<div className="flex items-center justify-center h-full text-muted-foreground">
No sandbox files available
</div>
)}
</TabsContent>
<TabsContent value="ide" className="h-full m-0 p-0">
<div className="h-full w-full">
<IDE />
</div>
</TabsContent>
</div>
</Tabs>
</div>
)
}