@dimah-s3/react 2.0.0-beta.2 → 2.0.0-beta.4
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 +4 -4
- package/dist/hooks/use-object-list.d.ts +16 -2
- package/dist/hooks/use-object-list.d.ts.map +1 -1
- package/dist/index.js +96 -1
- package/dist/index.js.map +1 -1
- package/dist/list/types.d.ts +18 -0
- package/dist/list/types.d.ts.map +1 -1
- package/dist/translations/types.d.ts +4 -0
- package/dist/translations/types.d.ts.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# @dimah-s3/react
|
|
2
2
|
|
|
3
|
-
**2.0 beta.** Install with `@beta`. `@latest` stays on 1.x, and betas can include breaking changes until 2.0.0.
|
|
3
|
+
**2.0 beta.** Install with `@beta`. `@latest` stays on 1.x ([v1.s3.dimah.dev](https://v1.s3.dimah.dev/docs)), and betas can include breaking changes until 2.0.0.
|
|
4
4
|
|
|
5
5
|
Headless React hooks for S3 upload, download, and delete flows.
|
|
6
6
|
|
|
7
|
-
Full documentation: [
|
|
8
|
-
[llms.txt](https://
|
|
7
|
+
Full documentation: [s3.dimah.dev/docs/react](https://s3.dimah.dev/docs/react) ·
|
|
8
|
+
[llms.txt](https://s3.dimah.dev/llms.txt)
|
|
9
9
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
@@ -68,7 +68,7 @@ export function AvatarPicker() {
|
|
|
68
68
|
- `APIError`, `S3_ERROR_CODES`, and the other protocol helpers come from
|
|
69
69
|
`@dimah-s3/core`.
|
|
70
70
|
|
|
71
|
-
For prebuilt components, see [`@dimah-s3/ui`](https://
|
|
71
|
+
For prebuilt components, see [`@dimah-s3/ui`](https://s3.dimah.dev/docs/react/ui).
|
|
72
72
|
|
|
73
73
|
## License
|
|
74
74
|
|
|
@@ -53,8 +53,9 @@ export type UseObjectListReturn<TItem> = {
|
|
|
53
53
|
/** Change the search text. Reloads the first page. Debounce it yourself. */
|
|
54
54
|
setSearch: (search: string) => void;
|
|
55
55
|
/**
|
|
56
|
-
* Edit the loaded files without a request, for example after
|
|
57
|
-
* The next reload replaces them.
|
|
56
|
+
* Edit the loaded files without a request, for example after your own
|
|
57
|
+
* update. The next reload replaces them. Prefer `renameFile` when the
|
|
58
|
+
* source can rename.
|
|
58
59
|
*/
|
|
59
60
|
setItems: (items: Updater<readonly TItem[]>) => void;
|
|
60
61
|
/** Edit the loaded folders without a request. */
|
|
@@ -73,6 +74,19 @@ export type UseObjectListReturn<TItem> = {
|
|
|
73
74
|
* `createFolder`.
|
|
74
75
|
*/
|
|
75
76
|
createFolder: ((name: string) => Promise<ObjectListFolder>) | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* Renames a file through the source and replaces the loaded row.
|
|
79
|
+
* Rejects with an `APIError`. The same name does nothing. A name sort
|
|
80
|
+
* or a search reloads afterwards, and so does a source that returns
|
|
81
|
+
* nothing. Set when the source has `renameFile`.
|
|
82
|
+
*/
|
|
83
|
+
renameFile: ((item: TItem, name: string) => Promise<TItem | void>) | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Renames a folder through the source, replaces the loaded row, and
|
|
86
|
+
* updates that id in the breadcrumb. Rejects with an `APIError`. The
|
|
87
|
+
* same name does nothing. Set when the source has `renameFolder`.
|
|
88
|
+
*/
|
|
89
|
+
renameFolder: ((folder: ObjectListFolder, name: string) => Promise<ObjectListFolder | void>) | undefined;
|
|
76
90
|
};
|
|
77
91
|
/**
|
|
78
92
|
* Page through a folder catalog without knowing the backend.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-object-list.d.ts","sourceRoot":"","sources":["../../src/hooks/use-object-list.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAI/C,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,eAAe,E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ACjC,OAAO,EAAE,oBAAoB,CAAC,KAAK,CAAC,GACnC,mBAAmB,CAAC,KAAK,CAAC,CAyd5B"}
|
package/dist/index.js
CHANGED
|
@@ -2156,6 +2156,96 @@ function useObjectList(options) {
|
|
|
2156
2156
|
},
|
|
2157
2157
|
[optionsRef, requestRef, update]
|
|
2158
2158
|
);
|
|
2159
|
+
const settleRename = useCallback(
|
|
2160
|
+
async (scope, replaced) => {
|
|
2161
|
+
if (requestRef.current.scope !== scope) return;
|
|
2162
|
+
if (!replaced || namesNeedReload(requestRef.current)) {
|
|
2163
|
+
await run("refresh");
|
|
2164
|
+
}
|
|
2165
|
+
},
|
|
2166
|
+
[requestRef, run]
|
|
2167
|
+
);
|
|
2168
|
+
const renameFile = useCallback(
|
|
2169
|
+
async (item, name) => {
|
|
2170
|
+
const rename = optionsRef.current.renameFile;
|
|
2171
|
+
if (!rename) {
|
|
2172
|
+
throw new Error("[dimah-s3] The source has no renameFile.");
|
|
2173
|
+
}
|
|
2174
|
+
const { getFile: read, getKey: keyOf } = optionsRef.current;
|
|
2175
|
+
if ((read ? read(item).name : keyOf(item)) === name) return item;
|
|
2176
|
+
const key = keyOf(item);
|
|
2177
|
+
const { scope } = requestRef.current;
|
|
2178
|
+
let next;
|
|
2179
|
+
try {
|
|
2180
|
+
next = await rename({ item, name });
|
|
2181
|
+
} catch (err) {
|
|
2182
|
+
throw toClientError(err);
|
|
2183
|
+
}
|
|
2184
|
+
if (next) {
|
|
2185
|
+
const replacement = next;
|
|
2186
|
+
update((draft) => {
|
|
2187
|
+
if (draft.snapshot.scope !== scope) return draft;
|
|
2188
|
+
const idOf = optionsRef.current.getKey;
|
|
2189
|
+
return {
|
|
2190
|
+
...draft,
|
|
2191
|
+
snapshot: {
|
|
2192
|
+
...draft.snapshot,
|
|
2193
|
+
items: draft.snapshot.items.map(
|
|
2194
|
+
(current) => idOf(current) === key ? replacement : current
|
|
2195
|
+
)
|
|
2196
|
+
}
|
|
2197
|
+
};
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
await settleRename(scope, next != null);
|
|
2201
|
+
return next;
|
|
2202
|
+
},
|
|
2203
|
+
[optionsRef, requestRef, settleRename, update]
|
|
2204
|
+
);
|
|
2205
|
+
const renameFolder = useCallback(
|
|
2206
|
+
async (folder, name) => {
|
|
2207
|
+
const rename = optionsRef.current.renameFolder;
|
|
2208
|
+
if (!rename) {
|
|
2209
|
+
throw new Error("[dimah-s3] The source has no renameFolder.");
|
|
2210
|
+
}
|
|
2211
|
+
if (folder.name === name) return folder;
|
|
2212
|
+
const { scope } = requestRef.current;
|
|
2213
|
+
let next;
|
|
2214
|
+
try {
|
|
2215
|
+
next = await rename({ folder, name });
|
|
2216
|
+
} catch (err) {
|
|
2217
|
+
throw toClientError(err);
|
|
2218
|
+
}
|
|
2219
|
+
if (next) {
|
|
2220
|
+
const replacement = next;
|
|
2221
|
+
update((draft) => {
|
|
2222
|
+
if (draft.snapshot.scope !== scope) return draft;
|
|
2223
|
+
return {
|
|
2224
|
+
...draft,
|
|
2225
|
+
snapshot: {
|
|
2226
|
+
...draft.snapshot,
|
|
2227
|
+
folders: draft.snapshot.folders.map(
|
|
2228
|
+
(current) => current.id === folder.id ? replacement : current
|
|
2229
|
+
)
|
|
2230
|
+
}
|
|
2231
|
+
};
|
|
2232
|
+
});
|
|
2233
|
+
const currentTrail = trailRef.current;
|
|
2234
|
+
if (currentTrail.some(
|
|
2235
|
+
(entry) => entry.id === folder.id && entry.name !== replacement.name
|
|
2236
|
+
)) {
|
|
2237
|
+
const renamedTrail = currentTrail.map(
|
|
2238
|
+
(entry) => entry.id === folder.id ? { ...entry, name: replacement.name } : entry
|
|
2239
|
+
);
|
|
2240
|
+
trailRef.current = renamedTrail;
|
|
2241
|
+
setTrailState(renamedTrail);
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
await settleRename(scope, next != null);
|
|
2245
|
+
return next;
|
|
2246
|
+
},
|
|
2247
|
+
[optionsRef, requestRef, settleRename, trailRef, update]
|
|
2248
|
+
);
|
|
2159
2249
|
const setSort = useCallback(
|
|
2160
2250
|
(next) => setSortValue(next),
|
|
2161
2251
|
[setSortValue]
|
|
@@ -2218,7 +2308,9 @@ function useObjectList(options) {
|
|
|
2218
2308
|
getKey,
|
|
2219
2309
|
getFile,
|
|
2220
2310
|
uploadOptions: options.uploadOptions,
|
|
2221
|
-
createFolder: options.createFolder ? createFolder : void 0
|
|
2311
|
+
createFolder: options.createFolder ? createFolder : void 0,
|
|
2312
|
+
renameFile: options.renameFile ? renameFile : void 0,
|
|
2313
|
+
renameFolder: options.renameFolder ? renameFolder : void 0
|
|
2222
2314
|
};
|
|
2223
2315
|
}
|
|
2224
2316
|
function readStatus({
|
|
@@ -2236,6 +2328,9 @@ function readStatus({
|
|
|
2236
2328
|
if (pending === "refresh") return hasRows ? "refreshing" : "loading";
|
|
2237
2329
|
return failure ? "error" : "ready";
|
|
2238
2330
|
}
|
|
2331
|
+
function namesNeedReload(request) {
|
|
2332
|
+
return request.sort?.field === "name" || request.search !== "";
|
|
2333
|
+
}
|
|
2239
2334
|
function sortsEqual(a, b) {
|
|
2240
2335
|
return a === b || a?.field === b?.field && a?.direction === b?.direction;
|
|
2241
2336
|
}
|