Skip to content

Commit 07527b0

Browse files
Update GitHub import to save files to workspace database
1 parent 44811c4 commit 07527b0

2 files changed

Lines changed: 135 additions & 49 deletions

File tree

components/github-import.tsx

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,49 @@ export function GitHubImport({ onImport, onClose }: GitHubImportProps) {
151151

152152
setIsImporting(true)
153153
setSelectedRepo(repo)
154-
154+
155155
try {
156-
// Simulate import process - in a real implementation, this would
157-
// clone the repository or fetch files and store them
158-
await new Promise(resolve => setTimeout(resolve, 1000)) // Simulate API call
156+
const [owner, repoName] = repo.full_name.split('/')
157+
158+
// Fetch root directory contents
159+
const rootResponse = await fetch(`/api/github/repos/${owner}/${repoName}`)
160+
if (!rootResponse.ok) {
161+
throw new Error('Failed to fetch repository contents')
162+
}
163+
164+
const rootData = await rootResponse.json()
159165

160-
// For demo purposes, we'll just count this as a successful import
161-
const importedFilesCount = Math.floor(Math.random() * 50) + 10 // Random number of files
166+
// Fetch all files recursively
167+
const allFiles = await fetchAllFiles(owner, repoName, rootData.contents || [])
168+
169+
// Save files to workspace
170+
let importedCount = 0
171+
for (const file of allFiles) {
172+
try {
173+
const response = await fetch('/api/files', {
174+
method: 'POST',
175+
headers: {
176+
'Content-Type': 'application/json',
177+
},
178+
body: JSON.stringify({
179+
sessionID: session.user.id,
180+
path: `${repo.name}/${file.path}`,
181+
isDirectory: false,
182+
content: file.content,
183+
}),
184+
})
185+
186+
if (response.ok) {
187+
importedCount++
188+
}
189+
} catch (error) {
190+
console.warn(`Failed to import file ${file.path}:`, error)
191+
}
192+
}
162193

163194
toast({
164195
title: "Success",
165-
description: `Successfully imported ${repo.name} with ${importedFilesCount} files. Repository information has been saved.`,
196+
description: `Successfully imported ${repo.name} with ${importedCount} files.`,
166197
})
167198

168199
// Update usage limits
@@ -174,11 +205,11 @@ export function GitHubImport({ onImport, onClose }: GitHubImportProps) {
174205
can_import: remainingImports > 0
175206
})
176207
}
177-
208+
178209
if (onImport) {
179-
onImport(repo, []) // Files are now handled by the API
210+
onImport(repo, allFiles)
180211
}
181-
212+
182213
} catch (error) {
183214
console.error('Error importing repository:', error)
184215
toast({

components/ide.tsx

Lines changed: 94 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,59 @@ import { Button } from './ui/button'
99
import { Github, FolderOpen } from 'lucide-react'
1010
import Spinner from './ui/spinner'
1111

12-
export function IDE() {
12+
interface IDEProps {
13+
sandboxId?: string // Optional sandbox ID for viewing sandbox files
14+
}
15+
16+
export function IDE({ sandboxId }: IDEProps = {}) {
1317
const { session, loading } = useAuth(() => {}, () => {})
1418
const [files, setFiles] = useState<FileSystemNode[]>([])
1519
const [selectedFile, setSelectedFile] = useState<{
1620
path: string
1721
content: string
1822
} | null>(null)
1923
const [showGitHubImport, setShowGitHubImport] = useState(false)
24+
const isSandboxMode = !!sandboxId
2025

2126
const fetchFiles = useCallback(async () => {
22-
if (!session) return
23-
try {
24-
const response = await fetch(`/api/files?sessionID=${session.user.id}`)
25-
if (response.ok) {
26-
const data = await response.json()
27-
setFiles(data)
28-
} else {
29-
console.error('Failed to fetch files')
27+
if (isSandboxMode && sandboxId) {
28+
// Fetch files from sandbox
29+
try {
30+
const response = await fetch(`/api/sandbox/${sandboxId}/files`)
31+
if (response.ok) {
32+
const data = await response.json()
33+
setFiles(data.files || [])
34+
} else {
35+
console.error('Failed to fetch sandbox files')
36+
setFiles([])
37+
}
38+
} catch (error) {
39+
console.error('Error fetching sandbox files:', error)
40+
setFiles([])
41+
}
42+
} else if (session) {
43+
// Fetch files from Supabase
44+
try {
45+
const response = await fetch(`/api/files?sessionID=${session.user.id}`)
46+
if (response.ok) {
47+
const data = await response.json()
48+
setFiles(data)
49+
} else {
50+
console.error('Failed to fetch files')
51+
setFiles([])
52+
}
53+
} catch (error) {
54+
console.error('Error fetching files:', error)
3055
setFiles([])
3156
}
32-
} catch (error) {
33-
console.error('Error fetching files:', error)
34-
setFiles([])
3557
}
36-
}, [session])
58+
}, [session, isSandboxMode, sandboxId])
3759

3860
useEffect(() => {
39-
if (session) {
61+
if (isSandboxMode || session) {
4062
fetchFiles()
4163
}
42-
}, [session, fetchFiles])
64+
}, [session, isSandboxMode, fetchFiles])
4365

4466
if (loading) {
4567
return (
@@ -50,24 +72,48 @@ export function IDE() {
5072
}
5173

5274
async function handleSelectFile(path: string) {
53-
if (!session) return
54-
const response = await fetch(`/api/files/content?sessionID=${session.user.id}&path=${path}`)
55-
const { content } = await response.json()
56-
setSelectedFile({ path, content })
75+
if (isSandboxMode && sandboxId) {
76+
// Load file from sandbox
77+
const response = await fetch(`/api/sandbox/${sandboxId}/files/content?path=${encodeURIComponent(path)}`)
78+
const { content } = await response.json()
79+
setSelectedFile({ path, content })
80+
} else if (session) {
81+
// Load file from Supabase
82+
const response = await fetch(`/api/files/content?sessionID=${session.user.id}&path=${path}`)
83+
const { content } = await response.json()
84+
setSelectedFile({ path, content })
85+
}
5786
}
5887

5988
async function handleSaveFile(path: string, content: string) {
60-
if (!session) return
61-
await fetch('/api/files/content', {
62-
method: 'POST',
63-
headers: {
64-
'Content-Type': 'application/json',
65-
},
66-
body: JSON.stringify({ sessionID: session.user.id, path, content }),
67-
})
89+
if (isSandboxMode && sandboxId) {
90+
// Save file to sandbox
91+
await fetch(`/api/sandbox/${sandboxId}/files/content`, {
92+
method: 'POST',
93+
headers: {
94+
'Content-Type': 'application/json',
95+
},
96+
body: JSON.stringify({ path, content }),
97+
})
98+
} else if (session) {
99+
// Save file to Supabase
100+
await fetch('/api/files/content', {
101+
method: 'POST',
102+
headers: {
103+
'Content-Type': 'application/json',
104+
},
105+
body: JSON.stringify({ sessionID: session.user.id, path, content }),
106+
})
107+
}
68108
}
69109

70110
async function handleCreateFile(path: string, isDirectory: boolean) {
111+
// File creation in sandbox mode is not supported via this UI
112+
if (isSandboxMode) {
113+
console.log('File creation in sandbox mode not supported')
114+
return
115+
}
116+
71117
if (!session) return
72118
try {
73119
const response = await fetch('/api/files', {
@@ -91,6 +137,12 @@ export function IDE() {
91137
}
92138

93139
async function handleDeleteFile(path: string) {
140+
// File deletion in sandbox mode is not supported via this UI
141+
if (isSandboxMode) {
142+
console.log('File deletion in sandbox mode not supported')
143+
return
144+
}
145+
94146
if (!session) return
95147
try {
96148
const response = await fetch('/api/files', {
@@ -117,11 +169,12 @@ export function IDE() {
117169
async function handleImportRepository(repo: any, repoFiles: any[]) {
118170
if (!session) return
119171
try {
120-
// The backend now handles file imports. We just need to refresh the file list.
172+
// The files have been imported via the GitHubImport component
173+
// Just refresh the file list to show the newly imported files
121174
await fetchFiles()
122175
setShowGitHubImport(false)
123176
} catch (error) {
124-
console.error('Error importing repository:', error)
177+
console.error('Error after repository import:', error)
125178
}
126179
}
127180

@@ -147,17 +200,19 @@ export function IDE() {
147200
size="sm"
148201
>
149202
<FolderOpen className="h-4 w-4 mr-2" />
150-
Refresh Files
151-
</Button>
152-
<Button
153-
onClick={() => setShowGitHubImport(true)}
154-
className="w-full"
155-
variant="outline"
156-
size="sm"
157-
>
158-
<Github className="h-4 w-4 mr-2" />
159-
Import from GitHub
203+
{isSandboxMode ? 'Refresh Sandbox Files' : 'Refresh Files'}
160204
</Button>
205+
{!isSandboxMode && (
206+
<Button
207+
onClick={() => setShowGitHubImport(true)}
208+
className="w-full"
209+
variant="outline"
210+
size="sm"
211+
>
212+
<Github className="h-4 w-4 mr-2" />
213+
Import from GitHub
214+
</Button>
215+
)}
161216
</div>
162217
<FileTree
163218
files={files}

0 commit comments

Comments
 (0)