Spaces:
Running
Running
Fix file loading to prevent overwriting clicked file
Browse files- Create loadAllFilesForTree() that loads file tree without changing active file
- Use loadAllFilesForTree() instead of loadFiles() when opening a specific file
- Prevents secure files from being overwritten by the first file in the list
- Public files show simple file structure, secure files show all dart files
- Both secure and public files now load correctly
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- app/components/FlutterRunner.tsx +89 -24
app/components/FlutterRunner.tsx
CHANGED
|
@@ -42,7 +42,68 @@ export function FlutterRunner({ onClose, onMinimize, onMaximize, onFocus, zIndex
|
|
| 42 |
const [passkey, setPasskey] = useState('')
|
| 43 |
const [loading, setLoading] = useState(false)
|
| 44 |
|
| 45 |
-
// Load files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
const loadFiles = async (key: string) => {
|
| 47 |
setLoading(true)
|
| 48 |
try {
|
|
@@ -133,20 +194,22 @@ export function FlutterRunner({ onClose, onMinimize, onMaximize, onFocus, zIndex
|
|
| 133 |
|
| 134 |
if (sessionPasskey) {
|
| 135 |
setPasskey(sessionPasskey)
|
| 136 |
-
loadFiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
-
// Set up file structure
|
| 140 |
-
setFiles([{
|
| 141 |
-
id: 'root',
|
| 142 |
-
name: 'lib',
|
| 143 |
-
type: 'folder',
|
| 144 |
-
isOpen: true,
|
| 145 |
-
children: [
|
| 146 |
-
{ id: 'main', name: sessionFileName || 'main.dart', type: 'file', content: sessionFileContent }
|
| 147 |
-
]
|
| 148 |
-
}])
|
| 149 |
-
|
| 150 |
// Clear session storage after loading
|
| 151 |
sessionStorage.removeItem('flutterFileContent')
|
| 152 |
} else if (sessionPasskey) {
|
|
@@ -182,19 +245,21 @@ export function FlutterRunner({ onClose, onMinimize, onMaximize, onFocus, zIndex
|
|
| 182 |
|
| 183 |
if (sessionPasskey) {
|
| 184 |
setPasskey(sessionPasskey)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
-
// Update file structure
|
| 188 |
-
setFiles([{
|
| 189 |
-
id: 'root',
|
| 190 |
-
name: 'lib',
|
| 191 |
-
type: 'folder',
|
| 192 |
-
isOpen: true,
|
| 193 |
-
children: [
|
| 194 |
-
{ id: 'main', name: sessionFileName || 'main.dart', type: 'file', content: sessionFileContent }
|
| 195 |
-
]
|
| 196 |
-
}])
|
| 197 |
-
|
| 198 |
// Clear session storage after loading
|
| 199 |
sessionStorage.removeItem('flutterFileContent')
|
| 200 |
}
|
|
|
|
| 42 |
const [passkey, setPasskey] = useState('')
|
| 43 |
const [loading, setLoading] = useState(false)
|
| 44 |
|
| 45 |
+
// Load all files for the file tree without changing the active file
|
| 46 |
+
const loadAllFilesForTree = async (key: string, currentFileName: string) => {
|
| 47 |
+
try {
|
| 48 |
+
const response = await fetch(`/api/data?key=${encodeURIComponent(key)}&folder=`)
|
| 49 |
+
const data = await response.json()
|
| 50 |
+
|
| 51 |
+
if (data.error) {
|
| 52 |
+
throw new Error(data.error)
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// Filter for .dart files
|
| 56 |
+
const dartFiles = data.files?.filter((f: any) => f.name.endsWith('.dart')) || []
|
| 57 |
+
|
| 58 |
+
if (dartFiles.length > 0) {
|
| 59 |
+
const fileNodes: FileNode[] = dartFiles.map((file: any, index: number) => ({
|
| 60 |
+
id: `file_${index}`,
|
| 61 |
+
name: file.name,
|
| 62 |
+
type: 'file' as const,
|
| 63 |
+
content: file.content
|
| 64 |
+
}))
|
| 65 |
+
|
| 66 |
+
setFiles([{
|
| 67 |
+
id: 'root',
|
| 68 |
+
name: 'Dart Files',
|
| 69 |
+
type: 'folder',
|
| 70 |
+
isOpen: true,
|
| 71 |
+
children: fileNodes
|
| 72 |
+
}])
|
| 73 |
+
|
| 74 |
+
// Set active file ID to match the current file
|
| 75 |
+
const currentFileNode = fileNodes.find(f => f.name === currentFileName)
|
| 76 |
+
if (currentFileNode) {
|
| 77 |
+
setActiveFileId(currentFileNode.id)
|
| 78 |
+
}
|
| 79 |
+
} else {
|
| 80 |
+
// No files found, just show the current file
|
| 81 |
+
setFiles([{
|
| 82 |
+
id: 'root',
|
| 83 |
+
name: 'lib',
|
| 84 |
+
type: 'folder',
|
| 85 |
+
isOpen: true,
|
| 86 |
+
children: [
|
| 87 |
+
{ id: 'main', name: currentFileName, type: 'file', content: '' }
|
| 88 |
+
]
|
| 89 |
+
}])
|
| 90 |
+
}
|
| 91 |
+
} catch (err) {
|
| 92 |
+
console.error('Error loading file tree:', err)
|
| 93 |
+
// On error, just show the current file
|
| 94 |
+
setFiles([{
|
| 95 |
+
id: 'root',
|
| 96 |
+
name: 'lib',
|
| 97 |
+
type: 'folder',
|
| 98 |
+
isOpen: true,
|
| 99 |
+
children: [
|
| 100 |
+
{ id: 'main', name: currentFileName, type: 'file', content: '' }
|
| 101 |
+
]
|
| 102 |
+
}])
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// Load files from secure storage (used when no specific file is selected)
|
| 107 |
const loadFiles = async (key: string) => {
|
| 108 |
setLoading(true)
|
| 109 |
try {
|
|
|
|
| 194 |
|
| 195 |
if (sessionPasskey) {
|
| 196 |
setPasskey(sessionPasskey)
|
| 197 |
+
// Don't call loadFiles here - it would overwrite the file we just loaded
|
| 198 |
+
// Just load all files for the file tree without changing the active file
|
| 199 |
+
loadAllFilesForTree(sessionPasskey, sessionFileName || 'main.dart')
|
| 200 |
+
} else {
|
| 201 |
+
// For public files, just set up a simple file structure
|
| 202 |
+
setFiles([{
|
| 203 |
+
id: 'root',
|
| 204 |
+
name: 'lib',
|
| 205 |
+
type: 'folder',
|
| 206 |
+
isOpen: true,
|
| 207 |
+
children: [
|
| 208 |
+
{ id: 'main', name: sessionFileName || 'main.dart', type: 'file', content: sessionFileContent }
|
| 209 |
+
]
|
| 210 |
+
}])
|
| 211 |
}
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
// Clear session storage after loading
|
| 214 |
sessionStorage.removeItem('flutterFileContent')
|
| 215 |
} else if (sessionPasskey) {
|
|
|
|
| 245 |
|
| 246 |
if (sessionPasskey) {
|
| 247 |
setPasskey(sessionPasskey)
|
| 248 |
+
// Load all files for the file tree without overwriting the current file
|
| 249 |
+
loadAllFilesForTree(sessionPasskey, sessionFileName || 'main.dart')
|
| 250 |
+
} else {
|
| 251 |
+
// For public files, just set up a simple file structure
|
| 252 |
+
setFiles([{
|
| 253 |
+
id: 'root',
|
| 254 |
+
name: 'lib',
|
| 255 |
+
type: 'folder',
|
| 256 |
+
isOpen: true,
|
| 257 |
+
children: [
|
| 258 |
+
{ id: 'main', name: sessionFileName || 'main.dart', type: 'file', content: sessionFileContent }
|
| 259 |
+
]
|
| 260 |
+
}])
|
| 261 |
}
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
// Clear session storage after loading
|
| 264 |
sessionStorage.removeItem('flutterFileContent')
|
| 265 |
}
|