-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathMain.tsx
279 lines (258 loc) · 7.35 KB
/
Main.tsx
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
// Main.tsx
import React, { useCallback, useEffect, useMemo, useState } from "react";
import {
Box,
Breadcrumbs,
Button,
CircularProgress,
Link,
Typography,
} from "@mui/material";
import { Home as HomeIcon, NoteAdd as NoteAddIcon } from "@mui/icons-material";
import FileGrid, { encodeKey, FileItem, isDirectory } from "./FileGrid";
import MultiSelectToolbar from "./MultiSelectToolbar";
import UploadDrawer, { UploadFab } from "./UploadDrawer";
import TextPadDrawer from "./TextPadDrawer";
import { copyPaste, fetchPath } from "./app/transfer";
import { useTransferQueue, useUploadEnqueue } from "./app/transferQueue";
// Centered helper
function Centered({ children }: { children: React.ReactNode }) {
return (
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100%",
}}
>
{children}
</Box>
);
}
// Breadcrumb component
function PathBreadcrumb({
path,
onCwdChange,
}: {
path: string;
onCwdChange: (newCwd: string) => void;
}) {
const parts = path.replace(/\/$/, "").split("/");
return (
<Breadcrumbs separator="›" sx={{ padding: 1 }}>
<Button onClick={() => onCwdChange("")} sx={{ minWidth: 0, padding: 0 }}>
<HomeIcon />
</Button>
{parts.map((part, index) =>
index === parts.length - 1 ? (
<Typography key={index} color="text.primary">
{part}
</Typography>
) : (
<Link
key={index}
component="button"
onClick={() => {
onCwdChange(parts.slice(0, index + 1).join("/") + "/");
}}
>
{part}
</Link>
)
)}
</Breadcrumbs>
);
}
// DropZone wrapper
function DropZone({
children,
onDrop,
}: {
children: React.ReactNode;
onDrop: (files: FileList) => void;
}) {
const [dragging, setDragging] = useState(false);
return (
<Box
sx={{
flexGrow: 1,
overflowY: "auto",
backgroundColor: (theme) => theme.palette.background.default,
filter: dragging ? "brightness(0.9)" : "none",
transition: "filter 0.2s",
}}
onDragEnter={(e) => {
e.preventDefault();
setDragging(true);
}}
onDragOver={(e) => {
e.preventDefault();
e.dataTransfer.dropEffect = "copy";
}}
onDragLeave={() => setDragging(false)}
onDrop={(e) => {
e.preventDefault();
onDrop(e.dataTransfer.files);
setDragging(false);
}}
>
{children}
</Box>
);
}
// Main Component
function Main({
search,
onError,
}: {
search: string;
onError: (error: Error) => void;
}) {
const [cwd, setCwd] = useState("");
const [files, setFiles] = useState<FileItem[]>([]);
const [loading, setLoading] = useState(true);
const [multiSelected, setMultiSelected] = useState<string[] | null>(null);
const [showUploadDrawer, setShowUploadDrawer] = useState(false);
const [showTextPadDrawer, setShowTextPadDrawer] = useState(false);
const [lastUploadKey, setLastUploadKey] = useState<string | null>(null);
const transferQueue = useTransferQueue();
const uploadEnqueue = useUploadEnqueue();
const fetchFiles = useCallback(() => {
fetchPath(cwd)
.then((files) => {
setFiles(files);
setMultiSelected(null);
})
.catch(onError)
.finally(() => setLoading(false));
}, [cwd, onError]);
useEffect(() => setLoading(true), [cwd]);
useEffect(() => {
fetchFiles();
}, [fetchFiles]);
useEffect(() => {
if (!transferQueue.length) return;
const lastFile = transferQueue[transferQueue.length - 1];
if (["pending", "in-progress"].includes(lastFile.status)) {
setLastUploadKey(lastFile.remoteKey);
} else if (lastUploadKey) {
fetchFiles();
setLastUploadKey(null);
}
}, [cwd, fetchFiles, lastUploadKey, transferQueue]);
const filteredFiles = useMemo(
() =>
(search
? files.filter((file) =>
file.key.toLowerCase().includes(search.toLowerCase())
)
: files
).sort((a, b) => (isDirectory(a) ? -1 : isDirectory(b) ? 1 : 0)),
[files, search]
);
const handleMultiSelect = useCallback((key: string) => {
setMultiSelected((prev) => {
if (prev === null) return [key];
if (prev.includes(key)) {
const updated = prev.filter((k) => k !== key);
return updated.length ? updated : null;
}
return [...prev, key];
});
}, []);
return (
<>
{cwd && <PathBreadcrumb path={cwd} onCwdChange={setCwd} />}
{loading ? (
<Centered>
<CircularProgress />
</Centered>
) : (
<DropZone
onDrop={(files) => {
uploadEnqueue(
...Array.from(files).map((file) => ({ file, basedir: cwd }))
);
}}
>
<FileGrid
files={filteredFiles}
onCwdChange={(newCwd: string) => setCwd(newCwd)}
multiSelected={multiSelected}
onMultiSelect={handleMultiSelect}
emptyMessage={<Centered>No files or folders</Centered>}
/>
</DropZone>
)}
{multiSelected === null && (
<>
<UploadFab onClick={() => setShowUploadDrawer(true)} />
<Button
variant="contained"
startIcon={<NoteAddIcon />}
sx={{
position: "fixed",
bottom: 90,
right: 24,
zIndex: 999,
}}
onClick={() => setShowTextPadDrawer(true)}
>
Open TextPad
</Button>
</>
)}
<UploadDrawer
open={showUploadDrawer}
setOpen={setShowUploadDrawer}
cwd={cwd}
onUpload={fetchFiles}
/>
<TextPadDrawer
open={showTextPadDrawer}
setOpen={setShowTextPadDrawer}
cwd={cwd}
onUpload={fetchFiles}
/>
<MultiSelectToolbar
multiSelected={multiSelected}
onClose={() => setMultiSelected(null)}
onDownload={() => {
if (multiSelected?.length !== 1) return;
const a = document.createElement("a");
a.href = `/webdav/${encodeKey(multiSelected[0])}`;
a.download = multiSelected[0].split("/").pop()!;
a.click();
}}
onRename={async () => {
if (multiSelected?.length !== 1) return;
const newName = window.prompt("Rename to:");
if (!newName) return;
await copyPaste(multiSelected[0], cwd + newName, true);
fetchFiles();
}}
onDelete={async () => {
if (!multiSelected?.length) return;
const filenames = multiSelected
.map((key) => key.replace(/\/$/, "").split("/").pop())
.join("\n");
const confirmMessage = "Delete the following file(s) permanently?";
if (!window.confirm(`${confirmMessage}\n${filenames}`)) return;
for (const key of multiSelected)
await fetch(`/webdav/${encodeKey(key)}`, { method: "DELETE" });
fetchFiles();
}}
onShare={() => {
if (multiSelected?.length !== 1) return;
const url = new URL(
`/webdav/${encodeKey(multiSelected[0])}`,
window.location.href
);
navigator.share({ url: url.toString() });
}}
/>
</>
);
}
export default Main;