@dimah-s3/react 0.6.2 → 0.7.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/README.md +5 -2
- package/dist/helpers/file-preview.d.ts +5 -0
- package/dist/helpers/file-preview.d.ts.map +1 -0
- package/dist/helpers/index.d.ts +2 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/to-dropzone-accept.d.ts +16 -0
- package/dist/helpers/to-dropzone-accept.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +5 -4
- 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.map +1 -1
- package/dist/hooks/use-fetch-download.d.ts.map +1 -1
- package/dist/hooks/use-file-intake.d.ts +46 -0
- package/dist/hooks/use-file-intake.d.ts.map +1 -0
- package/dist/hooks/use-file-upload.d.ts +76 -0
- package/dist/hooks/use-file-upload.d.ts.map +1 -0
- package/dist/hooks/use-multi-file-upload.d.ts +39 -0
- package/dist/hooks/use-multi-file-upload.d.ts.map +1 -0
- package/dist/hooks/use-multi-upload.d.ts +44 -22
- package/dist/hooks/use-multi-upload.d.ts.map +1 -1
- package/dist/hooks/use-upload.d.ts +51 -67
- package/dist/hooks/use-upload.d.ts.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +503 -247
- package/dist/index.js.map +1 -1
- package/dist/store/create-hook-store.d.ts +48 -0
- package/dist/store/create-hook-store.d.ts.map +1 -0
- package/dist/store/memory-store.d.ts +1 -1
- package/dist/translations/types.d.ts +1 -5
- package/dist/translations/types.d.ts.map +1 -1
- package/dist/types/multi-upload.d.ts +7 -0
- package/dist/types/multi-upload.d.ts.map +1 -1
- package/dist/types/upload.d.ts +15 -1
- package/dist/types/upload.d.ts.map +1 -1
- package/package.json +6 -3
- package/dist/hooks/use-multi-upload-controls.d.ts +0 -45
- package/dist/hooks/use-multi-upload-controls.d.ts.map +0 -1
- package/dist/hooks/use-upload-controls.d.ts +0 -54
- package/dist/hooks/use-upload-controls.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { DimahS3Error, createS3Client as createS3Client$1, formatFileSize, S3_ERROR_CODES, validateFile, parseFileName } from '@dimah-s3/core';
|
|
2
|
-
import { createContext, useContext, useCallback,
|
|
2
|
+
import { createContext, useContext, useCallback, useRef, useEffect, useState, useLayoutEffect } from 'react';
|
|
3
3
|
import { TranslationProvider, useTranslations } from '@fuma-translate/react';
|
|
4
4
|
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
import { produce } from 'immer';
|
|
6
|
+
import { useStore } from 'zustand';
|
|
7
|
+
import { useShallow } from 'zustand/react/shallow';
|
|
8
|
+
import { createStore } from 'zustand/vanilla';
|
|
9
|
+
import { useDropzone } from 'react-dropzone';
|
|
5
10
|
|
|
6
11
|
var S3UploadError = class extends DimahS3Error {
|
|
7
12
|
code;
|
|
@@ -754,6 +759,35 @@ function createThrottledSpeedUpdater(tracker, intervalMs = 500) {
|
|
|
754
759
|
}
|
|
755
760
|
};
|
|
756
761
|
}
|
|
762
|
+
|
|
763
|
+
// src/helpers/file-preview.ts
|
|
764
|
+
function createImagePreviewUrl(file) {
|
|
765
|
+
if (!file.type.startsWith("image/")) return null;
|
|
766
|
+
return URL.createObjectURL(file);
|
|
767
|
+
}
|
|
768
|
+
function revokePreviewUrl(url) {
|
|
769
|
+
if (!url) return;
|
|
770
|
+
URL.revokeObjectURL(url);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// src/helpers/to-dropzone-accept.ts
|
|
774
|
+
function toDropzoneAccept(accept) {
|
|
775
|
+
if (accept == null || accept.length === 0) return void 0;
|
|
776
|
+
const map = {};
|
|
777
|
+
const anyMime = "*/*";
|
|
778
|
+
for (const raw of accept) {
|
|
779
|
+
const item = raw.trim();
|
|
780
|
+
if (!item) continue;
|
|
781
|
+
if (item.includes("/")) {
|
|
782
|
+
map[item] ??= [];
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
const ext = item.startsWith(".") ? item.toLowerCase() : `.${item.toLowerCase()}`;
|
|
786
|
+
map[anyMime] ??= [];
|
|
787
|
+
if (!map[anyMime].includes(ext)) map[anyMime].push(ext);
|
|
788
|
+
}
|
|
789
|
+
return Object.keys(map).length > 0 ? map : void 0;
|
|
790
|
+
}
|
|
757
791
|
function useLiveRef(value) {
|
|
758
792
|
const ref = useRef(value);
|
|
759
793
|
useLayoutEffect(() => {
|
|
@@ -761,8 +795,24 @@ function useLiveRef(value) {
|
|
|
761
795
|
});
|
|
762
796
|
return ref;
|
|
763
797
|
}
|
|
798
|
+
function createHookStore(initialState) {
|
|
799
|
+
return createStore()(() => structuredClone(initialState));
|
|
800
|
+
}
|
|
801
|
+
function useHookStoreInstance(initialState) {
|
|
802
|
+
const [store] = useState(() => createHookStore(initialState));
|
|
803
|
+
return store;
|
|
804
|
+
}
|
|
805
|
+
function useHookStoreShallow(store, selector) {
|
|
806
|
+
return useStore(store, useShallow(selector));
|
|
807
|
+
}
|
|
808
|
+
function patchHookState(store, recipe) {
|
|
809
|
+
store.setState(produce(store.getState(), recipe));
|
|
810
|
+
}
|
|
811
|
+
function replaceHookState(store, next) {
|
|
812
|
+
store.setState(structuredClone(next), true);
|
|
813
|
+
}
|
|
764
814
|
|
|
765
|
-
// src/hooks/use-upload.ts
|
|
815
|
+
// src/hooks/use-file-upload.ts
|
|
766
816
|
var INITIAL_PROGRESS = { loaded: 0, total: 0, percent: 0 };
|
|
767
817
|
var INITIAL_STATE = {
|
|
768
818
|
phase: "idle",
|
|
@@ -770,10 +820,22 @@ var INITIAL_STATE = {
|
|
|
770
820
|
error: null,
|
|
771
821
|
result: null,
|
|
772
822
|
fileName: null,
|
|
773
|
-
fileSize: null
|
|
823
|
+
fileSize: null,
|
|
824
|
+
fileType: null,
|
|
825
|
+
previewUrl: null
|
|
774
826
|
};
|
|
775
|
-
function
|
|
776
|
-
const
|
|
827
|
+
function useFileUpload(options) {
|
|
828
|
+
const store = useHookStoreInstance(INITIAL_STATE);
|
|
829
|
+
const state = useHookStoreShallow(store, (s) => ({
|
|
830
|
+
phase: s.phase,
|
|
831
|
+
progress: s.progress,
|
|
832
|
+
error: s.error,
|
|
833
|
+
result: s.result,
|
|
834
|
+
fileName: s.fileName,
|
|
835
|
+
fileSize: s.fileSize,
|
|
836
|
+
fileType: s.fileType,
|
|
837
|
+
previewUrl: s.previewUrl
|
|
838
|
+
}));
|
|
777
839
|
const contextApi = useContext(S3Context);
|
|
778
840
|
const formatValidateFileError = useFormatValidateFileError();
|
|
779
841
|
const optsRef = useLiveRef(options);
|
|
@@ -781,22 +843,39 @@ function useUpload(options) {
|
|
|
781
843
|
const abortRef = useRef(null);
|
|
782
844
|
const activeUploadRef = useRef(null);
|
|
783
845
|
const detachingRef = useRef(false);
|
|
846
|
+
const previewUrlRef = useRef(null);
|
|
784
847
|
const speedUpdaterRef = useRef(
|
|
785
848
|
createThrottledSpeedUpdater(createSpeedTracker())
|
|
786
849
|
);
|
|
850
|
+
const revokeCurrentPreview = useCallback(() => {
|
|
851
|
+
revokePreviewUrl(previewUrlRef.current);
|
|
852
|
+
previewUrlRef.current = null;
|
|
853
|
+
}, []);
|
|
854
|
+
const clearToIdle = useCallback(() => {
|
|
855
|
+
revokeCurrentPreview();
|
|
856
|
+
replaceHookState(store, INITIAL_STATE);
|
|
857
|
+
}, [revokeCurrentPreview, store]);
|
|
858
|
+
useEffect(() => () => revokeCurrentPreview(), [revokeCurrentPreview]);
|
|
787
859
|
const upload = useCallback(
|
|
788
860
|
async (file, objectKey, requestOptions) => {
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
861
|
+
revokeCurrentPreview();
|
|
862
|
+
const previewUrl = createImagePreviewUrl(file);
|
|
863
|
+
previewUrlRef.current = previewUrl;
|
|
864
|
+
patchHookState(store, (draft) => {
|
|
865
|
+
draft.phase = "validating";
|
|
866
|
+
draft.progress = { ...INITIAL_PROGRESS };
|
|
867
|
+
draft.error = null;
|
|
868
|
+
draft.result = null;
|
|
869
|
+
draft.fileName = file.name;
|
|
870
|
+
draft.fileSize = file.size;
|
|
871
|
+
draft.fileType = file.type;
|
|
872
|
+
draft.previewUrl = previewUrl;
|
|
794
873
|
});
|
|
795
874
|
const opts = optsRef.current;
|
|
796
875
|
const api = opts.api ?? apiRef.current;
|
|
797
876
|
if (!api)
|
|
798
877
|
throw new Error(
|
|
799
|
-
"[dimah-s3] No S3Api found. Pass `api` to
|
|
878
|
+
"[dimah-s3] No S3Api found. Pass `api` to useFileUpload or wrap with <S3Provider>."
|
|
800
879
|
);
|
|
801
880
|
const validationError = validateFile(file, {
|
|
802
881
|
accept: opts.accept,
|
|
@@ -804,24 +883,28 @@ function useUpload(options) {
|
|
|
804
883
|
});
|
|
805
884
|
if (validationError) {
|
|
806
885
|
const message = formatValidateFileError(validationError);
|
|
807
|
-
|
|
886
|
+
patchHookState(store, (draft) => {
|
|
887
|
+
draft.phase = "error";
|
|
888
|
+
draft.error = message;
|
|
889
|
+
});
|
|
808
890
|
opts.onError?.(file, new Error(message), "validating");
|
|
809
891
|
return;
|
|
810
892
|
}
|
|
811
893
|
if (opts.beforeUpload) {
|
|
812
894
|
const allowed = await opts.beforeUpload(file);
|
|
813
895
|
if (!allowed) {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
}));
|
|
896
|
+
patchHookState(store, (draft) => {
|
|
897
|
+
draft.phase = "error";
|
|
898
|
+
draft.error = "Upload blocked by beforeUpload hook";
|
|
899
|
+
});
|
|
819
900
|
opts.onError?.(file, new Error("blocked"), "validating");
|
|
820
901
|
return;
|
|
821
902
|
}
|
|
822
903
|
}
|
|
823
904
|
speedUpdaterRef.current.reset();
|
|
824
|
-
|
|
905
|
+
patchHookState(store, (draft) => {
|
|
906
|
+
draft.phase = "presigning";
|
|
907
|
+
});
|
|
825
908
|
opts.onUploadStart?.(file, objectKey);
|
|
826
909
|
const controller = new AbortController();
|
|
827
910
|
abortRef.current = controller;
|
|
@@ -848,10 +931,14 @@ function useUpload(options) {
|
|
|
848
931
|
{
|
|
849
932
|
onProgress: (progress) => {
|
|
850
933
|
const p = speedUpdaterRef.current.apply(progress);
|
|
851
|
-
|
|
934
|
+
patchHookState(store, (draft) => {
|
|
935
|
+
draft.progress = p;
|
|
936
|
+
});
|
|
852
937
|
opts.onProgress?.(file, p);
|
|
853
938
|
},
|
|
854
|
-
onPhaseChange: (phase) =>
|
|
939
|
+
onPhaseChange: (phase) => patchHookState(store, (draft) => {
|
|
940
|
+
draft.phase = phase;
|
|
941
|
+
}),
|
|
855
942
|
onPartUpload: (partNumber, totalParts) => opts.onPartUpload?.(file, partNumber, totalParts),
|
|
856
943
|
onMultipartInit: (uploadId, serverKey) => {
|
|
857
944
|
if (activeUploadRef.current) {
|
|
@@ -864,12 +951,15 @@ function useUpload(options) {
|
|
|
864
951
|
controller.signal,
|
|
865
952
|
requestOptions
|
|
866
953
|
);
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
954
|
+
patchHookState(store, (draft) => {
|
|
955
|
+
draft.phase = "success";
|
|
956
|
+
draft.result = result;
|
|
957
|
+
draft.progress = {
|
|
958
|
+
loaded: file.size,
|
|
959
|
+
total: file.size,
|
|
960
|
+
percent: 100
|
|
961
|
+
};
|
|
962
|
+
});
|
|
873
963
|
await opts.onSuccess?.(file, result);
|
|
874
964
|
} catch (err) {
|
|
875
965
|
if (err.name === "AbortError") {
|
|
@@ -878,18 +968,28 @@ function useUpload(options) {
|
|
|
878
968
|
return;
|
|
879
969
|
}
|
|
880
970
|
opts.onCancel?.(file);
|
|
881
|
-
|
|
971
|
+
clearToIdle();
|
|
882
972
|
return;
|
|
883
973
|
}
|
|
884
974
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
885
|
-
|
|
975
|
+
patchHookState(store, (draft) => {
|
|
976
|
+
draft.phase = "error";
|
|
977
|
+
draft.error = message;
|
|
978
|
+
});
|
|
886
979
|
opts.onError?.(file, err, "uploading");
|
|
887
980
|
} finally {
|
|
888
981
|
abortRef.current = null;
|
|
889
982
|
activeUploadRef.current = null;
|
|
890
983
|
}
|
|
891
984
|
},
|
|
892
|
-
[
|
|
985
|
+
[
|
|
986
|
+
apiRef,
|
|
987
|
+
optsRef,
|
|
988
|
+
formatValidateFileError,
|
|
989
|
+
revokeCurrentPreview,
|
|
990
|
+
clearToIdle,
|
|
991
|
+
store
|
|
992
|
+
]
|
|
893
993
|
);
|
|
894
994
|
const cancel = useCallback(() => {
|
|
895
995
|
const opts = optsRef.current;
|
|
@@ -898,9 +998,9 @@ function useUpload(options) {
|
|
|
898
998
|
abortRef.current?.abort();
|
|
899
999
|
if (active && api) {
|
|
900
1000
|
const { objectKey, serverKey, uploadId, bucket } = active;
|
|
901
|
-
const
|
|
902
|
-
if (
|
|
903
|
-
void Promise.resolve(
|
|
1001
|
+
const storeOpt = opts.uploadStore;
|
|
1002
|
+
if (storeOpt != null && storeOpt !== false) {
|
|
1003
|
+
void Promise.resolve(storeOpt.delete(objectKey)).catch(() => {
|
|
904
1004
|
});
|
|
905
1005
|
}
|
|
906
1006
|
if (uploadId) {
|
|
@@ -908,19 +1008,19 @@ function useUpload(options) {
|
|
|
908
1008
|
});
|
|
909
1009
|
}
|
|
910
1010
|
}
|
|
911
|
-
|
|
912
|
-
}, [apiRef, optsRef]);
|
|
1011
|
+
clearToIdle();
|
|
1012
|
+
}, [apiRef, optsRef, clearToIdle]);
|
|
913
1013
|
const detach = useCallback(() => {
|
|
914
1014
|
const active = activeUploadRef.current;
|
|
915
1015
|
if (!active) return;
|
|
916
1016
|
detachingRef.current = true;
|
|
917
1017
|
abortRef.current?.abort();
|
|
918
|
-
|
|
919
|
-
}, []);
|
|
1018
|
+
clearToIdle();
|
|
1019
|
+
}, [clearToIdle]);
|
|
920
1020
|
const reset = useCallback(() => {
|
|
921
1021
|
abortRef.current?.abort();
|
|
922
|
-
|
|
923
|
-
}, []);
|
|
1022
|
+
clearToIdle();
|
|
1023
|
+
}, [clearToIdle]);
|
|
924
1024
|
return { ...state, upload, cancel, detach, reset };
|
|
925
1025
|
}
|
|
926
1026
|
var INITIAL_PROGRESS2 = { loaded: 0, total: 0, percent: 0 };
|
|
@@ -933,8 +1033,14 @@ var INITIAL_STATE2 = {
|
|
|
933
1033
|
function generateId() {
|
|
934
1034
|
return crypto.randomUUID();
|
|
935
1035
|
}
|
|
936
|
-
function
|
|
937
|
-
const
|
|
1036
|
+
function useMultiFileUpload(options) {
|
|
1037
|
+
const store = useHookStoreInstance(INITIAL_STATE2);
|
|
1038
|
+
const state = useHookStoreShallow(store, (s) => ({
|
|
1039
|
+
phase: s.phase,
|
|
1040
|
+
files: s.files,
|
|
1041
|
+
totalProgress: s.totalProgress,
|
|
1042
|
+
error: s.error
|
|
1043
|
+
}));
|
|
938
1044
|
const contextApi = useContext(S3Context);
|
|
939
1045
|
const formatValidateFileError = useFormatValidateFileError();
|
|
940
1046
|
const optsRef = useLiveRef(options);
|
|
@@ -944,25 +1050,41 @@ function useMultiUpload(options) {
|
|
|
944
1050
|
const detachingRef = useRef(false);
|
|
945
1051
|
const fileMapRef = useRef(/* @__PURE__ */ new Map());
|
|
946
1052
|
const activeUploadsRef = useRef(/* @__PURE__ */ new Map());
|
|
1053
|
+
const previewUrlsRef = useRef([]);
|
|
947
1054
|
const fileSpeedUpdatersRef = useRef(/* @__PURE__ */ new Map());
|
|
948
1055
|
const totalSpeedUpdaterRef = useRef(
|
|
949
1056
|
createThrottledSpeedUpdater(createSpeedTracker(), 1e3)
|
|
950
1057
|
);
|
|
1058
|
+
const revokeAllPreviews = useCallback(() => {
|
|
1059
|
+
for (const url of previewUrlsRef.current) revokePreviewUrl(url);
|
|
1060
|
+
previewUrlsRef.current = [];
|
|
1061
|
+
}, []);
|
|
1062
|
+
const clearToIdle = useCallback(() => {
|
|
1063
|
+
revokeAllPreviews();
|
|
1064
|
+
replaceHookState(store, INITIAL_STATE2);
|
|
1065
|
+
}, [revokeAllPreviews, store]);
|
|
1066
|
+
useEffect(() => () => revokeAllPreviews(), [revokeAllPreviews]);
|
|
951
1067
|
const upload = useCallback(
|
|
952
1068
|
async (files, resolveKey) => {
|
|
953
1069
|
const opts = optsRef.current;
|
|
954
1070
|
const api = opts.api ?? apiRef.current;
|
|
955
1071
|
if (!api)
|
|
956
1072
|
throw new Error(
|
|
957
|
-
"[dimah-s3] No S3Api found. Pass `api` to
|
|
1073
|
+
"[dimah-s3] No S3Api found. Pass `api` to useMultiFileUpload or wrap with <S3Provider>."
|
|
958
1074
|
);
|
|
959
1075
|
const items = [];
|
|
960
1076
|
const fileStates = [];
|
|
961
1077
|
const fileMap = /* @__PURE__ */ new Map();
|
|
962
|
-
|
|
1078
|
+
patchHookState(store, (draft) => {
|
|
1079
|
+
draft.phase = "validating";
|
|
1080
|
+
draft.error = null;
|
|
1081
|
+
});
|
|
963
1082
|
if (opts.maxFiles && files.length > opts.maxFiles) {
|
|
964
1083
|
const msg = `Too many files. Maximum is ${opts.maxFiles}.`;
|
|
965
|
-
|
|
1084
|
+
patchHookState(store, (draft) => {
|
|
1085
|
+
draft.phase = "error";
|
|
1086
|
+
draft.error = msg;
|
|
1087
|
+
});
|
|
966
1088
|
opts.onError?.(new Error(msg));
|
|
967
1089
|
return;
|
|
968
1090
|
}
|
|
@@ -974,7 +1096,10 @@ function useMultiUpload(options) {
|
|
|
974
1096
|
if (validationError) {
|
|
975
1097
|
const detail = formatValidateFileError(validationError);
|
|
976
1098
|
const msg = `${file.name}: ${detail}`;
|
|
977
|
-
|
|
1099
|
+
patchHookState(store, (draft) => {
|
|
1100
|
+
draft.phase = "error";
|
|
1101
|
+
draft.error = msg;
|
|
1102
|
+
});
|
|
978
1103
|
opts.onError?.(new Error(msg));
|
|
979
1104
|
return;
|
|
980
1105
|
}
|
|
@@ -982,29 +1107,35 @@ function useMultiUpload(options) {
|
|
|
982
1107
|
if (opts.beforeUpload) {
|
|
983
1108
|
const allowed = await opts.beforeUpload(files);
|
|
984
1109
|
if (!allowed) {
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
}));
|
|
1110
|
+
patchHookState(store, (draft) => {
|
|
1111
|
+
draft.phase = "error";
|
|
1112
|
+
draft.error = "Upload blocked by beforeUpload hook";
|
|
1113
|
+
});
|
|
990
1114
|
opts.onError?.(new Error("blocked"));
|
|
991
1115
|
return;
|
|
992
1116
|
}
|
|
993
1117
|
}
|
|
1118
|
+
revokeAllPreviews();
|
|
1119
|
+
const nextPreviewUrls = [];
|
|
994
1120
|
for (const file of files) {
|
|
995
1121
|
const id = generateId();
|
|
996
1122
|
const objectKey = resolveKey(file);
|
|
1123
|
+
const previewUrl = createImagePreviewUrl(file);
|
|
1124
|
+
if (previewUrl) nextPreviewUrls.push(previewUrl);
|
|
997
1125
|
items.push({ id, file, objectKey });
|
|
998
1126
|
fileMap.set(id, file);
|
|
999
1127
|
fileStates.push({
|
|
1000
1128
|
id,
|
|
1001
1129
|
fileName: file.name,
|
|
1002
1130
|
fileSize: file.size,
|
|
1131
|
+
fileType: file.type,
|
|
1132
|
+
previewUrl,
|
|
1003
1133
|
status: "pending",
|
|
1004
1134
|
progress: { loaded: 0, total: file.size, percent: 0 },
|
|
1005
1135
|
error: null
|
|
1006
1136
|
});
|
|
1007
1137
|
}
|
|
1138
|
+
previewUrlsRef.current = nextPreviewUrls;
|
|
1008
1139
|
fileMapRef.current = fileMap;
|
|
1009
1140
|
activeUploadsRef.current = new Map(
|
|
1010
1141
|
items.map((item) => [
|
|
@@ -1016,15 +1147,15 @@ function useMultiUpload(options) {
|
|
|
1016
1147
|
}
|
|
1017
1148
|
])
|
|
1018
1149
|
);
|
|
1019
|
-
|
|
1020
|
-
phase
|
|
1021
|
-
files
|
|
1022
|
-
totalProgress
|
|
1150
|
+
patchHookState(store, (draft) => {
|
|
1151
|
+
draft.phase = "uploading";
|
|
1152
|
+
draft.files = fileStates;
|
|
1153
|
+
draft.totalProgress = {
|
|
1023
1154
|
loaded: 0,
|
|
1024
1155
|
total: files.reduce((s, f) => s + f.size, 0),
|
|
1025
1156
|
percent: 0
|
|
1026
|
-
}
|
|
1027
|
-
error
|
|
1157
|
+
};
|
|
1158
|
+
draft.error = null;
|
|
1028
1159
|
});
|
|
1029
1160
|
opts.onUploadStart?.(files);
|
|
1030
1161
|
fileSpeedUpdatersRef.current.clear();
|
|
@@ -1054,47 +1185,48 @@ function useMultiUpload(options) {
|
|
|
1054
1185
|
onFileProgress: (id, progress) => {
|
|
1055
1186
|
const updater = fileSpeedUpdatersRef.current.get(id);
|
|
1056
1187
|
const p = updater ? updater.apply(progress) : progress;
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1188
|
+
patchHookState(store, (draft) => {
|
|
1189
|
+
const file2 = draft.files.find((f) => f.id === id);
|
|
1190
|
+
if (file2) {
|
|
1191
|
+
file2.status = "uploading";
|
|
1192
|
+
file2.progress = p;
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1063
1195
|
const file = fileMap.get(id);
|
|
1064
1196
|
if (file) opts.onFileProgress?.(file, p);
|
|
1065
1197
|
},
|
|
1066
1198
|
onFileSuccess: (id, result) => {
|
|
1067
1199
|
activeUploadsRef.current.delete(id);
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
} : f
|
|
1080
|
-
)
|
|
1081
|
-
}));
|
|
1200
|
+
patchHookState(store, (draft) => {
|
|
1201
|
+
const file2 = draft.files.find((f) => f.id === id);
|
|
1202
|
+
if (file2) {
|
|
1203
|
+
file2.status = "success";
|
|
1204
|
+
file2.progress = {
|
|
1205
|
+
loaded: file2.fileSize,
|
|
1206
|
+
total: file2.fileSize,
|
|
1207
|
+
percent: 100
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
});
|
|
1082
1211
|
const file = fileMap.get(id);
|
|
1083
1212
|
if (file) opts.onFileSuccess?.(file, result);
|
|
1084
1213
|
},
|
|
1085
1214
|
onFileError: (id, error) => {
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1215
|
+
patchHookState(store, (draft) => {
|
|
1216
|
+
const file2 = draft.files.find((f) => f.id === id);
|
|
1217
|
+
if (file2) {
|
|
1218
|
+
file2.status = "error";
|
|
1219
|
+
file2.error = error;
|
|
1220
|
+
}
|
|
1221
|
+
});
|
|
1092
1222
|
const file = fileMap.get(id);
|
|
1093
1223
|
if (file) opts.onFileError?.(file, error);
|
|
1094
1224
|
},
|
|
1095
1225
|
onTotalProgress: (progress) => {
|
|
1096
1226
|
const p = totalSpeedUpdaterRef.current.apply(progress);
|
|
1097
|
-
|
|
1227
|
+
patchHookState(store, (draft) => {
|
|
1228
|
+
draft.totalProgress = p;
|
|
1229
|
+
});
|
|
1098
1230
|
opts.onProgress?.(p);
|
|
1099
1231
|
},
|
|
1100
1232
|
onMultipartInit: (id, uploadId, serverKey) => {
|
|
@@ -1114,16 +1246,17 @@ function useMultiUpload(options) {
|
|
|
1114
1246
|
);
|
|
1115
1247
|
const hasErrors = results.some((r) => r.status === "error");
|
|
1116
1248
|
const successResults = results.filter((r) => r.result !== null).map((r) => r.result);
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1249
|
+
patchHookState(store, (draft) => {
|
|
1250
|
+
draft.phase = hasErrors ? "error" : "success";
|
|
1251
|
+
draft.error = hasErrors ? `${results.filter((r) => r.status === "error").length} file(s) failed` : null;
|
|
1252
|
+
if (!hasErrors) {
|
|
1253
|
+
draft.totalProgress = {
|
|
1254
|
+
loaded: draft.totalProgress.total,
|
|
1255
|
+
total: draft.totalProgress.total,
|
|
1256
|
+
percent: 100
|
|
1257
|
+
};
|
|
1125
1258
|
}
|
|
1126
|
-
})
|
|
1259
|
+
});
|
|
1127
1260
|
if (!hasErrors) {
|
|
1128
1261
|
await opts.onSuccess?.(successResults);
|
|
1129
1262
|
}
|
|
@@ -1135,29 +1268,41 @@ function useMultiUpload(options) {
|
|
|
1135
1268
|
}
|
|
1136
1269
|
if (!resettingRef.current) opts.onCancel?.();
|
|
1137
1270
|
resettingRef.current = false;
|
|
1138
|
-
|
|
1271
|
+
clearToIdle();
|
|
1139
1272
|
return;
|
|
1140
1273
|
}
|
|
1141
1274
|
const message = err instanceof Error ? err.message : "Upload failed";
|
|
1142
|
-
|
|
1275
|
+
patchHookState(store, (draft) => {
|
|
1276
|
+
draft.phase = "error";
|
|
1277
|
+
draft.error = message;
|
|
1278
|
+
});
|
|
1143
1279
|
opts.onError?.(err);
|
|
1144
1280
|
} finally {
|
|
1145
1281
|
abortRef.current = null;
|
|
1146
1282
|
activeUploadsRef.current.clear();
|
|
1147
1283
|
}
|
|
1148
1284
|
},
|
|
1149
|
-
[
|
|
1285
|
+
[
|
|
1286
|
+
apiRef,
|
|
1287
|
+
optsRef,
|
|
1288
|
+
formatValidateFileError,
|
|
1289
|
+
revokeAllPreviews,
|
|
1290
|
+
clearToIdle,
|
|
1291
|
+
store
|
|
1292
|
+
]
|
|
1150
1293
|
);
|
|
1151
1294
|
const cancel = useCallback(() => {
|
|
1152
1295
|
const opts = optsRef.current;
|
|
1153
1296
|
const api = opts.api ?? apiRef.current;
|
|
1154
1297
|
abortRef.current?.abort();
|
|
1155
1298
|
if (api) {
|
|
1156
|
-
const
|
|
1299
|
+
const uploadStore = opts.uploadStore;
|
|
1157
1300
|
for (const active of activeUploadsRef.current.values()) {
|
|
1158
|
-
if (
|
|
1159
|
-
void Promise.resolve(
|
|
1160
|
-
|
|
1301
|
+
if (uploadStore != null && uploadStore !== false) {
|
|
1302
|
+
void Promise.resolve(uploadStore.delete(active.objectKey)).catch(
|
|
1303
|
+
() => {
|
|
1304
|
+
}
|
|
1305
|
+
);
|
|
1161
1306
|
}
|
|
1162
1307
|
if (active.uploadId) {
|
|
1163
1308
|
api.multipart.abort({
|
|
@@ -1170,113 +1315,178 @@ function useMultiUpload(options) {
|
|
|
1170
1315
|
}
|
|
1171
1316
|
}
|
|
1172
1317
|
activeUploadsRef.current.clear();
|
|
1173
|
-
|
|
1174
|
-
}, [apiRef, optsRef]);
|
|
1318
|
+
clearToIdle();
|
|
1319
|
+
}, [apiRef, optsRef, clearToIdle]);
|
|
1175
1320
|
const detach = useCallback(() => {
|
|
1176
1321
|
if (activeUploadsRef.current.size === 0) return;
|
|
1177
1322
|
detachingRef.current = true;
|
|
1178
1323
|
abortRef.current?.abort();
|
|
1179
|
-
|
|
1180
|
-
}, []);
|
|
1324
|
+
clearToIdle();
|
|
1325
|
+
}, [clearToIdle]);
|
|
1181
1326
|
const reset = useCallback(() => {
|
|
1182
1327
|
resettingRef.current = true;
|
|
1183
1328
|
abortRef.current?.abort();
|
|
1184
|
-
|
|
1185
|
-
}, []);
|
|
1329
|
+
clearToIdle();
|
|
1330
|
+
}, [clearToIdle]);
|
|
1186
1331
|
return { ...state, upload, cancel, detach, reset };
|
|
1187
1332
|
}
|
|
1188
|
-
function
|
|
1189
|
-
const
|
|
1190
|
-
const
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1333
|
+
function useFileIntake(options) {
|
|
1334
|
+
const optsRef = useLiveRef(options);
|
|
1335
|
+
const onDropAccepted = useCallback(
|
|
1336
|
+
(files) => {
|
|
1337
|
+
optsRef.current.onAccept(files);
|
|
1338
|
+
},
|
|
1339
|
+
[optsRef]
|
|
1340
|
+
);
|
|
1341
|
+
const onDropRejected = useCallback(
|
|
1342
|
+
(rejections) => {
|
|
1343
|
+
optsRef.current.onReject?.(rejections);
|
|
1344
|
+
},
|
|
1345
|
+
[optsRef]
|
|
1346
|
+
);
|
|
1347
|
+
const {
|
|
1348
|
+
getRootProps,
|
|
1349
|
+
getInputProps,
|
|
1350
|
+
open,
|
|
1351
|
+
isDragActive,
|
|
1352
|
+
isDragAccept,
|
|
1353
|
+
isDragReject,
|
|
1354
|
+
fileRejections
|
|
1355
|
+
} = useDropzone({
|
|
1356
|
+
accept: toDropzoneAccept(options.accept),
|
|
1357
|
+
maxSize: options.maxFileSize,
|
|
1358
|
+
maxFiles: options.maxFiles,
|
|
1359
|
+
multiple: options.multiple ?? false,
|
|
1360
|
+
disabled: options.disabled,
|
|
1361
|
+
noClick: options.noClick,
|
|
1362
|
+
noDrag: options.noDrag,
|
|
1363
|
+
noKeyboard: options.noKeyboard,
|
|
1364
|
+
onDropAccepted,
|
|
1365
|
+
onDropRejected
|
|
1366
|
+
});
|
|
1367
|
+
return {
|
|
1368
|
+
getRootProps,
|
|
1369
|
+
getInputProps,
|
|
1370
|
+
open,
|
|
1371
|
+
isDragActive,
|
|
1372
|
+
isDragAccept,
|
|
1373
|
+
isDragReject,
|
|
1374
|
+
fileRejections
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
// src/hooks/use-upload.ts
|
|
1379
|
+
function useUpload(options) {
|
|
1380
|
+
const {
|
|
1381
|
+
objectKey,
|
|
1382
|
+
uploadOptions,
|
|
1383
|
+
getUploadOptions,
|
|
1384
|
+
disabled,
|
|
1385
|
+
noDrag,
|
|
1386
|
+
noClick,
|
|
1387
|
+
noKeyboard,
|
|
1388
|
+
onFileReject,
|
|
1389
|
+
...uploadOpts
|
|
1390
|
+
} = options;
|
|
1391
|
+
const single = useFileUpload(uploadOpts);
|
|
1392
|
+
const resolveKey = (file) => typeof objectKey === "function" ? objectKey(file) : objectKey;
|
|
1393
|
+
const handleFiles = (files) => {
|
|
1394
|
+
const list = files == null ? [] : Array.from(files);
|
|
1395
|
+
const file = list[0];
|
|
1195
1396
|
if (!file) return;
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
...
|
|
1199
|
-
...options.getUploadOptions?.(file)
|
|
1397
|
+
void single.upload(file, resolveKey(file), {
|
|
1398
|
+
...uploadOptions,
|
|
1399
|
+
...getUploadOptions?.(file)
|
|
1200
1400
|
});
|
|
1201
1401
|
};
|
|
1402
|
+
const isUploading = single.phase === "uploading";
|
|
1403
|
+
const intake = useFileIntake({
|
|
1404
|
+
accept: options.accept,
|
|
1405
|
+
maxFileSize: options.maxFileSize,
|
|
1406
|
+
maxFiles: 1,
|
|
1407
|
+
multiple: false,
|
|
1408
|
+
disabled: Boolean(disabled) || isUploading,
|
|
1409
|
+
noDrag,
|
|
1410
|
+
noClick,
|
|
1411
|
+
noKeyboard,
|
|
1412
|
+
onAccept: (files) => handleFiles(files),
|
|
1413
|
+
onReject: onFileReject
|
|
1414
|
+
});
|
|
1415
|
+
const fileInfo = single.fileName != null ? {
|
|
1416
|
+
name: single.fileName,
|
|
1417
|
+
size: single.fileSize ?? 0,
|
|
1418
|
+
type: single.fileType ?? "",
|
|
1419
|
+
previewUrl: single.previewUrl
|
|
1420
|
+
} : null;
|
|
1202
1421
|
return {
|
|
1203
1422
|
phase: single.phase,
|
|
1204
1423
|
fileInfo,
|
|
1205
1424
|
progress: single.progress,
|
|
1206
1425
|
error: single.error,
|
|
1207
|
-
isUploading
|
|
1426
|
+
isUploading,
|
|
1208
1427
|
handleFiles,
|
|
1209
|
-
|
|
1428
|
+
open: intake.open,
|
|
1210
1429
|
cancel: single.cancel,
|
|
1211
1430
|
detach: single.detach,
|
|
1212
|
-
reset:
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
accept: options.accept?.join(","),
|
|
1220
|
-
hidden: true,
|
|
1221
|
-
onChange: (e) => {
|
|
1222
|
-
handleFiles(e.target.files);
|
|
1223
|
-
e.target.value = "";
|
|
1224
|
-
}
|
|
1225
|
-
},
|
|
1226
|
-
dropHandlers: {
|
|
1227
|
-
onDragOver: (e) => {
|
|
1228
|
-
e.preventDefault();
|
|
1229
|
-
e.stopPropagation();
|
|
1230
|
-
},
|
|
1231
|
-
onDrop: (e) => {
|
|
1232
|
-
e.preventDefault();
|
|
1233
|
-
e.stopPropagation();
|
|
1234
|
-
if (single.phase !== "uploading") handleFiles(e.dataTransfer.files);
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1431
|
+
reset: single.reset,
|
|
1432
|
+
getRootProps: intake.getRootProps,
|
|
1433
|
+
getInputProps: intake.getInputProps,
|
|
1434
|
+
isDragActive: intake.isDragActive,
|
|
1435
|
+
isDragAccept: intake.isDragAccept,
|
|
1436
|
+
isDragReject: intake.isDragReject,
|
|
1437
|
+
fileRejections: intake.fileRejections
|
|
1237
1438
|
};
|
|
1238
1439
|
}
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
const
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1440
|
+
|
|
1441
|
+
// src/hooks/use-multi-upload.ts
|
|
1442
|
+
function useMultiUpload(options) {
|
|
1443
|
+
const {
|
|
1444
|
+
objectKey,
|
|
1445
|
+
disabled,
|
|
1446
|
+
noDrag,
|
|
1447
|
+
noClick,
|
|
1448
|
+
noKeyboard,
|
|
1449
|
+
onFileReject,
|
|
1450
|
+
...multiOpts
|
|
1451
|
+
} = options;
|
|
1452
|
+
const multi = useMultiFileUpload(multiOpts);
|
|
1453
|
+
const resolveKey = (file) => typeof objectKey === "function" ? objectKey(file) : objectKey;
|
|
1454
|
+
const handleFiles = (files) => {
|
|
1455
|
+
if (files == null) return;
|
|
1456
|
+
const list = Array.from(files);
|
|
1457
|
+
if (list.length === 0) return;
|
|
1458
|
+
void multi.upload(list, resolveKey);
|
|
1246
1459
|
};
|
|
1460
|
+
const isUploading = multi.phase === "uploading";
|
|
1461
|
+
const intake = useFileIntake({
|
|
1462
|
+
accept: options.accept,
|
|
1463
|
+
maxFileSize: options.maxFileSize,
|
|
1464
|
+
maxFiles: options.maxFiles,
|
|
1465
|
+
multiple: true,
|
|
1466
|
+
disabled: Boolean(disabled) || isUploading,
|
|
1467
|
+
noDrag,
|
|
1468
|
+
noClick,
|
|
1469
|
+
noKeyboard,
|
|
1470
|
+
onAccept: (files) => handleFiles(files),
|
|
1471
|
+
onReject: onFileReject
|
|
1472
|
+
});
|
|
1247
1473
|
return {
|
|
1248
1474
|
phase: multi.phase,
|
|
1249
1475
|
files: multi.files,
|
|
1250
1476
|
totalProgress: multi.totalProgress,
|
|
1251
1477
|
error: multi.error,
|
|
1252
|
-
isUploading
|
|
1478
|
+
isUploading,
|
|
1253
1479
|
handleFiles,
|
|
1254
|
-
|
|
1480
|
+
open: intake.open,
|
|
1255
1481
|
cancel: multi.cancel,
|
|
1256
1482
|
detach: multi.detach,
|
|
1257
1483
|
reset: multi.reset,
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
onChange: (e) => {
|
|
1265
|
-
handleFiles(e.target.files);
|
|
1266
|
-
e.target.value = "";
|
|
1267
|
-
}
|
|
1268
|
-
},
|
|
1269
|
-
dropHandlers: {
|
|
1270
|
-
onDragOver: (e) => {
|
|
1271
|
-
e.preventDefault();
|
|
1272
|
-
e.stopPropagation();
|
|
1273
|
-
},
|
|
1274
|
-
onDrop: (e) => {
|
|
1275
|
-
e.preventDefault();
|
|
1276
|
-
e.stopPropagation();
|
|
1277
|
-
if (multi.phase !== "uploading") handleFiles(e.dataTransfer.files);
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1484
|
+
getRootProps: intake.getRootProps,
|
|
1485
|
+
getInputProps: intake.getInputProps,
|
|
1486
|
+
isDragActive: intake.isDragActive,
|
|
1487
|
+
isDragAccept: intake.isDragAccept,
|
|
1488
|
+
isDragReject: intake.isDragReject,
|
|
1489
|
+
fileRejections: intake.fileRejections
|
|
1280
1490
|
};
|
|
1281
1491
|
}
|
|
1282
1492
|
var INITIAL_STATE3 = {
|
|
@@ -1286,7 +1496,13 @@ var INITIAL_STATE3 = {
|
|
|
1286
1496
|
expiresIn: null
|
|
1287
1497
|
};
|
|
1288
1498
|
function useDownload(options) {
|
|
1289
|
-
const
|
|
1499
|
+
const store = useHookStoreInstance(INITIAL_STATE3);
|
|
1500
|
+
const state = useHookStoreShallow(store, (s) => ({
|
|
1501
|
+
phase: s.phase,
|
|
1502
|
+
error: s.error,
|
|
1503
|
+
url: s.url,
|
|
1504
|
+
expiresIn: s.expiresIn
|
|
1505
|
+
}));
|
|
1290
1506
|
const contextApi = useContext(S3Context);
|
|
1291
1507
|
const optsRef = useLiveRef(options);
|
|
1292
1508
|
const apiRef = useLiveRef(contextApi);
|
|
@@ -1298,37 +1514,37 @@ function useDownload(options) {
|
|
|
1298
1514
|
throw new Error(
|
|
1299
1515
|
"[dimah-s3] No S3Api found. Pass `api` to useDownload or wrap with <S3Provider>."
|
|
1300
1516
|
);
|
|
1301
|
-
|
|
1302
|
-
phase
|
|
1303
|
-
error
|
|
1304
|
-
url
|
|
1305
|
-
expiresIn
|
|
1517
|
+
patchHookState(store, (draft) => {
|
|
1518
|
+
draft.phase = "presigning";
|
|
1519
|
+
draft.error = null;
|
|
1520
|
+
draft.url = null;
|
|
1521
|
+
draft.expiresIn = null;
|
|
1306
1522
|
});
|
|
1307
1523
|
try {
|
|
1308
1524
|
const result = await api.download(key, {
|
|
1309
1525
|
fileName: downloadName,
|
|
1310
1526
|
bucket: opts.bucket
|
|
1311
1527
|
});
|
|
1312
|
-
|
|
1313
|
-
phase
|
|
1314
|
-
error
|
|
1315
|
-
url
|
|
1316
|
-
expiresIn
|
|
1528
|
+
patchHookState(store, (draft) => {
|
|
1529
|
+
draft.phase = "idle";
|
|
1530
|
+
draft.error = null;
|
|
1531
|
+
draft.url = result.url;
|
|
1532
|
+
draft.expiresIn = result.expiresIn;
|
|
1317
1533
|
});
|
|
1318
1534
|
return { url: result.url, expiresIn: result.expiresIn };
|
|
1319
1535
|
} catch (err) {
|
|
1320
1536
|
const message = err instanceof Error ? err.message : "Download failed";
|
|
1321
|
-
|
|
1322
|
-
phase
|
|
1323
|
-
error
|
|
1324
|
-
url
|
|
1325
|
-
expiresIn
|
|
1537
|
+
patchHookState(store, (draft) => {
|
|
1538
|
+
draft.phase = "error";
|
|
1539
|
+
draft.error = message;
|
|
1540
|
+
draft.url = null;
|
|
1541
|
+
draft.expiresIn = null;
|
|
1326
1542
|
});
|
|
1327
1543
|
opts.onError?.(key, err);
|
|
1328
1544
|
return null;
|
|
1329
1545
|
}
|
|
1330
1546
|
},
|
|
1331
|
-
[apiRef, optsRef]
|
|
1547
|
+
[apiRef, optsRef, store]
|
|
1332
1548
|
);
|
|
1333
1549
|
const download = useCallback(
|
|
1334
1550
|
async (key, downloadName) => {
|
|
@@ -1336,11 +1552,11 @@ function useDownload(options) {
|
|
|
1336
1552
|
if (opts.beforeDownload) {
|
|
1337
1553
|
const allowed = await opts.beforeDownload(key);
|
|
1338
1554
|
if (!allowed) {
|
|
1339
|
-
|
|
1340
|
-
phase
|
|
1341
|
-
error
|
|
1342
|
-
url
|
|
1343
|
-
expiresIn
|
|
1555
|
+
patchHookState(store, (draft) => {
|
|
1556
|
+
draft.phase = "error";
|
|
1557
|
+
draft.error = "Download blocked by beforeDownload hook";
|
|
1558
|
+
draft.url = null;
|
|
1559
|
+
draft.expiresIn = null;
|
|
1344
1560
|
});
|
|
1345
1561
|
opts.onError?.(key, new Error("blocked"));
|
|
1346
1562
|
return;
|
|
@@ -1351,9 +1567,12 @@ function useDownload(options) {
|
|
|
1351
1567
|
window.location.href = result.url;
|
|
1352
1568
|
opts.onInitiated?.(key);
|
|
1353
1569
|
},
|
|
1354
|
-
[optsRef, presign]
|
|
1570
|
+
[optsRef, presign, store]
|
|
1571
|
+
);
|
|
1572
|
+
const reset = useCallback(
|
|
1573
|
+
() => replaceHookState(store, INITIAL_STATE3),
|
|
1574
|
+
[store]
|
|
1355
1575
|
);
|
|
1356
|
-
const reset = useCallback(() => setState(INITIAL_STATE3), []);
|
|
1357
1576
|
return { ...state, download, presign, reset };
|
|
1358
1577
|
}
|
|
1359
1578
|
var INITIAL_PROGRESS3 = { loaded: 0, total: 0, percent: 0 };
|
|
@@ -1365,7 +1584,14 @@ var INITIAL_STATE4 = {
|
|
|
1365
1584
|
fileSize: null
|
|
1366
1585
|
};
|
|
1367
1586
|
function useFetchDownload(options) {
|
|
1368
|
-
const
|
|
1587
|
+
const store = useHookStoreInstance(INITIAL_STATE4);
|
|
1588
|
+
const state = useHookStoreShallow(store, (s) => ({
|
|
1589
|
+
phase: s.phase,
|
|
1590
|
+
progress: s.progress,
|
|
1591
|
+
error: s.error,
|
|
1592
|
+
fileName: s.fileName,
|
|
1593
|
+
fileSize: s.fileSize
|
|
1594
|
+
}));
|
|
1369
1595
|
const contextApi = useContext(S3Context);
|
|
1370
1596
|
const optsRef = useLiveRef(options);
|
|
1371
1597
|
const apiRef = useLiveRef(contextApi);
|
|
@@ -1386,28 +1612,29 @@ function useFetchDownload(options) {
|
|
|
1386
1612
|
if (opts.beforeDownload) {
|
|
1387
1613
|
const allowed = await opts.beforeDownload(key);
|
|
1388
1614
|
if (!allowed) {
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
}));
|
|
1615
|
+
patchHookState(store, (draft) => {
|
|
1616
|
+
draft.phase = "error";
|
|
1617
|
+
draft.error = "Download blocked by beforeDownload hook";
|
|
1618
|
+
});
|
|
1394
1619
|
opts.onError?.(key, new Error("blocked"), "presigning");
|
|
1395
1620
|
return;
|
|
1396
1621
|
}
|
|
1397
1622
|
}
|
|
1398
|
-
|
|
1399
|
-
phase
|
|
1400
|
-
progress
|
|
1401
|
-
error
|
|
1402
|
-
fileName
|
|
1403
|
-
fileSize
|
|
1623
|
+
patchHookState(store, (draft) => {
|
|
1624
|
+
draft.phase = "presigning";
|
|
1625
|
+
draft.progress = { ...INITIAL_PROGRESS3 };
|
|
1626
|
+
draft.error = null;
|
|
1627
|
+
draft.fileName = downloadName ?? null;
|
|
1628
|
+
draft.fileSize = null;
|
|
1404
1629
|
});
|
|
1405
1630
|
try {
|
|
1406
1631
|
const { url } = await api.download(key, {
|
|
1407
1632
|
fileName: downloadName,
|
|
1408
1633
|
bucket: opts.bucket
|
|
1409
1634
|
});
|
|
1410
|
-
|
|
1635
|
+
patchHookState(store, (draft) => {
|
|
1636
|
+
draft.phase = "downloading";
|
|
1637
|
+
});
|
|
1411
1638
|
opts.onDownloadStart?.(key);
|
|
1412
1639
|
speedUpdaterRef.current.reset();
|
|
1413
1640
|
const controller = new AbortController();
|
|
@@ -1420,11 +1647,10 @@ function useFetchDownload(options) {
|
|
|
1420
1647
|
}
|
|
1421
1648
|
const contentLength = Number(res.headers.get("content-length") || 0);
|
|
1422
1649
|
const name = downloadName ?? parseFileName(res.headers.get("content-disposition")) ?? fallback;
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
}));
|
|
1650
|
+
patchHookState(store, (draft) => {
|
|
1651
|
+
draft.fileName = name;
|
|
1652
|
+
draft.fileSize = contentLength || null;
|
|
1653
|
+
});
|
|
1428
1654
|
const reader = res.body?.getReader();
|
|
1429
1655
|
if (!reader) throw new Error("ReadableStream not supported");
|
|
1430
1656
|
const chunks = [];
|
|
@@ -1440,7 +1666,9 @@ function useFetchDownload(options) {
|
|
|
1440
1666
|
total: contentLength,
|
|
1441
1667
|
percent
|
|
1442
1668
|
});
|
|
1443
|
-
|
|
1669
|
+
patchHookState(store, (draft) => {
|
|
1670
|
+
draft.progress = progress;
|
|
1671
|
+
});
|
|
1444
1672
|
opts.onProgress?.(key, progress);
|
|
1445
1673
|
}
|
|
1446
1674
|
const blob = new Blob(chunks);
|
|
@@ -1450,38 +1678,44 @@ function useFetchDownload(options) {
|
|
|
1450
1678
|
anchor.download = name ?? fallback;
|
|
1451
1679
|
anchor.click();
|
|
1452
1680
|
URL.revokeObjectURL(blobUrl);
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1681
|
+
patchHookState(store, (draft) => {
|
|
1682
|
+
draft.phase = "success";
|
|
1683
|
+
draft.fileSize = blob.size;
|
|
1684
|
+
draft.progress = {
|
|
1685
|
+
loaded: blob.size,
|
|
1686
|
+
total: blob.size,
|
|
1687
|
+
percent: 100
|
|
1688
|
+
};
|
|
1689
|
+
});
|
|
1459
1690
|
await opts.onSuccess?.(key, name ?? fallback);
|
|
1460
1691
|
} catch (err) {
|
|
1461
1692
|
if (err.name === "AbortError") {
|
|
1462
1693
|
if (!resettingRef.current) opts.onCancel?.(key);
|
|
1463
1694
|
resettingRef.current = false;
|
|
1464
|
-
|
|
1695
|
+
replaceHookState(store, INITIAL_STATE4);
|
|
1465
1696
|
return;
|
|
1466
1697
|
}
|
|
1467
1698
|
const message = err instanceof Error ? err.message : "Download failed";
|
|
1468
|
-
|
|
1699
|
+
patchHookState(store, (draft) => {
|
|
1700
|
+
draft.phase = "error";
|
|
1701
|
+
draft.error = message;
|
|
1702
|
+
});
|
|
1469
1703
|
opts.onError?.(key, err, "downloading");
|
|
1470
1704
|
} finally {
|
|
1471
1705
|
abortRef.current = null;
|
|
1472
1706
|
}
|
|
1473
1707
|
},
|
|
1474
|
-
[apiRef, optsRef]
|
|
1708
|
+
[apiRef, optsRef, store]
|
|
1475
1709
|
);
|
|
1476
1710
|
const cancel = useCallback(() => {
|
|
1477
1711
|
abortRef.current?.abort();
|
|
1478
|
-
|
|
1479
|
-
}, []);
|
|
1712
|
+
replaceHookState(store, INITIAL_STATE4);
|
|
1713
|
+
}, [store]);
|
|
1480
1714
|
const reset = useCallback(() => {
|
|
1481
1715
|
resettingRef.current = true;
|
|
1482
1716
|
abortRef.current?.abort();
|
|
1483
|
-
|
|
1484
|
-
}, []);
|
|
1717
|
+
replaceHookState(store, INITIAL_STATE4);
|
|
1718
|
+
}, [store]);
|
|
1485
1719
|
return { ...state, download, cancel, reset };
|
|
1486
1720
|
}
|
|
1487
1721
|
var INITIAL_STATE5 = {
|
|
@@ -1490,15 +1724,27 @@ var INITIAL_STATE5 = {
|
|
|
1490
1724
|
pendingKey: null
|
|
1491
1725
|
};
|
|
1492
1726
|
function useDelete(options) {
|
|
1493
|
-
const
|
|
1727
|
+
const store = useHookStoreInstance(INITIAL_STATE5);
|
|
1728
|
+
const state = useHookStoreShallow(store, (s) => ({
|
|
1729
|
+
phase: s.phase,
|
|
1730
|
+
error: s.error,
|
|
1731
|
+
pendingKey: s.pendingKey
|
|
1732
|
+
}));
|
|
1494
1733
|
const contextApi = useContext(S3Context);
|
|
1495
1734
|
const optsRef = useLiveRef(options);
|
|
1496
1735
|
const apiRef = useLiveRef(contextApi);
|
|
1497
1736
|
const pendingKeyRef = useRef(null);
|
|
1498
|
-
const requestDelete = useCallback(
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1737
|
+
const requestDelete = useCallback(
|
|
1738
|
+
(key) => {
|
|
1739
|
+
pendingKeyRef.current = key;
|
|
1740
|
+
patchHookState(store, (draft) => {
|
|
1741
|
+
draft.phase = "confirming";
|
|
1742
|
+
draft.error = null;
|
|
1743
|
+
draft.pendingKey = key;
|
|
1744
|
+
});
|
|
1745
|
+
},
|
|
1746
|
+
[store]
|
|
1747
|
+
);
|
|
1502
1748
|
const confirmDelete = useCallback(async () => {
|
|
1503
1749
|
const key = pendingKeyRef.current;
|
|
1504
1750
|
if (!key) return;
|
|
@@ -1511,37 +1757,47 @@ function useDelete(options) {
|
|
|
1511
1757
|
if (opts.beforeDelete) {
|
|
1512
1758
|
const allowed = await opts.beforeDelete(key);
|
|
1513
1759
|
if (!allowed) {
|
|
1514
|
-
|
|
1515
|
-
phase
|
|
1516
|
-
error
|
|
1517
|
-
pendingKey
|
|
1760
|
+
patchHookState(store, (draft) => {
|
|
1761
|
+
draft.phase = "error";
|
|
1762
|
+
draft.error = "Delete blocked by beforeDelete hook";
|
|
1763
|
+
draft.pendingKey = null;
|
|
1518
1764
|
});
|
|
1519
1765
|
opts.onError?.(key, new Error("blocked"), "confirming");
|
|
1520
1766
|
pendingKeyRef.current = null;
|
|
1521
1767
|
return;
|
|
1522
1768
|
}
|
|
1523
1769
|
}
|
|
1524
|
-
|
|
1770
|
+
patchHookState(store, (draft) => {
|
|
1771
|
+
draft.phase = "deleting";
|
|
1772
|
+
draft.error = null;
|
|
1773
|
+
});
|
|
1525
1774
|
opts.onDeleteStart?.(key);
|
|
1526
1775
|
try {
|
|
1527
1776
|
await api.delete(key, { bucket: opts.bucket });
|
|
1528
1777
|
pendingKeyRef.current = null;
|
|
1529
|
-
|
|
1778
|
+
patchHookState(store, (draft) => {
|
|
1779
|
+
draft.phase = "success";
|
|
1780
|
+
draft.error = null;
|
|
1781
|
+
draft.pendingKey = null;
|
|
1782
|
+
});
|
|
1530
1783
|
await opts.onSuccess?.(key);
|
|
1531
1784
|
} catch (err) {
|
|
1532
1785
|
const message = err instanceof Error ? err.message : "Delete failed";
|
|
1533
|
-
|
|
1786
|
+
patchHookState(store, (draft) => {
|
|
1787
|
+
draft.phase = "error";
|
|
1788
|
+
draft.error = message;
|
|
1789
|
+
});
|
|
1534
1790
|
opts.onError?.(key, err, "deleting");
|
|
1535
1791
|
}
|
|
1536
|
-
}, [apiRef, optsRef]);
|
|
1792
|
+
}, [apiRef, optsRef, store]);
|
|
1537
1793
|
const cancelDelete = useCallback(() => {
|
|
1538
1794
|
pendingKeyRef.current = null;
|
|
1539
|
-
|
|
1540
|
-
}, []);
|
|
1795
|
+
replaceHookState(store, INITIAL_STATE5);
|
|
1796
|
+
}, [store]);
|
|
1541
1797
|
const reset = useCallback(() => {
|
|
1542
1798
|
pendingKeyRef.current = null;
|
|
1543
|
-
|
|
1544
|
-
}, []);
|
|
1799
|
+
replaceHookState(store, INITIAL_STATE5);
|
|
1800
|
+
}, [store]);
|
|
1545
1801
|
return {
|
|
1546
1802
|
phase: state.phase,
|
|
1547
1803
|
error: state.error,
|
|
@@ -1553,6 +1809,6 @@ function useDelete(options) {
|
|
|
1553
1809
|
};
|
|
1554
1810
|
}
|
|
1555
1811
|
|
|
1556
|
-
export { S3Context, S3Provider, S3UploadError, createLocalStorageStore, createMemoryStore, createS3Client, createSpeedTracker, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, toUploadError, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFetchDownload, useFormatDimahError, useFormatValidateFileError,
|
|
1812
|
+
export { S3Context, S3Provider, S3UploadError, createImagePreviewUrl, createLocalStorageStore, createMemoryStore, createS3Client, createSpeedTracker, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, revokePreviewUrl, toDropzoneAccept, toUploadError, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFetchDownload, useFileIntake, useFileUpload, useFormatDimahError, useFormatValidateFileError, useMultiFileUpload, useMultiUpload, useUpload };
|
|
1557
1813
|
//# sourceMappingURL=index.js.map
|
|
1558
1814
|
//# sourceMappingURL=index.js.map
|