@dimah-s3/react 0.8.6 → 1.0.1
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/dist/helpers/format-dimah-error.d.ts.map +1 -1
- package/dist/helpers/index.d.ts +0 -2
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/throttled-speed.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +2 -3
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/use-delete.d.ts.map +1 -1
- package/dist/hooks/use-download.d.ts +0 -2
- package/dist/hooks/use-download.d.ts.map +1 -1
- package/dist/hooks/use-file-intake.d.ts +3 -2
- package/dist/hooks/use-file-intake.d.ts.map +1 -1
- package/dist/hooks/use-file-upload.d.ts +14 -12
- package/dist/hooks/use-file-upload.d.ts.map +1 -1
- package/dist/hooks/use-multi-file-upload.d.ts +6 -2
- package/dist/hooks/use-multi-file-upload.d.ts.map +1 -1
- package/dist/hooks/use-multi-upload.d.ts +6 -3
- package/dist/hooks/use-multi-upload.d.ts.map +1 -1
- package/dist/hooks/use-upload.d.ts +8 -6
- package/dist/hooks/use-upload.d.ts.map +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +265 -351
- package/dist/index.js.map +1 -1
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/local-storage-store.d.ts.map +1 -1
- package/dist/store/memory-store.d.ts.map +1 -1
- package/dist/store/use-immer-state.d.ts +8 -0
- package/dist/store/use-immer-state.d.ts.map +1 -0
- package/dist/translations/types.d.ts +5 -0
- package/dist/translations/types.d.ts.map +1 -1
- package/dist/types/multi-upload.d.ts +2 -13
- package/dist/types/multi-upload.d.ts.map +1 -1
- package/dist/types/upload.d.ts +11 -5
- package/dist/types/upload.d.ts.map +1 -1
- package/dist/upload/multipart.d.ts.map +1 -1
- package/dist/upload/presigned-http.d.ts.map +1 -1
- package/dist/upload/retry.d.ts.map +1 -1
- package/dist/upload/upload-file.d.ts.map +1 -1
- package/dist/upload/upload-files.d.ts +2 -2
- package/dist/upload/upload-files.d.ts.map +1 -1
- package/dist/upload/upload-part.d.ts.map +1 -1
- package/dist/upload/xhr-upload.d.ts +11 -0
- package/dist/upload/xhr-upload.d.ts.map +1 -0
- package/package.json +5 -5
- package/dist/helpers/to-dropzone-accept.d.ts +0 -10
- package/dist/helpers/to-dropzone-accept.d.ts.map +0 -1
- package/dist/store/create-hook-store.d.ts +0 -48
- package/dist/store/create-hook-store.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,49 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createS3Client as createS3Client$1, formatFileSize, isDimahS3Error, S3_ERROR_CODES, validateFile, DimahS3Error, parseFileName, defaultObjectKey } from '@dimah-s3/core';
|
|
2
2
|
export { DimahS3Error, S3_ERROR_CODES, buildObjectKey, defaultObjectKey, formatFileSize, isAPIError, isDimahS3Error, isS3ErrorCode, sanitizeFileName, validateFile } from '@dimah-s3/core';
|
|
3
3
|
import { createContext, useContext, useCallback, useRef, useEffect, useState, useLayoutEffect } from 'react';
|
|
4
4
|
import { TranslationProvider, useTranslations } from '@fuma-translate/react';
|
|
5
5
|
import { jsx } from 'react/jsx-runtime';
|
|
6
6
|
import { produce } from 'immer';
|
|
7
|
-
import {
|
|
8
|
-
import { useShallow } from 'zustand/react/shallow';
|
|
9
|
-
import { createStore } from 'zustand/vanilla';
|
|
10
|
-
import { useDropzone } from 'react-dropzone';
|
|
11
|
-
|
|
12
|
-
var S3UploadError = class extends DimahS3Error {
|
|
13
|
-
phase;
|
|
14
|
-
constructor(message, code, status = 400, phase) {
|
|
15
|
-
super(status, { message, code });
|
|
16
|
-
this.name = "S3UploadError";
|
|
17
|
-
this.phase = phase;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
function toUploadError(err, phase) {
|
|
21
|
-
if (err instanceof S3UploadError) {
|
|
22
|
-
if (phase != null && err.phase == null) {
|
|
23
|
-
return new S3UploadError(
|
|
24
|
-
err.message,
|
|
25
|
-
err.code ?? "UPLOAD_ERROR",
|
|
26
|
-
err.statusCode,
|
|
27
|
-
phase
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
return err;
|
|
31
|
-
}
|
|
32
|
-
if (err?.name === "AbortError") {
|
|
33
|
-
throw err;
|
|
34
|
-
}
|
|
35
|
-
if (isDimahS3Error(err)) return err;
|
|
36
|
-
const message = err instanceof Error ? err.message : "Upload failed";
|
|
37
|
-
return new S3UploadError(message, "UPLOAD_ERROR", 500, phase);
|
|
38
|
-
}
|
|
39
|
-
function toHookError(err, fallback = "Request failed") {
|
|
40
|
-
if (isDimahS3Error(err)) return err;
|
|
41
|
-
const message = err instanceof Error ? err.message : fallback;
|
|
42
|
-
return new DimahS3Error("BAD_REQUEST", { message });
|
|
43
|
-
}
|
|
44
|
-
function hookBlockedError(message) {
|
|
45
|
-
return new DimahS3Error("BAD_REQUEST", { message });
|
|
46
|
-
}
|
|
7
|
+
import { ErrorCode, useDropzone } from 'react-dropzone';
|
|
47
8
|
|
|
48
9
|
// src/api.ts
|
|
49
10
|
function defineApi(input) {
|
|
@@ -126,6 +87,41 @@ function createMemoryStore() {
|
|
|
126
87
|
}
|
|
127
88
|
};
|
|
128
89
|
}
|
|
90
|
+
var S3UploadError = class extends DimahS3Error {
|
|
91
|
+
phase;
|
|
92
|
+
constructor(message, code, status = 400, phase) {
|
|
93
|
+
super(status, { message, code });
|
|
94
|
+
this.name = "S3UploadError";
|
|
95
|
+
this.phase = phase;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
function toUploadError(err, phase) {
|
|
99
|
+
if (err instanceof S3UploadError) {
|
|
100
|
+
if (err.phase == null) {
|
|
101
|
+
return new S3UploadError(
|
|
102
|
+
err.message,
|
|
103
|
+
err.code ?? "UPLOAD_ERROR",
|
|
104
|
+
err.statusCode,
|
|
105
|
+
phase
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return err;
|
|
109
|
+
}
|
|
110
|
+
if (err?.name === "AbortError") {
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
if (isDimahS3Error(err)) return err;
|
|
114
|
+
const message = err instanceof Error ? err.message : "Upload failed";
|
|
115
|
+
return new S3UploadError(message, "UPLOAD_ERROR", 500, phase);
|
|
116
|
+
}
|
|
117
|
+
function toHookError(err, fallback = "Request failed") {
|
|
118
|
+
if (isDimahS3Error(err)) return err;
|
|
119
|
+
const message = err instanceof Error ? err.message : fallback;
|
|
120
|
+
return new DimahS3Error("BAD_REQUEST", { message });
|
|
121
|
+
}
|
|
122
|
+
function hookBlockedError(message) {
|
|
123
|
+
return new DimahS3Error("BAD_REQUEST", { message });
|
|
124
|
+
}
|
|
129
125
|
|
|
130
126
|
// src/upload/constants.ts
|
|
131
127
|
var DEFAULT_MULTIPART_THRESHOLD = 50 * 1024 * 1024;
|
|
@@ -157,17 +153,16 @@ async function withRetry(fn, retryConfig, signal) {
|
|
|
157
153
|
throw lastError;
|
|
158
154
|
}
|
|
159
155
|
|
|
160
|
-
// src/upload/
|
|
156
|
+
// src/upload/xhr-upload.ts
|
|
161
157
|
function sendXhrUpload(options) {
|
|
162
158
|
const {
|
|
163
159
|
method,
|
|
164
160
|
url,
|
|
165
161
|
body,
|
|
166
162
|
headers,
|
|
167
|
-
fileSize,
|
|
168
|
-
onProgress,
|
|
169
163
|
signal,
|
|
170
|
-
errorLabel = "Upload"
|
|
164
|
+
errorLabel = "Upload",
|
|
165
|
+
onProgress
|
|
171
166
|
} = options;
|
|
172
167
|
return new Promise((resolve, reject) => {
|
|
173
168
|
const xhr = new XMLHttpRequest();
|
|
@@ -178,17 +173,12 @@ function sendXhrUpload(options) {
|
|
|
178
173
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
179
174
|
xhr.upload.addEventListener("progress", (e) => {
|
|
180
175
|
if (e.lengthComputable) {
|
|
181
|
-
onProgress?.(
|
|
182
|
-
loaded: e.loaded,
|
|
183
|
-
total: e.total,
|
|
184
|
-
percent: Math.round(e.loaded / e.total * 100)
|
|
185
|
-
});
|
|
176
|
+
onProgress?.(e.loaded, e.total);
|
|
186
177
|
}
|
|
187
178
|
});
|
|
188
179
|
xhr.addEventListener("load", () => {
|
|
189
180
|
signal?.removeEventListener("abort", onAbort);
|
|
190
181
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
191
|
-
onProgress?.({ loaded: fileSize, total: fileSize, percent: 100 });
|
|
192
182
|
resolve();
|
|
193
183
|
} else {
|
|
194
184
|
reject(
|
|
@@ -225,6 +215,17 @@ function sendXhrUpload(options) {
|
|
|
225
215
|
xhr.send(body);
|
|
226
216
|
});
|
|
227
217
|
}
|
|
218
|
+
|
|
219
|
+
// src/upload/presigned-http.ts
|
|
220
|
+
function reportByteProgress(onProgress) {
|
|
221
|
+
return (loaded, total) => {
|
|
222
|
+
onProgress?.({
|
|
223
|
+
loaded,
|
|
224
|
+
total,
|
|
225
|
+
percent: total > 0 ? Math.round(loaded / total * 100) : 0
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
}
|
|
228
229
|
function uploadSimple(file, url, fields, onProgress, signal) {
|
|
229
230
|
const formData = new FormData();
|
|
230
231
|
for (const [k, v] of Object.entries(fields)) {
|
|
@@ -235,9 +236,10 @@ function uploadSimple(file, url, fields, onProgress, signal) {
|
|
|
235
236
|
method: "POST",
|
|
236
237
|
url,
|
|
237
238
|
body: formData,
|
|
238
|
-
|
|
239
|
-
onProgress
|
|
240
|
-
|
|
239
|
+
signal,
|
|
240
|
+
onProgress: reportByteProgress(onProgress)
|
|
241
|
+
}).then(() => {
|
|
242
|
+
onProgress?.({ loaded: file.size, total: file.size, percent: 100 });
|
|
241
243
|
});
|
|
242
244
|
}
|
|
243
245
|
function uploadPut(file, url, headers, onProgress, signal) {
|
|
@@ -246,61 +248,28 @@ function uploadPut(file, url, headers, onProgress, signal) {
|
|
|
246
248
|
url,
|
|
247
249
|
body: file,
|
|
248
250
|
headers,
|
|
249
|
-
|
|
250
|
-
onProgress
|
|
251
|
-
|
|
251
|
+
signal,
|
|
252
|
+
onProgress: reportByteProgress(onProgress)
|
|
253
|
+
}).then(() => {
|
|
254
|
+
onProgress?.({ loaded: file.size, total: file.size, percent: 100 });
|
|
252
255
|
});
|
|
253
256
|
}
|
|
254
257
|
|
|
255
258
|
// src/upload/upload-part.ts
|
|
256
259
|
function uploadPart(blob, presignedUrl, partLoaded, reportProgress, signal) {
|
|
257
|
-
return
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
xhr.addEventListener("load", () => {
|
|
271
|
-
signal?.removeEventListener("abort", onAbort);
|
|
272
|
-
if (xhr.status >= 200 && xhr.status < 300) {
|
|
273
|
-
partLoaded.bytes = blob.size;
|
|
274
|
-
reportProgress();
|
|
275
|
-
resolve();
|
|
276
|
-
} else {
|
|
277
|
-
reject(
|
|
278
|
-
new S3UploadError(
|
|
279
|
-
`Part upload failed: ${xhr.status}`,
|
|
280
|
-
"HTTP_ERROR",
|
|
281
|
-
xhr.status,
|
|
282
|
-
"uploading"
|
|
283
|
-
)
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
xhr.addEventListener("error", () => {
|
|
288
|
-
signal?.removeEventListener("abort", onAbort);
|
|
289
|
-
reject(
|
|
290
|
-
new S3UploadError(
|
|
291
|
-
"Part upload failed: network error",
|
|
292
|
-
"NETWORK_ERROR",
|
|
293
|
-
0,
|
|
294
|
-
"uploading"
|
|
295
|
-
)
|
|
296
|
-
);
|
|
297
|
-
});
|
|
298
|
-
xhr.addEventListener("abort", () => {
|
|
299
|
-
signal?.removeEventListener("abort", onAbort);
|
|
300
|
-
reject(new DOMException("Upload aborted", "AbortError"));
|
|
301
|
-
});
|
|
302
|
-
xhr.open("PUT", presignedUrl);
|
|
303
|
-
xhr.send(blob);
|
|
260
|
+
return sendXhrUpload({
|
|
261
|
+
method: "PUT",
|
|
262
|
+
url: presignedUrl,
|
|
263
|
+
body: blob,
|
|
264
|
+
signal,
|
|
265
|
+
errorLabel: "Part upload",
|
|
266
|
+
onProgress: (loaded) => {
|
|
267
|
+
partLoaded.bytes = loaded;
|
|
268
|
+
reportProgress();
|
|
269
|
+
}
|
|
270
|
+
}).then(() => {
|
|
271
|
+
partLoaded.bytes = blob.size;
|
|
272
|
+
reportProgress();
|
|
304
273
|
});
|
|
305
274
|
}
|
|
306
275
|
|
|
@@ -716,6 +685,15 @@ function useFormatDimahError() {
|
|
|
716
685
|
return t("File size is required", { note: "upload" });
|
|
717
686
|
case S3_ERROR_CODES.FILE_SIZE_REQUIRED_MULTIPART.code:
|
|
718
687
|
return t("File size is required", { note: "multipart" });
|
|
688
|
+
case S3_ERROR_CODES.MULTIPART_PART_MISSING.code:
|
|
689
|
+
return t("Uploaded part {partNumber} was not found", {
|
|
690
|
+
note: "API error",
|
|
691
|
+
variables: {
|
|
692
|
+
partNumber: param(err.params, "partNumber", "?")
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
case S3_ERROR_CODES.PAYLOAD_TOO_LARGE.code:
|
|
696
|
+
return t("File is too large", { note: "API error" });
|
|
719
697
|
case S3_ERROR_CODES.VALIDATION_ERROR.code:
|
|
720
698
|
return t("Invalid request", { note: "API error" });
|
|
721
699
|
default:
|
|
@@ -726,6 +704,28 @@ function useFormatDimahError() {
|
|
|
726
704
|
);
|
|
727
705
|
}
|
|
728
706
|
|
|
707
|
+
// src/helpers/throttled-speed.ts
|
|
708
|
+
function createThrottledSpeedUpdater(tracker, intervalMs = 500) {
|
|
709
|
+
let lastSpeed;
|
|
710
|
+
let lastUpdate = 0;
|
|
711
|
+
return {
|
|
712
|
+
reset() {
|
|
713
|
+
tracker.reset();
|
|
714
|
+
lastSpeed = void 0;
|
|
715
|
+
lastUpdate = 0;
|
|
716
|
+
},
|
|
717
|
+
apply(progress) {
|
|
718
|
+
const rawSpeed = tracker.update(progress.loaded);
|
|
719
|
+
const now = Date.now();
|
|
720
|
+
if (rawSpeed > 0 && now - lastUpdate >= intervalMs) {
|
|
721
|
+
lastSpeed = rawSpeed;
|
|
722
|
+
lastUpdate = now;
|
|
723
|
+
}
|
|
724
|
+
return lastSpeed ? { ...progress, speed: lastSpeed } : progress;
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
|
|
729
729
|
// src/helpers/speed-tracker.ts
|
|
730
730
|
function createSpeedTracker(windowMs = 3e3) {
|
|
731
731
|
const samples = [];
|
|
@@ -754,28 +754,6 @@ function createSpeedTracker(windowMs = 3e3) {
|
|
|
754
754
|
};
|
|
755
755
|
}
|
|
756
756
|
|
|
757
|
-
// src/helpers/throttled-speed.ts
|
|
758
|
-
function createThrottledSpeedUpdater(tracker, intervalMs = 500) {
|
|
759
|
-
let lastSpeed;
|
|
760
|
-
let lastUpdate = 0;
|
|
761
|
-
return {
|
|
762
|
-
reset() {
|
|
763
|
-
tracker.reset();
|
|
764
|
-
lastSpeed = void 0;
|
|
765
|
-
lastUpdate = 0;
|
|
766
|
-
},
|
|
767
|
-
apply(progress) {
|
|
768
|
-
const rawSpeed = tracker.update(progress.loaded);
|
|
769
|
-
const now = Date.now();
|
|
770
|
-
if (rawSpeed > 0 && now - lastUpdate >= intervalMs) {
|
|
771
|
-
lastSpeed = rawSpeed;
|
|
772
|
-
lastUpdate = now;
|
|
773
|
-
}
|
|
774
|
-
return lastSpeed ? { ...progress, speed: lastSpeed } : progress;
|
|
775
|
-
}
|
|
776
|
-
};
|
|
777
|
-
}
|
|
778
|
-
|
|
779
757
|
// src/helpers/file-preview.ts
|
|
780
758
|
function createImagePreviewUrl(file) {
|
|
781
759
|
if (!file.type.startsWith("image/")) return null;
|
|
@@ -792,21 +770,15 @@ function useLiveRef(value) {
|
|
|
792
770
|
});
|
|
793
771
|
return ref;
|
|
794
772
|
}
|
|
795
|
-
function
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
return
|
|
804
|
-
}
|
|
805
|
-
function patchHookState(store, recipe) {
|
|
806
|
-
store.setState(produce(store.getState(), recipe));
|
|
807
|
-
}
|
|
808
|
-
function replaceHookState(store, next) {
|
|
809
|
-
store.setState(structuredClone(next), true);
|
|
773
|
+
function useImmerState(initialState) {
|
|
774
|
+
const [state, setState] = useState(() => structuredClone(initialState));
|
|
775
|
+
const patch = useCallback((recipe) => {
|
|
776
|
+
setState(produce(recipe));
|
|
777
|
+
}, []);
|
|
778
|
+
const replace = useCallback((next) => {
|
|
779
|
+
setState(structuredClone(next));
|
|
780
|
+
}, []);
|
|
781
|
+
return [state, patch, replace];
|
|
810
782
|
}
|
|
811
783
|
|
|
812
784
|
// src/hooks/use-file-upload.ts
|
|
@@ -816,23 +788,24 @@ var INITIAL_STATE = {
|
|
|
816
788
|
progress: INITIAL_PROGRESS,
|
|
817
789
|
error: null,
|
|
818
790
|
result: null,
|
|
819
|
-
|
|
820
|
-
fileSize: null,
|
|
821
|
-
fileType: null,
|
|
822
|
-
previewUrl: null
|
|
791
|
+
fileInfo: null
|
|
823
792
|
};
|
|
793
|
+
var PENDING_PHASES = /* @__PURE__ */ new Set([
|
|
794
|
+
"validating",
|
|
795
|
+
"presigning",
|
|
796
|
+
"uploading",
|
|
797
|
+
"finalizing"
|
|
798
|
+
]);
|
|
799
|
+
function mergeRequestOptions(hook, file, requestOptions) {
|
|
800
|
+
const merged = {
|
|
801
|
+
...hook.uploadOptions,
|
|
802
|
+
...hook.getUploadOptions?.(file),
|
|
803
|
+
...requestOptions
|
|
804
|
+
};
|
|
805
|
+
return Object.keys(merged).length > 0 ? merged : void 0;
|
|
806
|
+
}
|
|
824
807
|
function useFileUpload(options) {
|
|
825
|
-
const
|
|
826
|
-
const state = useHookStoreShallow(store, (s) => ({
|
|
827
|
-
phase: s.phase,
|
|
828
|
-
progress: s.progress,
|
|
829
|
-
error: s.error,
|
|
830
|
-
result: s.result,
|
|
831
|
-
fileName: s.fileName,
|
|
832
|
-
fileSize: s.fileSize,
|
|
833
|
-
fileType: s.fileType,
|
|
834
|
-
previewUrl: s.previewUrl
|
|
835
|
-
}));
|
|
808
|
+
const [state, patch, replace] = useImmerState(INITIAL_STATE);
|
|
836
809
|
const contextApi = useContext(S3Context);
|
|
837
810
|
const formatValidateFileError = useFormatValidateFileError();
|
|
838
811
|
const optsRef = useLiveRef(options);
|
|
@@ -850,23 +823,26 @@ function useFileUpload(options) {
|
|
|
850
823
|
}, []);
|
|
851
824
|
const clearToIdle = useCallback(() => {
|
|
852
825
|
revokeCurrentPreview();
|
|
853
|
-
|
|
854
|
-
}, [revokeCurrentPreview,
|
|
826
|
+
replace(INITIAL_STATE);
|
|
827
|
+
}, [revokeCurrentPreview, replace]);
|
|
855
828
|
useEffect(() => () => revokeCurrentPreview(), [revokeCurrentPreview]);
|
|
856
829
|
const upload = useCallback(
|
|
857
830
|
async (file, objectKey, requestOptions) => {
|
|
858
831
|
revokeCurrentPreview();
|
|
859
832
|
const previewUrl = createImagePreviewUrl(file);
|
|
860
833
|
previewUrlRef.current = previewUrl;
|
|
861
|
-
|
|
834
|
+
const fileInfo = {
|
|
835
|
+
name: file.name,
|
|
836
|
+
size: file.size,
|
|
837
|
+
type: file.type,
|
|
838
|
+
previewUrl
|
|
839
|
+
};
|
|
840
|
+
patch((draft) => {
|
|
862
841
|
draft.phase = "validating";
|
|
863
842
|
draft.progress = { ...INITIAL_PROGRESS };
|
|
864
843
|
draft.error = null;
|
|
865
844
|
draft.result = null;
|
|
866
|
-
draft.
|
|
867
|
-
draft.fileSize = file.size;
|
|
868
|
-
draft.fileType = file.type;
|
|
869
|
-
draft.previewUrl = previewUrl;
|
|
845
|
+
draft.fileInfo = fileInfo;
|
|
870
846
|
});
|
|
871
847
|
const opts = optsRef.current;
|
|
872
848
|
const api = opts.api ?? apiRef.current;
|
|
@@ -880,7 +856,7 @@ function useFileUpload(options) {
|
|
|
880
856
|
});
|
|
881
857
|
if (validationError) {
|
|
882
858
|
const message = formatValidateFileError(validationError);
|
|
883
|
-
|
|
859
|
+
patch((draft) => {
|
|
884
860
|
draft.phase = "error";
|
|
885
861
|
draft.error = new DimahS3Error("BAD_REQUEST", { message });
|
|
886
862
|
});
|
|
@@ -890,7 +866,7 @@ function useFileUpload(options) {
|
|
|
890
866
|
if (opts.beforeUpload) {
|
|
891
867
|
const allowed = await opts.beforeUpload(file);
|
|
892
868
|
if (!allowed) {
|
|
893
|
-
|
|
869
|
+
patch((draft) => {
|
|
894
870
|
draft.phase = "error";
|
|
895
871
|
draft.error = hookBlockedError(
|
|
896
872
|
"Upload blocked by beforeUpload hook"
|
|
@@ -901,18 +877,19 @@ function useFileUpload(options) {
|
|
|
901
877
|
}
|
|
902
878
|
}
|
|
903
879
|
speedUpdaterRef.current.reset();
|
|
904
|
-
|
|
880
|
+
patch((draft) => {
|
|
905
881
|
draft.phase = "presigning";
|
|
906
882
|
});
|
|
907
883
|
opts.onUploadStart?.(file, objectKey);
|
|
884
|
+
const mergedOptions = mergeRequestOptions(opts, file, requestOptions);
|
|
908
885
|
const controller = new AbortController();
|
|
909
886
|
abortRef.current = controller;
|
|
910
887
|
activeUploadRef.current = {
|
|
911
888
|
file,
|
|
912
889
|
objectKey,
|
|
913
890
|
serverKey: objectKey,
|
|
914
|
-
bucket:
|
|
915
|
-
requestOptions
|
|
891
|
+
bucket: mergedOptions?.bucket,
|
|
892
|
+
requestOptions: mergedOptions
|
|
916
893
|
};
|
|
917
894
|
try {
|
|
918
895
|
const result = await uploadFile(
|
|
@@ -930,12 +907,12 @@ function useFileUpload(options) {
|
|
|
930
907
|
{
|
|
931
908
|
onProgress: (progress) => {
|
|
932
909
|
const p = speedUpdaterRef.current.apply(progress);
|
|
933
|
-
|
|
910
|
+
patch((draft) => {
|
|
934
911
|
draft.progress = p;
|
|
935
912
|
});
|
|
936
913
|
opts.onProgress?.(file, p);
|
|
937
914
|
},
|
|
938
|
-
onPhaseChange: (phase) =>
|
|
915
|
+
onPhaseChange: (phase) => patch((draft) => {
|
|
939
916
|
draft.phase = phase;
|
|
940
917
|
}),
|
|
941
918
|
onPartUpload: (partNumber, totalParts) => opts.onPartUpload?.(file, partNumber, totalParts),
|
|
@@ -948,9 +925,9 @@ function useFileUpload(options) {
|
|
|
948
925
|
}
|
|
949
926
|
},
|
|
950
927
|
controller.signal,
|
|
951
|
-
|
|
928
|
+
mergedOptions
|
|
952
929
|
);
|
|
953
|
-
|
|
930
|
+
patch((draft) => {
|
|
954
931
|
draft.phase = "success";
|
|
955
932
|
draft.result = result;
|
|
956
933
|
draft.progress = {
|
|
@@ -971,7 +948,7 @@ function useFileUpload(options) {
|
|
|
971
948
|
return;
|
|
972
949
|
}
|
|
973
950
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
974
|
-
|
|
951
|
+
patch((draft) => {
|
|
975
952
|
draft.phase = "error";
|
|
976
953
|
draft.error = toHookError(err, message);
|
|
977
954
|
});
|
|
@@ -987,7 +964,7 @@ function useFileUpload(options) {
|
|
|
987
964
|
formatValidateFileError,
|
|
988
965
|
revokeCurrentPreview,
|
|
989
966
|
clearToIdle,
|
|
990
|
-
|
|
967
|
+
patch
|
|
991
968
|
]
|
|
992
969
|
);
|
|
993
970
|
const cancel = useCallback(() => {
|
|
@@ -1020,7 +997,15 @@ function useFileUpload(options) {
|
|
|
1020
997
|
abortRef.current?.abort();
|
|
1021
998
|
clearToIdle();
|
|
1022
999
|
}, [clearToIdle]);
|
|
1023
|
-
return {
|
|
1000
|
+
return {
|
|
1001
|
+
...state,
|
|
1002
|
+
isUploading: state.phase === "uploading",
|
|
1003
|
+
isPending: PENDING_PHASES.has(state.phase),
|
|
1004
|
+
upload,
|
|
1005
|
+
cancel,
|
|
1006
|
+
detach,
|
|
1007
|
+
reset
|
|
1008
|
+
};
|
|
1024
1009
|
}
|
|
1025
1010
|
var INITIAL_PROGRESS2 = { loaded: 0, total: 0, percent: 0 };
|
|
1026
1011
|
var INITIAL_STATE2 = {
|
|
@@ -1029,17 +1014,15 @@ var INITIAL_STATE2 = {
|
|
|
1029
1014
|
totalProgress: INITIAL_PROGRESS2,
|
|
1030
1015
|
error: null
|
|
1031
1016
|
};
|
|
1017
|
+
var PENDING_PHASES2 = /* @__PURE__ */ new Set([
|
|
1018
|
+
"validating",
|
|
1019
|
+
"uploading"
|
|
1020
|
+
]);
|
|
1032
1021
|
function generateId() {
|
|
1033
1022
|
return crypto.randomUUID();
|
|
1034
1023
|
}
|
|
1035
1024
|
function useMultiFileUpload(options) {
|
|
1036
|
-
const
|
|
1037
|
-
const state = useHookStoreShallow(store, (s) => ({
|
|
1038
|
-
phase: s.phase,
|
|
1039
|
-
files: s.files,
|
|
1040
|
-
totalProgress: s.totalProgress,
|
|
1041
|
-
error: s.error
|
|
1042
|
-
}));
|
|
1025
|
+
const [state, patch, replace] = useImmerState(INITIAL_STATE2);
|
|
1043
1026
|
const contextApi = useContext(S3Context);
|
|
1044
1027
|
const formatValidateFileError = useFormatValidateFileError();
|
|
1045
1028
|
const optsRef = useLiveRef(options);
|
|
@@ -1060,8 +1043,8 @@ function useMultiFileUpload(options) {
|
|
|
1060
1043
|
}, []);
|
|
1061
1044
|
const clearToIdle = useCallback(() => {
|
|
1062
1045
|
revokeAllPreviews();
|
|
1063
|
-
|
|
1064
|
-
}, [revokeAllPreviews,
|
|
1046
|
+
replace(INITIAL_STATE2);
|
|
1047
|
+
}, [revokeAllPreviews, replace]);
|
|
1065
1048
|
useEffect(() => () => revokeAllPreviews(), [revokeAllPreviews]);
|
|
1066
1049
|
const upload = useCallback(
|
|
1067
1050
|
async (files, resolveKey) => {
|
|
@@ -1074,13 +1057,13 @@ function useMultiFileUpload(options) {
|
|
|
1074
1057
|
const items = [];
|
|
1075
1058
|
const fileStates = [];
|
|
1076
1059
|
const fileMap = /* @__PURE__ */ new Map();
|
|
1077
|
-
|
|
1060
|
+
patch((draft) => {
|
|
1078
1061
|
draft.phase = "validating";
|
|
1079
1062
|
draft.error = null;
|
|
1080
1063
|
});
|
|
1081
1064
|
if (opts.maxFiles && files.length > opts.maxFiles) {
|
|
1082
1065
|
const msg = `Too many files. Maximum is ${opts.maxFiles}.`;
|
|
1083
|
-
|
|
1066
|
+
patch((draft) => {
|
|
1084
1067
|
draft.phase = "error";
|
|
1085
1068
|
draft.error = new DimahS3Error("BAD_REQUEST", { message: msg });
|
|
1086
1069
|
});
|
|
@@ -1095,7 +1078,7 @@ function useMultiFileUpload(options) {
|
|
|
1095
1078
|
if (validationError) {
|
|
1096
1079
|
const detail = formatValidateFileError(validationError);
|
|
1097
1080
|
const msg = `${file.name}: ${detail}`;
|
|
1098
|
-
|
|
1081
|
+
patch((draft) => {
|
|
1099
1082
|
draft.phase = "error";
|
|
1100
1083
|
draft.error = new DimahS3Error("BAD_REQUEST", { message: msg });
|
|
1101
1084
|
});
|
|
@@ -1106,7 +1089,7 @@ function useMultiFileUpload(options) {
|
|
|
1106
1089
|
if (opts.beforeUpload) {
|
|
1107
1090
|
const allowed = await opts.beforeUpload(files);
|
|
1108
1091
|
if (!allowed) {
|
|
1109
|
-
|
|
1092
|
+
patch((draft) => {
|
|
1110
1093
|
draft.phase = "error";
|
|
1111
1094
|
draft.error = hookBlockedError(
|
|
1112
1095
|
"Upload blocked by beforeUpload hook"
|
|
@@ -1127,9 +1110,9 @@ function useMultiFileUpload(options) {
|
|
|
1127
1110
|
fileMap.set(id, file);
|
|
1128
1111
|
fileStates.push({
|
|
1129
1112
|
id,
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1113
|
+
name: file.name,
|
|
1114
|
+
size: file.size,
|
|
1115
|
+
type: file.type,
|
|
1133
1116
|
previewUrl,
|
|
1134
1117
|
status: "pending",
|
|
1135
1118
|
progress: { loaded: 0, total: file.size, percent: 0 },
|
|
@@ -1148,7 +1131,7 @@ function useMultiFileUpload(options) {
|
|
|
1148
1131
|
}
|
|
1149
1132
|
])
|
|
1150
1133
|
);
|
|
1151
|
-
|
|
1134
|
+
patch((draft) => {
|
|
1152
1135
|
draft.phase = "uploading";
|
|
1153
1136
|
draft.files = fileStates;
|
|
1154
1137
|
draft.totalProgress = {
|
|
@@ -1186,11 +1169,11 @@ function useMultiFileUpload(options) {
|
|
|
1186
1169
|
onFileProgress: (id, progress) => {
|
|
1187
1170
|
const updater = fileSpeedUpdatersRef.current.get(id);
|
|
1188
1171
|
const p = updater ? updater.apply(progress) : progress;
|
|
1189
|
-
|
|
1190
|
-
const
|
|
1191
|
-
if (
|
|
1192
|
-
|
|
1193
|
-
|
|
1172
|
+
patch((draft) => {
|
|
1173
|
+
const fileState = draft.files.find((f) => f.id === id);
|
|
1174
|
+
if (fileState) {
|
|
1175
|
+
fileState.status = "uploading";
|
|
1176
|
+
fileState.progress = p;
|
|
1194
1177
|
}
|
|
1195
1178
|
});
|
|
1196
1179
|
const file = fileMap.get(id);
|
|
@@ -1198,13 +1181,13 @@ function useMultiFileUpload(options) {
|
|
|
1198
1181
|
},
|
|
1199
1182
|
onFileSuccess: (id, result) => {
|
|
1200
1183
|
activeUploadsRef.current.delete(id);
|
|
1201
|
-
|
|
1202
|
-
const
|
|
1203
|
-
if (
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
loaded:
|
|
1207
|
-
total:
|
|
1184
|
+
patch((draft) => {
|
|
1185
|
+
const fileState = draft.files.find((f) => f.id === id);
|
|
1186
|
+
if (fileState) {
|
|
1187
|
+
fileState.status = "success";
|
|
1188
|
+
fileState.progress = {
|
|
1189
|
+
loaded: fileState.size,
|
|
1190
|
+
total: fileState.size,
|
|
1208
1191
|
percent: 100
|
|
1209
1192
|
};
|
|
1210
1193
|
}
|
|
@@ -1213,11 +1196,11 @@ function useMultiFileUpload(options) {
|
|
|
1213
1196
|
if (file) opts.onFileSuccess?.(file, result);
|
|
1214
1197
|
},
|
|
1215
1198
|
onFileError: (id, error) => {
|
|
1216
|
-
|
|
1217
|
-
const
|
|
1218
|
-
if (
|
|
1219
|
-
|
|
1220
|
-
|
|
1199
|
+
patch((draft) => {
|
|
1200
|
+
const fileState = draft.files.find((f) => f.id === id);
|
|
1201
|
+
if (fileState) {
|
|
1202
|
+
fileState.status = "error";
|
|
1203
|
+
fileState.error = error;
|
|
1221
1204
|
}
|
|
1222
1205
|
});
|
|
1223
1206
|
const file = fileMap.get(id);
|
|
@@ -1225,7 +1208,7 @@ function useMultiFileUpload(options) {
|
|
|
1225
1208
|
},
|
|
1226
1209
|
onTotalProgress: (progress) => {
|
|
1227
1210
|
const p = totalSpeedUpdaterRef.current.apply(progress);
|
|
1228
|
-
|
|
1211
|
+
patch((draft) => {
|
|
1229
1212
|
draft.totalProgress = p;
|
|
1230
1213
|
});
|
|
1231
1214
|
opts.onProgress?.(p);
|
|
@@ -1247,7 +1230,7 @@ function useMultiFileUpload(options) {
|
|
|
1247
1230
|
);
|
|
1248
1231
|
const hasErrors = results.some((r) => r.status === "error");
|
|
1249
1232
|
const successResults = results.filter((r) => r.result !== null).map((r) => r.result);
|
|
1250
|
-
|
|
1233
|
+
patch((draft) => {
|
|
1251
1234
|
draft.phase = hasErrors ? "error" : "success";
|
|
1252
1235
|
draft.error = hasErrors ? new DimahS3Error("BAD_REQUEST", {
|
|
1253
1236
|
message: `${results.filter((r) => r.status === "error").length} file(s) failed`
|
|
@@ -1274,7 +1257,7 @@ function useMultiFileUpload(options) {
|
|
|
1274
1257
|
clearToIdle();
|
|
1275
1258
|
return;
|
|
1276
1259
|
}
|
|
1277
|
-
|
|
1260
|
+
patch((draft) => {
|
|
1278
1261
|
draft.phase = "error";
|
|
1279
1262
|
draft.error = toHookError(err, "Upload failed");
|
|
1280
1263
|
});
|
|
@@ -1290,7 +1273,7 @@ function useMultiFileUpload(options) {
|
|
|
1290
1273
|
formatValidateFileError,
|
|
1291
1274
|
revokeAllPreviews,
|
|
1292
1275
|
clearToIdle,
|
|
1293
|
-
|
|
1276
|
+
patch
|
|
1294
1277
|
]
|
|
1295
1278
|
);
|
|
1296
1279
|
const cancel = useCallback(() => {
|
|
@@ -1330,75 +1313,16 @@ function useMultiFileUpload(options) {
|
|
|
1330
1313
|
abortRef.current?.abort();
|
|
1331
1314
|
clearToIdle();
|
|
1332
1315
|
}, [clearToIdle]);
|
|
1333
|
-
return {
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
".csv": "text/csv",
|
|
1343
|
-
".doc": "application/msword",
|
|
1344
|
-
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1345
|
-
".gif": "image/gif",
|
|
1346
|
-
".heic": "image/heic",
|
|
1347
|
-
".htm": "text/html",
|
|
1348
|
-
".html": "text/html",
|
|
1349
|
-
".jpeg": "image/jpeg",
|
|
1350
|
-
".jpg": "image/jpeg",
|
|
1351
|
-
".json": "application/json",
|
|
1352
|
-
".m4a": "audio/mp4",
|
|
1353
|
-
".md": "text/markdown",
|
|
1354
|
-
".mkv": "video/x-matroska",
|
|
1355
|
-
".mov": "video/quicktime",
|
|
1356
|
-
".mp3": "audio/mpeg",
|
|
1357
|
-
".mp4": "video/mp4",
|
|
1358
|
-
".ogg": "audio/ogg",
|
|
1359
|
-
".pdf": "application/pdf",
|
|
1360
|
-
".png": "image/png",
|
|
1361
|
-
".ppt": "application/vnd.ms-powerpoint",
|
|
1362
|
-
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
1363
|
-
".svg": "image/svg+xml",
|
|
1364
|
-
".tif": "image/tiff",
|
|
1365
|
-
".tiff": "image/tiff",
|
|
1366
|
-
".txt": "text/plain",
|
|
1367
|
-
".wav": "audio/wav",
|
|
1368
|
-
".webm": "video/webm",
|
|
1369
|
-
".webp": "image/webp",
|
|
1370
|
-
".xls": "application/vnd.ms-excel",
|
|
1371
|
-
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1372
|
-
".zip": "application/zip"
|
|
1373
|
-
};
|
|
1374
|
-
var FAMILY_WILDCARD = /* @__PURE__ */ new Set(["audio/*", "image/*", "video/*"]);
|
|
1375
|
-
function isMimeAcceptToken(token) {
|
|
1376
|
-
if (FAMILY_WILDCARD.has(token)) return true;
|
|
1377
|
-
const slash = token.indexOf("/");
|
|
1378
|
-
if (slash < 1) return false;
|
|
1379
|
-
const type = token.slice(0, slash);
|
|
1380
|
-
const subtype = token.slice(slash + 1);
|
|
1381
|
-
return type.length > 0 && subtype.length > 0 && !type.includes("*") && !subtype.includes("*") && !subtype.includes(";");
|
|
1382
|
-
}
|
|
1383
|
-
function toDropzoneAccept(accept) {
|
|
1384
|
-
if (accept == null || accept.length === 0) return void 0;
|
|
1385
|
-
const map = {};
|
|
1386
|
-
for (const raw of accept) {
|
|
1387
|
-
const token = raw.trim().toLowerCase();
|
|
1388
|
-
if (!token) continue;
|
|
1389
|
-
if (token.startsWith(".")) {
|
|
1390
|
-
if (token.length < 2) continue;
|
|
1391
|
-
const mime = EXTENSION_MIME[token] ?? "application/octet-stream";
|
|
1392
|
-
map[mime] ??= [];
|
|
1393
|
-
if (!map[mime].includes(token)) map[mime].push(token);
|
|
1394
|
-
continue;
|
|
1395
|
-
}
|
|
1396
|
-
if (isMimeAcceptToken(token)) map[token] ??= [];
|
|
1397
|
-
}
|
|
1398
|
-
return Object.keys(map).length > 0 ? map : void 0;
|
|
1316
|
+
return {
|
|
1317
|
+
...state,
|
|
1318
|
+
isUploading: state.phase === "uploading",
|
|
1319
|
+
isPending: PENDING_PHASES2.has(state.phase),
|
|
1320
|
+
upload,
|
|
1321
|
+
cancel,
|
|
1322
|
+
detach,
|
|
1323
|
+
reset
|
|
1324
|
+
};
|
|
1399
1325
|
}
|
|
1400
|
-
|
|
1401
|
-
// src/hooks/use-file-intake.ts
|
|
1402
1326
|
function useFileIntake(options) {
|
|
1403
1327
|
const optsRef = useLiveRef(options);
|
|
1404
1328
|
const onDropAccepted = useCallback(
|
|
@@ -1413,16 +1337,24 @@ function useFileIntake(options) {
|
|
|
1413
1337
|
},
|
|
1414
1338
|
[optsRef]
|
|
1415
1339
|
);
|
|
1340
|
+
const validator = useCallback(
|
|
1341
|
+
(file) => {
|
|
1342
|
+
const error = validateFile(file, { accept: optsRef.current.accept });
|
|
1343
|
+
if (error?.code !== "FILE_TYPE_NOT_ALLOWED") return null;
|
|
1344
|
+
return { code: ErrorCode.FileInvalidType, message: error.message };
|
|
1345
|
+
},
|
|
1346
|
+
[optsRef]
|
|
1347
|
+
);
|
|
1348
|
+
const htmlAccept = options.accept?.map((token) => token.trim()).filter(Boolean).join(",") || void 0;
|
|
1416
1349
|
const {
|
|
1417
1350
|
getRootProps,
|
|
1418
|
-
getInputProps,
|
|
1351
|
+
getInputProps: getDropzoneInputProps,
|
|
1419
1352
|
open,
|
|
1420
1353
|
isDragActive,
|
|
1421
1354
|
isDragAccept,
|
|
1422
1355
|
isDragReject,
|
|
1423
1356
|
fileRejections
|
|
1424
1357
|
} = useDropzone({
|
|
1425
|
-
accept: toDropzoneAccept(options.accept),
|
|
1426
1358
|
maxSize: options.maxFileSize,
|
|
1427
1359
|
maxFiles: options.maxFiles,
|
|
1428
1360
|
multiple: options.multiple ?? false,
|
|
@@ -1430,9 +1362,17 @@ function useFileIntake(options) {
|
|
|
1430
1362
|
noClick: options.noClick,
|
|
1431
1363
|
noDrag: options.noDrag,
|
|
1432
1364
|
noKeyboard: options.noKeyboard,
|
|
1365
|
+
validator,
|
|
1433
1366
|
onDropAccepted,
|
|
1434
1367
|
onDropRejected
|
|
1435
1368
|
});
|
|
1369
|
+
const getInputProps = useCallback(
|
|
1370
|
+
(props) => getDropzoneInputProps({
|
|
1371
|
+
...props,
|
|
1372
|
+
...htmlAccept != null ? { accept: htmlAccept } : {}
|
|
1373
|
+
}),
|
|
1374
|
+
[getDropzoneInputProps, htmlAccept]
|
|
1375
|
+
);
|
|
1436
1376
|
return {
|
|
1437
1377
|
getRootProps,
|
|
1438
1378
|
getInputProps,
|
|
@@ -1448,8 +1388,6 @@ function useFileIntake(options) {
|
|
|
1448
1388
|
function useUpload(options) {
|
|
1449
1389
|
const {
|
|
1450
1390
|
objectKey,
|
|
1451
|
-
uploadOptions,
|
|
1452
|
-
getUploadOptions,
|
|
1453
1391
|
disabled,
|
|
1454
1392
|
noDrag,
|
|
1455
1393
|
noClick,
|
|
@@ -1467,36 +1405,27 @@ function useUpload(options) {
|
|
|
1467
1405
|
const list = files == null ? [] : Array.from(files);
|
|
1468
1406
|
const file = list[0];
|
|
1469
1407
|
if (!file) return;
|
|
1470
|
-
void single.upload(file, resolveKey(file)
|
|
1471
|
-
...uploadOptions,
|
|
1472
|
-
...getUploadOptions?.(file)
|
|
1473
|
-
});
|
|
1408
|
+
void single.upload(file, resolveKey(file));
|
|
1474
1409
|
};
|
|
1475
|
-
const isUploading = single.phase === "uploading";
|
|
1476
1410
|
const intake = useFileIntake({
|
|
1477
1411
|
accept: options.accept,
|
|
1478
1412
|
maxFileSize: options.maxFileSize,
|
|
1479
1413
|
maxFiles: 1,
|
|
1480
1414
|
multiple: false,
|
|
1481
|
-
disabled: Boolean(disabled) ||
|
|
1415
|
+
disabled: Boolean(disabled) || single.isPending,
|
|
1482
1416
|
noDrag,
|
|
1483
1417
|
noClick,
|
|
1484
1418
|
noKeyboard,
|
|
1485
1419
|
onAccept: (files) => handleFiles(files),
|
|
1486
1420
|
onReject: onFileReject
|
|
1487
1421
|
});
|
|
1488
|
-
const fileInfo = single.fileName != null ? {
|
|
1489
|
-
name: single.fileName,
|
|
1490
|
-
size: single.fileSize ?? 0,
|
|
1491
|
-
type: single.fileType ?? "",
|
|
1492
|
-
previewUrl: single.previewUrl
|
|
1493
|
-
} : null;
|
|
1494
1422
|
return {
|
|
1495
1423
|
phase: single.phase,
|
|
1496
|
-
fileInfo,
|
|
1424
|
+
fileInfo: single.fileInfo,
|
|
1497
1425
|
progress: single.progress,
|
|
1498
1426
|
error: single.error,
|
|
1499
|
-
isUploading,
|
|
1427
|
+
isUploading: single.isUploading,
|
|
1428
|
+
isPending: single.isPending,
|
|
1500
1429
|
handleFiles,
|
|
1501
1430
|
open: intake.open,
|
|
1502
1431
|
cancel: single.cancel,
|
|
@@ -1522,8 +1451,7 @@ function useMultiUpload(options) {
|
|
|
1522
1451
|
} = options;
|
|
1523
1452
|
const multi = useMultiFileUpload(multiOpts);
|
|
1524
1453
|
const resolveKey = (file) => {
|
|
1525
|
-
if (
|
|
1526
|
-
if (objectKey) return objectKey;
|
|
1454
|
+
if (objectKey) return objectKey(file);
|
|
1527
1455
|
return defaultObjectKey(file);
|
|
1528
1456
|
};
|
|
1529
1457
|
const handleFiles = (files) => {
|
|
@@ -1532,13 +1460,12 @@ function useMultiUpload(options) {
|
|
|
1532
1460
|
if (list.length === 0) return;
|
|
1533
1461
|
void multi.upload(list, resolveKey);
|
|
1534
1462
|
};
|
|
1535
|
-
const isUploading = multi.phase === "uploading";
|
|
1536
1463
|
const intake = useFileIntake({
|
|
1537
1464
|
accept: options.accept,
|
|
1538
1465
|
maxFileSize: options.maxFileSize,
|
|
1539
1466
|
maxFiles: options.maxFiles,
|
|
1540
1467
|
multiple: true,
|
|
1541
|
-
disabled: Boolean(disabled) ||
|
|
1468
|
+
disabled: Boolean(disabled) || multi.isPending,
|
|
1542
1469
|
noDrag,
|
|
1543
1470
|
noClick,
|
|
1544
1471
|
noKeyboard,
|
|
@@ -1550,7 +1477,8 @@ function useMultiUpload(options) {
|
|
|
1550
1477
|
files: multi.files,
|
|
1551
1478
|
totalProgress: multi.totalProgress,
|
|
1552
1479
|
error: multi.error,
|
|
1553
|
-
isUploading,
|
|
1480
|
+
isUploading: multi.isUploading,
|
|
1481
|
+
isPending: multi.isPending,
|
|
1554
1482
|
handleFiles,
|
|
1555
1483
|
open: intake.open,
|
|
1556
1484
|
cancel: multi.cancel,
|
|
@@ -1578,16 +1506,7 @@ function missingApiMessage(hook) {
|
|
|
1578
1506
|
return `[dimah-s3] No S3Api found. Pass \`api\` to ${hook} or wrap with <S3Provider>.`;
|
|
1579
1507
|
}
|
|
1580
1508
|
function useDownload(options = {}) {
|
|
1581
|
-
const
|
|
1582
|
-
const state = useHookStoreShallow(store, (s) => ({
|
|
1583
|
-
phase: s.phase,
|
|
1584
|
-
error: s.error,
|
|
1585
|
-
url: s.url,
|
|
1586
|
-
expiresIn: s.expiresIn,
|
|
1587
|
-
progress: s.progress,
|
|
1588
|
-
fileName: s.fileName,
|
|
1589
|
-
fileSize: s.fileSize
|
|
1590
|
-
}));
|
|
1509
|
+
const [state, patch, replace] = useImmerState(INITIAL_STATE3);
|
|
1591
1510
|
const contextApi = useContext(S3Context);
|
|
1592
1511
|
const optsRef = useLiveRef(options);
|
|
1593
1512
|
const apiRef = useLiveRef(contextApi);
|
|
@@ -1601,7 +1520,7 @@ function useDownload(options = {}) {
|
|
|
1601
1520
|
const opts = optsRef.current;
|
|
1602
1521
|
const api = opts.api ?? apiRef.current;
|
|
1603
1522
|
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
1604
|
-
|
|
1523
|
+
patch((draft) => {
|
|
1605
1524
|
draft.phase = "presigning";
|
|
1606
1525
|
draft.error = null;
|
|
1607
1526
|
draft.url = null;
|
|
@@ -1612,7 +1531,7 @@ function useDownload(options = {}) {
|
|
|
1612
1531
|
fileName: downloadName,
|
|
1613
1532
|
bucket: opts.bucket
|
|
1614
1533
|
});
|
|
1615
|
-
|
|
1534
|
+
patch((draft) => {
|
|
1616
1535
|
draft.phase = "idle";
|
|
1617
1536
|
draft.error = null;
|
|
1618
1537
|
draft.url = result.url;
|
|
@@ -1620,7 +1539,7 @@ function useDownload(options = {}) {
|
|
|
1620
1539
|
});
|
|
1621
1540
|
return { url: result.url, expiresIn: result.expiresIn };
|
|
1622
1541
|
} catch (err) {
|
|
1623
|
-
|
|
1542
|
+
patch((draft) => {
|
|
1624
1543
|
draft.phase = "error";
|
|
1625
1544
|
draft.error = toHookError(err, "Download failed");
|
|
1626
1545
|
draft.url = null;
|
|
@@ -1630,7 +1549,7 @@ function useDownload(options = {}) {
|
|
|
1630
1549
|
return null;
|
|
1631
1550
|
}
|
|
1632
1551
|
},
|
|
1633
|
-
[apiRef, optsRef,
|
|
1552
|
+
[apiRef, optsRef, patch]
|
|
1634
1553
|
);
|
|
1635
1554
|
const downloadNavigate = useCallback(
|
|
1636
1555
|
async (key, downloadName) => {
|
|
@@ -1638,7 +1557,7 @@ function useDownload(options = {}) {
|
|
|
1638
1557
|
if (opts.beforeDownload) {
|
|
1639
1558
|
const allowed = await opts.beforeDownload(key);
|
|
1640
1559
|
if (!allowed) {
|
|
1641
|
-
|
|
1560
|
+
patch((draft) => {
|
|
1642
1561
|
draft.phase = "error";
|
|
1643
1562
|
draft.error = hookBlockedError(
|
|
1644
1563
|
"Download blocked by beforeDownload hook"
|
|
@@ -1655,7 +1574,7 @@ function useDownload(options = {}) {
|
|
|
1655
1574
|
window.location.href = result.url;
|
|
1656
1575
|
opts.onInitiated?.(key);
|
|
1657
1576
|
},
|
|
1658
|
-
[optsRef, presign,
|
|
1577
|
+
[optsRef, presign, patch]
|
|
1659
1578
|
);
|
|
1660
1579
|
const downloadFetch = useCallback(
|
|
1661
1580
|
async (key, downloadName) => {
|
|
@@ -1666,7 +1585,7 @@ function useDownload(options = {}) {
|
|
|
1666
1585
|
if (opts.beforeDownload) {
|
|
1667
1586
|
const allowed = await opts.beforeDownload(key);
|
|
1668
1587
|
if (!allowed) {
|
|
1669
|
-
|
|
1588
|
+
patch((draft) => {
|
|
1670
1589
|
draft.phase = "error";
|
|
1671
1590
|
draft.error = hookBlockedError(
|
|
1672
1591
|
"Download blocked by beforeDownload hook"
|
|
@@ -1676,7 +1595,7 @@ function useDownload(options = {}) {
|
|
|
1676
1595
|
return;
|
|
1677
1596
|
}
|
|
1678
1597
|
}
|
|
1679
|
-
|
|
1598
|
+
patch((draft) => {
|
|
1680
1599
|
draft.phase = "presigning";
|
|
1681
1600
|
draft.progress = { ...INITIAL_PROGRESS3 };
|
|
1682
1601
|
draft.error = null;
|
|
@@ -1688,7 +1607,7 @@ function useDownload(options = {}) {
|
|
|
1688
1607
|
fileName: downloadName,
|
|
1689
1608
|
bucket: opts.bucket
|
|
1690
1609
|
});
|
|
1691
|
-
|
|
1610
|
+
patch((draft) => {
|
|
1692
1611
|
draft.phase = "downloading";
|
|
1693
1612
|
});
|
|
1694
1613
|
opts.onDownloadStart?.(key);
|
|
@@ -1703,7 +1622,7 @@ function useDownload(options = {}) {
|
|
|
1703
1622
|
}
|
|
1704
1623
|
const contentLength = Number(res.headers.get("content-length") || 0);
|
|
1705
1624
|
const name = downloadName ?? parseFileName(res.headers.get("content-disposition")) ?? fallback;
|
|
1706
|
-
|
|
1625
|
+
patch((draft) => {
|
|
1707
1626
|
draft.fileName = name;
|
|
1708
1627
|
draft.fileSize = contentLength || null;
|
|
1709
1628
|
});
|
|
@@ -1722,7 +1641,7 @@ function useDownload(options = {}) {
|
|
|
1722
1641
|
total: contentLength,
|
|
1723
1642
|
percent
|
|
1724
1643
|
});
|
|
1725
|
-
|
|
1644
|
+
patch((draft) => {
|
|
1726
1645
|
draft.progress = progress;
|
|
1727
1646
|
});
|
|
1728
1647
|
opts.onProgress?.(key, progress);
|
|
@@ -1734,7 +1653,7 @@ function useDownload(options = {}) {
|
|
|
1734
1653
|
anchor.download = name ?? fallback;
|
|
1735
1654
|
anchor.click();
|
|
1736
1655
|
URL.revokeObjectURL(blobUrl);
|
|
1737
|
-
|
|
1656
|
+
patch((draft) => {
|
|
1738
1657
|
draft.phase = "success";
|
|
1739
1658
|
draft.fileSize = blob.size;
|
|
1740
1659
|
draft.progress = {
|
|
@@ -1748,10 +1667,10 @@ function useDownload(options = {}) {
|
|
|
1748
1667
|
if (err.name === "AbortError") {
|
|
1749
1668
|
if (!resettingRef.current) opts.onCancel?.(key);
|
|
1750
1669
|
resettingRef.current = false;
|
|
1751
|
-
|
|
1670
|
+
replace(INITIAL_STATE3);
|
|
1752
1671
|
return;
|
|
1753
1672
|
}
|
|
1754
|
-
|
|
1673
|
+
patch((draft) => {
|
|
1755
1674
|
draft.phase = "error";
|
|
1756
1675
|
draft.error = toHookError(err, "Download failed");
|
|
1757
1676
|
});
|
|
@@ -1760,7 +1679,7 @@ function useDownload(options = {}) {
|
|
|
1760
1679
|
abortRef.current = null;
|
|
1761
1680
|
}
|
|
1762
1681
|
},
|
|
1763
|
-
[apiRef, optsRef,
|
|
1682
|
+
[apiRef, optsRef, patch, replace]
|
|
1764
1683
|
);
|
|
1765
1684
|
const download = useCallback(
|
|
1766
1685
|
async (key, downloadName) => {
|
|
@@ -1774,13 +1693,13 @@ function useDownload(options = {}) {
|
|
|
1774
1693
|
);
|
|
1775
1694
|
const cancel = useCallback(() => {
|
|
1776
1695
|
abortRef.current?.abort();
|
|
1777
|
-
|
|
1778
|
-
}, [
|
|
1696
|
+
replace(INITIAL_STATE3);
|
|
1697
|
+
}, [replace]);
|
|
1779
1698
|
const reset = useCallback(() => {
|
|
1780
1699
|
resettingRef.current = true;
|
|
1781
1700
|
abortRef.current?.abort();
|
|
1782
|
-
|
|
1783
|
-
}, [
|
|
1701
|
+
replace(INITIAL_STATE3);
|
|
1702
|
+
}, [replace]);
|
|
1784
1703
|
if (options.mode === "fetch") {
|
|
1785
1704
|
return {
|
|
1786
1705
|
phase: state.phase,
|
|
@@ -1809,12 +1728,7 @@ var INITIAL_STATE4 = {
|
|
|
1809
1728
|
pendingKey: null
|
|
1810
1729
|
};
|
|
1811
1730
|
function useDelete(options) {
|
|
1812
|
-
const
|
|
1813
|
-
const state = useHookStoreShallow(store, (s) => ({
|
|
1814
|
-
phase: s.phase,
|
|
1815
|
-
error: s.error,
|
|
1816
|
-
pendingKey: s.pendingKey
|
|
1817
|
-
}));
|
|
1731
|
+
const [state, patch, replace] = useImmerState(INITIAL_STATE4);
|
|
1818
1732
|
const contextApi = useContext(S3Context);
|
|
1819
1733
|
const optsRef = useLiveRef(options);
|
|
1820
1734
|
const apiRef = useLiveRef(contextApi);
|
|
@@ -1822,13 +1736,13 @@ function useDelete(options) {
|
|
|
1822
1736
|
const requestDelete = useCallback(
|
|
1823
1737
|
(key) => {
|
|
1824
1738
|
pendingKeyRef.current = key;
|
|
1825
|
-
|
|
1739
|
+
patch((draft) => {
|
|
1826
1740
|
draft.phase = "confirming";
|
|
1827
1741
|
draft.error = null;
|
|
1828
1742
|
draft.pendingKey = key;
|
|
1829
1743
|
});
|
|
1830
1744
|
},
|
|
1831
|
-
[
|
|
1745
|
+
[patch]
|
|
1832
1746
|
);
|
|
1833
1747
|
const confirmDelete = useCallback(async () => {
|
|
1834
1748
|
const key = pendingKeyRef.current;
|
|
@@ -1842,7 +1756,7 @@ function useDelete(options) {
|
|
|
1842
1756
|
if (opts.beforeDelete) {
|
|
1843
1757
|
const allowed = await opts.beforeDelete(key);
|
|
1844
1758
|
if (!allowed) {
|
|
1845
|
-
|
|
1759
|
+
patch((draft) => {
|
|
1846
1760
|
draft.phase = "error";
|
|
1847
1761
|
draft.error = hookBlockedError("Delete blocked by beforeDelete hook");
|
|
1848
1762
|
draft.pendingKey = null;
|
|
@@ -1852,7 +1766,7 @@ function useDelete(options) {
|
|
|
1852
1766
|
return;
|
|
1853
1767
|
}
|
|
1854
1768
|
}
|
|
1855
|
-
|
|
1769
|
+
patch((draft) => {
|
|
1856
1770
|
draft.phase = "deleting";
|
|
1857
1771
|
draft.error = null;
|
|
1858
1772
|
});
|
|
@@ -1860,28 +1774,28 @@ function useDelete(options) {
|
|
|
1860
1774
|
try {
|
|
1861
1775
|
await api.delete(key, { bucket: opts.bucket });
|
|
1862
1776
|
pendingKeyRef.current = null;
|
|
1863
|
-
|
|
1777
|
+
patch((draft) => {
|
|
1864
1778
|
draft.phase = "success";
|
|
1865
1779
|
draft.error = null;
|
|
1866
1780
|
draft.pendingKey = null;
|
|
1867
1781
|
});
|
|
1868
1782
|
await opts.onSuccess?.(key);
|
|
1869
1783
|
} catch (err) {
|
|
1870
|
-
|
|
1784
|
+
patch((draft) => {
|
|
1871
1785
|
draft.phase = "error";
|
|
1872
1786
|
draft.error = toHookError(err, "Delete failed");
|
|
1873
1787
|
});
|
|
1874
1788
|
opts.onError?.(key, err, "deleting");
|
|
1875
1789
|
}
|
|
1876
|
-
}, [apiRef, optsRef,
|
|
1790
|
+
}, [apiRef, optsRef, patch]);
|
|
1877
1791
|
const cancelDelete = useCallback(() => {
|
|
1878
1792
|
pendingKeyRef.current = null;
|
|
1879
|
-
|
|
1880
|
-
}, [
|
|
1793
|
+
replace(INITIAL_STATE4);
|
|
1794
|
+
}, [replace]);
|
|
1881
1795
|
const reset = useCallback(() => {
|
|
1882
1796
|
pendingKeyRef.current = null;
|
|
1883
|
-
|
|
1884
|
-
}, [
|
|
1797
|
+
replace(INITIAL_STATE4);
|
|
1798
|
+
}, [replace]);
|
|
1885
1799
|
return {
|
|
1886
1800
|
phase: state.phase,
|
|
1887
1801
|
error: state.error,
|
|
@@ -1893,6 +1807,6 @@ function useDelete(options) {
|
|
|
1893
1807
|
};
|
|
1894
1808
|
}
|
|
1895
1809
|
|
|
1896
|
-
export { S3Provider,
|
|
1810
|
+
export { S3Provider, createLocalStorageStore, createMemoryStore, createS3Client, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFileUpload, useFormatDimahError, useFormatValidateFileError, useMultiFileUpload, useMultiUpload, useUpload };
|
|
1897
1811
|
//# sourceMappingURL=index.js.map
|
|
1898
1812
|
//# sourceMappingURL=index.js.map
|