@jsudc/react 0.4.0 → 0.5.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 +589 -11
- package/dist/actions/RowActionsMenu.d.ts +45 -0
- package/dist/actions/RowActionsMenu.js +161 -0
- package/dist/actions/RowActionsMenu.js.map +1 -0
- package/dist/actions/bulk.d.ts +81 -0
- package/dist/actions/bulk.js +54 -0
- package/dist/actions/bulk.js.map +1 -0
- package/dist/actions/row.d.ts +50 -0
- package/dist/actions/row.js +34 -0
- package/dist/actions/row.js.map +1 -0
- package/dist/actions/useRowActionRunner.d.ts +26 -0
- package/dist/actions/useRowActionRunner.js +198 -0
- package/dist/actions/useRowActionRunner.js.map +1 -0
- package/dist/calendar/CalendarRenderer.js +4 -3
- package/dist/calendar/CalendarRenderer.js.map +1 -1
- package/dist/cards/CardRenderer.d.ts +6 -0
- package/dist/cards/CardRenderer.js +42 -7
- package/dist/cards/CardRenderer.js.map +1 -1
- package/dist/chart/ChartRenderer.js +6 -4
- package/dist/chart/ChartRenderer.js.map +1 -1
- package/dist/chart/ChartWindowControl.js +1 -1
- package/dist/data-view/BulkBar.d.ts +36 -0
- package/dist/data-view/BulkBar.js +239 -0
- package/dist/data-view/BulkBar.js.map +1 -0
- package/dist/data-view/FilePreview.js +1 -1
- package/dist/data-view/FilterBuilder.js +1 -1
- package/dist/data-view/FilterChips.js +1 -1
- package/dist/data-view/LiveIndicator.d.ts +10 -0
- package/dist/data-view/LiveIndicator.js +33 -0
- package/dist/data-view/LiveIndicator.js.map +1 -0
- package/dist/data-view/UdcDataView.d.ts +28 -1
- package/dist/data-view/UdcDataView.js +69 -8
- package/dist/data-view/UdcDataView.js.map +1 -1
- package/dist/headless.d.ts +6 -0
- package/dist/headless.js +9 -2
- package/dist/i18n/translations.js +128 -0
- package/dist/i18n/translations.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +13 -4
- package/dist/kanban/KanbanCard.js +1 -1
- package/dist/kanban/KanbanRenderer.js +5 -4
- package/dist/kanban/KanbanRenderer.js.map +1 -1
- package/dist/lookup/batch.js +1 -1
- package/dist/lookup/options.js +4 -1
- package/dist/lookup/options.js.map +1 -1
- package/dist/lookup/useUdcLookup.js +9 -3
- package/dist/lookup/useUdcLookup.js.map +1 -1
- package/dist/map/MapRenderer.js +3 -2
- package/dist/map/MapRenderer.js.map +1 -1
- package/dist/renderers/types.d.ts +32 -1
- package/dist/renderers/types.js.map +1 -1
- package/dist/rows/useRowActivation.d.ts +17 -0
- package/dist/rows/useRowActivation.js +37 -5
- package/dist/rows/useRowActivation.js.map +1 -1
- package/dist/rows/useRowSelection.d.ts +92 -0
- package/dist/rows/useRowSelection.js +156 -0
- package/dist/rows/useRowSelection.js.map +1 -0
- package/dist/schema-editor/DeleteFieldDialog.js +1 -1
- package/dist/schema-editor/FieldFormDialog.js +1 -1
- package/dist/styles.css +1 -1
- package/dist/styles.scoped.css +1 -1
- package/dist/table/CellEditor.js +1 -1
- package/dist/table/SelectCheckbox.d.ts +15 -0
- package/dist/table/SelectCheckbox.js +37 -0
- package/dist/table/SelectCheckbox.js.map +1 -0
- package/dist/table/TableRenderer.d.ts +13 -0
- package/dist/table/TableRenderer.js +281 -123
- package/dist/table/TableRenderer.js.map +1 -1
- package/dist/table/cells.js +1 -1
- package/dist/table/index.d.ts +1 -0
- package/dist/table/states.js +1 -1
- package/dist/table/useRowWindow.d.ts +82 -0
- package/dist/table/useRowWindow.js +182 -0
- package/dist/table/useRowWindow.js.map +1 -0
- package/dist/table.js.map +1 -1
- package/dist/tree/TreeRenderer.d.ts +2 -0
- package/dist/tree/TreeRenderer.js +15 -9
- package/dist/tree/TreeRenderer.js.map +1 -1
- package/dist/udc/changeFeed.d.ts +94 -0
- package/dist/udc/changeFeed.js +250 -0
- package/dist/udc/changeFeed.js.map +1 -0
- package/dist/udc/changes.d.ts +34 -0
- package/dist/udc/changes.js +79 -0
- package/dist/udc/changes.js.map +1 -0
- package/dist/udc/client.js +5 -2
- package/dist/udc/client.js.map +1 -1
- package/dist/udc/hooks.d.ts +93 -20
- package/dist/udc/hooks.js +113 -10
- package/dist/udc/hooks.js.map +1 -1
- package/dist/udc/keys.d.ts +2 -0
- package/dist/udc/keys.js.map +1 -1
- package/dist/udc/listCache.d.ts +11 -0
- package/dist/udc/listCache.js +38 -0
- package/dist/udc/listCache.js.map +1 -0
- package/dist/udc/provider.d.ts +17 -2
- package/dist/udc/provider.js +18 -2
- package/dist/udc/provider.js.map +1 -1
- package/dist/udc/unauthenticated.d.ts +4 -0
- package/dist/udc/unauthenticated.js +17 -0
- package/dist/udc/unauthenticated.js.map +1 -0
- package/dist/ui-kit/types.d.ts +14 -0
- package/package.json +3 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Capabilities, type ChangeEvent } from "@jsudc/core";
|
|
2
|
+
import type { UdcChangeFeed, UdcChangeFeedStatus } from "./changeFeed.js";
|
|
3
|
+
/** The provider's change feed; `null` when `UdcProvider changes` is off (the default). */
|
|
4
|
+
export declare const UdcChangeFeedContext: import("react").Context<UdcChangeFeed | null>;
|
|
5
|
+
/** The provider's change feed, or `null` without one. */
|
|
6
|
+
export declare function useUdcChangeFeed(): UdcChangeFeed | null;
|
|
7
|
+
export interface UseUdcChangesOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The collection's capabilities when the caller has them (`describe`): with
|
|
10
|
+
* `changes` set, no `describe` is asked; without it the hook reads the
|
|
11
|
+
* cached `describe` of the collection (only when the provider has a feed).
|
|
12
|
+
*/
|
|
13
|
+
capabilities?: Pick<Capabilities, "changes"> | undefined;
|
|
14
|
+
/** `false`: no subscription (a hidden or disabled view). Default `true`. */
|
|
15
|
+
enabled?: boolean | undefined;
|
|
16
|
+
}
|
|
17
|
+
export interface UdcChangesResult {
|
|
18
|
+
/** Where the feed stands for this collection (see `UdcChangeFeedStatus`). */
|
|
19
|
+
status: UdcChangeFeedStatus;
|
|
20
|
+
/** `status === "live"`: the stream carries the collection, timer polling is suspended. */
|
|
21
|
+
live: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Follow the change feed of `collection`: while it is mounted the provider's
|
|
25
|
+
* stream carries the collection (`UdcProvider changes`), its lists and lookups
|
|
26
|
+
* are refetched when the server says its rows changed, and `onEvent` (if
|
|
27
|
+
* given) hears each `ChangeEvent` as it arrives (e.g. a badge counter).
|
|
28
|
+
* Answers where the feed stands; `live` is when the views of the collection
|
|
29
|
+
* stop polling. Without a feed in the provider, or for a collection that does
|
|
30
|
+
* not offer `capabilities.changes`, it does nothing and answers `off`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function useUdcChanges(collection: string, onEvent?: (event: ChangeEvent) => void, opts?: UseUdcChangesOptions): UdcChangesResult;
|
|
33
|
+
/** `useUdcChanges` for several collections at once, without a status (the lookup hooks). */
|
|
34
|
+
export declare function useUdcChangesMany(collections: readonly string[], opts?: Omit<UseUdcChangesOptions, "capabilities">): void;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { UdcClientContext } from "./client.js";
|
|
2
|
+
import { udcKeys } from "./keys.js";
|
|
3
|
+
import { sourcePushesChanges } from "@jsudc/core";
|
|
4
|
+
import { createContext, useContext, useEffect, useRef, useSyncExternalStore } from "react";
|
|
5
|
+
import { QueryClientContext, useQueries } from "@tanstack/react-query";
|
|
6
|
+
// src/udc/changes.ts
|
|
7
|
+
/** The provider's change feed; `null` when `UdcProvider changes` is off (the default). */
|
|
8
|
+
var UdcChangeFeedContext = createContext(null);
|
|
9
|
+
/** The provider's change feed, or `null` without one. */
|
|
10
|
+
function useUdcChangeFeed() {
|
|
11
|
+
return useContext(UdcChangeFeedContext);
|
|
12
|
+
}
|
|
13
|
+
var noSubscribe = () => () => {};
|
|
14
|
+
/**
|
|
15
|
+
* Subscribe `collections` to the provider's feed, each one that offers
|
|
16
|
+
* `capabilities.changes`. Nothing at all (no `describe`, no connection) when
|
|
17
|
+
* the provider has no feed. Answers the collections that are subscribed.
|
|
18
|
+
*/
|
|
19
|
+
function useFeedSubscriptions(collections, onEvent, opts) {
|
|
20
|
+
const feed = useContext(UdcChangeFeedContext);
|
|
21
|
+
const client = useContext(UdcClientContext);
|
|
22
|
+
const qc = useContext(QueryClientContext);
|
|
23
|
+
const wanted = feed !== null && client !== null && qc !== void 0 && (opts.enabled ?? true) ? [...new Set(collections.filter((c) => c !== ""))] : [];
|
|
24
|
+
const known = opts.capabilities?.changes;
|
|
25
|
+
const described = useQueries({ queries: known === void 0 && client ? wanted.map((c) => ({
|
|
26
|
+
queryKey: udcKeys.describe(c),
|
|
27
|
+
queryFn: () => client.describe(c),
|
|
28
|
+
staleTime: 3e5
|
|
29
|
+
})) : [] });
|
|
30
|
+
const offered = known === void 0 ? wanted.filter((_, i) => sourcePushesChanges(described[i]?.data?.capabilities)) : known ? wanted : [];
|
|
31
|
+
const onEventRef = useRef(onEvent);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
onEventRef.current = onEvent;
|
|
34
|
+
});
|
|
35
|
+
const hasOnEvent = onEvent !== void 0;
|
|
36
|
+
const key = offered.join("\n");
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!feed || !qc || key === "") return;
|
|
39
|
+
const listener = hasOnEvent ? (e) => onEventRef.current?.(e) : void 0;
|
|
40
|
+
const offs = key.split("\n").map((c) => feed.subscribe(c, qc, listener));
|
|
41
|
+
return () => {
|
|
42
|
+
for (const off of offs) off();
|
|
43
|
+
};
|
|
44
|
+
}, [
|
|
45
|
+
feed,
|
|
46
|
+
qc,
|
|
47
|
+
key,
|
|
48
|
+
hasOnEvent
|
|
49
|
+
]);
|
|
50
|
+
return {
|
|
51
|
+
feed,
|
|
52
|
+
offered
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Follow the change feed of `collection`: while it is mounted the provider's
|
|
57
|
+
* stream carries the collection (`UdcProvider changes`), its lists and lookups
|
|
58
|
+
* are refetched when the server says its rows changed, and `onEvent` (if
|
|
59
|
+
* given) hears each `ChangeEvent` as it arrives (e.g. a badge counter).
|
|
60
|
+
* Answers where the feed stands; `live` is when the views of the collection
|
|
61
|
+
* stop polling. Without a feed in the provider, or for a collection that does
|
|
62
|
+
* not offer `capabilities.changes`, it does nothing and answers `off`.
|
|
63
|
+
*/
|
|
64
|
+
function useUdcChanges(collection, onEvent, opts = {}) {
|
|
65
|
+
const { feed, offered } = useFeedSubscriptions([collection], onEvent, opts);
|
|
66
|
+
const subscribed = offered.length > 0;
|
|
67
|
+
const status = useSyncExternalStore(feed ? (l) => feed.onStatus(l) : noSubscribe, () => feed && subscribed ? feed.status(collection) : "off", () => "off");
|
|
68
|
+
return {
|
|
69
|
+
status,
|
|
70
|
+
live: status === "live"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** `useUdcChanges` for several collections at once, without a status (the lookup hooks). */
|
|
74
|
+
function useUdcChangesMany(collections, opts = {}) {
|
|
75
|
+
useFeedSubscriptions(collections, void 0, opts);
|
|
76
|
+
}
|
|
77
|
+
export { UdcChangeFeedContext, useUdcChangeFeed, useUdcChanges, useUdcChangesMany };
|
|
78
|
+
|
|
79
|
+
//# sourceMappingURL=changes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"changes.js","names":[],"sources":["../../src/udc/changes.ts"],"sourcesContent":["import { type Capabilities, type ChangeEvent, sourcePushesChanges } from \"@jsudc/core\";\nimport { QueryClientContext, useQueries } from \"@tanstack/react-query\";\nimport { createContext, useContext, useEffect, useRef, useSyncExternalStore } from \"react\";\nimport type { UdcChangeFeed, UdcChangeFeedStatus } from \"./changeFeed.js\";\nimport { UdcClientContext } from \"./client.js\";\nimport { udcKeys } from \"./keys.js\";\n\n/** The provider's change feed; `null` when `UdcProvider changes` is off (the default). */\nexport const UdcChangeFeedContext = createContext<UdcChangeFeed | null>(null);\n\n/** The provider's change feed, or `null` without one. */\nexport function useUdcChangeFeed(): UdcChangeFeed | null {\n return useContext(UdcChangeFeedContext);\n}\n\nexport interface UseUdcChangesOptions {\n /**\n * The collection's capabilities when the caller has them (`describe`): with\n * `changes` set, no `describe` is asked; without it the hook reads the\n * cached `describe` of the collection (only when the provider has a feed).\n */\n capabilities?: Pick<Capabilities, \"changes\"> | undefined;\n /** `false`: no subscription (a hidden or disabled view). Default `true`. */\n enabled?: boolean | undefined;\n}\n\nexport interface UdcChangesResult {\n /** Where the feed stands for this collection (see `UdcChangeFeedStatus`). */\n status: UdcChangeFeedStatus;\n /** `status === \"live\"`: the stream carries the collection, timer polling is suspended. */\n live: boolean;\n}\n\nconst noSubscribe = () => () => {};\n\n/**\n * Subscribe `collections` to the provider's feed, each one that offers\n * `capabilities.changes`. Nothing at all (no `describe`, no connection) when\n * the provider has no feed. Answers the collections that are subscribed.\n */\nfunction useFeedSubscriptions(\n collections: readonly string[],\n onEvent: ((event: ChangeEvent) => void) | undefined,\n opts: UseUdcChangesOptions,\n): { feed: UdcChangeFeed | null; offered: string[] } {\n const feed = useContext(UdcChangeFeedContext);\n const client = useContext(UdcClientContext);\n const qc = useContext(QueryClientContext);\n const on = feed !== null && client !== null && qc !== undefined && (opts.enabled ?? true);\n const wanted = on ? [...new Set(collections.filter((c) => c !== \"\"))] : [];\n const known = opts.capabilities?.changes;\n // The capability, from `describe` (cached for minutes, shared with the view that shows it).\n const described = useQueries({\n queries:\n known === undefined && client\n ? wanted.map((c) => ({\n queryKey: udcKeys.describe(c),\n queryFn: () => client.describe(c),\n staleTime: 5 * 60_000,\n }))\n : [],\n });\n const offered =\n known === undefined\n ? wanted.filter((_, i) => sourcePushesChanges(described[i]?.data?.capabilities))\n : known\n ? wanted\n : [];\n\n const onEventRef = useRef(onEvent);\n useEffect(() => {\n onEventRef.current = onEvent;\n });\n const hasOnEvent = onEvent !== undefined;\n const key = offered.join(\"\\n\");\n useEffect(() => {\n if (!feed || !qc || key === \"\") return;\n const listener = hasOnEvent ? (e: ChangeEvent) => onEventRef.current?.(e) : undefined;\n const offs = key.split(\"\\n\").map((c) => feed.subscribe(c, qc, listener));\n return () => {\n for (const off of offs) off();\n };\n }, [feed, qc, key, hasOnEvent]);\n return { feed, offered };\n}\n\n/**\n * Follow the change feed of `collection`: while it is mounted the provider's\n * stream carries the collection (`UdcProvider changes`), its lists and lookups\n * are refetched when the server says its rows changed, and `onEvent` (if\n * given) hears each `ChangeEvent` as it arrives (e.g. a badge counter).\n * Answers where the feed stands; `live` is when the views of the collection\n * stop polling. Without a feed in the provider, or for a collection that does\n * not offer `capabilities.changes`, it does nothing and answers `off`.\n */\nexport function useUdcChanges(\n collection: string,\n onEvent?: (event: ChangeEvent) => void,\n opts: UseUdcChangesOptions = {},\n): UdcChangesResult {\n const { feed, offered } = useFeedSubscriptions([collection], onEvent, opts);\n const subscribed = offered.length > 0;\n const status = useSyncExternalStore(\n feed ? (l: () => void) => feed.onStatus(l) : noSubscribe,\n () => (feed && subscribed ? feed.status(collection) : \"off\"),\n () => \"off\" as const,\n );\n return { status, live: status === \"live\" };\n}\n\n/** `useUdcChanges` for several collections at once, without a status (the lookup hooks). */\nexport function useUdcChangesMany(\n collections: readonly string[],\n opts: Omit<UseUdcChangesOptions, \"capabilities\"> = {},\n): void {\n useFeedSubscriptions(collections, undefined, opts);\n}\n"],"mappings":";;;;;;;AAQA,IAAa,uBAAuB,cAAoC,IAAI;;AAG5E,SAAgB,mBAAyC;CACvD,OAAO,WAAW,oBAAoB;AACxC;AAoBA,IAAM,0BAA0B,CAAC;;;;;;AAOjC,SAAS,qBACP,aACA,SACA,MACmD;CACnD,MAAM,OAAO,WAAW,oBAAoB;CAC5C,MAAM,SAAS,WAAW,gBAAgB;CAC1C,MAAM,KAAK,WAAW,kBAAkB;CAExC,MAAM,SADK,SAAS,QAAQ,WAAW,QAAQ,OAAO,KAAA,MAAc,KAAK,WAAW,QAChE,CAAC,GAAG,IAAI,IAAI,YAAY,QAAQ,MAAM,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC;CACzE,MAAM,QAAQ,KAAK,cAAc;CAEjC,MAAM,YAAY,WAAW,EAC3B,SACE,UAAU,KAAA,KAAa,SACnB,OAAO,KAAK,OAAO;EACjB,UAAU,QAAQ,SAAS,CAAC;EAC5B,eAAe,OAAO,SAAS,CAAC;EAChC,WAAW;CACb,EAAE,IACF,CAAC,EACT,CAAC;CACD,MAAM,UACJ,UAAU,KAAA,IACN,OAAO,QAAQ,GAAG,MAAM,oBAAoB,UAAU,EAAE,EAAE,MAAM,YAAY,CAAC,IAC7E,QACE,SACA,CAAC;CAET,MAAM,aAAa,OAAO,OAAO;CACjC,gBAAgB;EACd,WAAW,UAAU;CACvB,CAAC;CACD,MAAM,aAAa,YAAY,KAAA;CAC/B,MAAM,MAAM,QAAQ,KAAK,IAAI;CAC7B,gBAAgB;EACd,IAAI,CAAC,QAAQ,CAAC,MAAM,QAAQ,IAAI;EAChC,MAAM,WAAW,cAAc,MAAmB,WAAW,UAAU,CAAC,IAAI,KAAA;EAC5E,MAAM,OAAO,IAAI,MAAM,IAAI,CAAC,CAAC,KAAK,MAAM,KAAK,UAAU,GAAG,IAAI,QAAQ,CAAC;EACvE,aAAa;GACX,KAAK,MAAM,OAAO,MAAM,IAAI;EAC9B;CACF,GAAG;EAAC;EAAM;EAAI;EAAK;CAAU,CAAC;CAC9B,OAAO;EAAE;EAAM;CAAQ;AACzB;;;;;;;;;;AAWA,SAAgB,cACd,YACA,SACA,OAA6B,CAAC,GACZ;CAClB,MAAM,EAAE,MAAM,YAAY,qBAAqB,CAAC,UAAU,GAAG,SAAS,IAAI;CAC1E,MAAM,aAAa,QAAQ,SAAS;CACpC,MAAM,SAAS,qBACb,QAAQ,MAAkB,KAAK,SAAS,CAAC,IAAI,mBACtC,QAAQ,aAAa,KAAK,OAAO,UAAU,IAAI,aAChD,KACR;CACA,OAAO;EAAE;EAAQ,MAAM,WAAW;CAAO;AAC3C;;AAGA,SAAgB,kBACd,aACA,OAAmD,CAAC,GAC9C;CACN,qBAAqB,aAAa,KAAA,GAAW,IAAI;AACnD"}
|
package/dist/udc/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { registerUnauthenticated } from "./unauthenticated.js";
|
|
2
2
|
import { isUdcError, isUnauthenticated } from "@jsudc/core";
|
|
3
|
+
import { createContext, useContext } from "react";
|
|
3
4
|
import { MutationCache, QueryCache, QueryClient } from "@tanstack/react-query";
|
|
4
5
|
// src/udc/client.ts
|
|
5
6
|
/** The client every hook talks to; `UdcProvider` sets it. */
|
|
@@ -32,7 +33,7 @@ function unauthenticatedNotifier(fn, quietMs) {
|
|
|
32
33
|
}
|
|
33
34
|
function createUdcQueryClient(options = {}) {
|
|
34
35
|
const notify = options.onUnauthenticated ? unauthenticatedNotifier(options.onUnauthenticated, options.unauthenticatedQuietMs ?? 2e3) : void 0;
|
|
35
|
-
|
|
36
|
+
const qc = new QueryClient({
|
|
36
37
|
...notify ? {
|
|
37
38
|
queryCache: new QueryCache({ onError: notify }),
|
|
38
39
|
mutationCache: new MutationCache({ onError: notify })
|
|
@@ -45,6 +46,8 @@ function createUdcQueryClient(options = {}) {
|
|
|
45
46
|
mutations: { retry: false }
|
|
46
47
|
}
|
|
47
48
|
});
|
|
49
|
+
if (notify) registerUnauthenticated(qc, notify);
|
|
50
|
+
return qc;
|
|
48
51
|
}
|
|
49
52
|
export { UdcClientContext, createUdcQueryClient, shouldRetry, useUdcClient };
|
|
50
53
|
|
package/dist/udc/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":[],"sources":["../../src/udc/client.ts"],"sourcesContent":["import { isUdcError, isUnauthenticated, type UdcError } from \"@jsudc/core\";\nimport type { UdcClient } from \"@jsudc/core/client\";\nimport { MutationCache, QueryCache, QueryClient } from \"@tanstack/react-query\";\nimport { createContext, useContext } from \"react\";\n\n/** The client every hook talks to; `UdcProvider` sets it. */\nexport const UdcClientContext = createContext<UdcClient | null>(null);\n\nexport function useUdcClient(): UdcClient {\n const client = useContext(UdcClientContext);\n if (!client) throw new Error(\"useUdcClient needs a <UdcProvider client={...}> above it\");\n return client;\n}\n\n/**\n * A protocol error is the server's answer, not a hiccup: asking again gives\n * the same answer. Only transport failures are retried, twice.\n */\nexport function shouldRetry(failureCount: number, error: unknown): boolean {\n return !isUdcError(error) && failureCount < 2;\n}\n\nexport interface UdcQueryClientOptions {\n /**\n * Called when a query or a mutation fails because the user is not signed in\n * (`isUnauthenticated`: an HTTP 401 in front of `/udc`, or the server's\n * in-protocol refusal) — the place to send the user to the host's login.\n * Once per burst: the failures that arrive together (every query of a page,\n * say) make one call; it fires again only after `unauthenticatedQuietMs`\n * without such a failure.\n */\n onUnauthenticated?: (error: UdcError) => void;\n /** The quiet time, in ms, that ends a burst of unauthenticated failures. Default 2000. */\n unauthenticatedQuietMs?: number;\n}\n\n/**\n * Calls `fn` on the first unauthenticated error and swallows the rest until\n * `quietMs` pass without one (each further error restarts that wait).\n */\nfunction unauthenticatedNotifier(\n fn: (error: UdcError) => void,\n quietMs: number,\n): (error: unknown) => void {\n let lastAt: number | null = null;\n return (error) => {\n if (!isUnauthenticated(error)) return;\n const now = Date.now();\n const inBurst = lastAt !== null && now - lastAt < quietMs;\n lastAt = now;\n if (!inBurst) fn(error as UdcError);\n };\n}\n\nexport function createUdcQueryClient(options: UdcQueryClientOptions = {}): QueryClient {\n const notify = options.onUnauthenticated\n ? unauthenticatedNotifier(options.onUnauthenticated, options.unauthenticatedQuietMs ?? 2000)\n : undefined;\n
|
|
1
|
+
{"version":3,"file":"client.js","names":[],"sources":["../../src/udc/client.ts"],"sourcesContent":["import { isUdcError, isUnauthenticated, type UdcError } from \"@jsudc/core\";\nimport type { UdcClient } from \"@jsudc/core/client\";\nimport { MutationCache, QueryCache, QueryClient } from \"@tanstack/react-query\";\nimport { createContext, useContext } from \"react\";\nimport { registerUnauthenticated } from \"./unauthenticated.js\";\n\n/** The client every hook talks to; `UdcProvider` sets it. */\nexport const UdcClientContext = createContext<UdcClient | null>(null);\n\nexport function useUdcClient(): UdcClient {\n const client = useContext(UdcClientContext);\n if (!client) throw new Error(\"useUdcClient needs a <UdcProvider client={...}> above it\");\n return client;\n}\n\n/**\n * A protocol error is the server's answer, not a hiccup: asking again gives\n * the same answer. Only transport failures are retried, twice.\n */\nexport function shouldRetry(failureCount: number, error: unknown): boolean {\n return !isUdcError(error) && failureCount < 2;\n}\n\nexport interface UdcQueryClientOptions {\n /**\n * Called when a query or a mutation fails because the user is not signed in\n * (`isUnauthenticated`: an HTTP 401 in front of `/udc`, or the server's\n * in-protocol refusal) — the place to send the user to the host's login.\n * Once per burst: the failures that arrive together (every query of a page,\n * say) make one call; it fires again only after `unauthenticatedQuietMs`\n * without such a failure.\n */\n onUnauthenticated?: (error: UdcError) => void;\n /** The quiet time, in ms, that ends a burst of unauthenticated failures. Default 2000. */\n unauthenticatedQuietMs?: number;\n}\n\n/**\n * Calls `fn` on the first unauthenticated error and swallows the rest until\n * `quietMs` pass without one (each further error restarts that wait).\n */\nfunction unauthenticatedNotifier(\n fn: (error: UdcError) => void,\n quietMs: number,\n): (error: unknown) => void {\n let lastAt: number | null = null;\n return (error) => {\n if (!isUnauthenticated(error)) return;\n const now = Date.now();\n const inBurst = lastAt !== null && now - lastAt < quietMs;\n lastAt = now;\n if (!inBurst) fn(error as UdcError);\n };\n}\n\nexport function createUdcQueryClient(options: UdcQueryClientOptions = {}): QueryClient {\n const notify = options.onUnauthenticated\n ? unauthenticatedNotifier(options.onUnauthenticated, options.unauthenticatedQuietMs ?? 2000)\n : undefined;\n const qc = new QueryClient({\n ...(notify\n ? {\n queryCache: new QueryCache({ onError: notify }),\n mutationCache: new MutationCache({ onError: notify }),\n }\n : {}),\n defaultOptions: {\n queries: { retry: shouldRetry, refetchOnWindowFocus: false },\n mutations: { retry: false },\n },\n });\n // The change feed's unauthenticated error takes the same path (and the same burst).\n if (notify) registerUnauthenticated(qc, notify);\n return qc;\n}\n"],"mappings":";;;;;;AAOA,IAAa,mBAAmB,cAAgC,IAAI;AAEpE,SAAgB,eAA0B;CACxC,MAAM,SAAS,WAAW,gBAAgB;CAC1C,IAAI,CAAC,QAAQ,MAAM,IAAI,MAAM,0DAA0D;CACvF,OAAO;AACT;;;;;AAMA,SAAgB,YAAY,cAAsB,OAAyB;CACzE,OAAO,CAAC,WAAW,KAAK,KAAK,eAAe;AAC9C;;;;;AAoBA,SAAS,wBACP,IACA,SAC0B;CAC1B,IAAI,SAAwB;CAC5B,QAAQ,UAAU;EAChB,IAAI,CAAC,kBAAkB,KAAK,GAAG;EAC/B,MAAM,MAAM,KAAK,IAAI;EACrB,MAAM,UAAU,WAAW,QAAQ,MAAM,SAAS;EAClD,SAAS;EACT,IAAI,CAAC,SAAS,GAAG,KAAiB;CACpC;AACF;AAEA,SAAgB,qBAAqB,UAAiC,CAAC,GAAgB;CACrF,MAAM,SAAS,QAAQ,oBACnB,wBAAwB,QAAQ,mBAAmB,QAAQ,0BAA0B,GAAI,IACzF,KAAA;CACJ,MAAM,KAAK,IAAI,YAAY;EACzB,GAAI,SACA;GACE,YAAY,IAAI,WAAW,EAAE,SAAS,OAAO,CAAC;GAC9C,eAAe,IAAI,cAAc,EAAE,SAAS,OAAO,CAAC;EACtD,IACA,CAAC;EACL,gBAAgB;GACd,SAAS;IAAE,OAAO;IAAa,sBAAsB;GAAM;GAC3D,WAAW,EAAE,OAAO,MAAM;EAC5B;CACF,CAAC;CAED,IAAI,QAAQ,wBAAwB,IAAI,MAAM;CAC9C,OAAO;AACT"}
|
package/dist/udc/hooks.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { type ActionRequest, type Capabilities, type FilterNode, type ListResponse, type Row, type SortKey } from "@jsudc/core";
|
|
1
|
+
import { type ActionRequest, type Capabilities, type FilterNode, type HostAction, type HostActionPayload, type HostActionResponse, type ListResponse, type Row, type SortKey } from "@jsudc/core";
|
|
2
2
|
import type { UdcClient } from "@jsudc/core/client";
|
|
3
|
-
import { type
|
|
3
|
+
import { type UseMutationOptions, type UseMutationResult } from "@tanstack/react-query";
|
|
4
4
|
import { type PagingMode } from "./keys.js";
|
|
5
|
+
import { applyToListRows, type ListCache } from "./listCache.js";
|
|
6
|
+
export { applyToListRows };
|
|
5
7
|
export declare function useUdcCollections(): import("@tanstack/react-query").UseQueryResult<{
|
|
6
8
|
collections: {
|
|
7
9
|
[x: string]: unknown;
|
|
@@ -19,6 +21,9 @@ export declare function useUdcCollections(): import("@tanstack/react-query").Use
|
|
|
19
21
|
source_yaml: boolean;
|
|
20
22
|
supported_filter_ops: ("eq" | "neq" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | "gt" | "gte" | "lt" | "lte" | "between" | "is_null" | "is_not_null")[];
|
|
21
23
|
paging?: ("cursor" | "offset")[] | undefined;
|
|
24
|
+
mutate_many?: boolean | undefined;
|
|
25
|
+
search?: "source" | undefined;
|
|
26
|
+
changes?: boolean | undefined;
|
|
22
27
|
};
|
|
23
28
|
source_kind: "postgres_jsonb" | "memory" | "filesystem" | "http_api" | "yaml_menu" | "postgres_table";
|
|
24
29
|
label?: string | undefined;
|
|
@@ -41,6 +46,9 @@ export declare function useUdcDescribe(collection: string, opts?: {
|
|
|
41
46
|
source_yaml: boolean;
|
|
42
47
|
supported_filter_ops: ("eq" | "neq" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | "gt" | "gte" | "lt" | "lte" | "between" | "is_null" | "is_not_null")[];
|
|
43
48
|
paging?: ("cursor" | "offset")[] | undefined;
|
|
49
|
+
mutate_many?: boolean | undefined;
|
|
50
|
+
search?: "source" | undefined;
|
|
51
|
+
changes?: boolean | undefined;
|
|
44
52
|
};
|
|
45
53
|
source_kind: "postgres_jsonb" | "memory" | "filesystem" | "http_api" | "yaml_menu" | "postgres_table";
|
|
46
54
|
schema: {
|
|
@@ -278,12 +286,7 @@ export declare function useUdcDescribe(collection: string, opts?: {
|
|
|
278
286
|
icon_size: number;
|
|
279
287
|
radius_field?: string | undefined;
|
|
280
288
|
popup_fields?: string[] | undefined;
|
|
281
|
-
center
|
|
282
|
-
/**
|
|
283
|
-
* The three schema edits. Each answers the new schema; `describe` (which
|
|
284
|
-
* derives the columns) and the lists are refetched afterwards.
|
|
285
|
-
*/
|
|
286
|
-
?: [number, number] | undefined;
|
|
289
|
+
center?: [number, number] | undefined;
|
|
287
290
|
regions?: {
|
|
288
291
|
[x: string]: unknown;
|
|
289
292
|
id: string;
|
|
@@ -373,6 +376,9 @@ export declare function useUdcDescribe(collection: string, opts?: {
|
|
|
373
376
|
when?: boolean | FilterNode | undefined;
|
|
374
377
|
icon?: string | undefined;
|
|
375
378
|
variant?: "default" | "outline" | "destructive" | undefined;
|
|
379
|
+
set?: Record<string, unknown> | undefined;
|
|
380
|
+
action?: string | undefined;
|
|
381
|
+
confirm?: string | boolean | undefined;
|
|
376
382
|
}[] | undefined;
|
|
377
383
|
bulk_actions?: {
|
|
378
384
|
[x: string]: unknown;
|
|
@@ -381,6 +387,9 @@ export declare function useUdcDescribe(collection: string, opts?: {
|
|
|
381
387
|
when?: boolean | FilterNode | undefined;
|
|
382
388
|
icon?: string | undefined;
|
|
383
389
|
variant?: "default" | "outline" | "destructive" | undefined;
|
|
390
|
+
set?: Record<string, unknown> | undefined;
|
|
391
|
+
action?: string | undefined;
|
|
392
|
+
confirm?: string | boolean | undefined;
|
|
384
393
|
}[] | undefined;
|
|
385
394
|
multiselect?: boolean | undefined;
|
|
386
395
|
row_actions_display?: "inline" | "menu" | undefined;
|
|
@@ -436,10 +445,22 @@ export interface UseUdcCollectionArgs {
|
|
|
436
445
|
/** 1-based; pages mode only. */
|
|
437
446
|
page?: number;
|
|
438
447
|
enabled?: boolean;
|
|
439
|
-
/**
|
|
440
|
-
|
|
441
|
-
|
|
448
|
+
/**
|
|
449
|
+
* From `describe`: a source without `cursor` paging is walked by offset in
|
|
450
|
+
* infinite mode too; `changes` says whether it offers the change feed.
|
|
451
|
+
*/
|
|
452
|
+
capabilities?: Pick<Capabilities, "paging" | "changes"> | undefined;
|
|
453
|
+
/**
|
|
454
|
+
* Poll: refetch the loaded rows this often (the view's `refresh_interval_ms`).
|
|
455
|
+
* Suspended while the provider's change feed carries the collection.
|
|
456
|
+
*/
|
|
442
457
|
refetchInterval?: number | undefined;
|
|
458
|
+
/**
|
|
459
|
+
* The search box's text, sent as `ListRequest.search` for a source that
|
|
460
|
+
* answers search itself (`capabilities.search === "source"`); blank or
|
|
461
|
+
* absent: no `search` in the request, nor in the query key.
|
|
462
|
+
*/
|
|
463
|
+
search?: string | undefined;
|
|
443
464
|
}
|
|
444
465
|
export interface UdcCollectionResult {
|
|
445
466
|
rows: Row[];
|
|
@@ -463,6 +484,10 @@ export declare function freshnessOf(res: Pick<ListResponse, "fetched_at" | "stal
|
|
|
463
484
|
fetchedAt: string | null;
|
|
464
485
|
stale: boolean;
|
|
465
486
|
};
|
|
487
|
+
/** `{ search }` for a non-blank text, else nothing: spread into a list request and its query key. */
|
|
488
|
+
export declare function searchPart(search: string | undefined): {
|
|
489
|
+
search?: string;
|
|
490
|
+
};
|
|
466
491
|
/** The `offset` a 1-based page asks for. */
|
|
467
492
|
export declare function pageOffset(page: number, limit: number): number;
|
|
468
493
|
/**
|
|
@@ -485,6 +510,8 @@ export interface FetchAllRowsArgs {
|
|
|
485
510
|
pageLimit?: number;
|
|
486
511
|
/** Keyset paging unless the source pages by offset only. */
|
|
487
512
|
cursor: boolean;
|
|
513
|
+
/** Sent as `ListRequest.search` when not blank (a source that answers search). */
|
|
514
|
+
search?: string | undefined;
|
|
488
515
|
signal?: AbortSignal;
|
|
489
516
|
onProgress?: (loaded: number, total: number | null) => void;
|
|
490
517
|
}
|
|
@@ -502,11 +529,16 @@ export interface UseUdcAllRowsArgs {
|
|
|
502
529
|
sort?: SortKey[];
|
|
503
530
|
/** Default 2,000. */
|
|
504
531
|
max?: number;
|
|
505
|
-
/** From `describe`: whether the source pages by cursor. */
|
|
506
|
-
capabilities?: Pick<Capabilities, "paging"> | undefined;
|
|
532
|
+
/** From `describe`: whether the source pages by cursor, and offers the change feed. */
|
|
533
|
+
capabilities?: Pick<Capabilities, "paging" | "changes"> | undefined;
|
|
507
534
|
enabled?: boolean;
|
|
508
|
-
/**
|
|
535
|
+
/**
|
|
536
|
+
* Poll: refetch every row this often (the view's `refresh_interval_ms`).
|
|
537
|
+
* Suspended while the provider's change feed carries the collection.
|
|
538
|
+
*/
|
|
509
539
|
refetchInterval?: number | undefined;
|
|
540
|
+
/** Sent as `ListRequest.search` when not blank (`capabilities.search === "source"`). */
|
|
541
|
+
search?: string | undefined;
|
|
510
542
|
}
|
|
511
543
|
export interface UdcAllRowsResult {
|
|
512
544
|
rows: Row[];
|
|
@@ -532,7 +564,6 @@ export interface UdcAllRowsResult {
|
|
|
532
564
|
* patches and invalidations reach it like every other list.
|
|
533
565
|
*/
|
|
534
566
|
export declare function useUdcAllRows(args: UseUdcAllRowsArgs): UdcAllRowsResult;
|
|
535
|
-
type ListCache = ListResponse | InfiniteData<ListResponse, unknown>;
|
|
536
567
|
/** `data` with every row whose id is `id` passed through `patch`. */
|
|
537
568
|
export declare function patchRowInList(data: ListCache | undefined, id: string, patch: (row: Row) => Row): ListCache | undefined;
|
|
538
569
|
export type UdcMutateVars = Omit<ActionRequest<"mutate">, "collection">;
|
|
@@ -543,7 +574,7 @@ export type UdcMutateVars = Omit<ActionRequest<"mutate">, "collection">;
|
|
|
543
574
|
* value that won; the error still reaches the caller. Lists are invalidated
|
|
544
575
|
* once the call settles.
|
|
545
576
|
*/
|
|
546
|
-
export declare function useUdcMutate(collection: string):
|
|
577
|
+
export declare function useUdcMutate(collection: string): UseMutationResult<{
|
|
547
578
|
row: {
|
|
548
579
|
[x: string]: unknown;
|
|
549
580
|
__meta: {
|
|
@@ -560,7 +591,7 @@ export declare function useUdcMutate(collection: string): import("@tanstack/reac
|
|
|
560
591
|
* derives the columns) and the lists are refetched afterwards.
|
|
561
592
|
*/
|
|
562
593
|
export declare function useUdcSchema(collection: string): {
|
|
563
|
-
addField:
|
|
594
|
+
addField: UseMutationResult<{
|
|
564
595
|
schema: {
|
|
565
596
|
field_order: string[];
|
|
566
597
|
version: string;
|
|
@@ -616,7 +647,7 @@ export declare function useUdcSchema(collection: string): {
|
|
|
616
647
|
anchor?: string | undefined;
|
|
617
648
|
};
|
|
618
649
|
}, "collection">, unknown>;
|
|
619
|
-
updateField:
|
|
650
|
+
updateField: UseMutationResult<{
|
|
620
651
|
schema: {
|
|
621
652
|
field_order: string[];
|
|
622
653
|
version: string;
|
|
@@ -668,7 +699,7 @@ export declare function useUdcSchema(collection: string): {
|
|
|
668
699
|
} | undefined;
|
|
669
700
|
};
|
|
670
701
|
}, "collection">, unknown>;
|
|
671
|
-
softDeleteField:
|
|
702
|
+
softDeleteField: UseMutationResult<{
|
|
672
703
|
schema: {
|
|
673
704
|
field_order: string[];
|
|
674
705
|
version: string;
|
|
@@ -697,4 +728,46 @@ export declare function useUdcSchema(collection: string): {
|
|
|
697
728
|
};
|
|
698
729
|
}, Error, string, unknown>;
|
|
699
730
|
};
|
|
700
|
-
export
|
|
731
|
+
export type UdcMutateManyVars = Omit<ActionRequest<"mutate_many">, "collection">;
|
|
732
|
+
/**
|
|
733
|
+
* Which cached row a `mutate_many` request touches: one of its `ids`, or a row
|
|
734
|
+
* its `filter` matches (evaluated in the browser, `evaluateFilter`) that is
|
|
735
|
+
* not among `except_ids`. A filter the browser cannot evaluate touches no
|
|
736
|
+
* cached row: the invalidation after the call brings the server's answer.
|
|
737
|
+
*/
|
|
738
|
+
export declare function mutateManyTouches(vars: UdcMutateManyVars): (row: Row) => boolean;
|
|
739
|
+
/**
|
|
740
|
+
* Change (`update`, the patch in `row`) or delete many rows of a collection in
|
|
741
|
+
* one `mutate_many` request: by `ids`, or by `filter` with `except_ids` ("all
|
|
742
|
+
* that match, except these"). Like `useUdcMutate` for one row, the loaded
|
|
743
|
+
* rows are patched (or removed) in every cached list at once, put back if the
|
|
744
|
+
* call fails, and the collection's lists and lookups are invalidated once it
|
|
745
|
+
* settles. Resolves to the server's `{ updated, failed }`: `failed` names the
|
|
746
|
+
* ids that were not changed, and why.
|
|
747
|
+
*/
|
|
748
|
+
export declare function useUdcMutateMany(collection: string): UseMutationResult<{
|
|
749
|
+
updated: number;
|
|
750
|
+
failed: {
|
|
751
|
+
id: string;
|
|
752
|
+
code: "not_found" | "validation_error" | "version_conflict" | "forbidden" | "unsupported_op" | "internal";
|
|
753
|
+
message?: string | undefined;
|
|
754
|
+
}[];
|
|
755
|
+
}, Error, UdcMutateManyVars, {
|
|
756
|
+
snapshot: [readonly unknown[], ListCache | undefined][];
|
|
757
|
+
}>;
|
|
758
|
+
/** What `useUdcAction` takes besides TanStack's mutation options. */
|
|
759
|
+
export interface UseUdcActionOptions<Res, Req> extends Omit<UseMutationOptions<Res, Error, Req>, "mutationFn"> {
|
|
760
|
+
/** Collections whose lists and lookups to invalidate once the call settles (the rows it changed). */
|
|
761
|
+
invalidates?: readonly string[] | undefined;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* A mutation over the client's typed `call`, for any host action: pass the
|
|
765
|
+
* action's definition (`defineHostAction`, shared with the server) and the
|
|
766
|
+
* payload and the result are typed from its schemas; or pass a name and type
|
|
767
|
+
* it yourself (`useUdcAction<Res, Req>("mail.snooze")`). `invalidates` names
|
|
768
|
+
* the collections whose lists to refetch afterwards.
|
|
769
|
+
*/
|
|
770
|
+
export declare function useUdcAction<H extends HostAction>(action: H, options?: UseUdcActionOptions<HostActionResponse<H>, HostActionPayload<H>>): UseMutationResult<HostActionResponse<H>, Error, HostActionPayload<H>>;
|
|
771
|
+
export declare function useUdcAction<Res = unknown, Req = Record<string, unknown>>(action: string, options?: UseUdcActionOptions<Res, Req>): UseMutationResult<Res, Error, Req>;
|
|
772
|
+
/** `client.call` for a host action given by definition or by name (the answer unchecked by name). */
|
|
773
|
+
export declare function callAction(client: UdcClient, action: string | HostAction, payload: unknown): Promise<unknown>;
|
package/dist/udc/hooks.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { useUdcChanges } from "./changes.js";
|
|
1
2
|
import { useUdcClient } from "./client.js";
|
|
2
3
|
import { udcKeys } from "./keys.js";
|
|
4
|
+
import { applyToListRows, isInfinite } from "./listCache.js";
|
|
5
|
+
import { evaluateFilter, isVersionConflict } from "@jsudc/core";
|
|
3
6
|
import { useState } from "react";
|
|
4
|
-
import { isVersionConflict } from "@jsudc/core";
|
|
5
7
|
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
6
8
|
// src/udc/hooks.ts
|
|
7
9
|
function useUdcCollections() {
|
|
@@ -41,6 +43,11 @@ function freshnessOf(res) {
|
|
|
41
43
|
stale: res?.stale === true
|
|
42
44
|
};
|
|
43
45
|
}
|
|
46
|
+
/** `{ search }` for a non-blank text, else nothing: spread into a list request and its query key. */
|
|
47
|
+
function searchPart(search) {
|
|
48
|
+
const text = search?.trim();
|
|
49
|
+
return text ? { search: text } : {};
|
|
50
|
+
}
|
|
44
51
|
/** The `offset` a 1-based page asks for. */
|
|
45
52
|
function pageOffset(page, limit) {
|
|
46
53
|
return Math.max(0, Math.floor(page) - 1) * limit;
|
|
@@ -60,12 +67,17 @@ function useUdcCollection(args) {
|
|
|
60
67
|
const page = Math.max(1, args.page ?? 1);
|
|
61
68
|
const enabled = args.enabled ?? true;
|
|
62
69
|
const byCursor = args.capabilities?.paging?.includes("cursor") ?? true;
|
|
63
|
-
const refetchInterval =
|
|
70
|
+
const refetchInterval = useUdcChanges(collection, void 0, {
|
|
71
|
+
capabilities: args.capabilities,
|
|
72
|
+
enabled
|
|
73
|
+
}).live ? false : pollInterval(args.refetchInterval);
|
|
74
|
+
const searched = searchPart(args.search);
|
|
64
75
|
const infiniteParams = {
|
|
65
76
|
filter,
|
|
66
77
|
sort,
|
|
67
78
|
limit,
|
|
68
|
-
mode: "infinite"
|
|
79
|
+
mode: "infinite",
|
|
80
|
+
...searched
|
|
69
81
|
};
|
|
70
82
|
const infinite = useInfiniteQuery({
|
|
71
83
|
queryKey: udcKeys.list(collection, infiniteParams),
|
|
@@ -73,6 +85,7 @@ function useUdcCollection(args) {
|
|
|
73
85
|
collection,
|
|
74
86
|
...filter ? { filter } : {},
|
|
75
87
|
sort,
|
|
88
|
+
...searched,
|
|
76
89
|
page: typeof pageParam === "number" ? {
|
|
77
90
|
limit,
|
|
78
91
|
offset: pageParam
|
|
@@ -96,7 +109,8 @@ function useUdcCollection(args) {
|
|
|
96
109
|
sort,
|
|
97
110
|
limit,
|
|
98
111
|
mode: "pages",
|
|
99
|
-
page
|
|
112
|
+
page,
|
|
113
|
+
...searched
|
|
100
114
|
};
|
|
101
115
|
const paged = useQuery({
|
|
102
116
|
queryKey: udcKeys.list(collection, pagesParams),
|
|
@@ -104,6 +118,7 @@ function useUdcCollection(args) {
|
|
|
104
118
|
collection,
|
|
105
119
|
...filter ? { filter } : {},
|
|
106
120
|
sort,
|
|
121
|
+
...searched,
|
|
107
122
|
page: {
|
|
108
123
|
limit,
|
|
109
124
|
offset: pageOffset(page, limit)
|
|
@@ -172,6 +187,7 @@ async function fetchAllRows(client, args) {
|
|
|
172
187
|
collection,
|
|
173
188
|
...filter ? { filter } : {},
|
|
174
189
|
sort,
|
|
190
|
+
...searchPart(args.search),
|
|
175
191
|
page
|
|
176
192
|
}, args.signal ? { signal: args.signal } : void 0);
|
|
177
193
|
if (rows.length === 0) freshness = {
|
|
@@ -208,11 +224,17 @@ function useUdcAllRows(args) {
|
|
|
208
224
|
const max = args.max ?? 2e3;
|
|
209
225
|
const cursor = args.capabilities?.paging?.includes("cursor") ?? true;
|
|
210
226
|
const [progress, setProgress] = useState(null);
|
|
227
|
+
const feed = useUdcChanges(collection, void 0, {
|
|
228
|
+
capabilities: args.capabilities,
|
|
229
|
+
enabled: args.enabled ?? true
|
|
230
|
+
});
|
|
231
|
+
const searched = searchPart(args.search);
|
|
211
232
|
const key = {
|
|
212
233
|
filter,
|
|
213
234
|
sort,
|
|
214
235
|
limit: max,
|
|
215
|
-
mode: "all"
|
|
236
|
+
mode: "all",
|
|
237
|
+
...searched
|
|
216
238
|
};
|
|
217
239
|
const query = useQuery({
|
|
218
240
|
queryKey: udcKeys.list(collection, key),
|
|
@@ -228,6 +250,7 @@ function useUdcAllRows(args) {
|
|
|
228
250
|
sort,
|
|
229
251
|
max,
|
|
230
252
|
cursor,
|
|
253
|
+
...searched,
|
|
231
254
|
signal,
|
|
232
255
|
onProgress: (loaded, total) => setProgress({
|
|
233
256
|
loaded,
|
|
@@ -240,7 +263,7 @@ function useUdcAllRows(args) {
|
|
|
240
263
|
},
|
|
241
264
|
placeholderData: keepPreviousData,
|
|
242
265
|
enabled: args.enabled ?? true,
|
|
243
|
-
refetchInterval: pollInterval(args.refetchInterval)
|
|
266
|
+
refetchInterval: feed.live ? false : pollInterval(args.refetchInterval)
|
|
244
267
|
});
|
|
245
268
|
const rows = query.data?.rows ?? [];
|
|
246
269
|
const total = query.data?.total ?? null;
|
|
@@ -256,9 +279,6 @@ function useUdcAllRows(args) {
|
|
|
256
279
|
...freshnessOf(query.data)
|
|
257
280
|
};
|
|
258
281
|
}
|
|
259
|
-
function isInfinite(data) {
|
|
260
|
-
return "pages" in data;
|
|
261
|
-
}
|
|
262
282
|
/** `data` with every row whose id is `id` passed through `patch`. */
|
|
263
283
|
function patchRowInList(data, id, patch) {
|
|
264
284
|
if (!data) return data;
|
|
@@ -356,6 +376,89 @@ function useUdcSchema(collection) {
|
|
|
356
376
|
})
|
|
357
377
|
};
|
|
358
378
|
}
|
|
359
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Which cached row a `mutate_many` request touches: one of its `ids`, or a row
|
|
381
|
+
* its `filter` matches (evaluated in the browser, `evaluateFilter`) that is
|
|
382
|
+
* not among `except_ids`. A filter the browser cannot evaluate touches no
|
|
383
|
+
* cached row: the invalidation after the call brings the server's answer.
|
|
384
|
+
*/
|
|
385
|
+
function mutateManyTouches(vars) {
|
|
386
|
+
if (vars.ids) {
|
|
387
|
+
const ids = new Set(vars.ids);
|
|
388
|
+
return (row) => ids.has(row.__meta.id);
|
|
389
|
+
}
|
|
390
|
+
const except = new Set(vars.except_ids ?? []);
|
|
391
|
+
const filter = vars.filter;
|
|
392
|
+
return (row) => {
|
|
393
|
+
if (except.has(row.__meta.id)) return false;
|
|
394
|
+
try {
|
|
395
|
+
return evaluateFilter(filter, row);
|
|
396
|
+
} catch {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Change (`update`, the patch in `row`) or delete many rows of a collection in
|
|
403
|
+
* one `mutate_many` request: by `ids`, or by `filter` with `except_ids` ("all
|
|
404
|
+
* that match, except these"). Like `useUdcMutate` for one row, the loaded
|
|
405
|
+
* rows are patched (or removed) in every cached list at once, put back if the
|
|
406
|
+
* call fails, and the collection's lists and lookups are invalidated once it
|
|
407
|
+
* settles. Resolves to the server's `{ updated, failed }`: `failed` names the
|
|
408
|
+
* ids that were not changed, and why.
|
|
409
|
+
*/
|
|
410
|
+
function useUdcMutateMany(collection) {
|
|
411
|
+
const client = useUdcClient();
|
|
412
|
+
const qc = useQueryClient();
|
|
413
|
+
return useMutation({
|
|
414
|
+
mutationFn: (vars) => client.mutateMany({
|
|
415
|
+
collection,
|
|
416
|
+
...vars
|
|
417
|
+
}),
|
|
418
|
+
onMutate: async (vars) => {
|
|
419
|
+
await qc.cancelQueries({ queryKey: udcKeys.lists(collection) });
|
|
420
|
+
const snapshot = qc.getQueriesData({ queryKey: udcKeys.lists(collection) });
|
|
421
|
+
const touches = mutateManyTouches(vars);
|
|
422
|
+
const set = vars.row;
|
|
423
|
+
const patch = vars.op === "update" && set ? (r) => ({
|
|
424
|
+
...r,
|
|
425
|
+
...set,
|
|
426
|
+
__meta: r.__meta
|
|
427
|
+
}) : void 0;
|
|
428
|
+
if (vars.op === "delete" || patch) qc.setQueriesData({ queryKey: udcKeys.lists(collection) }, (data) => applyToListRows(data, touches, patch));
|
|
429
|
+
return { snapshot };
|
|
430
|
+
},
|
|
431
|
+
onError: (_err, _vars, ctx) => {
|
|
432
|
+
for (const [key, data] of ctx?.snapshot ?? []) qc.setQueryData(key, data);
|
|
433
|
+
},
|
|
434
|
+
onSettled: () => Promise.all([
|
|
435
|
+
qc.invalidateQueries({ queryKey: udcKeys.lists(collection) }),
|
|
436
|
+
qc.invalidateQueries({ queryKey: udcKeys.lookups(collection) }),
|
|
437
|
+
qc.invalidateQueries({ queryKey: [
|
|
438
|
+
"udc",
|
|
439
|
+
"lookup-options",
|
|
440
|
+
collection
|
|
441
|
+
] })
|
|
442
|
+
])
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
function useUdcAction(action, options = {}) {
|
|
446
|
+
const client = useUdcClient();
|
|
447
|
+
const qc = useQueryClient();
|
|
448
|
+
const { invalidates, onSettled, ...rest } = options;
|
|
449
|
+
return useMutation({
|
|
450
|
+
...rest,
|
|
451
|
+
mutationFn: (payload) => callAction(client, action, payload),
|
|
452
|
+
onSettled: async (...args) => {
|
|
453
|
+
await Promise.all((invalidates ?? []).flatMap((c) => [qc.invalidateQueries({ queryKey: udcKeys.lists(c) }), qc.invalidateQueries({ queryKey: udcKeys.lookups(c) })]));
|
|
454
|
+
return onSettled?.(...args);
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
/** `client.call` for a host action given by definition or by name (the answer unchecked by name). */
|
|
459
|
+
function callAction(client, action, payload) {
|
|
460
|
+
return client.call.bind(client)(action, payload);
|
|
461
|
+
}
|
|
462
|
+
export { ALL_ROWS_MAX, applyToListRows, callAction, fetchAllRows, freshnessOf, mutateManyTouches, pageOffset, patchRowInList, pollInterval, searchPart, useUdcAction, useUdcAllRows, useUdcCollection, useUdcCollections, useUdcDescribe, useUdcMutate, useUdcMutateMany, useUdcSchema, useUdcSource };
|
|
360
463
|
|
|
361
464
|
//# sourceMappingURL=hooks.js.map
|