@asteby/metacore-runtime-react 53.7.0 → 53.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +3 -1
- package/dist/api-context.d.ts.map +1 -1
- package/dist/api-context.js +15 -0
- package/dist/cache-scope.d.ts +52 -0
- package/dist/cache-scope.d.ts.map +1 -0
- package/dist/cache-scope.js +200 -0
- package/dist/capability-providers/index.d.ts +4 -0
- package/dist/capability-providers/index.d.ts.map +1 -0
- package/dist/capability-providers/index.js +3 -0
- package/dist/capability-providers/model.d.ts +201 -0
- package/dist/capability-providers/model.d.ts.map +1 -0
- package/dist/capability-providers/model.js +293 -0
- package/dist/capability-providers/provider-sortable-list.d.ts +86 -0
- package/dist/capability-providers/provider-sortable-list.d.ts.map +1 -0
- package/dist/capability-providers/provider-sortable-list.js +263 -0
- package/dist/capability-providers/reorder.d.ts +10 -0
- package/dist/capability-providers/reorder.d.ts.map +1 -0
- package/dist/capability-providers/reorder.js +31 -0
- package/dist/dashboard-types.d.ts +2 -4
- package/dist/dashboard-types.d.ts.map +1 -1
- package/dist/dynamic-kanban.d.ts.map +1 -1
- package/dist/dynamic-kanban.js +9 -4
- package/dist/dynamic-relation.d.ts.map +1 -1
- package/dist/dynamic-relation.js +5 -2
- package/dist/dynamic-table.d.ts +2 -0
- package/dist/dynamic-table.d.ts.map +1 -1
- package/dist/dynamic-table.js +40 -6
- package/dist/dynamic-view.d.ts.map +1 -1
- package/dist/dynamic-view.js +3 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/metadata-cache.d.ts +22 -2
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +73 -13
- package/dist/query-batch.d.ts +12 -0
- package/dist/query-batch.d.ts.map +1 -1
- package/dist/query-batch.js +70 -29
- package/dist/use-option-ids.d.ts.map +1 -1
- package/dist/use-option-ids.js +3 -1
- package/dist/use-options-resolver.d.ts +4 -1
- package/dist/use-options-resolver.d.ts.map +1 -1
- package/dist/use-options-resolver.js +27 -16
- package/dist/widgets/charts.d.ts.map +1 -1
- package/dist/widgets/charts.js +11 -43
- package/dist/widgets/widget-format.d.ts +1 -41
- package/dist/widgets/widget-format.d.ts.map +1 -1
- package/dist/widgets/widget-format.js +3 -138
- package/package.json +4 -4
- package/src/__tests__/charts-reexport.test.ts +30 -0
- package/src/action-modal-dispatcher.tsx +3 -1
- package/src/api-context.tsx +14 -0
- package/src/cache-scope-inflight.test.ts +369 -0
- package/src/cache-scope.test.ts +234 -0
- package/src/cache-scope.ts +202 -0
- package/src/capability-providers/index.ts +46 -0
- package/src/capability-providers/model.test.ts +186 -0
- package/src/capability-providers/model.ts +464 -0
- package/src/capability-providers/provider-sortable-list.test.tsx +284 -0
- package/src/capability-providers/provider-sortable-list.tsx +452 -0
- package/src/capability-providers/reorder.ts +29 -0
- package/src/dashboard-types.ts +2 -11
- package/src/dynamic-kanban.tsx +9 -4
- package/src/dynamic-relation.tsx +5 -2
- package/src/dynamic-table.tsx +41 -10
- package/src/dynamic-view.tsx +3 -1
- package/src/index.ts +13 -0
- package/src/metadata-cache.ts +85 -15
- package/src/query-batch.ts +71 -30
- package/src/use-option-ids.ts +3 -1
- package/src/use-options-resolver.ts +28 -15
- package/src/widgets/charts.tsx +26 -189
- package/src/widgets/widget-format.ts +14 -181
- package/vitest.setup.ts +16 -0
package/dist/metadata-cache.js
CHANGED
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
// including `actions[]`. Skipping the live fetch on a cache hit is how
|
|
11
11
|
// cross-addon toolbar actions disappear after an addon upgrade while
|
|
12
12
|
// localStorage still holds the pre-upgrade catalog.
|
|
13
|
+
// - Entries are keyed by the `org:user:lang` scope (see cache-scope.ts):
|
|
14
|
+
// metadata carries the user's permissions, so a different account,
|
|
15
|
+
// organization or language never sees them. Unknown scope = no cache.
|
|
16
|
+
// Hosts call `clearMetadataCache()` on logout.
|
|
13
17
|
// - `prefetchAll` replaces the catalog from `/metadata/all` and advances
|
|
14
18
|
// `metadataVersion`; a version change wipes the prior cache.
|
|
15
19
|
//
|
|
@@ -18,6 +22,7 @@
|
|
|
18
22
|
// never invokes prefetchAll, the `api` dep is not required.
|
|
19
23
|
import { create } from 'zustand';
|
|
20
24
|
import { persist } from 'zustand/middleware';
|
|
25
|
+
import { cacheScope, onCacheScopeChange, peekCacheScope } from './cache-scope';
|
|
21
26
|
/**
|
|
22
27
|
* Default matcher used by {@link MetadataCacheState.invalidateAddon}.
|
|
23
28
|
* Mirrors the convention used by the kernel installer when it scopes
|
|
@@ -32,25 +37,43 @@ export function defaultAddonKeyMatcher(cacheKey, addonKey) {
|
|
|
32
37
|
cacheKey.startsWith(`${addonKey}:`) ||
|
|
33
38
|
cacheKey.startsWith(`${addonKey}/`));
|
|
34
39
|
}
|
|
40
|
+
/** Scope to write under, or null when the answer must be dropped. */
|
|
41
|
+
function writeScope(opts) {
|
|
42
|
+
const now = cacheScope();
|
|
43
|
+
if (!now)
|
|
44
|
+
return null;
|
|
45
|
+
if (opts && opts.scope !== undefined && opts.scope !== now)
|
|
46
|
+
return null;
|
|
47
|
+
return now;
|
|
48
|
+
}
|
|
35
49
|
export const useMetadataCache = create()(persist((set, get) => ({
|
|
36
50
|
cache: {},
|
|
37
51
|
modalCache: {},
|
|
38
52
|
metadataVersion: '',
|
|
39
53
|
prefetched: false,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
scope: '',
|
|
55
|
+
getMetadata: (key) => (inScope(get()) ? get().cache[key] : undefined),
|
|
56
|
+
getModalMetadata: (key) => (inScope(get()) ? get().modalCache[key] : undefined),
|
|
57
|
+
setMetadata: (key, metadata, opts) => {
|
|
58
|
+
const scope = writeScope(opts);
|
|
59
|
+
if (!scope)
|
|
60
|
+
return;
|
|
43
61
|
set((state) => ({
|
|
44
|
-
|
|
62
|
+
...(state.scope === scope ? {} : emptyFor(scope)),
|
|
63
|
+
cache: { ...(state.scope === scope ? state.cache : {}), [key]: metadata },
|
|
45
64
|
}));
|
|
46
65
|
},
|
|
47
|
-
setModalMetadata: (key, metadata) => {
|
|
66
|
+
setModalMetadata: (key, metadata, opts) => {
|
|
67
|
+
const scope = writeScope(opts);
|
|
68
|
+
if (!scope)
|
|
69
|
+
return;
|
|
48
70
|
set((state) => ({
|
|
49
|
-
|
|
71
|
+
...(state.scope === scope ? {} : emptyFor(scope)),
|
|
72
|
+
modalCache: { ...(state.scope === scope ? state.modalCache : {}), [key]: metadata },
|
|
50
73
|
}));
|
|
51
74
|
},
|
|
52
|
-
hasMetadata: (key) => key in get().cache,
|
|
53
|
-
hasModalMetadata: (key) => key in get().modalCache,
|
|
75
|
+
hasMetadata: (key) => inScope(get()) && key in get().cache,
|
|
76
|
+
hasModalMetadata: (key) => inScope(get()) && key in get().modalCache,
|
|
54
77
|
invalidateAddon: (addonKey, matcher = defaultAddonKeyMatcher) => {
|
|
55
78
|
if (!addonKey)
|
|
56
79
|
return 0;
|
|
@@ -90,16 +113,21 @@ export const useMetadataCache = create()(persist((set, get) => ({
|
|
|
90
113
|
modalCache: {},
|
|
91
114
|
metadataVersion: '',
|
|
92
115
|
prefetched: false,
|
|
116
|
+
scope: '',
|
|
93
117
|
});
|
|
94
118
|
},
|
|
95
119
|
prefetchAll: async (api) => {
|
|
96
|
-
|
|
120
|
+
const scope = cacheScope();
|
|
121
|
+
// Unknown identity: do not fetch into a cache nobody may read.
|
|
122
|
+
if (!scope)
|
|
123
|
+
return;
|
|
124
|
+
if (get().prefetched && get().scope === scope)
|
|
97
125
|
return;
|
|
98
126
|
try {
|
|
99
127
|
const res = await api.get('/metadata/all');
|
|
100
128
|
const { tables, modals, version } = res.data.data;
|
|
101
129
|
const serverVersion = version || '';
|
|
102
|
-
const localVersion = get().metadataVersion;
|
|
130
|
+
const localVersion = get().scope === scope ? get().metadataVersion : '';
|
|
103
131
|
// Always rebuild from the server catalog. Merging into a
|
|
104
132
|
// persisted cache left hosts with pre-upgrade actions /
|
|
105
133
|
// columns after an addon HotRegister while the tab (or
|
|
@@ -116,32 +144,64 @@ export const useMetadataCache = create()(persist((set, get) => ({
|
|
|
116
144
|
newModalCache[key] = meta;
|
|
117
145
|
}
|
|
118
146
|
}
|
|
147
|
+
// The identity moved while the request was in flight: this
|
|
148
|
+
// answer belongs to the previous scope, drop it.
|
|
149
|
+
if (peekCacheScope() !== scope)
|
|
150
|
+
return;
|
|
119
151
|
set({
|
|
120
152
|
cache: newCache,
|
|
121
153
|
modalCache: newModalCache,
|
|
122
154
|
metadataVersion: serverVersion || localVersion || String(Date.now()),
|
|
123
155
|
prefetched: true,
|
|
156
|
+
scope,
|
|
124
157
|
});
|
|
125
158
|
}
|
|
126
159
|
catch {
|
|
127
|
-
// Offline or error — keep using cached data.
|
|
128
|
-
|
|
160
|
+
// Offline or error — keep using cached data of this scope.
|
|
161
|
+
if (get().scope === scope)
|
|
162
|
+
set({ prefetched: true });
|
|
129
163
|
}
|
|
130
164
|
},
|
|
131
165
|
}), {
|
|
132
166
|
name: 'metacore-metadata-cache',
|
|
133
167
|
// Bump when display hints (e.g. image_stack) must wipe stale cache.
|
|
134
168
|
// v5: always replace catalog on prefetchAll (no merge-with-stale).
|
|
135
|
-
|
|
169
|
+
// v6: entries are keyed by the org:user:lang scope; anything
|
|
170
|
+
// persisted before has no owner and is discarded.
|
|
171
|
+
version: 6,
|
|
136
172
|
migrate: () => ({
|
|
137
173
|
cache: {},
|
|
138
174
|
modalCache: {},
|
|
139
175
|
metadataVersion: '',
|
|
140
176
|
prefetched: false,
|
|
177
|
+
scope: '',
|
|
141
178
|
}),
|
|
142
179
|
partialize: (state) => ({
|
|
143
180
|
cache: state.cache,
|
|
144
181
|
modalCache: state.modalCache,
|
|
145
182
|
metadataVersion: state.metadataVersion,
|
|
183
|
+
scope: state.scope,
|
|
146
184
|
}),
|
|
147
185
|
}));
|
|
186
|
+
function inScope(state) {
|
|
187
|
+
const scope = peekCacheScope();
|
|
188
|
+
return !!scope && state.scope === scope;
|
|
189
|
+
}
|
|
190
|
+
function emptyFor(scope) {
|
|
191
|
+
return { cache: {}, modalCache: {}, metadataVersion: '', prefetched: false, scope };
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Logout / account switch: drops the in-memory metadata AND the persisted copy
|
|
195
|
+
* (`metacore-metadata-cache`). Hosts call it when the session ends; the cache
|
|
196
|
+
* also clears itself when the org/user/lang scope changes.
|
|
197
|
+
*/
|
|
198
|
+
export function clearMetadataCache() {
|
|
199
|
+
useMetadataCache.getState().clearAll();
|
|
200
|
+
try {
|
|
201
|
+
useMetadataCache.persist.clearStorage();
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
/* storage unavailable */
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
onCacheScopeChange(() => clearMetadataCache());
|
package/dist/query-batch.d.ts
CHANGED
|
@@ -13,6 +13,11 @@ export interface QueryPart {
|
|
|
13
13
|
message?: string;
|
|
14
14
|
etag?: string;
|
|
15
15
|
not_modified?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* HTTP-like status of this part. Servers that do not send it leave it
|
|
18
|
+
* undefined and the message heuristics apply (v2 adds it).
|
|
19
|
+
*/
|
|
20
|
+
status?: number;
|
|
16
21
|
}
|
|
17
22
|
export interface InListToken {
|
|
18
23
|
field: string;
|
|
@@ -41,6 +46,13 @@ export declare function narrowInToken(token: string, now?: number): NarrowedIn |
|
|
|
41
46
|
*/
|
|
42
47
|
export declare function rememberInRows(token: string, ids: string[], rows: unknown, now?: number): void;
|
|
43
48
|
export declare function resetQueryBatchCache(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Drops everything the batch remembers (parts, entities) and rejects reads
|
|
51
|
+
* still waiting for the window, so nothing asked under the previous identity
|
|
52
|
+
* resolves after it ended. Hosts call it on logout; it also runs by itself
|
|
53
|
+
* when the org/user/lang scope changes.
|
|
54
|
+
*/
|
|
55
|
+
export declare function clearQueryBatchCaches(): void;
|
|
44
56
|
/**
|
|
45
57
|
* Forgets every row, record and option read. useApi calls it after each
|
|
46
58
|
* mutation so the list that just gained, changed or lost a row reads it
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-batch.d.ts","sourceRoot":"","sources":["../src/query-batch.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"query-batch.d.ts","sourceRoot":"","sources":["../src/query-batch.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAG9C,MAAM,WAAW,SAAS;IACtB,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAClB;AA2CD,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,EAAE,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CAChB;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAuB9D;AAkBD,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;IAC/B,UAAU,EAAE,MAAM,EAAE,CAAA;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CAChB;AAED,0FAA0F;AAC1F,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,UAAU,GAAG,IAAI,CAwBhF;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC1B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EAAE,EACb,IAAI,EAAE,OAAO,EACb,GAAG,SAAa,GACjB,IAAI,CAmBN;AAaD,wBAAgB,oBAAoB,IAAI,IAAI,CAG3C;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAQ5C;AAID;;;;GAIG;AACH,wBAAgB,wBAAwB,IAAI,IAAI,CAM/C;AACD;;;;;GAKG;AACH,wBAAgB,uBAAuB,IAAI,IAAI,CAG9C;AAWD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG9D;AAED,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,MAAM,CAQR;AAED,oEAAoE;AACpE,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,CAYjG;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAwB/E;AAoFD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CA8BxF;AAwBD;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,GAAG,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE;;;GAsBxG"}
|
package/dist/query-batch.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cacheScope, onCacheScopeChange } from './cache-scope';
|
|
1
2
|
const WINDOW_MS = 8;
|
|
2
3
|
const PART_TTL_MS = 30_000;
|
|
3
4
|
const ENTITY_TTL_MS = 30_000;
|
|
@@ -74,6 +75,9 @@ export function narrowInToken(token, now = Date.now()) {
|
|
|
74
75
|
if (!parsed)
|
|
75
76
|
return null;
|
|
76
77
|
const scope = cacheScope();
|
|
78
|
+
// Unknown org/user/lang: never serve rows from memory.
|
|
79
|
+
if (!scope)
|
|
80
|
+
return null;
|
|
77
81
|
const rows = [];
|
|
78
82
|
const missingIds = [];
|
|
79
83
|
for (const id of parsed.ids) {
|
|
@@ -104,6 +108,8 @@ export function rememberInRows(token, ids, rows, now = Date.now()) {
|
|
|
104
108
|
if (!parsed)
|
|
105
109
|
return;
|
|
106
110
|
const scope = cacheScope();
|
|
111
|
+
if (!scope)
|
|
112
|
+
return;
|
|
107
113
|
const field = rowField(parsed.field);
|
|
108
114
|
const byId = new Map();
|
|
109
115
|
for (const id of ids)
|
|
@@ -133,13 +139,26 @@ function errorStatus(err) {
|
|
|
133
139
|
}
|
|
134
140
|
export function resetQueryBatchCache() {
|
|
135
141
|
unsupportedClients = new WeakSet();
|
|
142
|
+
clearQueryBatchCaches();
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Drops everything the batch remembers (parts, entities) and rejects reads
|
|
146
|
+
* still waiting for the window, so nothing asked under the previous identity
|
|
147
|
+
* resolves after it ended. Hosts call it on logout; it also runs by itself
|
|
148
|
+
* when the org/user/lang scope changes.
|
|
149
|
+
*/
|
|
150
|
+
export function clearQueryBatchCaches() {
|
|
136
151
|
partCache.clear();
|
|
137
152
|
entityCache.clear();
|
|
153
|
+
const pending = waiters;
|
|
138
154
|
waiters = [];
|
|
139
155
|
if (timer != null)
|
|
140
156
|
clearTimeout(timer);
|
|
141
157
|
timer = null;
|
|
158
|
+
for (const waiter of pending)
|
|
159
|
+
waiter.reject(new Error('batch cache cleared'));
|
|
142
160
|
}
|
|
161
|
+
onCacheScopeChange(() => clearQueryBatchCaches());
|
|
143
162
|
/**
|
|
144
163
|
* Forgets every row, record and option read. useApi calls it after each
|
|
145
164
|
* mutation so the list that just gained, changed or lost a row reads it
|
|
@@ -172,19 +191,6 @@ function mergeCachedRows(cached, part) {
|
|
|
172
191
|
const fresh = Array.isArray(part.data) ? part.data : [];
|
|
173
192
|
return { ...part, data: [...cached, ...fresh] };
|
|
174
193
|
}
|
|
175
|
-
function cacheScope() {
|
|
176
|
-
if (typeof localStorage === 'undefined')
|
|
177
|
-
return '';
|
|
178
|
-
try {
|
|
179
|
-
const raw = localStorage.getItem('auth_user');
|
|
180
|
-
if (!raw)
|
|
181
|
-
return '';
|
|
182
|
-
return String(JSON.parse(raw).organization_id ?? '');
|
|
183
|
-
}
|
|
184
|
-
catch {
|
|
185
|
-
return '';
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
194
|
export function optionsModelFromUrl(url) {
|
|
189
195
|
const match = url.match(/^\/options\/([A-Za-z][A-Za-z0-9_]*)$/);
|
|
190
196
|
return match ? match[1] : null;
|
|
@@ -218,7 +224,8 @@ export function nameBatchTokens(tokens) {
|
|
|
218
224
|
});
|
|
219
225
|
}
|
|
220
226
|
export function loadQueryPart(api, token) {
|
|
221
|
-
const
|
|
227
|
+
const scope = cacheScope();
|
|
228
|
+
const cached = scope ? partCache.get(`${scope}\n${token}`) : undefined;
|
|
222
229
|
if (cached && servedFromMemory(token) && Date.now() - cached.at < PART_TTL_MS && cached.part.success) {
|
|
223
230
|
return Promise.resolve(cached.part);
|
|
224
231
|
}
|
|
@@ -236,6 +243,7 @@ export function loadQueryPart(api, token) {
|
|
|
236
243
|
cachedRows: narrowed?.rows,
|
|
237
244
|
missingIds: narrowed?.missingIds,
|
|
238
245
|
field: narrowed?.field,
|
|
246
|
+
scope,
|
|
239
247
|
});
|
|
240
248
|
if (timer == null)
|
|
241
249
|
timer = setTimeout(() => void flushQueryBatch(), WINDOW_MS);
|
|
@@ -243,17 +251,23 @@ export function loadQueryPart(api, token) {
|
|
|
243
251
|
}
|
|
244
252
|
async function flushQueryBatch() {
|
|
245
253
|
timer = null;
|
|
246
|
-
const
|
|
254
|
+
const all = waiters;
|
|
247
255
|
waiters = [];
|
|
256
|
+
const scope = cacheScope();
|
|
257
|
+
// Reads asked under another identity never travel with this one.
|
|
258
|
+
const batch = all.filter((waiter) => waiter.scope === scope);
|
|
259
|
+
for (const waiter of all) {
|
|
260
|
+
if (waiter.scope !== scope)
|
|
261
|
+
waiter.reject(new Error('batch scope changed'));
|
|
262
|
+
}
|
|
248
263
|
const api = transport;
|
|
249
264
|
if (!api || batch.length === 0)
|
|
250
265
|
return;
|
|
251
266
|
const named = nameBatchTokens(batch.map((waiter) => waiter.wire));
|
|
252
267
|
const byToken = new Map(named.map((part) => [part.token, part.name]));
|
|
253
268
|
const inm = {};
|
|
254
|
-
const scope = cacheScope();
|
|
255
269
|
for (const part of named) {
|
|
256
|
-
const cached = partCache.get(`${scope}\n${part.token}`);
|
|
270
|
+
const cached = scope ? partCache.get(`${scope}\n${part.token}`) : undefined;
|
|
257
271
|
const etag = cached?.part.etag;
|
|
258
272
|
if (etag && (servedFromMemory(part.token) || contentEtag(etag)))
|
|
259
273
|
inm[part.name] = etag;
|
|
@@ -265,6 +279,13 @@ async function flushQueryBatch() {
|
|
|
265
279
|
throw new Error('batch unavailable');
|
|
266
280
|
}
|
|
267
281
|
for (const waiter of batch) {
|
|
282
|
+
// First of all: the identity may have changed while the request
|
|
283
|
+
// travelled. Neither the answer (rows or not_modified) nor the
|
|
284
|
+
// caches of the previous identity may reach this reader.
|
|
285
|
+
if (cacheScope() !== scope) {
|
|
286
|
+
waiter.reject(new Error('batch scope changed'));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
268
289
|
const name = byToken.get(waiter.wire);
|
|
269
290
|
const part = name ? body.parts[name] : undefined;
|
|
270
291
|
if (!part) {
|
|
@@ -272,7 +293,7 @@ async function flushQueryBatch() {
|
|
|
272
293
|
continue;
|
|
273
294
|
}
|
|
274
295
|
if (part.not_modified) {
|
|
275
|
-
const cached = partCache.get(`${scope}\n${waiter.token}`);
|
|
296
|
+
const cached = scope ? partCache.get(`${scope}\n${waiter.token}`) : undefined;
|
|
276
297
|
if (cached) {
|
|
277
298
|
cached.at = Date.now();
|
|
278
299
|
waiter.resolve(cached.part);
|
|
@@ -284,10 +305,10 @@ async function flushQueryBatch() {
|
|
|
284
305
|
continue;
|
|
285
306
|
}
|
|
286
307
|
const merged = mergeCachedRows(waiter.cachedRows, part);
|
|
287
|
-
if (waiter.missingIds && waiter.field) {
|
|
308
|
+
if (scope && waiter.missingIds && waiter.field) {
|
|
288
309
|
rememberInRows(waiter.token, waiter.missingIds, part.data);
|
|
289
310
|
}
|
|
290
|
-
if (part.success) {
|
|
311
|
+
if (scope && part.success) {
|
|
291
312
|
partCache.set(`${scope}\n${waiter.token}`, { part: merged, at: Date.now() });
|
|
292
313
|
}
|
|
293
314
|
waiter.resolve(merged);
|
|
@@ -350,8 +371,28 @@ export function tokenForGet(url, params) {
|
|
|
350
371
|
const limit = Number(search.get('limit'));
|
|
351
372
|
return optionsBatchToken(opt[1], search.get('field') || 'id', search.get('q') || search.get('search') || '', Number.isFinite(limit) && limit > 0 ? limit : undefined, search.get('filter_value') || undefined);
|
|
352
373
|
}
|
|
353
|
-
|
|
354
|
-
|
|
374
|
+
/**
|
|
375
|
+
* Reads a failed part. The per-part `status` (when the server sends it) wins:
|
|
376
|
+
* 403 forbidden, 424 dependency failed (rejects, no retry), 5xx/413 a failure
|
|
377
|
+
* that gets ONE direct GET. Without `status` the message heuristic applies.
|
|
378
|
+
*/
|
|
379
|
+
function failureKind(part) {
|
|
380
|
+
const status = part.status;
|
|
381
|
+
if (typeof status === 'number') {
|
|
382
|
+
if (status === 403)
|
|
383
|
+
return 'forbidden';
|
|
384
|
+
if (status === 424)
|
|
385
|
+
return 'dependency';
|
|
386
|
+
if (status === 413 || status >= 500)
|
|
387
|
+
return 'retry';
|
|
388
|
+
}
|
|
389
|
+
return /forbidden|permiso/i.test(part.message || '') ? 'forbidden' : 'plain';
|
|
390
|
+
}
|
|
391
|
+
function statusError(message, status) {
|
|
392
|
+
const err = new Error(message);
|
|
393
|
+
err.status = status;
|
|
394
|
+
err.response = { status };
|
|
395
|
+
return err;
|
|
355
396
|
}
|
|
356
397
|
/**
|
|
357
398
|
* GET through the batch when the URL is a list, table metadata, modal metadata
|
|
@@ -367,12 +408,12 @@ export async function batchGet(api, url, config) {
|
|
|
367
408
|
try {
|
|
368
409
|
const part = await loadQueryPart(api, token);
|
|
369
410
|
if (!part.success) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
throw
|
|
375
|
-
|
|
411
|
+
const kind = failureKind(part);
|
|
412
|
+
if (kind === 'forbidden')
|
|
413
|
+
throw statusError(part.message || 'forbidden', 403);
|
|
414
|
+
if (kind === 'dependency')
|
|
415
|
+
throw statusError(part.message || 'dependency failed', 424);
|
|
416
|
+
// 'retry' and 'plain': one direct GET, whose own error propagates.
|
|
376
417
|
return plainGet();
|
|
377
418
|
}
|
|
378
419
|
return { data: { success: true, data: part.data, meta: part.meta } };
|
|
@@ -380,7 +421,7 @@ export async function batchGet(api, url, config) {
|
|
|
380
421
|
catch (err) {
|
|
381
422
|
const status = err?.status
|
|
382
423
|
?? err?.response?.status;
|
|
383
|
-
if (status === 403)
|
|
424
|
+
if (status === 403 || status === 424)
|
|
384
425
|
throw err;
|
|
385
426
|
return plainGet();
|
|
386
427
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-option-ids.d.ts","sourceRoot":"","sources":["../src/use-option-ids.ts"],"names":[],"mappings":"AAuBA,OAAO,EAA0D,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAEpH,MAAM,MAAM,kBAAkB,GACxB;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,cAAc,CAAA;CAAE;AAC7C,qFAAqF;GACnF;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE;AACvB,4DAA4D;GAC1D;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CAAA;AAE3B,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,MAAM,CAAA;AA8EnC,MAAM,WAAW,uBAAuB;IACpC,mCAAmC;IACnC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,gDAAgD;IAChD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iEAAiE;IACjE,GAAG,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;IACjC,OAAO,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,yBAAyB;IACtC,yDAAyD;IACzD,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;IACjD,uCAAuC;IACvC,OAAO,EAAE,OAAO,CAAA;CACnB;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,uBAAuB,GAAG,yBAAyB,
|
|
1
|
+
{"version":3,"file":"use-option-ids.d.ts","sourceRoot":"","sources":["../src/use-option-ids.ts"],"names":[],"mappings":"AAuBA,OAAO,EAA0D,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAEpH,MAAM,MAAM,kBAAkB,GACxB;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,cAAc,CAAA;CAAE;AAC7C,qFAAqF;GACnF;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE;AACvB,4DAA4D;GAC1D;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CAAA;AAE3B,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,MAAM,CAAA;AA8EnC,MAAM,WAAW,uBAAuB;IACpC,mCAAmC;IACnC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,gDAAgD;IAChD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iEAAiE;IACjE,GAAG,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;IACjC,OAAO,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,yBAAyB;IACtC,yDAAyD;IACzD,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;IACjD,uCAAuC;IACvC,OAAO,EAAE,OAAO,CAAA;CACnB;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,uBAAuB,GAAG,yBAAyB,CAuC5F"}
|
package/dist/use-option-ids.js
CHANGED
|
@@ -116,7 +116,9 @@ export function useResolveOptionIds(args) {
|
|
|
116
116
|
queries: (active ? list : []).map((id) => ({
|
|
117
117
|
queryKey: [QUERY_ROOT, scope, url, effectiveField, id],
|
|
118
118
|
queryFn: () => enqueue(api, scope, url, effectiveField, id),
|
|
119
|
-
|
|
119
|
+
// Unknown identity: nothing may be reused from a previous account.
|
|
120
|
+
staleTime: scope ? OPTION_IDS_TTL_MS : 0,
|
|
121
|
+
...(scope ? {} : { gcTime: 0 }),
|
|
120
122
|
retry: false,
|
|
121
123
|
refetchOnWindowFocus: false,
|
|
122
124
|
})),
|
|
@@ -113,7 +113,10 @@ export declare function onOptionsCacheInvalidated(fn: () => void): () => void;
|
|
|
113
113
|
export declare function optionsRequestKey(scope: string, url: string, field: string, query: string, limit: number | undefined, filter: string | undefined, context?: Record<string, string>): string;
|
|
114
114
|
/** Normalized picker context: string values only, empty dropped (null when none). */
|
|
115
115
|
export declare function normalizeOptionsContext(context: Record<string, string | null | undefined> | undefined): Record<string, string> | undefined;
|
|
116
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* `org:user:lang` scope + branch of the host session: part of every options
|
|
118
|
+
* cache key. Empty when the identity is unknown (callers then skip the cache).
|
|
119
|
+
*/
|
|
117
120
|
export declare function optionsScope(): string;
|
|
118
121
|
export declare function useOptionsResolver(args: UseOptionsResolverArgs): UseOptionsResolverResult;
|
|
119
122
|
export declare function projectOption(raw: any): ResolvedOption;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-options-resolver.d.ts","sourceRoot":"","sources":["../src/use-options-resolver.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-options-resolver.d.ts","sourceRoot":"","sources":["../src/use-options-resolver.ts"],"names":[],"mappings":"AAeA,OAAO,EAAqB,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAE1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,MAAM,WAAW,cAAc;IAC3B,8CAA8C;IAC9C,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,2DAA2D;IAC3D,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IACtB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,8DAA8D;IAC9D,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAAA;IAClC;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC;AAED,MAAM,WAAW,WAAW;IACxB,oEAAoE;IACpE,IAAI,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAA;IACnC,2DAA2D;IAC3D,KAAK,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,sBAAsB;IACnC;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,YAAY,CAAC,EAAE,gBAAgB,EAAE,CAAA;IACjC,gFAAgF;IAChF,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAA;CACtD;AAED,MAAM,WAAW,wBAAwB;IACrC,OAAO,EAAE,cAAc,EAAE,CAAA;IACzB,IAAI,EAAE,WAAW,GAAG,IAAI,CAAA;IACxB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IACnB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAA;CACtB;AAuBD;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAK7C;AAOD,iFAAiF;AACjF,wBAAgB,yBAAyB,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAGpE;AAED,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,MAAM,CAKR;AAED,qFAAqF;AACrF,wBAAgB,uBAAuB,CACnC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,SAAS,GAC/D,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CASpC;AAUD;;;GAGG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAUrC;AAiBD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,sBAAsB,GAAG,wBAAwB,CAmLzF;AASD,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,cAAc,CAsBtD"}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
// free; legacy callers that still ship `searchEndpoint` keep working.
|
|
13
13
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
14
14
|
import { useApi } from './api-context';
|
|
15
|
+
import { cacheScope, onCacheScopeChange } from './cache-scope';
|
|
15
16
|
import { applyOptionFilter } from './option-filter';
|
|
16
17
|
import { loadQueryPart, optionsBatchToken, optionsModelFromUrl } from './query-batch';
|
|
17
18
|
/**
|
|
@@ -44,6 +45,8 @@ export function invalidateOptionsCache() {
|
|
|
44
45
|
fn();
|
|
45
46
|
}
|
|
46
47
|
const invalidationListeners = new Set();
|
|
48
|
+
// Options are shaped by who asks (permissions, org): drop them with the scope.
|
|
49
|
+
onCacheScopeChange(() => invalidateOptionsCache());
|
|
47
50
|
/** Runs `fn` on every invalidateOptionsCache (id-label lookups drop with it). */
|
|
48
51
|
export function onOptionsCacheInvalidated(fn) {
|
|
49
52
|
invalidationListeners.add(fn);
|
|
@@ -78,21 +81,22 @@ function contextKey(context) {
|
|
|
78
81
|
.map((k) => `${k}=${context[k]}`)
|
|
79
82
|
.join('&');
|
|
80
83
|
}
|
|
81
|
-
/**
|
|
84
|
+
/**
|
|
85
|
+
* `org:user:lang` scope + branch of the host session: part of every options
|
|
86
|
+
* cache key. Empty when the identity is unknown (callers then skip the cache).
|
|
87
|
+
*/
|
|
82
88
|
export function optionsScope() {
|
|
83
|
-
|
|
89
|
+
const scope = cacheScope();
|
|
90
|
+
if (!scope)
|
|
84
91
|
return '';
|
|
85
|
-
let
|
|
92
|
+
let branch = '';
|
|
86
93
|
try {
|
|
87
|
-
|
|
88
|
-
if (raw)
|
|
89
|
-
org = String(JSON.parse(raw).organization_id ?? '');
|
|
94
|
+
branch = typeof localStorage === 'undefined' ? '' : (localStorage.getItem('active_branch_id') ?? '');
|
|
90
95
|
}
|
|
91
96
|
catch {
|
|
92
|
-
|
|
97
|
+
branch = '';
|
|
93
98
|
}
|
|
94
|
-
|
|
95
|
-
return `${org}|${branch}`;
|
|
99
|
+
return `${scope}|${branch}`;
|
|
96
100
|
}
|
|
97
101
|
function readOptionsEnvelope(body) {
|
|
98
102
|
const rawOptions = Array.isArray(body?.data) ? body.data : [];
|
|
@@ -151,7 +155,10 @@ export function useOptionsResolver(args) {
|
|
|
151
155
|
setError(null);
|
|
152
156
|
return;
|
|
153
157
|
}
|
|
154
|
-
const
|
|
158
|
+
const askedScope = optionsScope();
|
|
159
|
+
// Unknown identity: no cache, no shared in-flight request.
|
|
160
|
+
const scoped = askedScope !== '';
|
|
161
|
+
const key = optionsRequestKey(askedScope, url, effectiveField, query ?? '', limit, filterValue, context);
|
|
155
162
|
if (refreshKey !== seenRefresh.current) {
|
|
156
163
|
seenRefresh.current = refreshKey;
|
|
157
164
|
optionsEpoch.set(key, (optionsEpoch.get(key) ?? 0) + 1);
|
|
@@ -160,7 +167,7 @@ export function useOptionsResolver(args) {
|
|
|
160
167
|
}
|
|
161
168
|
const epoch = optionsEpoch.get(key) ?? 0;
|
|
162
169
|
const generation = optionsGeneration;
|
|
163
|
-
const cached = optionsCache.get(key);
|
|
170
|
+
const cached = scoped ? optionsCache.get(key) : undefined;
|
|
164
171
|
if (cached && Date.now() - cached.at < OPTIONS_TTL_MS) {
|
|
165
172
|
setOptions(cached.payload.options);
|
|
166
173
|
setMeta(cached.payload.meta);
|
|
@@ -170,7 +177,7 @@ export function useOptionsResolver(args) {
|
|
|
170
177
|
}
|
|
171
178
|
setLoading(true);
|
|
172
179
|
setError(null);
|
|
173
|
-
let pending = optionsInflight.get(key);
|
|
180
|
+
let pending = scoped ? optionsInflight.get(key) : undefined;
|
|
174
181
|
if (!pending) {
|
|
175
182
|
const params = { field: effectiveField };
|
|
176
183
|
if (query)
|
|
@@ -194,14 +201,17 @@ export function useOptionsResolver(args) {
|
|
|
194
201
|
data: part.data,
|
|
195
202
|
meta: part.meta,
|
|
196
203
|
});
|
|
197
|
-
if ((optionsEpoch.get(key) ?? 0) === epoch && optionsGeneration === generation) {
|
|
204
|
+
if (scoped && optionsScope() === askedScope && (optionsEpoch.get(key) ?? 0) === epoch && optionsGeneration === generation) {
|
|
198
205
|
optionsCache.set(key, { payload, at: Date.now() });
|
|
199
206
|
}
|
|
200
207
|
return payload;
|
|
201
208
|
}
|
|
202
209
|
catch (err) {
|
|
203
210
|
const msg = err instanceof Error ? err.message : '';
|
|
204
|
-
const batchMiss = msg === 'batch unavailable' ||
|
|
211
|
+
const batchMiss = msg === 'batch unavailable' ||
|
|
212
|
+
msg === 'batch part missing' ||
|
|
213
|
+
msg === 'batch scope changed' ||
|
|
214
|
+
msg === 'batch cache cleared';
|
|
205
215
|
if (!batchMiss)
|
|
206
216
|
throw err;
|
|
207
217
|
}
|
|
@@ -212,7 +222,7 @@ export function useOptionsResolver(args) {
|
|
|
212
222
|
throw new Error(body?.message || 'options resolver: unsuccessful response');
|
|
213
223
|
}
|
|
214
224
|
const payload = readOptionsEnvelope(body);
|
|
215
|
-
if ((optionsEpoch.get(key) ?? 0) === epoch && optionsGeneration === generation) {
|
|
225
|
+
if (scoped && optionsScope() === askedScope && (optionsEpoch.get(key) ?? 0) === epoch && optionsGeneration === generation) {
|
|
216
226
|
optionsCache.set(key, { payload, at: Date.now() });
|
|
217
227
|
}
|
|
218
228
|
return payload;
|
|
@@ -220,7 +230,8 @@ export function useOptionsResolver(args) {
|
|
|
220
230
|
if (optionsInflight.get(key) === pending)
|
|
221
231
|
optionsInflight.delete(key);
|
|
222
232
|
});
|
|
223
|
-
|
|
233
|
+
if (scoped)
|
|
234
|
+
optionsInflight.set(key, pending);
|
|
224
235
|
}
|
|
225
236
|
let cancelled = false;
|
|
226
237
|
pending
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"charts.d.ts","sourceRoot":"","sources":["../../src/widgets/charts.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"charts.d.ts","sourceRoot":"","sources":["../../src/widgets/charts.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAU9B,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE/D,YAAY,EAAE,iBAAiB,EAAE,CAAA;AA4BjC,wBAAgB,SAAS,CAAC,CAAC,EAAE,iBAAiB,qBAE7C;AACD,wBAAgB,UAAU,CAAC,CAAC,EAAE,iBAAiB,qBAE9C;AACD,wBAAgB,UAAU,CAAC,CAAC,EAAE,iBAAiB,qBAE9C;AACD,wBAAgB,SAAS,CAAC,CAAC,EAAE,iBAAiB,qBAE7C;AACD,wBAAgB,WAAW,CAAC,CAAC,EAAE,iBAAiB,qBAE/C"}
|
package/dist/widgets/charts.js
CHANGED
|
@@ -1,55 +1,23 @@
|
|
|
1
|
-
import { jsx as _jsx
|
|
2
|
-
import {
|
|
3
|
-
import { cn } from '@asteby/metacore-ui/lib';
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { AreaChartBody, BarChartBody, DonutChartBody, LineChartBody, PieChartBody, } from '@asteby/metacore-charts/recharts';
|
|
4
3
|
import { WidgetCard, WidgetEmpty } from './widget-card';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// resolve. This height + the card chrome gives every chart card a consistent,
|
|
10
|
-
// taller footprint that balances cleanly against compact stat cards.
|
|
11
|
-
const CHART_H = 'h-[190px]';
|
|
12
|
-
function ChartArea({ children }) {
|
|
13
|
-
return (_jsx("div", { className: cn(CHART_H, 'w-full'), children: _jsx(ResponsiveContainer, { width: "100%", height: "100%", children: children }) }));
|
|
4
|
+
import { hasSeries } from './renderers';
|
|
5
|
+
function ChartWidget(p) {
|
|
6
|
+
const { Body } = p;
|
|
7
|
+
return (_jsx(WidgetCard, { "data-testid": `widget-${p.spec.key}`, title: p.spec.title, subtitle: p.spec.subtitle, icon: p.spec.icon, accent: p.spec.accent, children: hasSeries(p.data) ? (_jsx(Body, { series: p.data.series, accent: p.spec.accent, format: p.spec.format, locale: p.locale, currency: p.currency, id: p.spec.key })) : (_jsx(WidgetEmpty, { message: p.emptyText })) }));
|
|
14
8
|
}
|
|
15
|
-
// Compact recharts tooltip styled with theme tokens.
|
|
16
|
-
function ChartTooltip({ ctx }) {
|
|
17
|
-
return (_jsx(Tooltip, { cursor: { fill: 'var(--muted, rgba(148,163,184,0.12))', opacity: 0.4 }, contentStyle: {
|
|
18
|
-
background: 'var(--popover, #fff)',
|
|
19
|
-
border: '1px solid var(--border, #e2e8f0)',
|
|
20
|
-
borderRadius: 8,
|
|
21
|
-
fontSize: 12,
|
|
22
|
-
color: 'var(--popover-foreground, #0f172a)',
|
|
23
|
-
boxShadow: '0 4px 16px rgba(0,0,0,0.08)',
|
|
24
|
-
}, labelStyle: { color: 'var(--muted-foreground, #64748b)', marginBottom: 2 }, formatter: (v) => formatWidgetValue(Number(v), ctx) }));
|
|
25
|
-
}
|
|
26
|
-
// --- bar ------------------------------------------------------------------
|
|
27
9
|
export function BarWidget(p) {
|
|
28
|
-
|
|
29
|
-
const a = accentClasses(p.spec.accent);
|
|
30
|
-
return (_jsx(WidgetCard, { "data-testid": `widget-${p.spec.key}`, title: p.spec.title, subtitle: p.spec.subtitle, icon: p.spec.icon, accent: p.spec.accent, children: hasSeries(p.data) ? (_jsx(ChartArea, { children: _jsxs(BarChart, { data: p.data.series, margin: { top: 4, right: 4, left: -16, bottom: 0 }, children: [_jsx(CartesianGrid, { vertical: false, stroke: CHART_GRID, strokeDasharray: "3 3" }), _jsx(XAxis, { dataKey: "label", tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, interval: "preserveStartEnd" }), _jsx(YAxis, { tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, width: 44, tickFormatter: (v) => formatAxisTick(v, ctx) }), _jsx(ChartTooltip, { ctx: ctx }), _jsx(Bar, { dataKey: "value", fill: a.chartVar, radius: [4, 4, 0, 0], maxBarSize: 48 })] }) })) : (_jsx(WidgetEmpty, { message: p.emptyText })) }));
|
|
31
|
-
}
|
|
32
|
-
// --- line / area (shared) -------------------------------------------------
|
|
33
|
-
function TimeSeriesWidget(p) {
|
|
34
|
-
const ctx = fmtCtx(p.spec, p.locale, p.currency);
|
|
35
|
-
const a = accentClasses(p.spec.accent);
|
|
36
|
-
const gradId = `wg-grad-${p.spec.key}`;
|
|
37
|
-
return (_jsx(WidgetCard, { "data-testid": `widget-${p.spec.key}`, title: p.spec.title, subtitle: p.spec.subtitle, icon: p.spec.icon, accent: p.spec.accent, children: hasSeries(p.data) ? (_jsx(ChartArea, { children: p.variant === 'area' ? (_jsxs(AreaChart, { data: p.data.series, margin: { top: 4, right: 6, left: -16, bottom: 0 }, children: [_jsx("defs", { children: _jsxs("linearGradient", { id: gradId, x1: "0", y1: "0", x2: "0", y2: "1", children: [_jsx("stop", { offset: "0%", stopColor: a.chartVar, stopOpacity: 0.35 }), _jsx("stop", { offset: "100%", stopColor: a.chartVar, stopOpacity: 0.02 })] }) }), _jsx(CartesianGrid, { vertical: false, stroke: CHART_GRID, strokeDasharray: "3 3" }), _jsx(XAxis, { dataKey: "label", tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, interval: "preserveStartEnd" }), _jsx(YAxis, { tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, width: 44, tickFormatter: (v) => formatAxisTick(v, ctx) }), _jsx(ChartTooltip, { ctx: ctx }), _jsx(Area, { type: "monotone", dataKey: "value", stroke: a.chartVar, strokeWidth: 2, fill: `url(#${gradId})` })] })) : (_jsxs(LineChart, { data: p.data.series, margin: { top: 4, right: 6, left: -16, bottom: 0 }, children: [_jsx(CartesianGrid, { vertical: false, stroke: CHART_GRID, strokeDasharray: "3 3" }), _jsx(XAxis, { dataKey: "label", tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, interval: "preserveStartEnd" }), _jsx(YAxis, { tick: { fontSize: 10, fill: CHART_MUTED }, tickLine: false, axisLine: false, width: 44, tickFormatter: (v) => formatAxisTick(v, ctx) }), _jsx(ChartTooltip, { ctx: ctx }), _jsx(Line, { type: "monotone", dataKey: "value", stroke: a.chartVar, strokeWidth: 2, dot: false, activeDot: { r: 4 } })] })) })) : (_jsx(WidgetEmpty, { message: p.emptyText })) }));
|
|
10
|
+
return _jsx(ChartWidget, { ...p, Body: BarChartBody });
|
|
38
11
|
}
|
|
39
12
|
export function LineWidget(p) {
|
|
40
|
-
return _jsx(
|
|
13
|
+
return _jsx(ChartWidget, { ...p, Body: LineChartBody });
|
|
41
14
|
}
|
|
42
15
|
export function AreaWidget(p) {
|
|
43
|
-
return _jsx(
|
|
44
|
-
}
|
|
45
|
-
// --- pie / donut (shared) -------------------------------------------------
|
|
46
|
-
function CircularWidget(p) {
|
|
47
|
-
const ctx = fmtCtx(p.spec, p.locale, p.currency);
|
|
48
|
-
return (_jsx(WidgetCard, { "data-testid": `widget-${p.spec.key}`, title: p.spec.title, subtitle: p.spec.subtitle, icon: p.spec.icon, accent: p.spec.accent, children: hasSeries(p.data) ? (_jsxs("div", { className: cn(CHART_H, 'flex items-center gap-3'), children: [_jsx("div", { className: "h-full min-h-0 w-[46%] shrink-0", children: _jsx(ResponsiveContainer, { width: "100%", height: "100%", children: _jsxs(PieChart, { children: [_jsx(ChartTooltip, { ctx: ctx }), _jsx(Pie, { data: p.data.series, dataKey: "value", nameKey: "label", innerRadius: p.variant === 'donut' ? '55%' : 0, outerRadius: "92%", paddingAngle: p.variant === 'donut' ? 2 : 0, stroke: "var(--background, #fff)", strokeWidth: 2, children: p.data.series.map((_, i) => (_jsx(Cell, { fill: paletteColor(i) }, i))) })] }) }) }), _jsx("ul", { className: "flex min-w-0 flex-1 flex-col gap-1.5", children: p.data.series.slice(0, 6).map((pt, i) => (_jsxs("li", { className: "flex items-center gap-2 text-xs", children: [_jsx("span", { className: "size-2.5 shrink-0 rounded-sm", style: { background: paletteColor(i) } }), _jsx("span", { className: "truncate text-muted-foreground", children: pt.label }), _jsx("span", { className: "ml-auto shrink-0 font-medium tabular-nums text-foreground", children: formatWidgetValue(pt.value, ctx) })] }, pt.key))) })] })) : (_jsx(WidgetEmpty, { message: p.emptyText })) }));
|
|
16
|
+
return _jsx(ChartWidget, { ...p, Body: AreaChartBody });
|
|
49
17
|
}
|
|
50
18
|
export function PieWidget(p) {
|
|
51
|
-
return _jsx(
|
|
19
|
+
return _jsx(ChartWidget, { ...p, Body: PieChartBody });
|
|
52
20
|
}
|
|
53
21
|
export function DonutWidget(p) {
|
|
54
|
-
return _jsx(
|
|
22
|
+
return _jsx(ChartWidget, { ...p, Body: DonutChartBody });
|
|
55
23
|
}
|