Skip to content

Commit 2865357

Browse files
committed
fix(console): a child written through an authored related list refreshes the record page's header
The record page re-read its record on a child write only for the children the relationship graph derives. An authored page's related-list blocks and reference-rail entries can list a child whose FK opts out of the derived lists, so a create through that list left the header's rollup stale until a reload. The child-write subscription now also watches the children the page itself lists. Claude-Session: https://claude.ai/code/session_01JQwxbZdCt7JoRJa32i5BHs Co-Authored-By: Claude <noreply@anthropic.com>
1 parent cbb3a1a commit 2865357

4 files changed

Lines changed: 500 additions & 3 deletions

File tree

Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#26890 — a child write through a related list the record page's
11+
* AUTHOR declared refreshes the parent record's header, in place.
12+
*
13+
* The reported reach (HotCRM on 18.2.0, quote QTE-0007 → Related → Quote Line
14+
* Items → New → Create): the line-item hook rolled the total onto the quote,
15+
* the server held the new total, and the header kept the old one until a
16+
* reload.
17+
*
18+
* objectui#12244 already re-reads the parent on a child write, but only for the
19+
* children the record page DERIVES from the relationship graph
20+
* (`deriveRelatedLists`). An authored record page declares its own related
21+
* lists — `record:related_list` blocks and `record:reference_rail` entries —
22+
* and a child it lists there need not be one the graph derives: its FK may opt
23+
* out of the derived lists (`relatedList: false`), the page then being the only
24+
* place that child is shown. A write to such a child reached the bus under the
25+
* child's name and matched nothing, so the parent row stayed as first loaded.
26+
*
27+
* What this file pins, through the real page and the real MutationEvent bridge:
28+
*
29+
* 1. A create, an update and a delete of a child the authored page lists in a
30+
* `record:related_list` block each re-read the parent once, and the
31+
* header shows the server's recomputed total.
32+
* 2. The same for a child listed only in a `record:reference_rail` entry.
33+
* 3. Scope stays tight: a write to an object the page does not list and the
34+
* graph does not derive does not re-read the parent.
35+
*
36+
* The fake backend recomputes the rollup itself, as the server's hook does, so
37+
* the figure the header shows can only have come from a fresh read of the
38+
* parent.
39+
*/
40+
41+
import * as React from 'react';
42+
import { describe, it, expect, vi, beforeAll, beforeEach, afterEach } from 'vitest';
43+
import { render, screen, waitFor, cleanup, act } from '@testing-library/react';
44+
import { MemoryRouter, Route, Routes } from 'react-router-dom';
45+
import { MetadataCtx, useMutationInvalidationBridge } from '@object-ui/react';
46+
import { RelatedCountStore } from '@object-ui/components';
47+
import type { DataSourceMutationEvent } from '@object-ui/types';
48+
49+
vi.mock('@object-ui/auth', async (importOriginal) => ({
50+
...(await importOriginal<Record<string, unknown>>()),
51+
useAuth: () => ({ user: { id: 'u1', name: 'Ada', image: null }, activeOrganization: null }),
52+
createAuthenticatedFetch: () => vi.fn(),
53+
}));
54+
55+
vi.mock('@object-ui/collaboration', async (importOriginal) => ({
56+
...(await importOriginal<Record<string, unknown>>()),
57+
useRecordPresence: () => [],
58+
PresenceAvatars: () => null,
59+
}));
60+
61+
vi.mock('sonner', () => ({
62+
toast: Object.assign(vi.fn(), {
63+
success: vi.fn(),
64+
error: vi.fn(),
65+
info: vi.fn(),
66+
warning: vi.fn(),
67+
loading: vi.fn(),
68+
dismiss: vi.fn(),
69+
}),
70+
}));
71+
72+
// Orthogonal chrome — same posture as RecordDetailView.childWriteRefreshesParent-12244.
73+
vi.mock('./ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null }));
74+
vi.mock('./ActionParamDialog', () => ({ ActionParamDialog: () => null }));
75+
vi.mock('./ActionResultDialog', () => ({ ActionResultDialog: () => null }));
76+
vi.mock('./FlowRunner', () => ({ FlowRunner: () => null }));
77+
vi.mock('./MetadataInspector', () => ({
78+
MetadataPanel: () => null,
79+
useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }),
80+
}));
81+
82+
import { RecordDetailView } from './RecordDetailView';
83+
84+
beforeAll(() => {
85+
Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 });
86+
});
87+
88+
const APP = 'crm';
89+
const PARENT = 'os_26890_quote';
90+
/** Listed in a `record:related_list` block; its FK opts out of the derived lists. */
91+
const LINE = 'os_26890_quote_line';
92+
/** Listed only in the rail; its FK opts out of the derived lists too. */
93+
const NOTE = 'os_26890_quote_note';
94+
/** Points at the parent with `relatedList: false` and appears nowhere on the page. */
95+
const AUDIT = 'os_26890_quote_audit';
96+
const RECORD_ID = 'q-7';
97+
98+
const parentObject = {
99+
name: PARENT,
100+
label: 'Quote',
101+
fields: {
102+
id: { type: 'text', label: 'Id' },
103+
name: { type: 'text', label: 'Name' },
104+
total_price: { type: 'number', label: 'Total' },
105+
},
106+
};
107+
108+
const childOf = (name: string, label: string) => ({
109+
name,
110+
label,
111+
fields: {
112+
id: { type: 'text', label: 'Id' },
113+
name: { type: 'text', label: 'Name' },
114+
quantity: { type: 'number', label: 'Quantity' },
115+
unit_price: { type: 'number', label: 'Unit Price' },
116+
quote: { type: 'master_detail', reference: PARENT, label: 'Quote', relatedList: false },
117+
},
118+
});
119+
120+
const OBJECTS = [parentObject, childOf(LINE, 'Quote Line'), childOf(NOTE, 'Quote Note'), childOf(AUDIT, 'Quote Audit')];
121+
122+
/** The quote's authored record page, reduced to the blocks this card is about. */
123+
const RECORD_PAGE = {
124+
name: 'os_26890_quote_detail',
125+
label: 'Quote',
126+
type: 'record',
127+
object: PARENT,
128+
kind: 'full',
129+
template: 'default',
130+
isDefault: true,
131+
regions: [
132+
{
133+
name: 'main',
134+
width: 'large',
135+
components: [
136+
{ type: 'page:header', properties: { title: '{name}' } },
137+
{ type: 'record:details', properties: { sections: [{ name: 'o', label: 'Overview', fields: ['total_price'] }] } },
138+
{
139+
type: 'record:related_list',
140+
id: 'quote_lines',
141+
properties: { objectName: LINE, relationshipField: 'quote', columns: ['name', 'quantity'], limit: 10 },
142+
},
143+
],
144+
},
145+
{
146+
name: 'aside',
147+
width: 'small',
148+
components: [
149+
{
150+
type: 'record:reference_rail',
151+
id: 'quote_rail',
152+
properties: { entries: [{ objectName: NOTE, relationshipField: 'quote', limit: 3 }] },
153+
},
154+
],
155+
},
156+
],
157+
};
158+
159+
/**
160+
* A backend that owns the rollup: every write to a line recomputes the
161+
* parent's `total_price` server-side; the client only learns the new figure by
162+
* reading the parent again. Writes are announced through `onMutation`, the
163+
* event the console's MutationEvent bridge fans out onto the bus — the create
164+
* the record-form overlay makes included.
165+
*/
166+
function makeBackend() {
167+
const lines = [
168+
{ id: 'l-1', name: 'Seat licence', quantity: 3, unit_price: 2000, quote: RECORD_ID },
169+
{ id: 'l-2', name: 'Onboarding', quantity: 1, unit_price: 3000, quote: RECORD_ID },
170+
];
171+
const rollup = () => lines.reduce((sum, l) => sum + l.quantity * l.unit_price, 0);
172+
const listeners = new Set<(e: DataSourceMutationEvent) => void>();
173+
const emit = (e: DataSourceMutationEvent) => {
174+
for (const l of Array.from(listeners)) l(e);
175+
};
176+
const ds = {
177+
find: vi.fn(async (objectName: string) => {
178+
if (objectName !== LINE) return { data: [], total: 0 };
179+
return { data: lines.map((l) => ({ ...l })), total: lines.length };
180+
}),
181+
findOne: vi.fn(async (objectName: string, id: string) => {
182+
if (objectName !== PARENT || id !== RECORD_ID) return null;
183+
return { id: RECORD_ID, name: 'QTE-0007', total_price: rollup() };
184+
}),
185+
create: vi.fn(async (objectName: string, row: { name: string; quantity: number; unit_price: number }) => {
186+
const created = { id: `l-${lines.length + 1}`, quote: RECORD_ID, ...row };
187+
if (objectName === LINE) lines.push(created);
188+
// The adapter's create event names the resource, not the id.
189+
emit({ type: 'create', resource: objectName, record: created });
190+
return created;
191+
}),
192+
update: vi.fn(async (objectName: string, id: string, patch: { quantity?: number }) => {
193+
if (objectName === LINE) {
194+
const line = lines.find((l) => l.id === id);
195+
if (line) Object.assign(line, patch);
196+
}
197+
emit({ type: 'update', resource: objectName, id });
198+
return { id, ...patch };
199+
}),
200+
delete: vi.fn(async (objectName: string, id: string) => {
201+
if (objectName === LINE) {
202+
const at = lines.findIndex((l) => l.id === id);
203+
if (at >= 0) lines.splice(at, 1);
204+
}
205+
emit({ type: 'delete', resource: objectName, id });
206+
return true;
207+
}),
208+
onMutation: (cb: (e: DataSourceMutationEvent) => void) => {
209+
listeners.add(cb);
210+
return () => listeners.delete(cb);
211+
},
212+
};
213+
return { ds, rollup };
214+
}
215+
216+
type FakeDataSource = ReturnType<typeof makeBackend>['ds'];
217+
218+
/** The console mounts the MutationEvent bridge once, in `AppContent`. */
219+
function ConsoleHost({ dataSource, children }: { dataSource: unknown; children: React.ReactNode }) {
220+
useMutationInvalidationBridge(dataSource);
221+
return <>{children}</>;
222+
}
223+
224+
function renderPage(ds: FakeDataSource) {
225+
const metadata = {
226+
objects: OBJECTS,
227+
pages: [RECORD_PAGE],
228+
loading: false,
229+
error: null,
230+
refresh: async () => {},
231+
invalidate: () => {},
232+
ensureType: async () => [],
233+
getItem: async () => null,
234+
getItemsByType: () => [],
235+
} as unknown as React.ContextType<typeof MetadataCtx>;
236+
return render(
237+
<MemoryRouter initialEntries={[`/apps/${APP}/${PARENT}/record/${RECORD_ID}`]}>
238+
<MetadataCtx.Provider value={metadata}>
239+
<ConsoleHost dataSource={ds}>
240+
<Routes>
241+
<Route
242+
path="/apps/:appName/:objectName/record/:recordId"
243+
element={<RecordDetailView dataSource={ds} objects={OBJECTS} onEdit={() => {}} />}
244+
/>
245+
</Routes>
246+
</ConsoleHost>
247+
</MetadataCtx.Provider>
248+
</MemoryRouter>,
249+
);
250+
}
251+
252+
/** Reads of the PARENT record — the read every header block draws from. */
253+
const parentReads = (ds: FakeDataSource) =>
254+
ds.findOne.mock.calls.filter(([objectName, id]) => objectName === PARENT && id === RECORD_ID).length;
255+
256+
/** Is `figure` (e.g. 9000) on screen, in any grouping the number formatter picks? */
257+
const figureShown = (figure: number) => {
258+
const text = document.body.textContent ?? '';
259+
return text.includes(String(figure)) || text.includes(figure.toLocaleString('en-US'));
260+
};
261+
262+
/** Let any refetch a change could have scheduled land before reading. */
263+
const settle = () => act(async () => {
264+
await new Promise((r) => setTimeout(r, 50));
265+
});
266+
267+
async function settledPage() {
268+
const backend = makeBackend();
269+
renderPage(backend.ds);
270+
await waitFor(() => expect(parentReads(backend.ds)).toBe(1));
271+
await screen.findByText('Seat licence');
272+
await waitFor(() => expect(figureShown(backend.rollup())).toBe(true));
273+
return backend;
274+
}
275+
276+
beforeEach(() => {
277+
cleanup();
278+
RelatedCountStore._reset();
279+
vi.stubGlobal(
280+
'fetch',
281+
vi.fn(async () =>
282+
new Response(JSON.stringify({ data: [] }), {
283+
status: 200,
284+
headers: { 'content-type': 'application/json' },
285+
}),
286+
),
287+
);
288+
});
289+
290+
afterEach(() => {
291+
vi.unstubAllGlobals();
292+
vi.restoreAllMocks();
293+
});
294+
295+
describe('RecordDetailView — a child write through an AUTHORED related list refreshes the parent (objectstack#26890)', () => {
296+
it('a create of a line the authored related list shows re-reads the parent once and shows the recomputed total', async () => {
297+
const { ds, rollup } = await settledPage();
298+
expect(rollup()).toBe(9000);
299+
const headerBefore = document.querySelector('header');
300+
301+
await act(async () => {
302+
await ds.create(LINE, { name: 'Training', quantity: 2, unit_price: 2700 });
303+
});
304+
305+
expect(rollup()).toBe(14400);
306+
await waitFor(() => expect(parentReads(ds)).toBe(2));
307+
await waitFor(() => expect(figureShown(14400)).toBe(true));
308+
expect(figureShown(9000)).toBe(false);
309+
await settle();
310+
expect(parentReads(ds)).toBe(2);
311+
// In place: the same header node.
312+
expect(document.querySelector('header')).toBe(headerBefore);
313+
});
314+
315+
it('an update and a delete of such a line each re-read the parent', async () => {
316+
const { ds } = await settledPage();
317+
318+
await act(async () => {
319+
await ds.update(LINE, 'l-2', { quantity: 2 });
320+
});
321+
await waitFor(() => expect(parentReads(ds)).toBe(2));
322+
await waitFor(() => expect(figureShown(12000)).toBe(true));
323+
324+
await act(async () => {
325+
await ds.delete(LINE, 'l-1');
326+
});
327+
await waitFor(() => expect(parentReads(ds)).toBe(3));
328+
await waitFor(() => expect(figureShown(6000)).toBe(true));
329+
});
330+
331+
it('a write to a child the page lists only in its reference rail re-reads the parent', async () => {
332+
const { ds } = await settledPage();
333+
334+
await act(async () => {
335+
await ds.create(NOTE, { name: 'Discount approved', quantity: 0, unit_price: 0 });
336+
});
337+
338+
await waitFor(() => expect(parentReads(ds)).toBe(2));
339+
});
340+
341+
it('a write to a child the page does not list and the graph does not derive does not re-read the parent', async () => {
342+
const { ds } = await settledPage();
343+
344+
await act(async () => {
345+
await ds.create(AUDIT, { name: 'Viewed', quantity: 0, unit_price: 0 });
346+
});
347+
await settle();
348+
349+
expect(parentReads(ds)).toBe(1);
350+
});
351+
});

‎ui/packages/app-shell/src/views/RecordDetailView.tsx‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { SkeletonDetail } from '../skeletons/index.js';
2525
import { ManagedByBadge, useManagedByNote } from '../components/ManagedByBadge.js';
2626
import { isSetupAppSegment } from '../console/setupApp.js';
2727
import { deriveRelatedLists } from '../utils/deriveRelatedLists.js';
28+
import { recordPageChildObjects } from './recordPageChildObjects.js';
2829
import { stripDiscussionNodes, hasExplicitAttachments, hasExplicitApprovals } from '../utils/pageSchemaIntrospect.js';
2930
import { ActionConfirmDialog, type ConfirmDialogState } from './ActionConfirmDialog.js';
3031
import { ActionParamDialog, type ParamDialogState } from './ActionParamDialog.js';
@@ -1850,13 +1851,19 @@ export function RecordDetailView({ dataSource, objects, onEdit, objectNameOverri
18501851
// knows what it depends on. That covers every child-write path at one seam,
18511852
// and costs one parent read per change, however many lists are mounted.
18521853
//
1853-
// The children are the page's derived related lists, keyed by CONTENT
1854-
// (AGENTS.md #10 — `childRelations` is a memo). A change both subscriptions
1854+
// The children are the page's derived related lists, plus the ones its
1855+
// author lists on it (objectstack#26890): an authored page's
1856+
// `record:related_list` blocks and `record:reference_rail` entries may name a
1857+
// child the graph does not derive — its FK opted out with `relatedList:
1858+
// false`, the page being the only place it is shown — and a create through
1859+
// that list left the header's total stale. Keyed by CONTENT (AGENTS.md #10 —
1860+
// `childRelations` and the page are memos). A change both subscriptions
18551861
// match — a `'*'`, or this record when the object references itself — bumps
18561862
// both nonces from one dispatch; React batches the two updates into one
18571863
// render, so the row is still read once.
1864+
const authoredChildObjects = useMemo(() => recordPageChildObjects(effectivePage), [effectivePage]);
18581865
const relatedChildObjectsKey = JSON.stringify(
1859-
Array.from(new Set(childRelations.map((r) => r.childObject))).sort(),
1866+
Array.from(new Set([...childRelations.map((r) => r.childObject), ...authoredChildObjects])).sort(),
18601867
);
18611868
useEffect(() => {
18621869
const childObjects = JSON.parse(relatedChildObjectsKey) as string[];

0 commit comments

Comments
 (0)