forked from Gerome-Elassaad/CodingIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathide.tsx
More file actions
238 lines (223 loc) · 6.98 KB
/
Copy pathide.tsx
File metadata and controls
238 lines (223 loc) · 6.98 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
'use client'
import { useState, useEffect, useCallback } from 'react'
import { FileTree, FileSystemNode } from '@/components/file-tree'
import { CodeEditor } from '@/components/code-editor'
import { GitHubImport } from '@/components/github-import'
import { useAuth } from '@/lib/auth'
import { Button } from './ui/button'
import { Github, FolderOpen } from 'lucide-react'
import Spinner from './ui/spinner'
interface IDEProps {
sandboxId?: string // Optional sandbox ID for viewing sandbox files
}
export function IDE({ sandboxId }: IDEProps = {}) {
const { session, loading } = useAuth(() => {}, () => {})
const [files, setFiles] = useState<FileSystemNode[]>([])
const [selectedFile, setSelectedFile] = useState<{
path: string
content: string
} | null>(null)
const [showGitHubImport, setShowGitHubImport] = useState(false)
const isSandboxMode = !!sandboxId
const fetchFiles = useCallback(async () => {
if (isSandboxMode && sandboxId) {
// Fetch files from sandbox
try {
const response = await fetch(`/api/sandbox/${sandboxId}/files`)
if (response.ok) {
const data = await response.json()
setFiles(data.files || [])
} else {
console.error('Failed to fetch sandbox files')
setFiles([])
}
} catch (error) {
console.error('Error fetching sandbox files:', error)
setFiles([])
}
} else if (session) {
// Fetch files from Supabase
try {
const response = await fetch('/api/files')
if (response.ok) {
const data = await response.json()
setFiles(data)
} else {
console.error('Failed to fetch files')
setFiles([])
}
} catch (error) {
console.error('Error fetching files:', error)
setFiles([])
}
}
}, [session, isSandboxMode, sandboxId])
useEffect(() => {
if (isSandboxMode || session) {
fetchFiles()
}
}, [session, isSandboxMode, fetchFiles])
if (loading) {
return (
<div className="flex items-center justify-center h-full">
<Spinner />
</div>
)
}
async function handleSelectFile(path: string) {
if (isSandboxMode && sandboxId) {
// Load file from sandbox
const response = await fetch(`/api/sandbox/${sandboxId}/files/content?path=${encodeURIComponent(path)}`)
const { content } = await response.json()
setSelectedFile({ path, content })
} else if (session) {
// Load file from Supabase
const response = await fetch(`/api/files/content?path=${encodeURIComponent(path)}`)
const { content } = await response.json()
setSelectedFile({ path, content })
}
}
async function handleSaveFile(path: string, content: string) {
if (isSandboxMode && sandboxId) {
// Save file to sandbox
await fetch(`/api/sandbox/${sandboxId}/files/content`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ path, content }),
})
} else if (session) {
// Save file to Supabase
await fetch('/api/files/content', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ path, content }),
})
}
}
async function handleCreateFile(path: string, isDirectory: boolean) {
// File creation in sandbox mode is not supported via this UI
if (isSandboxMode) {
console.log('File creation in sandbox mode not supported')
return
}
if (!session) return
try {
const response = await fetch('/api/files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
path,
isDirectory,
content: isDirectory ? '' : '// New file\n'
}),
})
if (response.ok) {
await fetchFiles()
}
} catch (error) {
console.error('Error creating file:', error)
}
}
async function handleDeleteFile(path: string) {
// File deletion in sandbox mode is not supported via this UI
if (isSandboxMode) {
console.log('File deletion in sandbox mode not supported')
return
}
if (!session) return
try {
const response = await fetch('/api/files', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
path,
}),
})
if (response.ok) {
await fetchFiles()
if (selectedFile?.path === path) {
setSelectedFile(null)
}
}
} catch (error) {
console.error('Error deleting file:', error)
}
}
async function handleImportRepository(repo: any, repoFiles: any[]) {
if (!session) return
try {
// The files have been imported via the GitHubImport component
// Just refresh the file list to show the newly imported files
await fetchFiles()
setShowGitHubImport(false)
} catch (error) {
console.error('Error after repository import:', error)
}
}
if (showGitHubImport) {
return (
<div className="h-full p-4 overflow-auto">
<GitHubImport
onImport={handleImportRepository}
onClose={() => setShowGitHubImport(false)}
/>
</div>
)
}
return (
<div className="flex h-full">
<div className="w-1/4 border-r overflow-auto">
<div className="p-2 border-b space-y-2">
<Button
onClick={fetchFiles}
className="w-full"
variant="outline"
size="sm"
>
<FolderOpen className="h-4 w-4 mr-2" />
{isSandboxMode ? 'Refresh Sandbox Files' : 'Refresh Files'}
</Button>
{!isSandboxMode && (
<Button
onClick={() => setShowGitHubImport(true)}
className="w-full"
variant="outline"
size="sm"
>
<Github className="h-4 w-4 mr-2" />
Import from GitHub
</Button>
)}
</div>
<FileTree
files={files}
onSelectFile={handleSelectFile}
onCreateFile={handleCreateFile}
onDeleteFile={handleDeleteFile}
/>
</div>
<div className="w-3/4">
{selectedFile ? (
<CodeEditor
key={selectedFile.path}
code={selectedFile.content}
lang={selectedFile.path.split('.').pop() || 'typescript'}
onChange={(content) => handleSaveFile(selectedFile.path, content || '')}
/>
) : (
<div className="flex items-center justify-center h-full">
<p>Select a file to view its content</p>
</div>
)}
</div>
</div>
)
}