forked from SmooSenseAI/smoosense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNodeComponent.tsx
More file actions
284 lines (251 loc) · 8.7 KB
/
TreeNodeComponent.tsx
File metadata and controls
284 lines (251 loc) · 8.7 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
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
'use client'
import { useCallback, useEffect } from 'react'
import { NodeApi } from 'react-arborist'
import { ExternalLink, RefreshCw } from 'lucide-react'
import { useAppSelector, useAppDispatch } from '@/lib/hooks'
import { loadFolderContents, setViewingId, toggleNodeExpansion } from '@/lib/features/folderTree/folderTreeSlice'
import { cn } from '@/lib/utils'
import { getFileType, FileType } from '@/lib/utils/fileTypes'
import { ICONS } from '@/lib/utils/iconUtils'
import { formatDate, formatRelativeTime } from '@/lib/utils/timeUtils'
export interface ArboristNodeData {
id: string
name: string
path: string
isDir: boolean
size: number
lastModified: number
isBrokenSymlink: boolean
symlinkTarget?: string
isLoaded: boolean
loading: boolean
isExpanded: boolean
children?: ArboristNodeData[]
isLoadMore?: boolean
parentPath?: string
loadedCount?: number
}
interface TreeNodeComponentProps {
node: NodeApi<ArboristNodeData>
style: React.CSSProperties
dragHandle?: (el: HTMLDivElement | null) => void
}
function formatFileSize(bytes: number): string {
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return Math.round((bytes / Math.pow(k, i)) * 10) / 10 + ' ' + sizes[i]
}
export default function TreeNodeComponent({ node, style }: TreeNodeComponentProps) {
const dispatch = useAppDispatch()
const viewingId = useAppSelector(state => state.folderTree.viewingId)
const fileInfoToShow = useAppSelector(state => state.ui.fileInfoToShow)
const nodeData = node.data
const isViewing = viewingId === nodeData.id
// Sync react-arborist expansion state with Redux state
useEffect(() => {
if (nodeData.isDir && nodeData.isExpanded !== node.isOpen) {
if (nodeData.isExpanded) {
node.open()
} else {
node.close()
}
}
}, [nodeData.isExpanded, nodeData.isDir, node])
const handleClick = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
// Set the viewing ID for both files and folders (also sets tablePath for table files)
dispatch(setViewingId(nodeData.id))
// If it's a directory and not loaded, load its contents (but don't expand)
if (nodeData.isDir && !nodeData.isLoaded) {
dispatch(loadFolderContents({ path: nodeData.path }))
}
}, [nodeData, dispatch])
const handleDoubleClick = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
if (nodeData.isDir) {
// For directories, toggle expand/collapse
if (!nodeData.isLoaded) {
dispatch(loadFolderContents({ path: nodeData.path }))
}
dispatch(toggleNodeExpansion(nodeData.path))
} else {
// For files, open table files in Table view
const fileType = getFileType(nodeData.name)
if (fileType === FileType.ColumnarTable || fileType === FileType.RowTable) {
const url = `./Table?tablePath=${encodeURIComponent(nodeData.path)}`
window.open(url, '_blank')
}
}
}, [nodeData, dispatch])
const handleOpenInNewTab = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
e.preventDefault()
// For .lance folders, open in Table view
if (nodeData.name.endsWith('.lance')) {
const url = `./Table?tablePath=${encodeURIComponent(nodeData.path)}`
window.open(url, '_blank')
return
}
// For other folders, open in FolderBrowser
const url = `./FolderBrowser?rootFolder=${encodeURIComponent(nodeData.path)}`
window.open(url, '_blank')
}, [nodeData.path, nodeData.name])
const handleRefresh = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
e.preventDefault()
// Refresh children data by reloading folder contents
dispatch(loadFolderContents({ path: nodeData.path }))
}, [nodeData.path, dispatch])
const renderIcon = () => {
if (nodeData.isBrokenSymlink) {
return ICONS.BROKEN_SYMLINK
}
if (nodeData.isDir) {
return node.isOpen ? ICONS.FOLDER_OPEN : ICONS.FOLDER_CLOSED
}
const fileType = getFileType(nodeData.name)
switch (fileType) {
case FileType.Json:
return ICONS.JSON
case FileType.ColumnarTable:
return ICONS.COLUMNAR_TABLE
case FileType.RowTable:
return ICONS.ROW_TABLE
case FileType.Image:
return ICONS.IMAGE
case FileType.Video:
return ICONS.VIDEO
case FileType.Text:
return ICONS.TEXT
default:
return ICONS.FILE_DEFAULT
}
}
const handleExpandClick = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
// If expanding and not loaded yet, load children first
if (!node.isOpen && !nodeData.isLoaded) {
dispatch(loadFolderContents({ path: nodeData.path }))
}
// Toggle expansion state in Redux (will sync to react-arborist via useEffect)
dispatch(toggleNodeExpansion(nodeData.path))
}, [node.isOpen, nodeData.isLoaded, nodeData.path, dispatch])
const renderExpandIcon = () => {
if (!nodeData.isDir) return <div className="w-4 h-4" />
if (nodeData.loading) {
return (
<div className="w-4 h-4 flex items-center justify-center">
<div className="w-2 h-2 border border-muted-foreground border-t-transparent rounded-full animate-spin" />
</div>
)
}
return (
<div onClick={handleExpandClick} className="cursor-pointer">
{node.isOpen ? ICONS.CHEVRON_DOWN : ICONS.CHEVRON_RIGHT}
</div>
)
}
const renderFileInfo = () => {
// Don't show info for directories
if (nodeData.isDir) return null
let content: string
switch (fileInfoToShow) {
case 'size':
content = formatFileSize(nodeData.size)
break
case 'lastModified':
content = formatDate(nodeData.lastModified)
break
case 'lastModifiedRelative':
content = formatRelativeTime(nodeData.lastModified)
break
default:
return null
}
return (
<span className="text-xs text-muted-foreground ml-auto">
{content}
</span>
)
}
// Render "load more" button for pseudo-nodes (after all hooks)
if (nodeData.isLoadMore) {
return (
<div
style={style}
className="flex items-center px-2 py-1 text-xs text-muted-foreground cursor-pointer hover:text-foreground hover:bg-muted/30 rounded"
onClick={(e) => {
e.stopPropagation()
if (nodeData.parentPath == null || nodeData.loadedCount == null) return
dispatch(loadFolderContents({
path: nodeData.parentPath,
offset: nodeData.loadedCount,
append: true,
}))
}}
>
<span className="ml-8">{nodeData.name}</span>
</div>
)
}
return (
<div
style={style}
className={cn(
"group flex items-center space-x-2 px-2 py-1 text-sm cursor-pointer hover:bg-muted/50 rounded relative",
"select-none",
isViewing && "bg-primary/20 hover:bg-primary/10 font-bold"
)}
onClick={handleClick}
onDoubleClick={handleDoubleClick}
>
{renderExpandIcon()}
{renderIcon()}
<span
className={cn(
"truncate flex-1",
nodeData.isBrokenSymlink && "text-red-500",
nodeData.symlinkTarget && !nodeData.isBrokenSymlink && "text-blue-500",
)}
title={nodeData.symlinkTarget
? `${nodeData.name} (→ ${nodeData.symlinkTarget})${nodeData.isBrokenSymlink ? ' (broken symlink)' : ''}`
: nodeData.name}
>
{nodeData.symlinkTarget
? `${nodeData.name} (→ ${nodeData.symlinkTarget})`
: nodeData.name}
</span>
{/* File info display */}
{renderFileInfo()}
{/* Floating buttons for folders */}
{nodeData.isDir && (
<div className="absolute right-1 opacity-0 group-hover:opacity-100 transition-opacity flex gap-1">
<button
onClick={handleRefresh}
className={cn(
"bg-background border border-border rounded p-1 shadow-sm hover:bg-muted cursor-pointer",
"flex items-center justify-center",
nodeData.loading && "animate-spin"
)}
title={`Refresh ${nodeData.name}`}
disabled={nodeData.loading}
>
<RefreshCw className="h-3 w-3" />
</button>
<button
onClick={handleOpenInNewTab}
className={cn(
"bg-background border border-border rounded p-1 shadow-sm hover:bg-muted cursor-pointer",
"flex items-center justify-center"
)}
title={`Open ${nodeData.name} in new tab`}
>
<ExternalLink className="h-3 w-3" />
</button>
</div>
)}
</div>
)
}