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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
// Copyright (c) 2026 Yuren Hao
// Licensed under AGPL-3.0 - see LICENSE file
import { useState, useCallback, useEffect, useRef } from 'react'
import { useAppStore, type FileNode } from '../stores/appStore'
const BINARY_EXTS = new Set(['pdf', 'png', 'jpg', 'jpeg', 'gif', 'svg', 'eps', 'zip', 'tiff', 'bmp'])
interface ContextMenuState {
x: number
y: number
node: FileNode
view: 'project' | 'workspace'
}
function fileIcon(node: FileNode, expanded: boolean): string {
if (node.isDir) return expanded ? '📂' : '📁'
const ext = node.name.split('.').pop()?.toLowerCase() ?? ''
return ext === 'tex' ? '📄'
: ext === 'bib' ? '📚'
: ext === 'pdf' ? '📕'
: ext === 'png' || ext === 'jpg' || ext === 'jpeg' ? '🖼️'
: ext === 'py' || ext === 'sh' ? '⚙️'
: '📝'
}
function FileTreeNode({
node,
depth,
onContextMenu
}: {
node: FileNode
depth: number
onContextMenu: (e: React.MouseEvent, node: FileNode) => void
}) {
const [expanded, setExpanded] = useState(depth < 2)
const { activeTab, openFile, setFileContent, setStatusMessage, mainDocument } = useAppStore()
const isActive = activeTab === node.path
const isMainDoc = node.docId && mainDocument === node.docId
const handleClick = useCallback(async () => {
if (node.isDir) {
setExpanded(!expanded)
return
}
// Binary files — skip
const ext = node.name.split('.').pop()?.toLowerCase()
if (ext === 'pdf' || ext === 'png' || ext === 'jpg' || ext === 'jpeg' || ext === 'svg') {
if (ext === 'pdf') {
useAppStore.getState().setPdfPath(node.path)
}
return
}
// Join doc via socket
if (node.docId) {
setStatusMessage('Opening document...')
try {
const result = await window.api.otJoinDoc(node.docId)
if (result.success && result.content !== undefined) {
setFileContent(node.path, result.content)
openFile(node.path, node.name)
if (result.version !== undefined) {
useAppStore.getState().setDocVersion(node.docId, result.version)
}
if (result.ranges?.comments) {
const contexts: Record<string, { file: string; text: string; pos: number }> = {}
for (const c of result.ranges.comments) {
if (c.op?.t) {
contexts[c.op.t] = { file: node.path, text: c.op.c || '', pos: c.op.p || 0 }
}
}
const existing = useAppStore.getState().commentContexts
useAppStore.getState().setCommentContexts({ ...existing, ...contexts })
}
setStatusMessage('Ready')
} else {
setStatusMessage(result.message || 'Failed to open document')
}
} catch {
setStatusMessage('Failed to open document')
}
}
}, [node, expanded, openFile, setFileContent, setStatusMessage])
return (
<div>
<div
className={`file-tree-item ${isActive ? 'active' : ''}`}
style={{ paddingLeft: depth * 16 + 8 }}
onClick={handleClick}
onContextMenu={(e) => onContextMenu(e, node)}
>
<span className="file-icon">{fileIcon(node, expanded)}</span>
<span className="file-name">
{node.name}
{isMainDoc && <span className="main-doc-badge">main</span>}
</span>
</div>
{node.isDir && expanded && node.children?.map((child) => (
<FileTreeNode key={child.path} node={child} depth={depth + 1} onContextMenu={onContextMenu} />
))}
</div>
)
}
/** Workspace (agent scratch space) node — files live on disk, not on Overleaf */
function WorkspaceNode({
node,
depth,
onContextMenu
}: {
node: FileNode
depth: number
onContextMenu: (e: React.MouseEvent, node: FileNode) => void
}) {
const [expanded, setExpanded] = useState(depth < 2)
const { activeTab, openFile, setFileContent, setStatusMessage, syncDir } = useAppStore()
const isActive = activeTab === node.path
const handleClick = useCallback(async () => {
if (node.isDir) {
setExpanded(!expanded)
return
}
const abs = `${syncDir}/${node.path}`
const ext = node.name.split('.').pop()?.toLowerCase() ?? ''
if (BINARY_EXTS.has(ext)) {
// Open binaries with the system default app
window.api.openPath(abs)
return
}
try {
const content = await window.api.readFile(abs)
setFileContent(node.path, content)
openFile(node.path, node.name)
} catch {
setStatusMessage(`Failed to open ${node.name}`)
}
}, [node, expanded, syncDir, openFile, setFileContent, setStatusMessage])
return (
<div>
<div
className={`file-tree-item ${isActive ? 'active' : ''}`}
style={{ paddingLeft: depth * 16 + 8 }}
onClick={handleClick}
onContextMenu={(e) => onContextMenu(e, node)}
>
<span className="file-icon">{fileIcon(node, expanded)}</span>
<span className="file-name">{node.name}</span>
</div>
{node.isDir && expanded && node.children?.map((child) => (
<WorkspaceNode key={child.path} node={child} depth={depth + 1} onContextMenu={onContextMenu} />
))}
</div>
)
}
export default function FileTree() {
const { files, syncDir } = useAppStore()
const [view, setView] = useState<'project' | 'workspace'>('project')
const [wsNodes, setWsNodes] = useState<FileNode[]>([])
const [ctxMenu, setCtxMenu] = useState<ContextMenuState | null>(null)
const menuRef = useRef<HTMLDivElement>(null)
const workspaceRoot = syncDir ? `${syncDir}/claude-workspace` : ''
const loadWorkspace = useCallback(async () => {
if (!workspaceRoot) return
try {
const nodes = await window.api.listDirTree(workspaceRoot, 'claude-workspace/')
setWsNodes(nodes as FileNode[])
} catch { /* workspace may not exist yet */ }
}, [workspaceRoot])
// Refresh workspace on switch + poll while visible (agents write here)
useEffect(() => {
if (view !== 'workspace') return
loadWorkspace()
const timer = setInterval(loadWorkspace, 5000)
return () => clearInterval(timer)
}, [view, loadWorkspace])
// Close context menu on outside click or escape
useEffect(() => {
if (!ctxMenu) return
const handleClick = (e: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
setCtxMenu(null)
}
}
const handleKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') setCtxMenu(null)
}
document.addEventListener('mousedown', handleClick)
document.addEventListener('keydown', handleKey)
return () => {
document.removeEventListener('mousedown', handleClick)
document.removeEventListener('keydown', handleKey)
}
}, [ctxMenu])
const handleContextMenu = useCallback((e: React.MouseEvent, node: FileNode) => {
e.preventDefault()
e.stopPropagation()
setCtxMenu({ x: e.clientX, y: e.clientY, node, view })
}, [view])
const closeMenu = () => setCtxMenu(null)
// ── Project view actions (Overleaf entities) ──
const handleSetMainDoc = () => {
if (!ctxMenu) return
const node = ctxMenu.node
if (node.docId) {
useAppStore.getState().setMainDocument(node.docId)
useAppStore.getState().setStatusMessage(`Main document set to ${node.name}`)
}
closeMenu()
}
const handleCopyPath = () => {
if (!ctxMenu) return
navigator.clipboard.writeText(ctxMenu.node.path)
useAppStore.getState().setStatusMessage('Path copied')
closeMenu()
}
const handleRename = async () => {
if (!ctxMenu) return
const node = ctxMenu.node
const projectId = useAppStore.getState().overleafProjectId
if (!projectId) { closeMenu(); return }
const newName = prompt('New name:', node.name)
if (!newName?.trim() || newName === node.name) { closeMenu(); return }
let entityType: string
let entityId: string
if (node.isDir && node.folderId) {
entityType = 'folder'
entityId = node.folderId
} else if (node.docId) {
entityType = 'doc'
entityId = node.docId
} else if (node.fileRefId) {
entityType = 'file'
entityId = node.fileRefId
} else {
closeMenu(); return
}
const result = await window.api.overleafRenameEntity(projectId, entityType, entityId, newName.trim())
if (result.success) {
useAppStore.getState().setStatusMessage(`Renamed to ${newName.trim()}`)
} else {
useAppStore.getState().setStatusMessage(`Rename failed: ${result.message}`)
}
closeMenu()
}
const handleDelete = async () => {
if (!ctxMenu) return
const node = ctxMenu.node
const projectId = useAppStore.getState().overleafProjectId
if (!projectId) { closeMenu(); return }
if (!confirm(`Delete "${node.name}"?`)) { closeMenu(); return }
let entityType: string
let entityId: string
if (node.isDir && node.folderId) {
entityType = 'folder'
entityId = node.folderId
} else if (node.docId) {
entityType = 'doc'
entityId = node.docId
} else if (node.fileRefId) {
entityType = 'file'
entityId = node.fileRefId
} else {
closeMenu(); return
}
const result = await window.api.overleafDeleteEntity(projectId, entityType, entityId)
if (result.success) {
useAppStore.getState().setStatusMessage(`Deleted ${node.name}`)
} else {
useAppStore.getState().setStatusMessage(`Delete failed: ${result.message}`)
}
closeMenu()
}
const handleNewFile = async () => {
if (!ctxMenu) return
const node = ctxMenu.node
const projectId = useAppStore.getState().overleafProjectId
if (!projectId) { closeMenu(); return }
const name = prompt('New file name:', 'untitled.tex')
if (!name?.trim()) { closeMenu(); return }
const parentId = node.isDir && node.folderId
? node.folderId
: useAppStore.getState().rootFolderId
const result = await window.api.overleafCreateDoc(projectId, parentId, name.trim())
if (result.success) {
useAppStore.getState().setStatusMessage(`Created ${name.trim()}`)
} else {
useAppStore.getState().setStatusMessage(`Create failed: ${result.message}`)
}
closeMenu()
}
const handleNewFolder = async () => {
if (!ctxMenu) return
const node = ctxMenu.node
const projectId = useAppStore.getState().overleafProjectId
if (!projectId) { closeMenu(); return }
const name = prompt('New folder name:', 'new-folder')
if (!name?.trim()) { closeMenu(); return }
const parentId = node.isDir && node.folderId
? node.folderId
: useAppStore.getState().rootFolderId
const result = await window.api.overleafCreateFolder(projectId, parentId, name.trim())
if (result.success) {
useAppStore.getState().setStatusMessage(`Created folder ${name.trim()}`)
} else {
useAppStore.getState().setStatusMessage(`Create failed: ${result.message}`)
}
closeMenu()
}
// ── Workspace view actions (disk files under claude-workspace/) ──
const wsAbs = (node: FileNode) => `${syncDir}/${node.path}`
/** Copy a workspace file/folder into the project root — the sync bridge
detects it and creates it on Overleaf. */
const handleImportToProject = async () => {
if (!ctxMenu || !syncDir) return
const node = ctxMenu.node
closeMenu()
// Avoid clobbering an existing project file with the same name
let destName = node.name
if (await window.api.pathExists(`${syncDir}/${destName}`)) {
const dot = destName.lastIndexOf('.')
const stem = dot > 0 ? destName.slice(0, dot) : destName
const ext = dot > 0 ? destName.slice(dot) : ''
destName = `${stem}-imported${ext}`
if (await window.api.pathExists(`${syncDir}/${destName}`)) {
useAppStore.getState().setStatusMessage(`Import failed: ${destName} already exists`)
return
}
}
try {
await window.api.copyPath(wsAbs(node), `${syncDir}/${destName}`)
useAppStore.getState().setStatusMessage(`Imported ${destName} — syncing to Overleaf`)
} catch (e) {
useAppStore.getState().setStatusMessage(`Import failed: ${e}`)
}
}
const handleWsRename = async () => {
if (!ctxMenu || !syncDir) return
const node = ctxMenu.node
const newName = prompt('New name:', node.name)
closeMenu()
if (!newName?.trim() || newName === node.name) return
const parent = node.path.slice(0, node.path.lastIndexOf('/'))
try {
await window.api.renamePath(wsAbs(node), `${syncDir}/${parent}/${newName.trim()}`)
loadWorkspace()
} catch (e) {
useAppStore.getState().setStatusMessage(`Rename failed: ${e}`)
}
}
const handleWsDelete = async () => {
if (!ctxMenu || !syncDir) return
const node = ctxMenu.node
closeMenu()
if (!confirm(`Delete "${node.name}" from workspace?`)) return
try {
await window.api.deletePath(wsAbs(node))
useAppStore.getState().closeTab(node.path)
loadWorkspace()
} catch (e) {
useAppStore.getState().setStatusMessage(`Delete failed: ${e}`)
}
}
const handleWsNewFile = async (parentNode?: FileNode) => {
if (!syncDir) return
closeMenu()
const name = prompt('New file name:', 'notes.md')
if (!name?.trim()) return
const parentPath = parentNode?.isDir ? parentNode.path : 'claude-workspace'
try {
await window.api.writeFile(`${syncDir}/${parentPath}/${name.trim()}`, '')
loadWorkspace()
} catch (e) {
useAppStore.getState().setStatusMessage(`Create failed: ${e}`)
}
}
const handleWsNewFolder = async (parentNode?: FileNode) => {
if (!syncDir) return
closeMenu()
const name = prompt('New folder name:', 'new-folder')
if (!name?.trim()) return
const parentPath = parentNode?.isDir ? parentNode.path : 'claude-workspace'
try {
await window.api.mkdirp(`${syncDir}/${parentPath}/${name.trim()}`)
loadWorkspace()
} catch (e) {
useAppStore.getState().setStatusMessage(`Create failed: ${e}`)
}
}
const handleRevealInFinder = () => {
if (!ctxMenu || !syncDir) return
window.api.showInFinder(wsAbs(ctxMenu.node))
closeMenu()
}
// ── Drag & drop ──
const [dragOver, setDragOver] = useState(false)
const handleDragOver = useCallback((e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
setDragOver(true)
}, [])
const handleDragLeave = useCallback((e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
setDragOver(false)
}, [])
const handleDrop = useCallback(async (e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
setDragOver(false)
const droppedFiles = e.dataTransfer.files
if (droppedFiles.length === 0) return
if (view === 'workspace') {
// Copy into the agent workspace on disk (not synced to Overleaf)
const dir = useAppStore.getState().syncDir
if (!dir) return
for (let i = 0; i < droppedFiles.length; i++) {
const file = droppedFiles[i]
const srcPath = window.api.getPathForFile(file)
try {
await window.api.copyPath(srcPath, `${dir}/claude-workspace/${file.name}`)
useAppStore.getState().setStatusMessage(`Copied ${file.name} to workspace`)
} catch (err) {
useAppStore.getState().setStatusMessage(`Copy failed: ${err}`)
return
}
}
loadWorkspace()
return
}
const projectId = useAppStore.getState().overleafProjectId
const folderId = useAppStore.getState().rootFolderId
if (!projectId || !folderId) return
for (let i = 0; i < droppedFiles.length; i++) {
const file = droppedFiles[i]
const filePath = window.api.getPathForFile(file)
const fileName = file.name
useAppStore.getState().setStatusMessage(`Uploading ${fileName}...`)
const result = await window.api.uploadFileToProject(projectId, folderId, filePath, fileName)
if (result.success) {
useAppStore.getState().setStatusMessage(`Uploaded ${fileName}`)
} else {
useAppStore.getState().setStatusMessage(`Upload failed: ${result.message}`)
return
}
}
}, [view, loadWorkspace])
const handleOpenInOverleaf = () => {
const projectId = useAppStore.getState().overleafProjectId
if (projectId) {
window.api.openExternal(`https://www.overleaf.com/project/${projectId}`)
}
closeMenu()
}
return (
<div
className={`file-tree${dragOver ? ' file-tree-dragover' : ''}`}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
<div className="file-tree-header file-tree-tabs">
<button
className={`file-tree-tab ${view === 'project' ? 'active' : ''}`}
onClick={() => setView('project')}
title="Overleaf project files (synced)"
>
Project
</button>
<button
className={`file-tree-tab ${view === 'workspace' ? 'active' : ''}`}
onClick={() => setView('workspace')}
title="Agent scratch space (claude-workspace/, not synced to Overleaf)"
>
Workspace
</button>
</div>
{view === 'workspace' && (
<div className="file-tree-ws-toolbar">
<button className="file-tree-ws-btn" title="New file" onClick={() => handleWsNewFile()}>+ File</button>
<button className="file-tree-ws-btn" title="New folder" onClick={() => handleWsNewFolder()}>+ Folder</button>
<button className="file-tree-ws-btn" title="Refresh" onClick={loadWorkspace}>↻</button>
<button
className="file-tree-ws-btn"
title="Reveal workspace in Finder"
onClick={() => workspaceRoot && window.api.showInFinder(workspaceRoot)}
>
Finder
</button>
</div>
)}
<div className="file-tree-content">
{view === 'project' ? (
<>
{files.map((node) => (
<FileTreeNode key={node.path} node={node} depth={0} onContextMenu={handleContextMenu} />
))}
{files.length === 0 && (
<div className="file-tree-empty">No files found</div>
)}
</>
) : (
<>
{wsNodes.map((node) => (
<WorkspaceNode key={node.path} node={node} depth={0} onContextMenu={handleContextMenu} />
))}
{wsNodes.length === 0 && (
<div className="file-tree-empty">
Workspace is empty — agents use this scratch space for notes,
experiments, and generated files. Drop files here to add them.
</div>
)}
</>
)}
</div>
{ctxMenu && ctxMenu.view === 'project' && (
<div
ref={menuRef}
className="context-menu"
style={{ left: ctxMenu.x, top: ctxMenu.y }}
>
{ctxMenu.node.docId && ctxMenu.node.name.endsWith('.tex') && (
<div className="context-menu-item" onClick={handleSetMainDoc}>
Set as Main Document
</div>
)}
<div className="context-menu-item" onClick={handleCopyPath}>
Copy Path
</div>
<div className="context-menu-separator" />
<div className="context-menu-item" onClick={handleRename}>
Rename
</div>
{ctxMenu.node.isDir && (
<>
<div className="context-menu-item" onClick={handleNewFile}>
New File
</div>
<div className="context-menu-item" onClick={handleNewFolder}>
New Folder
</div>
</>
)}
<div className="context-menu-separator" />
<div className="context-menu-item danger" onClick={handleDelete}>
Delete
</div>
<div className="context-menu-separator" />
<div className="context-menu-item" onClick={handleOpenInOverleaf}>
Open in Overleaf
</div>
</div>
)}
{ctxMenu && ctxMenu.view === 'workspace' && (
<div
ref={menuRef}
className="context-menu"
style={{ left: ctxMenu.x, top: ctxMenu.y }}
>
{!ctxMenu.node.isDir && (
<div className="context-menu-item" onClick={handleImportToProject}>
Import to Project
</div>
)}
{ctxMenu.node.isDir && (
<div className="context-menu-item" onClick={handleImportToProject}>
Import Folder to Project
</div>
)}
<div className="context-menu-item" onClick={handleCopyPath}>
Copy Path
</div>
<div className="context-menu-separator" />
<div className="context-menu-item" onClick={handleWsRename}>
Rename
</div>
{ctxMenu.node.isDir && (
<>
<div className="context-menu-item" onClick={() => handleWsNewFile(ctxMenu.node)}>
New File
</div>
<div className="context-menu-item" onClick={() => handleWsNewFolder(ctxMenu.node)}>
New Folder
</div>
</>
)}
<div className="context-menu-item" onClick={handleRevealInFinder}>
Reveal in Finder
</div>
<div className="context-menu-separator" />
<div className="context-menu-item danger" onClick={handleWsDelete}>
Delete
</div>
</div>
)}
</div>
)
}
|