@asteby/metacore-runtime-react 39.2.2 → 39.2.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/CHANGELOG.md +12 -0
- package/dist/action-modal-dispatcher.d.ts +13 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +57 -0
- package/dist/dynamic-crud-page.d.ts.map +1 -1
- package/dist/dynamic-crud-page.js +6 -2
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +19 -5
- package/package.json +1 -1
- package/src/__tests__/prefill-from-relation.test.ts +42 -0
- package/src/action-modal-dispatcher.tsx +72 -0
- package/src/dynamic-crud-page.tsx +6 -2
- package/src/metadata-cache.ts +19 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @asteby/metacore-runtime-react
|
|
2
2
|
|
|
3
|
+
## 39.2.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c759e48: Los modales de acción que precargan renglones desde el registro (`$prefillFromRecord`, p. ej. "Recibir" de una orden de compra) ahora cargan las líneas desde la relación declarada del modelo cuando la fila de la lista no las trae. Antes el modal abría con "Sin renglones".
|
|
8
|
+
|
|
9
|
+
## 39.2.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- caf2015: Always revalidate `/metadata/table` on DynamicCRUDPage cache hit and replace the catalog on `prefetchAll`, so addon toolbar actions cannot stick after HotRegister while zustand localStorage still holds the pre-upgrade snapshot.
|
|
14
|
+
|
|
3
15
|
## 39.2.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -20,6 +20,19 @@ export interface PrefillSpec {
|
|
|
20
20
|
export declare function isPrefillSpec(v: unknown): v is PrefillSpec;
|
|
21
21
|
export declare function applyPrefillLock(field: ActionFieldDef): ActionFieldDef;
|
|
22
22
|
export declare function buildPrefillRows(spec: PrefillSpec, record: any): Array<Record<string, any>>;
|
|
23
|
+
export interface PrefillRelationRequest {
|
|
24
|
+
fieldKey: string;
|
|
25
|
+
spec: PrefillSpec;
|
|
26
|
+
endpoint: string;
|
|
27
|
+
params: Record<string, string | number>;
|
|
28
|
+
}
|
|
29
|
+
export declare function prefillRelationRequests(fields: ActionFieldDef[] | undefined, record: any, relations: Array<{
|
|
30
|
+
name?: string;
|
|
31
|
+
kind?: string;
|
|
32
|
+
through?: string;
|
|
33
|
+
foreign_key?: string;
|
|
34
|
+
scope?: Record<string, unknown>;
|
|
35
|
+
}>): PrefillRelationRequest[];
|
|
23
36
|
export declare function unwrapRecordScalar(value: unknown): unknown;
|
|
24
37
|
export declare function readRecordPath(record: any, path: string): unknown;
|
|
25
38
|
/** Scalar seed for one action field from the row being acted on. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"action-modal-dispatcher.d.ts","sourceRoot":"","sources":["../src/action-modal-dispatcher.tsx"],"names":[],"mappings":"AAwEA,OAAO,KAAK,EAAE,cAAc,EAAmC,MAAM,SAAS,CAAA;AAE9E,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAExB,MAAM,sBAAsB,CAAA;AAE7B,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAA;AAiBhD,MAAM,WAAW,WAAW;IACxB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,SAAS,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IAC1D;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAClB;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,sCAkFlB"}
|
|
@@ -23,6 +23,7 @@ import { validateValues, bagHasErrors } from './validator';
|
|
|
23
23
|
import { clearFieldErrorTree, formatFieldErrorsDescription, labelsForValidationFields, labelForValidationPath, lineItemErrorsFor, } from './field-validation-ui';
|
|
24
24
|
import { validationCatalog } from './validation-catalog';
|
|
25
25
|
import { useApi } from './api-context';
|
|
26
|
+
import { buildRelationFilterParams } from './dynamic-relation-helpers';
|
|
26
27
|
import { DynamicIcon } from './dynamic-icon';
|
|
27
28
|
import { DynamicLineItems } from './dynamic-line-items';
|
|
28
29
|
import { DynamicRelations } from './dynamic-relations';
|
|
@@ -94,6 +95,36 @@ export function buildPrefillRows(spec, record) {
|
|
|
94
95
|
}
|
|
95
96
|
return rows;
|
|
96
97
|
}
|
|
98
|
+
export function prefillRelationRequests(fields, record, relations) {
|
|
99
|
+
const out = [];
|
|
100
|
+
const recordId = record?.id;
|
|
101
|
+
if (!fields || recordId === undefined || recordId === null || recordId === '')
|
|
102
|
+
return out;
|
|
103
|
+
for (const field of fields) {
|
|
104
|
+
if (!isLineItemsField(field))
|
|
105
|
+
continue;
|
|
106
|
+
const spec = lineItemsDefault(field);
|
|
107
|
+
if (!isPrefillSpec(spec))
|
|
108
|
+
continue;
|
|
109
|
+
if (Array.isArray(record?.[spec.$prefillFromRecord]))
|
|
110
|
+
continue; // already embedded
|
|
111
|
+
const rel = (relations || []).find((r) => r?.name === spec.$prefillFromRecord && (r.kind ?? 'one_to_many') === 'one_to_many');
|
|
112
|
+
if (!rel?.through || !rel.foreign_key)
|
|
113
|
+
continue;
|
|
114
|
+
const scope = {};
|
|
115
|
+
for (const [k, v] of Object.entries(rel.scope ?? {})) {
|
|
116
|
+
if (v !== undefined && v !== null)
|
|
117
|
+
scope[k] = String(v);
|
|
118
|
+
}
|
|
119
|
+
out.push({
|
|
120
|
+
fieldKey: field.key,
|
|
121
|
+
spec,
|
|
122
|
+
endpoint: `/data/${rel.through}`,
|
|
123
|
+
params: { ...buildRelationFilterParams(rel.foreign_key, recordId, scope), per_page: 200 },
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
97
128
|
// ---- scalar prefill from the acted-on record --------------------------------
|
|
98
129
|
//
|
|
99
130
|
// Row actions (stamp / refactura / cancel-with-reason) should open with the
|
|
@@ -457,6 +488,32 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
457
488
|
setFieldErrors({});
|
|
458
489
|
}
|
|
459
490
|
}, [open, action.fields, record]);
|
|
491
|
+
// Lines the list row does not carry: load them from the declared relation
|
|
492
|
+
// (see prefillRelationRequests) and seed the line-items grid.
|
|
493
|
+
useEffect(() => {
|
|
494
|
+
if (!open || relations.length === 0)
|
|
495
|
+
return;
|
|
496
|
+
const reqs = prefillRelationRequests(action.fields, record, relations);
|
|
497
|
+
if (reqs.length === 0)
|
|
498
|
+
return;
|
|
499
|
+
let cancelled = false;
|
|
500
|
+
for (const req of reqs) {
|
|
501
|
+
api.get(req.endpoint, { params: req.params })
|
|
502
|
+
.then((res) => {
|
|
503
|
+
if (cancelled)
|
|
504
|
+
return;
|
|
505
|
+
const rows = res?.data?.data ?? res?.data ?? [];
|
|
506
|
+
if (!Array.isArray(rows))
|
|
507
|
+
return;
|
|
508
|
+
const built = buildPrefillRows(req.spec, { [req.spec.$prefillFromRecord]: rows });
|
|
509
|
+
setFormData((prev) => ({ ...prev, [req.fieldKey]: built }));
|
|
510
|
+
})
|
|
511
|
+
.catch(() => { });
|
|
512
|
+
}
|
|
513
|
+
return () => {
|
|
514
|
+
cancelled = true;
|
|
515
|
+
};
|
|
516
|
+
}, [open, action.fields, record, relations, api]);
|
|
460
517
|
const updateField = (key, value) => {
|
|
461
518
|
setFormData((prev) => ({ ...prev, [key]: value }));
|
|
462
519
|
setFieldErrors((prev) => clearFieldErrorTree(prev, key));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-crud-page.d.ts","sourceRoot":"","sources":["../src/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AC,EAAE,MAAM,IAAI,CAAA;CACxB;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,qBAuM1D"}
|
|
@@ -58,9 +58,13 @@ export function DynamicCRUDPage(props) {
|
|
|
58
58
|
const [openExport, setOpenExport] = useState(false);
|
|
59
59
|
const [openImport, setOpenImport] = useState(false);
|
|
60
60
|
useEffect(() => {
|
|
61
|
+
// Stale-while-revalidate: paint from the persisted cache immediately,
|
|
62
|
+
// then ALWAYS fetch /metadata/table/:model. Returning early on a cache
|
|
63
|
+
// hit left hosts stranded on pre-upgrade actions (missing toolbar
|
|
64
|
+
// buttons after an addon HotRegister) because hard refresh does not
|
|
65
|
+
// clear zustand localStorage.
|
|
61
66
|
if (cachedMeta) {
|
|
62
67
|
setMetadata(cachedMeta);
|
|
63
|
-
return;
|
|
64
68
|
}
|
|
65
69
|
let cancelled = false;
|
|
66
70
|
api
|
|
@@ -72,7 +76,7 @@ export function DynamicCRUDPage(props) {
|
|
|
72
76
|
setMetadata(meta ?? null);
|
|
73
77
|
})
|
|
74
78
|
.catch(() => {
|
|
75
|
-
if (!cancelled)
|
|
79
|
+
if (!cancelled && !cachedMeta)
|
|
76
80
|
setMetadata(null);
|
|
77
81
|
});
|
|
78
82
|
return () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata-cache.d.ts","sourceRoot":"","sources":["../src/metadata-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"metadata-cache.d.ts","sourceRoot":"","sources":["../src/metadata-cache.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,MAAM,WAAW,iBAAiB;IAC9B,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,GAAG,CAAA;KAAE,CAAC,CAAA;CAC7D;AAED;;;;;;;GAO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
|
// metadata-cache store so the runtime-react package no longer depends on
|
|
4
4
|
// a host-specific alias.
|
|
5
5
|
//
|
|
6
|
+
// Contract (stale-while-revalidate):
|
|
7
|
+
// - Cache entries are a FIRST PAINT hint only.
|
|
8
|
+
// - Every consumer (DynamicTable, DynamicCRUDPage, ModelActionToolbar hosts)
|
|
9
|
+
// MUST still GET /metadata/table/:model and apply the live payload —
|
|
10
|
+
// including `actions[]`. Skipping the live fetch on a cache hit is how
|
|
11
|
+
// cross-addon toolbar actions disappear after an addon upgrade while
|
|
12
|
+
// localStorage still holds the pre-upgrade catalog.
|
|
13
|
+
// - `prefetchAll` replaces the catalog from `/metadata/all` and advances
|
|
14
|
+
// `metadataVersion`; a version change wipes the prior cache.
|
|
15
|
+
//
|
|
6
16
|
// The prefetchAll() method needs an `api` client (axios-like); we keep that
|
|
7
17
|
// as an injectable parameter so the store stays host-agnostic. If a caller
|
|
8
18
|
// never invokes prefetchAll, the `api` dep is not required.
|
|
@@ -90,9 +100,12 @@ export const useMetadataCache = create()(persist((set, get) => ({
|
|
|
90
100
|
const { tables, modals, version } = res.data.data;
|
|
91
101
|
const serverVersion = version || '';
|
|
92
102
|
const localVersion = get().metadataVersion;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
103
|
+
// Always rebuild from the server catalog. Merging into a
|
|
104
|
+
// persisted cache left hosts with pre-upgrade actions /
|
|
105
|
+
// columns after an addon HotRegister while the tab (or
|
|
106
|
+
// localStorage) stayed open.
|
|
107
|
+
const newCache = {};
|
|
108
|
+
const newModalCache = {};
|
|
96
109
|
if (tables) {
|
|
97
110
|
for (const [key, meta] of Object.entries(tables)) {
|
|
98
111
|
newCache[key] = meta;
|
|
@@ -106,7 +119,7 @@ export const useMetadataCache = create()(persist((set, get) => ({
|
|
|
106
119
|
set({
|
|
107
120
|
cache: newCache,
|
|
108
121
|
modalCache: newModalCache,
|
|
109
|
-
metadataVersion: serverVersion,
|
|
122
|
+
metadataVersion: serverVersion || localVersion || String(Date.now()),
|
|
110
123
|
prefetched: true,
|
|
111
124
|
});
|
|
112
125
|
}
|
|
@@ -118,7 +131,8 @@ export const useMetadataCache = create()(persist((set, get) => ({
|
|
|
118
131
|
}), {
|
|
119
132
|
name: 'metacore-metadata-cache',
|
|
120
133
|
// Bump when display hints (e.g. image_stack) must wipe stale cache.
|
|
121
|
-
|
|
134
|
+
// v5: always replace catalog on prefetchAll (no merge-with-stale).
|
|
135
|
+
version: 5,
|
|
122
136
|
migrate: () => ({
|
|
123
137
|
cache: {},
|
|
124
138
|
modalCache: {},
|
package/package.json
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { prefillRelationRequests } from '../action-modal-dispatcher'
|
|
3
|
+
import type { ActionFieldDef } from '../types'
|
|
4
|
+
|
|
5
|
+
// QA 7Leguas: the purchase-order receive modal opened from the LIST showed
|
|
6
|
+
// "Sin renglones" — the list row carries no `items`, so the
|
|
7
|
+
// $prefillFromRecord grid had nothing to project. The modal must load the
|
|
8
|
+
// lines from the model's declared one_to_many relation.
|
|
9
|
+
const itemsField: ActionFieldDef = {
|
|
10
|
+
key: 'items',
|
|
11
|
+
label: 'Renglones',
|
|
12
|
+
type: 'array',
|
|
13
|
+
itemFields: [
|
|
14
|
+
{ key: 'product_id', label: 'Producto', type: 'dynamic_select', ref: 'Product' },
|
|
15
|
+
{ key: 'qty_received', label: 'Recibido', type: 'number' },
|
|
16
|
+
],
|
|
17
|
+
default: { $prefillFromRecord: 'items', map: { product_id: 'product_variant_id' } },
|
|
18
|
+
} as unknown as ActionFieldDef
|
|
19
|
+
|
|
20
|
+
const relations = [
|
|
21
|
+
{ name: 'items', kind: 'one_to_many', through: 'PurchaseOrderItem', foreign_key: 'purchase_order_id' },
|
|
22
|
+
{ name: 'goods_receipts', kind: 'one_to_many', through: 'GoodsReceipt', foreign_key: 'purchase_order_id' },
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
describe('prefillRelationRequests', () => {
|
|
26
|
+
it('pide las líneas por la relación declarada cuando el registro no las trae', () => {
|
|
27
|
+
const reqs = prefillRelationRequests([itemsField], { id: 'po-1' }, relations)
|
|
28
|
+
expect(reqs).toHaveLength(1)
|
|
29
|
+
expect(reqs[0].fieldKey).toBe('items')
|
|
30
|
+
expect(reqs[0].endpoint).toBe('/data/PurchaseOrderItem')
|
|
31
|
+
expect(reqs[0].params).toMatchObject({ f_purchase_order_id: 'eq:po-1' })
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('no pide nada si el registro ya trae las líneas embebidas', () => {
|
|
35
|
+
expect(prefillRelationRequests([itemsField], { id: 'po-1', items: [] }, relations)).toHaveLength(0)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('no pide nada sin id (acción de creación) o sin relación con ese nombre', () => {
|
|
39
|
+
expect(prefillRelationRequests([itemsField], {}, relations)).toHaveLength(0)
|
|
40
|
+
expect(prefillRelationRequests([itemsField], { id: 'po-1' }, [relations[1]])).toHaveLength(0)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
@@ -53,6 +53,7 @@ import {
|
|
|
53
53
|
} from './field-validation-ui'
|
|
54
54
|
import { validationCatalog } from './validation-catalog'
|
|
55
55
|
import { useApi } from './api-context'
|
|
56
|
+
import { buildRelationFilterParams } from './dynamic-relation-helpers'
|
|
56
57
|
import { DynamicIcon } from './dynamic-icon'
|
|
57
58
|
import { DynamicLineItems } from './dynamic-line-items'
|
|
58
59
|
import { DynamicRelations } from './dynamic-relations'
|
|
@@ -167,6 +168,54 @@ export function buildPrefillRows(spec: PrefillSpec, record: any): Array<Record<s
|
|
|
167
168
|
return rows
|
|
168
169
|
}
|
|
169
170
|
|
|
171
|
+
// ---- prefill lines from the record's declared relation ----------------------
|
|
172
|
+
//
|
|
173
|
+
// A row action opened from a LIST gets the list row, which does not carry the
|
|
174
|
+
// document's lines: `record.items` is undefined and a receive modal opened
|
|
175
|
+
// with "Sin renglones" (QA 7Leguas: OC receive modal empty, the user could not
|
|
176
|
+
// see what was being received). When the prefill source key is missing from
|
|
177
|
+
// the record but the model declares a one_to_many relation with that name, the
|
|
178
|
+
// modal fetches the child rows the same way the relation panel does
|
|
179
|
+
// (`/data/<through>?f_<fk>=eq:<id>`) and builds the rows from them.
|
|
180
|
+
|
|
181
|
+
export interface PrefillRelationRequest {
|
|
182
|
+
fieldKey: string
|
|
183
|
+
spec: PrefillSpec
|
|
184
|
+
endpoint: string
|
|
185
|
+
params: Record<string, string | number>
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function prefillRelationRequests(
|
|
189
|
+
fields: ActionFieldDef[] | undefined,
|
|
190
|
+
record: any,
|
|
191
|
+
relations: Array<{ name?: string; kind?: string; through?: string; foreign_key?: string; scope?: Record<string, unknown> }>,
|
|
192
|
+
): PrefillRelationRequest[] {
|
|
193
|
+
const out: PrefillRelationRequest[] = []
|
|
194
|
+
const recordId = record?.id
|
|
195
|
+
if (!fields || recordId === undefined || recordId === null || recordId === '') return out
|
|
196
|
+
for (const field of fields) {
|
|
197
|
+
if (!isLineItemsField(field)) continue
|
|
198
|
+
const spec = lineItemsDefault(field)
|
|
199
|
+
if (!isPrefillSpec(spec)) continue
|
|
200
|
+
if (Array.isArray(record?.[spec.$prefillFromRecord])) continue // already embedded
|
|
201
|
+
const rel = (relations || []).find(
|
|
202
|
+
(r) => r?.name === spec.$prefillFromRecord && (r.kind ?? 'one_to_many') === 'one_to_many',
|
|
203
|
+
)
|
|
204
|
+
if (!rel?.through || !rel.foreign_key) continue
|
|
205
|
+
const scope: Record<string, string> = {}
|
|
206
|
+
for (const [k, v] of Object.entries(rel.scope ?? {})) {
|
|
207
|
+
if (v !== undefined && v !== null) scope[k] = String(v)
|
|
208
|
+
}
|
|
209
|
+
out.push({
|
|
210
|
+
fieldKey: field.key,
|
|
211
|
+
spec,
|
|
212
|
+
endpoint: `/data/${rel.through}`,
|
|
213
|
+
params: { ...buildRelationFilterParams(rel.foreign_key, recordId, scope), per_page: 200 },
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
return out
|
|
217
|
+
}
|
|
218
|
+
|
|
170
219
|
// ---- scalar prefill from the acted-on record --------------------------------
|
|
171
220
|
//
|
|
172
221
|
// Row actions (stamp / refactura / cancel-with-reason) should open with the
|
|
@@ -686,6 +735,29 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
686
735
|
}
|
|
687
736
|
}, [open, action.fields, record])
|
|
688
737
|
|
|
738
|
+
// Lines the list row does not carry: load them from the declared relation
|
|
739
|
+
// (see prefillRelationRequests) and seed the line-items grid.
|
|
740
|
+
useEffect(() => {
|
|
741
|
+
if (!open || relations.length === 0) return
|
|
742
|
+
const reqs = prefillRelationRequests(action.fields, record, relations)
|
|
743
|
+
if (reqs.length === 0) return
|
|
744
|
+
let cancelled = false
|
|
745
|
+
for (const req of reqs) {
|
|
746
|
+
api.get(req.endpoint, { params: req.params })
|
|
747
|
+
.then((res: any) => {
|
|
748
|
+
if (cancelled) return
|
|
749
|
+
const rows = res?.data?.data ?? res?.data ?? []
|
|
750
|
+
if (!Array.isArray(rows)) return
|
|
751
|
+
const built = buildPrefillRows(req.spec, { [req.spec.$prefillFromRecord]: rows })
|
|
752
|
+
setFormData((prev: Record<string, any>) => ({ ...prev, [req.fieldKey]: built }))
|
|
753
|
+
})
|
|
754
|
+
.catch(() => {})
|
|
755
|
+
}
|
|
756
|
+
return () => {
|
|
757
|
+
cancelled = true
|
|
758
|
+
}
|
|
759
|
+
}, [open, action.fields, record, relations, api])
|
|
760
|
+
|
|
689
761
|
const updateField = (key: string, value: any) => {
|
|
690
762
|
setFormData((prev: Record<string, any>) => ({ ...prev, [key]: value }))
|
|
691
763
|
setFieldErrors((prev) => clearFieldErrorTree(prev, key))
|
|
@@ -127,9 +127,13 @@ export function DynamicCRUDPage(props: DynamicCRUDPageProps) {
|
|
|
127
127
|
const [openImport, setOpenImport] = useState(false)
|
|
128
128
|
|
|
129
129
|
useEffect(() => {
|
|
130
|
+
// Stale-while-revalidate: paint from the persisted cache immediately,
|
|
131
|
+
// then ALWAYS fetch /metadata/table/:model. Returning early on a cache
|
|
132
|
+
// hit left hosts stranded on pre-upgrade actions (missing toolbar
|
|
133
|
+
// buttons after an addon HotRegister) because hard refresh does not
|
|
134
|
+
// clear zustand localStorage.
|
|
130
135
|
if (cachedMeta) {
|
|
131
136
|
setMetadata(cachedMeta)
|
|
132
|
-
return
|
|
133
137
|
}
|
|
134
138
|
let cancelled = false
|
|
135
139
|
api
|
|
@@ -140,7 +144,7 @@ export function DynamicCRUDPage(props: DynamicCRUDPageProps) {
|
|
|
140
144
|
setMetadata(meta ?? null)
|
|
141
145
|
})
|
|
142
146
|
.catch(() => {
|
|
143
|
-
if (!cancelled) setMetadata(null)
|
|
147
|
+
if (!cancelled && !cachedMeta) setMetadata(null)
|
|
144
148
|
})
|
|
145
149
|
return () => {
|
|
146
150
|
cancelled = true
|
package/src/metadata-cache.ts
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
// metadata-cache store so the runtime-react package no longer depends on
|
|
4
4
|
// a host-specific alias.
|
|
5
5
|
//
|
|
6
|
+
// Contract (stale-while-revalidate):
|
|
7
|
+
// - Cache entries are a FIRST PAINT hint only.
|
|
8
|
+
// - Every consumer (DynamicTable, DynamicCRUDPage, ModelActionToolbar hosts)
|
|
9
|
+
// MUST still GET /metadata/table/:model and apply the live payload —
|
|
10
|
+
// including `actions[]`. Skipping the live fetch on a cache hit is how
|
|
11
|
+
// cross-addon toolbar actions disappear after an addon upgrade while
|
|
12
|
+
// localStorage still holds the pre-upgrade catalog.
|
|
13
|
+
// - `prefetchAll` replaces the catalog from `/metadata/all` and advances
|
|
14
|
+
// `metadataVersion`; a version change wipes the prior cache.
|
|
15
|
+
//
|
|
6
16
|
// The prefetchAll() method needs an `api` client (axios-like); we keep that
|
|
7
17
|
// as an injectable parameter so the store stays host-agnostic. If a caller
|
|
8
18
|
// never invokes prefetchAll, the `api` dep is not required.
|
|
@@ -148,10 +158,13 @@ export const useMetadataCache = create<MetadataCacheState>()(
|
|
|
148
158
|
|
|
149
159
|
const serverVersion = version || ''
|
|
150
160
|
const localVersion = get().metadataVersion
|
|
151
|
-
const versionChanged = serverVersion !== localVersion && localVersion !== ''
|
|
152
161
|
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
// Always rebuild from the server catalog. Merging into a
|
|
163
|
+
// persisted cache left hosts with pre-upgrade actions /
|
|
164
|
+
// columns after an addon HotRegister while the tab (or
|
|
165
|
+
// localStorage) stayed open.
|
|
166
|
+
const newCache: Record<string, TableMetadata> = {}
|
|
167
|
+
const newModalCache: Record<string, TableMetadata> = {}
|
|
155
168
|
|
|
156
169
|
if (tables) {
|
|
157
170
|
for (const [key, meta] of Object.entries(tables)) {
|
|
@@ -167,7 +180,7 @@ export const useMetadataCache = create<MetadataCacheState>()(
|
|
|
167
180
|
set({
|
|
168
181
|
cache: newCache,
|
|
169
182
|
modalCache: newModalCache,
|
|
170
|
-
metadataVersion: serverVersion,
|
|
183
|
+
metadataVersion: serverVersion || localVersion || String(Date.now()),
|
|
171
184
|
prefetched: true,
|
|
172
185
|
})
|
|
173
186
|
} catch {
|
|
@@ -179,7 +192,8 @@ export const useMetadataCache = create<MetadataCacheState>()(
|
|
|
179
192
|
{
|
|
180
193
|
name: 'metacore-metadata-cache',
|
|
181
194
|
// Bump when display hints (e.g. image_stack) must wipe stale cache.
|
|
182
|
-
|
|
195
|
+
// v5: always replace catalog on prefetchAll (no merge-with-stale).
|
|
196
|
+
version: 5,
|
|
183
197
|
migrate: () => ({
|
|
184
198
|
cache: {},
|
|
185
199
|
modalCache: {},
|