-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTreeRow.tsx
More file actions
413 lines (390 loc) · 12.5 KB
/
FileTreeRow.tsx
File metadata and controls
413 lines (390 loc) · 12.5 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
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
// for rendering the preview and download buttons in folder structure row
import type { TreeNode } from "./types";
import { formatLeafValue, isPreviewable } from "./utils";
import CheckIcon from "@mui/icons-material/Check";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import DownloadIcon from "@mui/icons-material/Download";
import ExpandLess from "@mui/icons-material/ExpandLess";
import ExpandMore from "@mui/icons-material/ExpandMore";
import FolderIcon from "@mui/icons-material/Folder";
import FolderOpenIcon from "@mui/icons-material/FolderOpen";
import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile";
import VisibilityIcon from "@mui/icons-material/Visibility";
import { Box, Button, Collapse, Typography } from "@mui/material";
import { Tooltip, IconButton } from "@mui/material";
import { Colors } from "design/theme";
import React, { useState } from "react";
import { Color } from "three";
// show more / show less button for long string
const LeafString: React.FC<{ value: string }> = ({ value }) => {
const LIMIT = 120;
const [expanded, setExpanded] = useState(false);
const isLong = value.length > LIMIT;
const display = expanded
? value
: isLong
? value.slice(0, LIMIT) + "…"
: value;
return (
<Box
sx={{
display: "flex",
alignItems: "baseline",
gap: 1,
mt: 0.25,
minWidth: 0,
}}
>
<Typography
sx={{
fontFamily: "monospace",
fontSize: "0.85rem",
color: "text.secondary",
// collapsed: clamp to 2 lines; expanded: fully wrap
display: expanded ? "block" : "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: expanded ? ("unset" as any) : 1,
whiteSpace: expanded ? "pre-wrap" : "normal",
overflow: expanded ? "visible" : "hidden",
textOverflow: expanded ? "unset" : "ellipsis",
flex: 1,
}}
>
{display}
</Typography>
{isLong && (
<Button
size="small"
variant="text"
onClick={(e) => {
e.stopPropagation(); // don’t toggle the row
setExpanded((v) => !v);
}}
sx={{
px: 0.5,
minWidth: "auto",
color: Colors.purple,
}}
>
{expanded ? "Show less" : "Show more"}
</Button>
)}
</Box>
);
};
type Props = {
node: TreeNode;
level: number;
// src is either an external URL(string) or the internal object
onPreview: (src: string | any, index: number, isInternal?: boolean) => void;
getInternalByPath: (path: string) => { data: any; index: number } | undefined;
getJsonByPath?: (path: string) => any;
highlightText?: string;
};
// copy helper function
const copyText = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
// fallback if the copy api not working
const ta = document.createElement("textarea");
ta.value = text;
ta.style.position = "fixed";
ta.style.opacity = "0";
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand("copy");
document.body.removeChild(ta);
return ok;
}
};
const FileTreeRow: React.FC<Props> = ({
node,
level,
onPreview,
getInternalByPath,
getJsonByPath,
highlightText,
}) => {
const [open, setOpen] = useState(false);
const [copied, setCopied] = useState(false);
// const internal = getInternalByPath?.(node.path);
// const internal = getInternalByPath ? getInternalByPath(node.path) : undefined;
const internal = getInternalByPath(node.path);
const externalUrl = node.link?.url;
const rowRef = React.useRef<HTMLDivElement | null>(null);
// Highlight only if this row is exactly the subject folder (e.g., "sub-04")
const isSubjectFolder =
node.kind === "folder" && /^sub-[A-Za-z0-9]+$/i.test(node.name);
const isExactHit =
!!highlightText &&
isSubjectFolder &&
node.name.toLowerCase() === highlightText.toLowerCase();
React.useEffect(() => {
if (isExactHit && rowRef.current) {
rowRef.current.scrollIntoView({ behavior: "smooth", block: "center" });
// subtle flash
// rowRef.current.animate(
// [
// { backgroundColor: `${Colors.yellow}`, offset: 0 }, // turn yellow
// { backgroundColor: `${Colors.yellow}`, offset: 0.85 }, // stay yellow 85% of time
// { backgroundColor: "transparent", offset: 1 }, // then fade out
// ],
// { duration: 8000, easing: "ease", fill: "forwards" }
// );
}
}, [isExactHit]);
const rowHighlightSx = isExactHit
? { backgroundColor: `${Colors.yellow}`, borderRadius: 4 }
: {};
const handleCopy = async (e: React.MouseEvent) => {
e.stopPropagation(); // prevent expand/ collapse from firing when click the copy button
const json = getJsonByPath?.(node.path); // call getJsonByPath(node.path)
const asText = JSON.stringify(json, null, 2); // subtree at this row
if (await copyText(asText ?? "null")) {
// call copyText function
setCopied(true);
setTimeout(() => setCopied(false), 1200);
}
};
if (node.kind === "folder") {
const isJson = /\.json$/i.test(node.name); // end with .json only
return (
<>
<Box
ref={rowRef}
sx={{
display: "flex",
alignItems: "flex-start",
gap: 1,
py: 0.5,
px: 1,
cursor: "pointer",
...rowHighlightSx,
"&:hover": { backgroundColor: "rgba(0,0,0,0.04)" },
}}
onClick={() => setOpen((o) => !o)}
>
<Box sx={{ pl: level * 1.25 }}>
{/* <FolderIcon
fontSize="small"
sx={{
color: isJson ? Colors.orange : Colors.darkPurple,
}}
/> */}
{open ? (
<FolderOpenIcon
fontSize="small"
sx={{ color: isJson ? Colors.orange : Colors.darkPurple }}
/>
) : (
<FolderIcon
fontSize="small"
sx={{ color: isJson ? Colors.orange : Colors.darkPurple }}
/>
)}
</Box>
<Typography
sx={{
flex: 1,
color: Colors.darkPurple,
}}
>
{node.name}
</Typography>
{/* Actions on folder if it carries a link (from linkHere) */}
{node.link?.url && (
<Box
sx={{ display: "flex", gap: 1, mr: 0.5, flexShrink: 0 }}
onClick={(e) => e.stopPropagation()} // don't toggle expand
>
<Button
size="small"
variant="text"
startIcon={<DownloadIcon fontSize="small" />}
onClick={() => window.open(node.link!.url, "_blank")}
sx={{ color: Colors.purple }}
>
Download
</Button>
{isPreviewable(node.link.url) && (
<Button
size="small"
variant="text"
startIcon={<VisibilityIcon fontSize="small" />}
onClick={() => onPreview(node.link!.url, node.link!.index)}
sx={{ color: Colors.purple }}
>
Preview
</Button>
)}
</Box>
)}
{/* internal preview action for folders */}
{internal && (
<Box
sx={{ display: "flex", gap: 1, mr: 0.5, flexShrink: 0 }}
onClick={(e) => e.stopPropagation()}
>
<Button
size="small"
variant="text"
startIcon={<VisibilityIcon fontSize="small" />}
onClick={() => onPreview(internal.data, internal.index, true)}
sx={{ color: Colors.purple }}
>
Preview
</Button>
</Box>
)}
{/* Copy subtree JSON button */}
<Box
sx={{ display: "flex", gap: 1, mr: 0.5, flexShrink: 0 }}
onClick={(e) => e.stopPropagation()}
>
<Tooltip title={copied ? "Copied!" : "Copy subtree JSON"} arrow>
<IconButton size="small" onClick={handleCopy}>
{copied ? (
<CheckIcon fontSize="inherit" />
) : (
<ContentCopyIcon fontSize="inherit" />
)}
</IconButton>
</Tooltip>
</Box>
{open ? <ExpandLess /> : <ExpandMore />}
</Box>
{/*timeout controls the duration of the expand/collapse animation*/}
<Collapse in={open} timeout="auto" unmountOnExit>
{node.children.map((child) => (
<FileTreeRow
key={child.path}
node={child}
level={level + 1}
onPreview={onPreview}
getInternalByPath={getInternalByPath}
getJsonByPath={getJsonByPath}
highlightText={highlightText} // for subject highlight
/>
))}
</Collapse>
</>
);
}
// if the node is a file
return (
<Box
sx={{ display: "flex", alignItems: "flex-start", gap: 1, py: 0.5, px: 1 }}
>
<Box sx={{ pl: level * 1.25, pt: "2px" }}>
<InsertDriveFileIcon
fontSize="small"
sx={{ color: Colors.darkGreen }}
/>
</Box>
<Box sx={{ flex: 1, minWidth: 0, overflow: "hidden" }}>
<Typography
title={node.name}
sx={{
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
{node.name}
</Typography>
{!node.link &&
node.value !== undefined &&
(typeof node.value === "string" ? (
<LeafString value={node.value} />
) : (
<Typography
title={
node.name === "_ArrayZipData_"
? "[compressed data]"
: JSON.stringify(node.value)
}
sx={{
fontFamily: "monospace",
fontSize: "0.85rem",
color: "text.secondary",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
mt: 0.25,
}}
>
{node.name === "_ArrayZipData_"
? "[compressed data]"
: formatLeafValue(node.value)}
</Typography>
))}
</Box>
{/* ALWAYS show copy for files, even when no external/internal */}
<Box sx={{ alignSelf: "flex-start" }}>
<Tooltip title={copied ? "Copied!" : "Copy JSON"} arrow>
<span>
<IconButton
size="small"
onClick={handleCopy}
disabled={!getJsonByPath} // optional safety
>
{copied ? (
<CheckIcon fontSize="inherit" />
) : (
<ContentCopyIcon fontSize="inherit" />
)}
</IconButton>
</span>
</Tooltip>
</Box>
{/* Placeholder to align with folder chevron */}
<Box sx={{ width: 28 }} />
{(externalUrl || internal) && (
<Box
sx={{ display: "flex", gap: 1, flexShrink: 0 }}
onClick={(e) => e.stopPropagation()}
>
{externalUrl && (
<>
<Button
size="small"
variant="text"
onClick={() => window.open(externalUrl, "_blank")}
>
Download
</Button>
{isPreviewable(externalUrl) && (
<Button
size="small"
variant="text"
onClick={() =>
onPreview(externalUrl, node.link!.index, false)
}
>
Preview
</Button>
)}
</>
)}
{internal && (
<Button
size="small"
variant="text"
startIcon={<VisibilityIcon fontSize="small" />}
onClick={(e) => {
e.stopPropagation();
onPreview(internal.data, internal.index, true);
}}
sx={{ color: Colors.purple }}
>
Preview
</Button>
)}
</Box>
)}
</Box>
);
};
export default FileTreeRow;