@dimah-s3/react 1.5.11 → 1.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/dist/helpers/format-number.d.ts +12 -0
- package/dist/helpers/format-number.d.ts.map +1 -0
- package/dist/helpers/format-speed.d.ts +2 -1
- package/dist/helpers/format-speed.d.ts.map +1 -1
- package/dist/helpers/format-upload-progress.d.ts +3 -1
- package/dist/helpers/format-upload-progress.d.ts.map +1 -1
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/use-delete.d.ts +4 -1
- package/dist/hooks/use-delete.d.ts.map +1 -1
- package/dist/hooks/use-download.d.ts +24 -2
- package/dist/hooks/use-download.d.ts.map +1 -1
- package/dist/hooks/use-file-intake.d.ts.map +1 -1
- package/dist/hooks/use-object-list.d.ts +49 -0
- package/dist/hooks/use-object-list.d.ts.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +394 -24
- package/dist/index.js.map +1 -1
- package/dist/list/read-object-list-level.d.ts +17 -0
- package/dist/list/read-object-list-level.d.ts.map +1 -0
- package/dist/list/types.d.ts +73 -0
- package/dist/list/types.d.ts.map +1 -0
- package/dist/s3-provider.d.ts +9 -1
- package/dist/s3-provider.d.ts.map +1 -1
- package/dist/translations/types.d.ts +48 -0
- package/dist/translations/types.d.ts.map +1 -1
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createS3Client as createS3Client$1, sha256File, formatFileSize, isAPIError, S3_ERROR_CODES, parseFileName, DELETE_BATCH_MAX_KEYS, isS3ErrorCode, APIError, validateFile } from '@dimah-s3/core';
|
|
2
|
-
export { APIError, S3_ERROR_CODES, buildContentDisposition, buildObjectKey, buildPublicObjectUrl, fileNameFromKey, formatFileSize, isAPIError, isS3ErrorCode, matchesMagicBytes, sanitizeFileName, sha256Base64, sha256File, sniffContentType, validateFile } from '@dimah-s3/core';
|
|
3
|
-
import { createContext, useContext, useCallback, useState, useEffect, useRef, useLayoutEffect } from 'react';
|
|
2
|
+
export { APIError, S3_ERROR_CODES, buildContentDisposition, buildObjectKey, buildPublicObjectUrl, fileNameFromKey, formatFileSize, isAPIError, isS3ErrorCode, matchesMagicBytes, resolveInlineDisposition, sanitizeFileName, sha256Base64, sha256File, sniffContentType, validateFile } from '@dimah-s3/core';
|
|
3
|
+
import { createContext, useContext, useCallback, useMemo, useState, useEffect, useRef, 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';
|
|
@@ -57,12 +57,14 @@ function defineApi(input) {
|
|
|
57
57
|
return input;
|
|
58
58
|
}
|
|
59
59
|
var S3Context = createContext(null);
|
|
60
|
+
var S3LocaleContext = createContext(void 0);
|
|
60
61
|
function S3Provider({
|
|
61
62
|
api,
|
|
62
63
|
translations,
|
|
64
|
+
locale,
|
|
63
65
|
children
|
|
64
66
|
}) {
|
|
65
|
-
return /* @__PURE__ */ jsx(S3Context.Provider, { value: api, children: translations ? /* @__PURE__ */ jsx(TranslationProvider, { translations, children }) : children });
|
|
67
|
+
return /* @__PURE__ */ jsx(S3Context.Provider, { value: api, children: /* @__PURE__ */ jsx(S3LocaleContext.Provider, { value: locale, children: translations ? /* @__PURE__ */ jsx(TranslationProvider, { translations, children }) : children }) });
|
|
66
68
|
}
|
|
67
69
|
function useApi() {
|
|
68
70
|
const ctx = useContext(S3Context);
|
|
@@ -655,13 +657,13 @@ function formatAcceptLabels(accept) {
|
|
|
655
657
|
if (!accept?.length) return [];
|
|
656
658
|
return [...new Set(accept.map(formatAcceptLabel))];
|
|
657
659
|
}
|
|
658
|
-
function formatUploadProgress(loaded, total, percent) {
|
|
659
|
-
const loadedStr = formatFileSize(loaded);
|
|
660
|
+
function formatUploadProgress(loaded, total, percent, options) {
|
|
661
|
+
const loadedStr = formatFileSize(loaded, options);
|
|
660
662
|
if (!total) return loadedStr;
|
|
661
|
-
return `${loadedStr} / ${formatFileSize(total)} (${Math.round(percent)}%)`;
|
|
663
|
+
return `${loadedStr} / ${formatFileSize(total, options)} (${Math.round(percent)}%)`;
|
|
662
664
|
}
|
|
663
|
-
function formatSpeed(bytesPerSecond) {
|
|
664
|
-
return `${formatFileSize(bytesPerSecond)}/s`;
|
|
665
|
+
function formatSpeed(bytesPerSecond, options) {
|
|
666
|
+
return `${formatFileSize(bytesPerSecond, options)}/s`;
|
|
665
667
|
}
|
|
666
668
|
|
|
667
669
|
// src/helpers/format-eta.ts
|
|
@@ -759,6 +761,38 @@ function useFormatDimahError() {
|
|
|
759
761
|
[t]
|
|
760
762
|
);
|
|
761
763
|
}
|
|
764
|
+
function useFormatFileSize() {
|
|
765
|
+
const locale = useContext(S3LocaleContext);
|
|
766
|
+
const t = useTranslations();
|
|
767
|
+
return useMemo(() => {
|
|
768
|
+
const units = {
|
|
769
|
+
B: t("B", { note: "file size unit" }),
|
|
770
|
+
KB: t("KB", { note: "file size unit" }),
|
|
771
|
+
MB: t("MB", { note: "file size unit" }),
|
|
772
|
+
GB: t("GB", { note: "file size unit" }),
|
|
773
|
+
TB: t("TB", { note: "file size unit" }),
|
|
774
|
+
PB: t("PB", { note: "file size unit" }),
|
|
775
|
+
EB: t("EB", { note: "file size unit" })
|
|
776
|
+
};
|
|
777
|
+
return (bytes) => formatFileSize(bytes, { locale, units });
|
|
778
|
+
}, [locale, t]);
|
|
779
|
+
}
|
|
780
|
+
function useFormatNumber() {
|
|
781
|
+
const locale = useContext(S3LocaleContext);
|
|
782
|
+
return useCallback(
|
|
783
|
+
(value) => locale ? numberFormat(locale).format(value) : String(value),
|
|
784
|
+
[locale]
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
var numberFormats = /* @__PURE__ */ new Map();
|
|
788
|
+
function numberFormat(locale) {
|
|
789
|
+
let format = numberFormats.get(locale);
|
|
790
|
+
if (!format) {
|
|
791
|
+
format = new Intl.NumberFormat(locale);
|
|
792
|
+
numberFormats.set(locale, format);
|
|
793
|
+
}
|
|
794
|
+
return format;
|
|
795
|
+
}
|
|
762
796
|
|
|
763
797
|
// src/helpers/throttled-speed.ts
|
|
764
798
|
function createThrottledSpeedUpdater(tracker, intervalMs = 500) {
|
|
@@ -1328,7 +1362,8 @@ function useFileIntake(options) {
|
|
|
1328
1362
|
const {
|
|
1329
1363
|
getRootProps,
|
|
1330
1364
|
getInputProps: getDropzoneInputProps,
|
|
1331
|
-
|
|
1365
|
+
inputRef,
|
|
1366
|
+
open: openDropzone,
|
|
1332
1367
|
isDragActive,
|
|
1333
1368
|
isDragAccept,
|
|
1334
1369
|
isDragReject,
|
|
@@ -1345,13 +1380,34 @@ function useFileIntake(options) {
|
|
|
1345
1380
|
onDropAccepted,
|
|
1346
1381
|
onDropRejected
|
|
1347
1382
|
});
|
|
1383
|
+
const inputsRef = useRef(/* @__PURE__ */ new Set());
|
|
1384
|
+
const attachInput = useCallback(
|
|
1385
|
+
(node) => {
|
|
1386
|
+
if (!node) return;
|
|
1387
|
+
inputsRef.current.add(node);
|
|
1388
|
+
inputRef.current = node;
|
|
1389
|
+
},
|
|
1390
|
+
[inputRef]
|
|
1391
|
+
);
|
|
1348
1392
|
const getInputProps = useCallback(
|
|
1349
|
-
(props) =>
|
|
1350
|
-
...
|
|
1351
|
-
|
|
1393
|
+
(props) => ({
|
|
1394
|
+
...getDropzoneInputProps({
|
|
1395
|
+
...props,
|
|
1396
|
+
...htmlAccept != null ? { accept: htmlAccept } : {}
|
|
1397
|
+
}),
|
|
1398
|
+
ref: attachInput
|
|
1352
1399
|
}),
|
|
1353
|
-
[getDropzoneInputProps, htmlAccept]
|
|
1400
|
+
[attachInput, getDropzoneInputProps, htmlAccept]
|
|
1354
1401
|
);
|
|
1402
|
+
const open = useCallback(() => {
|
|
1403
|
+
const inputs = inputsRef.current;
|
|
1404
|
+
for (const node of inputs) {
|
|
1405
|
+
if (!node.isConnected) inputs.delete(node);
|
|
1406
|
+
}
|
|
1407
|
+
const ref = inputRef;
|
|
1408
|
+
if (!ref.current?.isConnected) ref.current = [...inputs].at(-1) ?? null;
|
|
1409
|
+
openDropzone();
|
|
1410
|
+
}, [inputRef, openDropzone]);
|
|
1355
1411
|
return {
|
|
1356
1412
|
getRootProps,
|
|
1357
1413
|
getInputProps,
|
|
@@ -1530,7 +1586,7 @@ function useDownload(options) {
|
|
|
1530
1586
|
createThrottledSpeedUpdater(createSpeedTracker())
|
|
1531
1587
|
);
|
|
1532
1588
|
const requestDownloadUrl = useCallback(
|
|
1533
|
-
async (key, downloadName, generation) => {
|
|
1589
|
+
async (key, downloadName, generation, expiresIn) => {
|
|
1534
1590
|
const opts = optsRef.current;
|
|
1535
1591
|
const api = opts.api ?? apiRef.current;
|
|
1536
1592
|
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
@@ -1546,7 +1602,8 @@ function useDownload(options) {
|
|
|
1546
1602
|
route: opts.route,
|
|
1547
1603
|
key,
|
|
1548
1604
|
fileName: downloadName,
|
|
1549
|
-
...opts.disposition ? { disposition: opts.disposition } : {}
|
|
1605
|
+
...opts.disposition ? { disposition: opts.disposition } : {},
|
|
1606
|
+
...expiresIn != null ? { expiresIn } : {}
|
|
1550
1607
|
});
|
|
1551
1608
|
if (generation !== generationRef.current) {
|
|
1552
1609
|
return { url: result.url, expiresIn: result.expiresIn };
|
|
@@ -1573,12 +1630,51 @@ function useDownload(options) {
|
|
|
1573
1630
|
[apiRef, optsRef, patch]
|
|
1574
1631
|
);
|
|
1575
1632
|
const presign = useCallback(
|
|
1576
|
-
async (key, downloadName) => {
|
|
1633
|
+
async (key, downloadName, expiresIn) => {
|
|
1577
1634
|
const generation = ++generationRef.current;
|
|
1578
|
-
return requestDownloadUrl(key, downloadName, generation);
|
|
1635
|
+
return requestDownloadUrl(key, downloadName, generation, expiresIn);
|
|
1579
1636
|
},
|
|
1580
1637
|
[requestDownloadUrl]
|
|
1581
1638
|
);
|
|
1639
|
+
const presignMany = useCallback(
|
|
1640
|
+
async (files) => {
|
|
1641
|
+
const signed = [];
|
|
1642
|
+
const failed = [];
|
|
1643
|
+
if (files.length === 0) return { signed, failed };
|
|
1644
|
+
const opts = optsRef.current;
|
|
1645
|
+
const api = opts.api ?? apiRef.current;
|
|
1646
|
+
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
1647
|
+
for (const file of files) {
|
|
1648
|
+
if (opts.beforeDownload) {
|
|
1649
|
+
const allowed = await opts.beforeDownload(file.key);
|
|
1650
|
+
if (!allowed) {
|
|
1651
|
+
failed.push(file.key);
|
|
1652
|
+
opts.onError?.(file.key, new Error("blocked"));
|
|
1653
|
+
continue;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
try {
|
|
1657
|
+
const result = await api.download({
|
|
1658
|
+
route: opts.route,
|
|
1659
|
+
key: file.key,
|
|
1660
|
+
fileName: file.fileName,
|
|
1661
|
+
...opts.disposition ? { disposition: opts.disposition } : {},
|
|
1662
|
+
...file.expiresIn != null ? { expiresIn: file.expiresIn } : {}
|
|
1663
|
+
});
|
|
1664
|
+
signed.push({
|
|
1665
|
+
key: file.key,
|
|
1666
|
+
url: result.url,
|
|
1667
|
+
expiresIn: result.expiresIn
|
|
1668
|
+
});
|
|
1669
|
+
} catch (err) {
|
|
1670
|
+
failed.push(file.key);
|
|
1671
|
+
opts.onError?.(file.key, err);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
return { signed, failed };
|
|
1675
|
+
},
|
|
1676
|
+
[apiRef, optsRef]
|
|
1677
|
+
);
|
|
1582
1678
|
const downloadNavigate = useCallback(
|
|
1583
1679
|
async (key, downloadName) => {
|
|
1584
1680
|
const opts = optsRef.current;
|
|
@@ -1783,6 +1879,7 @@ function useDownload(options) {
|
|
|
1783
1879
|
isPending: state.phase === "presigning",
|
|
1784
1880
|
download,
|
|
1785
1881
|
presign,
|
|
1882
|
+
presignMany,
|
|
1786
1883
|
reset
|
|
1787
1884
|
};
|
|
1788
1885
|
}
|
|
@@ -1894,7 +1991,8 @@ function useDelete(options) {
|
|
|
1894
1991
|
);
|
|
1895
1992
|
const removeMany = useCallback(
|
|
1896
1993
|
async (keys) => {
|
|
1897
|
-
|
|
1994
|
+
const empty = { deleted: [], failed: [] };
|
|
1995
|
+
if (inFlightRef.current || keys.length === 0) return empty;
|
|
1898
1996
|
const opts = optsRef.current;
|
|
1899
1997
|
const api = opts.api ?? apiRef.current;
|
|
1900
1998
|
if (!api)
|
|
@@ -1913,21 +2011,24 @@ function useDelete(options) {
|
|
|
1913
2011
|
draft.error = null;
|
|
1914
2012
|
draft.objectKey = null;
|
|
1915
2013
|
});
|
|
2014
|
+
const deleted = [];
|
|
2015
|
+
const failed = [];
|
|
1916
2016
|
try {
|
|
1917
2017
|
const { results } = await api.deleteMany({
|
|
1918
2018
|
route: opts.route,
|
|
1919
2019
|
keys
|
|
1920
2020
|
});
|
|
1921
|
-
if (generation !== generationRef.current) return;
|
|
1922
|
-
const failed = results.filter((item) => !item.success);
|
|
2021
|
+
if (generation !== generationRef.current) return { deleted, failed };
|
|
1923
2022
|
for (const item of results) {
|
|
1924
2023
|
if (item.success) {
|
|
2024
|
+
deleted.push(item.key);
|
|
1925
2025
|
try {
|
|
1926
2026
|
await opts.onSuccess?.(item.key);
|
|
1927
2027
|
} catch (err) {
|
|
1928
2028
|
opts.onError?.(item.key, err, "success");
|
|
1929
2029
|
}
|
|
1930
2030
|
} else {
|
|
2031
|
+
failed.push(item.key);
|
|
1931
2032
|
opts.onError?.(
|
|
1932
2033
|
item.key,
|
|
1933
2034
|
new Error(item.error?.message ?? "Delete failed"),
|
|
@@ -1939,23 +2040,30 @@ function useDelete(options) {
|
|
|
1939
2040
|
patch((draft) => {
|
|
1940
2041
|
draft.phase = "error";
|
|
1941
2042
|
draft.error = toHookError(
|
|
1942
|
-
new Error(
|
|
2043
|
+
new Error(
|
|
2044
|
+
results.find((item) => !item.success)?.error?.message ?? "Delete failed"
|
|
2045
|
+
),
|
|
1943
2046
|
"Delete failed"
|
|
1944
2047
|
);
|
|
1945
2048
|
});
|
|
1946
|
-
return;
|
|
2049
|
+
return { deleted, failed };
|
|
1947
2050
|
}
|
|
1948
2051
|
patch((draft) => {
|
|
1949
2052
|
draft.phase = "success";
|
|
1950
2053
|
draft.error = null;
|
|
1951
2054
|
});
|
|
2055
|
+
return { deleted, failed };
|
|
1952
2056
|
} catch (err) {
|
|
1953
|
-
if (generation !== generationRef.current) return;
|
|
2057
|
+
if (generation !== generationRef.current) return { deleted, failed };
|
|
1954
2058
|
patch((draft) => {
|
|
1955
2059
|
draft.phase = "error";
|
|
1956
2060
|
draft.error = toHookError(err, "Delete failed");
|
|
1957
2061
|
});
|
|
1958
2062
|
opts.onError?.(keys[0] ?? "", err, "deleting");
|
|
2063
|
+
return {
|
|
2064
|
+
deleted,
|
|
2065
|
+
failed: keys.filter((key) => !deleted.includes(key))
|
|
2066
|
+
};
|
|
1959
2067
|
} finally {
|
|
1960
2068
|
if (generation === generationRef.current) {
|
|
1961
2069
|
inFlightRef.current = false;
|
|
@@ -2090,7 +2198,269 @@ function useObjectUrl(options) {
|
|
|
2090
2198
|
}, [disposition, fileName, objectKey, route]);
|
|
2091
2199
|
return { url, expiresIn, error, isLoading, refresh };
|
|
2092
2200
|
}
|
|
2201
|
+
function serializeQueryKey(key) {
|
|
2202
|
+
return JSON.stringify(key ?? []);
|
|
2203
|
+
}
|
|
2204
|
+
function trailsEqual(a, b) {
|
|
2205
|
+
return a.length === b.length && a.every((folder, index) => {
|
|
2206
|
+
const other = b[index];
|
|
2207
|
+
return other != null && folder.id === other.id && folder.name === other.name;
|
|
2208
|
+
});
|
|
2209
|
+
}
|
|
2210
|
+
function syncTrail(trail, parent) {
|
|
2211
|
+
if (parent == null) return [];
|
|
2212
|
+
const index = trail.findIndex((folder) => folder.id === parent);
|
|
2213
|
+
if (index >= 0) return trail.slice(0, index + 1);
|
|
2214
|
+
return [{ id: parent, name: parent }];
|
|
2215
|
+
}
|
|
2216
|
+
function useObjectList(options) {
|
|
2217
|
+
const optionsRef = useLiveRef(options);
|
|
2218
|
+
const controlled = options.parent !== void 0;
|
|
2219
|
+
const enabled = options.enabled !== false;
|
|
2220
|
+
const queryKey = serializeQueryKey(options.queryKey);
|
|
2221
|
+
const [parentState, setParentState] = useState(() => {
|
|
2222
|
+
if (options.defaultParent !== void 0) return options.defaultParent;
|
|
2223
|
+
return options.defaultTrail?.at(-1)?.id ?? null;
|
|
2224
|
+
});
|
|
2225
|
+
const parent = controlled ? options.parent ?? null : parentState;
|
|
2226
|
+
const [trailState, setTrailState] = useState(
|
|
2227
|
+
() => {
|
|
2228
|
+
const initial = options.defaultTrail ?? [];
|
|
2229
|
+
if (options.parent !== void 0)
|
|
2230
|
+
return syncTrail(initial, options.parent);
|
|
2231
|
+
if (options.defaultParent !== void 0) {
|
|
2232
|
+
return syncTrail(initial, options.defaultParent);
|
|
2233
|
+
}
|
|
2234
|
+
return initial;
|
|
2235
|
+
}
|
|
2236
|
+
);
|
|
2237
|
+
const trail = useMemo(
|
|
2238
|
+
() => controlled ? syncTrail(trailState, parent) : trailState,
|
|
2239
|
+
[controlled, trailState, parent]
|
|
2240
|
+
);
|
|
2241
|
+
const [items, setItems] = useState([]);
|
|
2242
|
+
const [folders, setFolders] = useState([]);
|
|
2243
|
+
const [nextCursor, setNextCursor] = useState(null);
|
|
2244
|
+
const [usage, setUsage] = useState(null);
|
|
2245
|
+
const [error, setError] = useState(null);
|
|
2246
|
+
const [status, setStatus] = useState(enabled ? "loading" : "ready");
|
|
2247
|
+
const parentRef = useLiveRef(parent);
|
|
2248
|
+
const trailRef = useLiveRef(trail);
|
|
2249
|
+
const cursorRef = useLiveRef(nextCursor);
|
|
2250
|
+
const statusRef = useLiveRef(status);
|
|
2251
|
+
const itemsRef = useLiveRef(items);
|
|
2252
|
+
const foldersRef = useLiveRef(folders);
|
|
2253
|
+
const abortRef = useRef(null);
|
|
2254
|
+
const generationRef = useRef(0);
|
|
2255
|
+
const applyPage = useCallback(
|
|
2256
|
+
(page, mode) => {
|
|
2257
|
+
if (mode === "append") {
|
|
2258
|
+
setItems((current) => [...current, ...page.items]);
|
|
2259
|
+
if (page.usage) setUsage(page.usage);
|
|
2260
|
+
} else {
|
|
2261
|
+
setItems(page.items);
|
|
2262
|
+
setFolders(page.folders ?? []);
|
|
2263
|
+
setUsage(page.usage ?? null);
|
|
2264
|
+
}
|
|
2265
|
+
const cursor = page.nextCursor ? page.nextCursor : null;
|
|
2266
|
+
cursorRef.current = cursor;
|
|
2267
|
+
statusRef.current = "ready";
|
|
2268
|
+
setNextCursor(cursor);
|
|
2269
|
+
setError(null);
|
|
2270
|
+
setStatus("ready");
|
|
2271
|
+
},
|
|
2272
|
+
[cursorRef, statusRef]
|
|
2273
|
+
);
|
|
2274
|
+
const run = useCallback(
|
|
2275
|
+
async (mode) => {
|
|
2276
|
+
if (optionsRef.current.enabled === false) return;
|
|
2277
|
+
const cursor = mode === "append" ? cursorRef.current : null;
|
|
2278
|
+
if (mode === "append") {
|
|
2279
|
+
if (!cursor) return;
|
|
2280
|
+
const current = statusRef.current;
|
|
2281
|
+
if (current === "loading" || current === "loading-more" || current === "refreshing") {
|
|
2282
|
+
return;
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
abortRef.current?.abort();
|
|
2286
|
+
const controller = new AbortController();
|
|
2287
|
+
abortRef.current = controller;
|
|
2288
|
+
const generation = ++generationRef.current;
|
|
2289
|
+
const requestParent = parentRef.current;
|
|
2290
|
+
const hasRows = itemsRef.current.length > 0 || foldersRef.current.length > 0;
|
|
2291
|
+
if (mode === "replace") {
|
|
2292
|
+
setItems([]);
|
|
2293
|
+
setFolders([]);
|
|
2294
|
+
setNextCursor(null);
|
|
2295
|
+
setError(null);
|
|
2296
|
+
statusRef.current = "loading";
|
|
2297
|
+
setStatus("loading");
|
|
2298
|
+
} else if (mode === "refresh") {
|
|
2299
|
+
setError(null);
|
|
2300
|
+
const nextStatus = hasRows ? "refreshing" : "loading";
|
|
2301
|
+
statusRef.current = nextStatus;
|
|
2302
|
+
setStatus(nextStatus);
|
|
2303
|
+
} else {
|
|
2304
|
+
statusRef.current = "loading-more";
|
|
2305
|
+
setStatus("loading-more");
|
|
2306
|
+
}
|
|
2307
|
+
try {
|
|
2308
|
+
const page = await optionsRef.current.query({
|
|
2309
|
+
parent: requestParent,
|
|
2310
|
+
cursor,
|
|
2311
|
+
signal: controller.signal
|
|
2312
|
+
});
|
|
2313
|
+
if (generation !== generationRef.current || controller.signal.aborted) {
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
2316
|
+
applyPage(page, mode);
|
|
2317
|
+
} catch (err) {
|
|
2318
|
+
if (generation !== generationRef.current || controller.signal.aborted || isAbortError(err)) {
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
setError(toHookError(err, "Could not load files"));
|
|
2322
|
+
statusRef.current = "error";
|
|
2323
|
+
setStatus("error");
|
|
2324
|
+
}
|
|
2325
|
+
},
|
|
2326
|
+
[
|
|
2327
|
+
abortRef,
|
|
2328
|
+
applyPage,
|
|
2329
|
+
cursorRef,
|
|
2330
|
+
foldersRef,
|
|
2331
|
+
generationRef,
|
|
2332
|
+
itemsRef,
|
|
2333
|
+
optionsRef,
|
|
2334
|
+
parentRef,
|
|
2335
|
+
statusRef
|
|
2336
|
+
]
|
|
2337
|
+
);
|
|
2338
|
+
useEffect(() => {
|
|
2339
|
+
if (!enabled) {
|
|
2340
|
+
abortRef.current?.abort();
|
|
2341
|
+
return;
|
|
2342
|
+
}
|
|
2343
|
+
let active = true;
|
|
2344
|
+
void (async () => {
|
|
2345
|
+
await Promise.resolve();
|
|
2346
|
+
if (!active) return;
|
|
2347
|
+
await run("replace");
|
|
2348
|
+
})();
|
|
2349
|
+
return () => {
|
|
2350
|
+
active = false;
|
|
2351
|
+
abortRef.current?.abort();
|
|
2352
|
+
};
|
|
2353
|
+
}, [abortRef, enabled, parent, queryKey, run]);
|
|
2354
|
+
const navigate = useCallback(
|
|
2355
|
+
(nextTrail) => {
|
|
2356
|
+
const nextParent = nextTrail.at(-1)?.id ?? null;
|
|
2357
|
+
if (nextParent === parentRef.current && trailsEqual(nextTrail, trailRef.current)) {
|
|
2358
|
+
return;
|
|
2359
|
+
}
|
|
2360
|
+
const previousParent = parentRef.current;
|
|
2361
|
+
trailRef.current = nextTrail;
|
|
2362
|
+
setTrailState(nextTrail);
|
|
2363
|
+
if (optionsRef.current.parent === void 0) {
|
|
2364
|
+
parentRef.current = nextParent;
|
|
2365
|
+
setParentState(nextParent);
|
|
2366
|
+
}
|
|
2367
|
+
if (nextParent !== previousParent) {
|
|
2368
|
+
cursorRef.current = null;
|
|
2369
|
+
setItems([]);
|
|
2370
|
+
setFolders([]);
|
|
2371
|
+
setNextCursor(null);
|
|
2372
|
+
setError(null);
|
|
2373
|
+
statusRef.current = "loading";
|
|
2374
|
+
setStatus("loading");
|
|
2375
|
+
}
|
|
2376
|
+
optionsRef.current.onParentChange?.(nextParent);
|
|
2377
|
+
},
|
|
2378
|
+
[cursorRef, optionsRef, parentRef, statusRef, trailRef]
|
|
2379
|
+
);
|
|
2380
|
+
const openFolder = useCallback(
|
|
2381
|
+
(folder) => {
|
|
2382
|
+
const current = trailRef.current;
|
|
2383
|
+
const index = current.findIndex((entry) => entry.id === folder.id);
|
|
2384
|
+
const next = index >= 0 ? current.slice(0, index + 1) : [...current, folder];
|
|
2385
|
+
navigate(next);
|
|
2386
|
+
},
|
|
2387
|
+
[navigate, trailRef]
|
|
2388
|
+
);
|
|
2389
|
+
const loadMore = useCallback(() => run("append"), [run]);
|
|
2390
|
+
const refresh = useCallback(() => run("refresh"), [run]);
|
|
2391
|
+
return {
|
|
2392
|
+
items,
|
|
2393
|
+
folders,
|
|
2394
|
+
trail,
|
|
2395
|
+
parent,
|
|
2396
|
+
nextCursor,
|
|
2397
|
+
error,
|
|
2398
|
+
isLoading: enabled && status === "loading",
|
|
2399
|
+
isLoadingMore: enabled && status === "loading-more",
|
|
2400
|
+
isRefreshing: enabled && status === "refreshing",
|
|
2401
|
+
usage,
|
|
2402
|
+
hasMore: nextCursor != null,
|
|
2403
|
+
loadMore,
|
|
2404
|
+
refresh,
|
|
2405
|
+
openFolder,
|
|
2406
|
+
navigate
|
|
2407
|
+
};
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
// src/list/read-object-list-level.ts
|
|
2411
|
+
function readObjectListLevel(records, input) {
|
|
2412
|
+
const parent = normalizeFolder(input.parent);
|
|
2413
|
+
const folders = /* @__PURE__ */ new Map();
|
|
2414
|
+
const items = [];
|
|
2415
|
+
for (const record of records) {
|
|
2416
|
+
const folder = normalizeFolder(input.getFolder(record));
|
|
2417
|
+
if (parent == null) {
|
|
2418
|
+
if (folder == null) {
|
|
2419
|
+
items.push(record);
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
const name2 = folder.split("/")[0];
|
|
2423
|
+
if (!name2) continue;
|
|
2424
|
+
folders.set(name2, { id: name2, name: name2 });
|
|
2425
|
+
continue;
|
|
2426
|
+
}
|
|
2427
|
+
if (folder == null) continue;
|
|
2428
|
+
if (folder === parent) {
|
|
2429
|
+
items.push(record);
|
|
2430
|
+
continue;
|
|
2431
|
+
}
|
|
2432
|
+
const prefix = `${parent}/`;
|
|
2433
|
+
if (!folder.startsWith(prefix)) continue;
|
|
2434
|
+
const name = folder.slice(prefix.length).split("/")[0];
|
|
2435
|
+
if (!name) continue;
|
|
2436
|
+
const id = `${parent}/${name}`;
|
|
2437
|
+
folders.set(id, { id, name });
|
|
2438
|
+
}
|
|
2439
|
+
return {
|
|
2440
|
+
folders: [...folders.values()].sort(
|
|
2441
|
+
(a, b) => a.name.localeCompare(b.name, void 0, {
|
|
2442
|
+
numeric: true,
|
|
2443
|
+
sensitivity: "base"
|
|
2444
|
+
})
|
|
2445
|
+
),
|
|
2446
|
+
items
|
|
2447
|
+
};
|
|
2448
|
+
}
|
|
2449
|
+
function normalizeFolder(folder) {
|
|
2450
|
+
if (folder == null) return null;
|
|
2451
|
+
const parts = [];
|
|
2452
|
+
for (const raw of folder.split("/")) {
|
|
2453
|
+
const part = raw.trim();
|
|
2454
|
+
if (!part || part === ".") continue;
|
|
2455
|
+
if (part === "..") {
|
|
2456
|
+
parts.pop();
|
|
2457
|
+
continue;
|
|
2458
|
+
}
|
|
2459
|
+
parts.push(part);
|
|
2460
|
+
}
|
|
2461
|
+
return parts.length > 0 ? parts.join("/") : null;
|
|
2462
|
+
}
|
|
2093
2463
|
|
|
2094
|
-
export { DEFAULT_MULTIPART_THRESHOLD, S3Provider, S3UploadError, createLocalStorageStore, createMemoryStore, createS3Client, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, isFetchDownload, loadRouteCatalog, mergeRouteUploadPolicy, multipartResumeKey, resolveRouteUploadPolicy, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFormatDimahError, useFormatValidateFileError, useObjectUrl, useRouteUploadPolicy, useUpload };
|
|
2464
|
+
export { DEFAULT_MULTIPART_THRESHOLD, S3Provider, S3UploadError, createLocalStorageStore, createMemoryStore, createS3Client, defineApi, formatAcceptLabels, formatEta, formatSpeed, formatUploadProgress, isFetchDownload, loadRouteCatalog, mergeRouteUploadPolicy, multipartResumeKey, readObjectListLevel, resolveRouteUploadPolicy, uploadFile, uploadFiles, useApi, useDelete, useDownload, useFormatDimahError, useFormatFileSize, useFormatNumber, useFormatValidateFileError, useObjectList, useObjectUrl, useRouteUploadPolicy, useUpload };
|
|
2095
2465
|
//# sourceMappingURL=index.js.map
|
|
2096
2466
|
//# sourceMappingURL=index.js.map
|