@@ -9,37 +9,59 @@ import { Button } from './ui/button'
99import { Github , FolderOpen } from 'lucide-react'
1010import 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