@vryzel/file-next-headless 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -0
- package/dist/index.cjs +559 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +271 -0
- package/dist/index.d.ts +271 -0
- package/dist/index.js +552 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 file-next contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @vryzel/file-next-headless
|
|
2
|
+
|
|
3
|
+
Headless React hooks for `@vryzel/file-next`. No UI, no Tailwind, no Radix. Server work is injected as callbacks.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @vryzel/file-next-headless
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
| Hook | Role |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `useFileBrowser` | List + loading/empty/error |
|
|
12
|
+
| `useFileExplorer` | View mode, selection, drag on top of the list |
|
|
13
|
+
| `useUploader` | XHR upload with progress and cancel |
|
|
14
|
+
| `useFileActions` | Optimistic delete/move/copy/rename |
|
|
15
|
+
| `useFileUrl` | Presigned download URL |
|
|
16
|
+
| `useDownloadProgress` | Fetch download with progress |
|
|
17
|
+
|
|
18
|
+
Public on npmjs. See the repository README.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var errors = require('@vryzel/file-next/errors');
|
|
5
|
+
|
|
6
|
+
// src/use-file-browser.ts
|
|
7
|
+
var initial = {
|
|
8
|
+
status: "idle",
|
|
9
|
+
files: [],
|
|
10
|
+
error: null
|
|
11
|
+
};
|
|
12
|
+
var reducer = (state, action) => {
|
|
13
|
+
switch (action.type) {
|
|
14
|
+
case "FETCH_START":
|
|
15
|
+
return { status: "loading", files: [], error: null };
|
|
16
|
+
case "FETCH_SUCCESS":
|
|
17
|
+
return { status: "success", files: action.files, error: null };
|
|
18
|
+
case "FETCH_ERROR":
|
|
19
|
+
return { status: "error", files: [], error: action.error };
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function useFileBrowser(options) {
|
|
23
|
+
const { listFiles, tenantId, parentId, prefix, limit, autoFetch = false } = options;
|
|
24
|
+
const [state, dispatch] = react.useReducer(reducer, initial);
|
|
25
|
+
const run = react.useCallback(async () => {
|
|
26
|
+
dispatch({ type: "FETCH_START" });
|
|
27
|
+
const result = await listFiles({ tenantId, parentId, prefix, limit });
|
|
28
|
+
if (result.ok) {
|
|
29
|
+
dispatch({ type: "FETCH_SUCCESS", files: result.value.items });
|
|
30
|
+
} else {
|
|
31
|
+
dispatch({ type: "FETCH_ERROR", error: result.error });
|
|
32
|
+
}
|
|
33
|
+
}, [listFiles, tenantId, parentId, prefix, limit]);
|
|
34
|
+
react.useEffect(() => {
|
|
35
|
+
if (autoFetch) {
|
|
36
|
+
void run();
|
|
37
|
+
}
|
|
38
|
+
}, [autoFetch, run]);
|
|
39
|
+
return {
|
|
40
|
+
status: state.status,
|
|
41
|
+
files: state.files,
|
|
42
|
+
error: state.error,
|
|
43
|
+
refetch: run
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
var initial2 = {
|
|
47
|
+
status: "idle",
|
|
48
|
+
progress: 0,
|
|
49
|
+
error: null
|
|
50
|
+
};
|
|
51
|
+
var reducer2 = (state, action) => {
|
|
52
|
+
switch (action.type) {
|
|
53
|
+
case "UPLOAD_START":
|
|
54
|
+
return { status: "uploading", progress: 0, error: null };
|
|
55
|
+
case "PROGRESS": {
|
|
56
|
+
if (action.total <= 0) return state;
|
|
57
|
+
const pct = Math.min(100, Math.round(action.loaded / action.total * 100));
|
|
58
|
+
return { ...state, progress: pct };
|
|
59
|
+
}
|
|
60
|
+
case "UPLOAD_SUCCESS":
|
|
61
|
+
return { status: "success", progress: 100, error: null };
|
|
62
|
+
case "UPLOAD_ERROR":
|
|
63
|
+
return { status: "error", progress: state.progress, error: action.error };
|
|
64
|
+
case "UPLOAD_ABORTED":
|
|
65
|
+
return { status: "aborted", progress: state.progress, error: null };
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function useUploader(options) {
|
|
69
|
+
const { uploadUrl, requestUpload, confirmUpload } = options;
|
|
70
|
+
const [state, dispatch] = react.useReducer(reducer2, initial2);
|
|
71
|
+
const xhrRef = react.useRef(null);
|
|
72
|
+
const canceledRef = react.useRef(false);
|
|
73
|
+
const startXhr = react.useCallback(
|
|
74
|
+
(file, target) => {
|
|
75
|
+
if (xhrRef.current !== null) return;
|
|
76
|
+
const xhr = new XMLHttpRequest();
|
|
77
|
+
xhrRef.current = xhr;
|
|
78
|
+
canceledRef.current = false;
|
|
79
|
+
xhr.upload.addEventListener("progress", (event) => {
|
|
80
|
+
if (event.lengthComputable) {
|
|
81
|
+
dispatch({ type: "PROGRESS", loaded: event.loaded, total: event.total });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
xhr.addEventListener("load", () => {
|
|
85
|
+
if (xhrRef.current !== xhr || canceledRef.current) return;
|
|
86
|
+
xhrRef.current = null;
|
|
87
|
+
if (xhr.status < 200 || xhr.status >= 300) {
|
|
88
|
+
let message = `Upload failed (${xhr.status})`;
|
|
89
|
+
try {
|
|
90
|
+
const parsed = JSON.parse(xhr.responseText);
|
|
91
|
+
if (parsed.error?.message) message = parsed.error.message;
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
dispatch({
|
|
95
|
+
type: "UPLOAD_ERROR",
|
|
96
|
+
error: new errors.FileSystemError({
|
|
97
|
+
code: "NetworkError",
|
|
98
|
+
retryable: true,
|
|
99
|
+
message,
|
|
100
|
+
cause: { code: "HttpError", message: `status ${xhr.status}` }
|
|
101
|
+
})
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
dispatch({ type: "UPLOAD_SUCCESS" });
|
|
106
|
+
void Promise.resolve(confirmUpload?.(xhr, file, { id: target.id })).catch(
|
|
107
|
+
(error) => {
|
|
108
|
+
dispatch({
|
|
109
|
+
type: "UPLOAD_ERROR",
|
|
110
|
+
error: new errors.FileSystemError({
|
|
111
|
+
code: "InternalError",
|
|
112
|
+
retryable: false,
|
|
113
|
+
message: error instanceof Error ? error.message : "confirmUpload failed"
|
|
114
|
+
})
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
xhr.addEventListener("error", () => {
|
|
120
|
+
if (xhrRef.current !== xhr || canceledRef.current) return;
|
|
121
|
+
xhrRef.current = null;
|
|
122
|
+
dispatch({
|
|
123
|
+
type: "UPLOAD_ERROR",
|
|
124
|
+
error: new errors.FileSystemError({
|
|
125
|
+
code: "NetworkError",
|
|
126
|
+
retryable: true,
|
|
127
|
+
message: "XHR upload failed (usually CORS). Same-origin /api/upload avoids this.",
|
|
128
|
+
cause: { code: "XhrError", message: "XHR error event" }
|
|
129
|
+
})
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
dispatch({ type: "UPLOAD_START" });
|
|
133
|
+
xhr.open(target.method ?? "PUT", target.url);
|
|
134
|
+
xhr.setRequestHeader("Content-Type", file.type || "application/octet-stream");
|
|
135
|
+
for (const [header, value] of Object.entries(target.headers ?? {})) {
|
|
136
|
+
xhr.setRequestHeader(header, value);
|
|
137
|
+
}
|
|
138
|
+
xhr.send(file.content);
|
|
139
|
+
},
|
|
140
|
+
[confirmUpload]
|
|
141
|
+
);
|
|
142
|
+
const upload = react.useCallback(
|
|
143
|
+
(file) => {
|
|
144
|
+
if (xhrRef.current !== null) return;
|
|
145
|
+
if (requestUpload) {
|
|
146
|
+
dispatch({ type: "UPLOAD_START" });
|
|
147
|
+
void requestUpload(file).then((target) => {
|
|
148
|
+
if (canceledRef.current) return;
|
|
149
|
+
xhrRef.current = null;
|
|
150
|
+
startXhr(file, target);
|
|
151
|
+
}).catch((error) => {
|
|
152
|
+
dispatch({
|
|
153
|
+
type: "UPLOAD_ERROR",
|
|
154
|
+
error: new errors.FileSystemError({
|
|
155
|
+
code: "NetworkError",
|
|
156
|
+
retryable: true,
|
|
157
|
+
message: error instanceof Error ? error.message : "requestUpload failed"
|
|
158
|
+
})
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (!uploadUrl) return;
|
|
164
|
+
startXhr(file, { url: uploadUrl, method: "PUT" });
|
|
165
|
+
},
|
|
166
|
+
[requestUpload, startXhr, uploadUrl]
|
|
167
|
+
);
|
|
168
|
+
const cancel = react.useCallback(() => {
|
|
169
|
+
const xhr = xhrRef.current;
|
|
170
|
+
if (xhr === null) return;
|
|
171
|
+
xhr.abort();
|
|
172
|
+
canceledRef.current = true;
|
|
173
|
+
xhrRef.current = null;
|
|
174
|
+
dispatch({ type: "UPLOAD_ABORTED" });
|
|
175
|
+
}, []);
|
|
176
|
+
return {
|
|
177
|
+
status: state.status,
|
|
178
|
+
progress: state.progress,
|
|
179
|
+
error: state.error,
|
|
180
|
+
upload,
|
|
181
|
+
cancel
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
var initial3 = {
|
|
185
|
+
status: "idle",
|
|
186
|
+
error: null
|
|
187
|
+
};
|
|
188
|
+
var reducer3 = (state, action) => {
|
|
189
|
+
switch (action.type) {
|
|
190
|
+
case "START":
|
|
191
|
+
return { status: "pending", error: null };
|
|
192
|
+
case "SUCCESS":
|
|
193
|
+
return { status: "idle", error: null };
|
|
194
|
+
case "ERROR":
|
|
195
|
+
return { status: "idle", error: action.error };
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function applyDelete(files, id) {
|
|
199
|
+
return files.filter((f) => f.id !== id);
|
|
200
|
+
}
|
|
201
|
+
function applyMove(files, id, newParentId) {
|
|
202
|
+
return files.map((f) => f.id === id ? { ...f, parentId: newParentId } : f);
|
|
203
|
+
}
|
|
204
|
+
function applyCopy(files, node) {
|
|
205
|
+
return [...files, node];
|
|
206
|
+
}
|
|
207
|
+
function applyRename(files, id, newName) {
|
|
208
|
+
return files.map((file) => file.id === id ? { ...file, name: newName } : file);
|
|
209
|
+
}
|
|
210
|
+
function useFileActions(options) {
|
|
211
|
+
const { files, setFiles, actions } = options;
|
|
212
|
+
const [state, dispatch] = react.useReducer(reducer3, initial3);
|
|
213
|
+
const run = react.useCallback(
|
|
214
|
+
async (args) => {
|
|
215
|
+
const snapshot = files;
|
|
216
|
+
const optimisticNext = args.optimistic();
|
|
217
|
+
dispatch({ type: "START" });
|
|
218
|
+
if (optimisticNext !== snapshot) {
|
|
219
|
+
setFiles(optimisticNext);
|
|
220
|
+
}
|
|
221
|
+
const result = await args.act(args.input);
|
|
222
|
+
if (result.ok) {
|
|
223
|
+
if (args.onSuccess) {
|
|
224
|
+
setFiles(args.onSuccess(result.value));
|
|
225
|
+
}
|
|
226
|
+
dispatch({ type: "SUCCESS" });
|
|
227
|
+
} else {
|
|
228
|
+
setFiles(snapshot);
|
|
229
|
+
dispatch({ type: "ERROR", error: result.error });
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
[files, setFiles]
|
|
233
|
+
);
|
|
234
|
+
const deleteFile = react.useCallback(
|
|
235
|
+
(id) => run({
|
|
236
|
+
input: { id },
|
|
237
|
+
optimistic: () => applyDelete(files, id),
|
|
238
|
+
act: actions.deleteFile
|
|
239
|
+
}),
|
|
240
|
+
[run, actions.deleteFile, files]
|
|
241
|
+
);
|
|
242
|
+
const moveFile = react.useCallback(
|
|
243
|
+
(id, newParentId) => run({
|
|
244
|
+
input: { id, newParentId },
|
|
245
|
+
optimistic: () => applyMove(files, id, newParentId),
|
|
246
|
+
act: actions.moveFile
|
|
247
|
+
}),
|
|
248
|
+
[run, actions.moveFile, files]
|
|
249
|
+
);
|
|
250
|
+
const copyFile = react.useCallback(
|
|
251
|
+
(id, newParentId) => run({
|
|
252
|
+
input: { id, newParentId },
|
|
253
|
+
optimistic: () => files,
|
|
254
|
+
// copy doesn't remove anything
|
|
255
|
+
act: actions.copyFile,
|
|
256
|
+
onSuccess: (output) => applyCopy(files, output.node)
|
|
257
|
+
}),
|
|
258
|
+
[run, actions.copyFile, files]
|
|
259
|
+
);
|
|
260
|
+
const renameFile = react.useCallback(
|
|
261
|
+
async (id, newName) => {
|
|
262
|
+
const snapshot = files;
|
|
263
|
+
dispatch({ type: "START" });
|
|
264
|
+
setFiles(applyRename(files, id, newName));
|
|
265
|
+
try {
|
|
266
|
+
await actions.renameFile(id, newName);
|
|
267
|
+
dispatch({ type: "SUCCESS" });
|
|
268
|
+
} catch (error) {
|
|
269
|
+
setFiles(snapshot);
|
|
270
|
+
dispatch({ type: "ERROR", error });
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
[actions.renameFile, files, setFiles]
|
|
274
|
+
);
|
|
275
|
+
return {
|
|
276
|
+
status: state.status,
|
|
277
|
+
isPending: state.status === "pending",
|
|
278
|
+
error: state.error,
|
|
279
|
+
deleteFile,
|
|
280
|
+
moveFile,
|
|
281
|
+
copyFile,
|
|
282
|
+
renameFile
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function useFileUrl(options) {
|
|
286
|
+
const { getDownloadUrl, key, enabled = true } = options;
|
|
287
|
+
const [url, setUrl] = react.useState(null);
|
|
288
|
+
const [error, setError] = react.useState(null);
|
|
289
|
+
const [status, setStatus] = react.useState(enabled ? "loading" : "idle");
|
|
290
|
+
const latestKeyRef = react.useRef(null);
|
|
291
|
+
const latestEnabledRef = react.useRef(null);
|
|
292
|
+
react.useEffect(() => {
|
|
293
|
+
if (!enabled) {
|
|
294
|
+
latestEnabledRef.current = false;
|
|
295
|
+
setStatus("idle");
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
latestEnabledRef.current = true;
|
|
299
|
+
latestKeyRef.current = key;
|
|
300
|
+
setStatus("loading");
|
|
301
|
+
setError(null);
|
|
302
|
+
let cancelled = false;
|
|
303
|
+
void (async () => {
|
|
304
|
+
const result = await getDownloadUrl({ key });
|
|
305
|
+
if (cancelled) return;
|
|
306
|
+
if (latestKeyRef.current !== key) return;
|
|
307
|
+
if (latestEnabledRef.current === false) return;
|
|
308
|
+
if (result.ok) {
|
|
309
|
+
setUrl(result.value.url);
|
|
310
|
+
setStatus("success");
|
|
311
|
+
} else {
|
|
312
|
+
setError(result.error);
|
|
313
|
+
setStatus("error");
|
|
314
|
+
}
|
|
315
|
+
})();
|
|
316
|
+
return () => {
|
|
317
|
+
cancelled = true;
|
|
318
|
+
};
|
|
319
|
+
}, [getDownloadUrl, key, enabled]);
|
|
320
|
+
return { url, status, error };
|
|
321
|
+
}
|
|
322
|
+
var initial4 = {
|
|
323
|
+
status: "idle",
|
|
324
|
+
progress: 0,
|
|
325
|
+
blob: null,
|
|
326
|
+
error: null
|
|
327
|
+
};
|
|
328
|
+
var reducer4 = (state, action) => {
|
|
329
|
+
switch (action.type) {
|
|
330
|
+
case "START":
|
|
331
|
+
return { status: "loading", progress: 0, blob: null, error: null };
|
|
332
|
+
case "PROGRESS": {
|
|
333
|
+
if (action.total <= 0) return state;
|
|
334
|
+
const pct = Math.min(100, Math.round(action.loaded / action.total * 100));
|
|
335
|
+
return { ...state, progress: pct };
|
|
336
|
+
}
|
|
337
|
+
case "SUCCESS":
|
|
338
|
+
return { status: "success", progress: 100, blob: action.blob, error: null };
|
|
339
|
+
case "ERROR":
|
|
340
|
+
return { ...state, status: "error", error: action.error };
|
|
341
|
+
case "ABORTED":
|
|
342
|
+
return { ...state, status: "aborted" };
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
function useDownloadProgress(options) {
|
|
346
|
+
const { url, externalSignal } = options;
|
|
347
|
+
const [state, dispatch] = react.useReducer(reducer4, initial4);
|
|
348
|
+
const acRef = react.useRef(null);
|
|
349
|
+
const fetchIdRef = react.useRef(0);
|
|
350
|
+
react.useEffect(() => {
|
|
351
|
+
const fetchId = ++fetchIdRef.current;
|
|
352
|
+
const ac = new AbortController();
|
|
353
|
+
acRef.current = ac;
|
|
354
|
+
let onExternalAbort = null;
|
|
355
|
+
if (externalSignal) {
|
|
356
|
+
onExternalAbort = () => ac.abort();
|
|
357
|
+
if (externalSignal.aborted) {
|
|
358
|
+
ac.abort();
|
|
359
|
+
} else {
|
|
360
|
+
externalSignal.addEventListener("abort", onExternalAbort);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
dispatch({ type: "START" });
|
|
364
|
+
void (async () => {
|
|
365
|
+
try {
|
|
366
|
+
const res = await fetch(url, { signal: ac.signal });
|
|
367
|
+
if (!res.ok) {
|
|
368
|
+
throw new Error(`HTTP ${res.status}`);
|
|
369
|
+
}
|
|
370
|
+
const body = res.body;
|
|
371
|
+
if (!body) {
|
|
372
|
+
if (fetchId !== fetchIdRef.current) return;
|
|
373
|
+
dispatch({ type: "SUCCESS", blob: new Blob([]) });
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const reader = body.getReader();
|
|
377
|
+
const chunks = [];
|
|
378
|
+
let loaded = 0;
|
|
379
|
+
const totalRaw = res.headers?.get?.("content-length");
|
|
380
|
+
const total = totalRaw ? Number(totalRaw) : 0;
|
|
381
|
+
while (true) {
|
|
382
|
+
const { done, value } = await reader.read();
|
|
383
|
+
if (done) break;
|
|
384
|
+
if (!value) continue;
|
|
385
|
+
chunks.push(value);
|
|
386
|
+
loaded += value.byteLength;
|
|
387
|
+
if (fetchId !== fetchIdRef.current) return;
|
|
388
|
+
if (total > 0) {
|
|
389
|
+
dispatch({ type: "PROGRESS", loaded, total });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (fetchId !== fetchIdRef.current) return;
|
|
393
|
+
const blob = new Blob(chunks);
|
|
394
|
+
dispatch({ type: "SUCCESS", blob });
|
|
395
|
+
} catch (err) {
|
|
396
|
+
if (fetchId !== fetchIdRef.current) return;
|
|
397
|
+
if (ac.signal.aborted || err instanceof Error && err.name === "AbortError") {
|
|
398
|
+
dispatch({ type: "ABORTED" });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
dispatch({
|
|
402
|
+
type: "ERROR",
|
|
403
|
+
error: new errors.FileSystemError({
|
|
404
|
+
code: "NetworkError",
|
|
405
|
+
retryable: true,
|
|
406
|
+
message: err instanceof Error ? err.message : "fetch failed"
|
|
407
|
+
})
|
|
408
|
+
});
|
|
409
|
+
} finally {
|
|
410
|
+
if (onExternalAbort && externalSignal) {
|
|
411
|
+
externalSignal.removeEventListener("abort", onExternalAbort);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
})();
|
|
415
|
+
return () => {
|
|
416
|
+
ac.abort();
|
|
417
|
+
};
|
|
418
|
+
}, [url, externalSignal]);
|
|
419
|
+
const cancel = react.useCallback(() => {
|
|
420
|
+
acRef.current?.abort();
|
|
421
|
+
dispatch({ type: "ABORTED" });
|
|
422
|
+
}, []);
|
|
423
|
+
return {
|
|
424
|
+
status: state.status,
|
|
425
|
+
progress: state.progress,
|
|
426
|
+
blob: state.blob,
|
|
427
|
+
error: state.error,
|
|
428
|
+
cancel
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
var reducer5 = (state, action) => {
|
|
432
|
+
switch (action.type) {
|
|
433
|
+
case "SET_VIEW":
|
|
434
|
+
return { ...state, view: action.view };
|
|
435
|
+
case "SELECT":
|
|
436
|
+
return { ...state, selectedId: action.id };
|
|
437
|
+
case "OPEN_CONTEXT":
|
|
438
|
+
return { ...state, contextTarget: action.file };
|
|
439
|
+
case "BEGIN_DRAG":
|
|
440
|
+
return { ...state, draggingId: action.id, dropTargetId: null };
|
|
441
|
+
case "SET_DROP_TARGET":
|
|
442
|
+
return { ...state, dropTargetId: action.id };
|
|
443
|
+
case "END_DRAG":
|
|
444
|
+
return { ...state, draggingId: null, dropTargetId: null };
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
var initialState = (initialView) => ({
|
|
448
|
+
view: initialView,
|
|
449
|
+
selectedId: null,
|
|
450
|
+
contextTarget: null,
|
|
451
|
+
draggingId: null,
|
|
452
|
+
dropTargetId: null
|
|
453
|
+
});
|
|
454
|
+
function useFileExplorer(options) {
|
|
455
|
+
const {
|
|
456
|
+
listFiles,
|
|
457
|
+
tenantId,
|
|
458
|
+
parentId,
|
|
459
|
+
initialView = "list",
|
|
460
|
+
autoFetch = true,
|
|
461
|
+
onActivate,
|
|
462
|
+
onMove
|
|
463
|
+
} = options;
|
|
464
|
+
const [state, dispatch] = react.useReducer(reducer5, initialView, initialState);
|
|
465
|
+
const lastParentId = react.useRef(parentId);
|
|
466
|
+
react.useEffect(() => {
|
|
467
|
+
if (lastParentId.current !== parentId) {
|
|
468
|
+
lastParentId.current = parentId;
|
|
469
|
+
dispatch({ type: "SELECT", id: null });
|
|
470
|
+
dispatch({ type: "OPEN_CONTEXT", file: null });
|
|
471
|
+
dispatch({ type: "END_DRAG" });
|
|
472
|
+
}
|
|
473
|
+
}, [parentId]);
|
|
474
|
+
const { status, files, error, refetch } = useFileBrowser({
|
|
475
|
+
listFiles,
|
|
476
|
+
tenantId,
|
|
477
|
+
parentId,
|
|
478
|
+
autoFetch
|
|
479
|
+
});
|
|
480
|
+
const setView = react.useCallback((view) => {
|
|
481
|
+
dispatch({ type: "SET_VIEW", view });
|
|
482
|
+
}, []);
|
|
483
|
+
const setSelected = react.useCallback((id) => {
|
|
484
|
+
dispatch({ type: "SELECT", id });
|
|
485
|
+
if (id && onActivate) {
|
|
486
|
+
const file = files.find((f) => f.id === id);
|
|
487
|
+
if (file) onActivate(file);
|
|
488
|
+
}
|
|
489
|
+
}, [files, onActivate]);
|
|
490
|
+
const openContextMenu = react.useCallback((file) => {
|
|
491
|
+
dispatch({ type: "OPEN_CONTEXT", file });
|
|
492
|
+
}, []);
|
|
493
|
+
const beginDrag = react.useCallback((id) => {
|
|
494
|
+
dispatch({ type: "BEGIN_DRAG", id });
|
|
495
|
+
}, []);
|
|
496
|
+
const setDropTarget = react.useCallback((id) => {
|
|
497
|
+
dispatch({ type: "SET_DROP_TARGET", id });
|
|
498
|
+
}, []);
|
|
499
|
+
const commitDrop = react.useCallback((destinationFolderId) => {
|
|
500
|
+
const destination = files.find((file) => file.id === destinationFolderId);
|
|
501
|
+
if (state.draggingId && destination?.kind === "folder" && state.draggingId !== destinationFolderId) {
|
|
502
|
+
void onMove?.({
|
|
503
|
+
itemIds: [state.draggingId],
|
|
504
|
+
destinationFolderId
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
dispatch({ type: "END_DRAG" });
|
|
508
|
+
}, [files, onMove, state.draggingId]);
|
|
509
|
+
const endDrag = react.useCallback(() => {
|
|
510
|
+
dispatch({ type: "END_DRAG" });
|
|
511
|
+
}, []);
|
|
512
|
+
return react.useMemo(
|
|
513
|
+
() => ({
|
|
514
|
+
status,
|
|
515
|
+
files,
|
|
516
|
+
error,
|
|
517
|
+
refetch,
|
|
518
|
+
view: state.view,
|
|
519
|
+
setView,
|
|
520
|
+
selectedId: state.selectedId,
|
|
521
|
+
setSelected,
|
|
522
|
+
contextTarget: state.contextTarget,
|
|
523
|
+
openContextMenu,
|
|
524
|
+
draggingId: state.draggingId,
|
|
525
|
+
dropTargetId: state.dropTargetId,
|
|
526
|
+
beginDrag,
|
|
527
|
+
setDropTarget,
|
|
528
|
+
commitDrop,
|
|
529
|
+
endDrag
|
|
530
|
+
}),
|
|
531
|
+
[
|
|
532
|
+
status,
|
|
533
|
+
files,
|
|
534
|
+
error,
|
|
535
|
+
refetch,
|
|
536
|
+
state.view,
|
|
537
|
+
state.selectedId,
|
|
538
|
+
state.contextTarget,
|
|
539
|
+
state.draggingId,
|
|
540
|
+
state.dropTargetId,
|
|
541
|
+
setView,
|
|
542
|
+
setSelected,
|
|
543
|
+
openContextMenu,
|
|
544
|
+
beginDrag,
|
|
545
|
+
setDropTarget,
|
|
546
|
+
commitDrop,
|
|
547
|
+
endDrag
|
|
548
|
+
]
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
exports.useDownloadProgress = useDownloadProgress;
|
|
553
|
+
exports.useFileActions = useFileActions;
|
|
554
|
+
exports.useFileBrowser = useFileBrowser;
|
|
555
|
+
exports.useFileExplorer = useFileExplorer;
|
|
556
|
+
exports.useFileUrl = useFileUrl;
|
|
557
|
+
exports.useUploader = useUploader;
|
|
558
|
+
//# sourceMappingURL=index.cjs.map
|
|
559
|
+
//# sourceMappingURL=index.cjs.map
|