Skip to content

Commit dfdebfa

Browse files
fix(plugin-detail): a related list that already shows 0 keeps showing 0 while it reloads (#37220)
Fixes #36822 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:记录页上的相关列表原本显示 0 条,重新加载(删除、筛选或其它刷新)时数量角标不再消失,一直显示 0;第一次加载还没结果时、加载失败后点重试时,仍不显示数字,不会显示假的 0。 - 为什么做:记录页加载状态的后续收尾,替上一张「加载中不显示假的 0」的卡补上刷新时角标闪没的一半。 - 出错会怎样:刷新期间角标显示上一次成功加载的数字,直到新结果到达;加载失败后、或上级记录重新加载时,数字不再显示,直到新结果到达。改动只在一个组件内,可直接撤回。 - 要你做什么:无。 ## Responsibility - Whose code: the platform's own `@object-ui/plugin-detail` `RelatedList`. - Existing platform path: none; the badge has one read, `countPending`. - Who reaches it: anyone whose related list empties on a refetch (after a delete, a filter, or a reparent). ## What changes - `ui/packages/plugin-detail/src/RelatedList.tsx`: `countPending` now also requires `!countKnown`. `countKnown` is set to true only where a read the list actually issued resolves (the record-payload seeded read, the `dataSource.find` read, the raw-URL read), and set back to false where one of those reads rejects, because the rejection empties the rows and 0 is not their count. A path that reads nothing never sets it: the no-scope early return (parent still loading, `parentId` null), a refused filter, the no-adapter refusals. The paths among those that empty the rows without reading (the no-scope return and both no-adapter refusals) also set it back to false, so an earlier count does not survive them. Result: - a refetch over a list that already answered 0 keeps drawing 0 instead of blanking the badge; - a failed read forgets the count, so Retry's read draws no count, never a false 0; - a parent that reloads through no scope forgets the count too, so the new parent's read draws no count, never a false 0; - a list's first real read, including one that starts only once the parent has loaded, draws nothing while in flight (the #36235 rule); - a list that answered rows behaves as before. - Pins: a new top-level `describe` for #36822 in the existing badge harness `ui/packages/plugin-detail/src/__tests__/RelatedList.countPending-36235.test.tsx`. - One `patch` changeset for `@object-ui/plugin-detail`. ## Files to read first 1. `ui/packages/plugin-detail/src/RelatedList.tsx`, the `countKnown` / `countPending` hunk: the only behaviour change. 2. The `#36822` describe in the test file: the held in-flight frame assertions (refetch, Retry, first scoped read, read after a scope loss). ## Two-minute check `cd ui && pnpm exec vitest run packages/plugin-detail/src/__tests__/RelatedList.countPending-36235.test.tsx` — expect 8 passed. ## Tests - `a list that answered 0 keeps drawing 0 through a refetch that answers no rows`: holds the refetch in flight and asserts the badge reads `0` on that held frame; that assertion carries the pin. A MutationObserver also samples the badge across the refetch and finds no reading other than `0`. It is a secondary signal only: it samples when its callback runs, not on each commit, so two commits inside one task would be coalesced. - `a list that answered rows draws their count through a refetch, then the 0 it answers`: `2` during the held refetch, then `0`, nothing else in between. - `a read that fails after rows were known draws no count while Retry reads, never a false 0`: answers 2 rows, makes the refetch reject, clicks Retry, holds Retry's read and asserts no badge, then `0` once it answers no rows. - `a list with no scope on its first render draws no count during its first real read`: renders with `parentId` null (no read is sent), rerenders with the parent id, holds that first read and asserts no badge, then `2`. - `a list whose parent reloads through no scope draws no count during the new parent's read`: `ACC-1` answers 2 rows (badge `2`), then `parentId` null (no read), then `ACC-2` with its read held: no badge on that frame, then `1`. - The #36235 pins (first read draws nothing, CONTROL empty read draws 0, compact card) stay green. <details><summary>Readings (head 8bfd2f7, origin/main merged at 83585e7)</summary> - Pin file: `Test Files 1 passed (1)`, `Tests 8 passed (8)`. - Full plugin-detail suite (`cd ui && pnpm exec vitest run packages/plugin-detail/`): `Test Files 418 passed | 1 skipped (419)`, `Tests 3951 passed | 18 skipped (3969)`, exit 0. - `pnpm --filter @object-ui/plugin-detail run type-check`: exit 0 (the test file is in the `tsconfig.test.json` program). - Ablations with `scripts/ablation-replace.mjs`. Each mutation landed (anchor 1 to 0) and was restored to blob == HEAD with `git diff HEAD` empty. - Round 1, at `1426a1d54928`: - A1: `!countKnown` dropped from `countPending`. The 0-refetch pin fails on the held frame (`expected null to be '0'`). - A2: `setCountKnown(false)` removed from the `find` rejection. The Retry pin fails (`expected '0' to be null`). - A3: `setCountKnown(true)` added to the no-scope early return. The first-scoped-read pin fails (`expected '0' to be null`). - Each run read `1 failed | 6 passed (7)`. - Round 2, at `c6a0a1451618`: - A4: `setCountKnown(false)` removed from the no-scope early return. The scope-loss pin fails (`expected '0' to be null`), `1 failed | 7 passed (8)`. - A1, re-run: the 0-refetch pin fails (`expected null to be '0'`), `1 failed | 7 passed (8)`. - The N-rows pin stays green under every mutation. That is expected, since that behaviour held before the fix. </details> ## Acceptance notes - `countKnown` is not reset when the parent or the list's filter changes directly, from one scope to the next: such a list keeps drawing its last count during the new read, as it already did when its last answer had rows. A parent that reloads through no scope (`parentId` null while the parent record loads) empties the rows without reading and forgets the count, so the new parent's read draws nothing. - Not changed here: while the parent record is still loading and the list has no scope yet, nothing is read, `loading` is false and the badge draws `0`. That is the same on `main` (read from the code, not measured in a browser). This PR only makes sure that phase does not mark the count known, so the first real read draws nothing. - Browser reach was not measured; the pins mount the real `RelatedList` in jsdom with a deferred `find`. ## Verification - Gate families derived by `node scripts/pm/dispatch-gates.mjs --commands` for this diff are run locally; CI-only families (type-check lanes, whole-tree families) are left to the PR's check-runs. --- _Generated by [Claude Code](https://claude.ai/code/session_01QSeKYmRepQK9DkZeU7F53D)_ <!-- signed: repo:objectstack#4 · os-justin · session_01QSeKYmRepQK9DkZeU7F53D --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ff04943 commit dfdebfa

3 files changed

Lines changed: 197 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/plugin-detail': patch
3+
---
4+
5+
fix(plugin-detail): a related list that already shows 0 keeps showing 0 while it reloads
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A related list on a record page whose count was known to be 0 used to drop its count badge for as long as a reload took (after a delete, a filter change or any other refetch), because an empty list being read looked like a list read for the first time. Once a read the list issued has answered, its count is known, so a later reload keeps drawing that count until the new one lands. A read that fails forgets the count, so its Retry draws no count until it answers, never a false 0; so do rows emptied without a read (the parent record reloading, or no data adapter), so the next parent's read draws no count either. A list's first read, including one that starts only once its parent record has loaded, still draws no count while it is in flight.

‎ui/packages/plugin-detail/src/RelatedList.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,15 @@ export const RelatedList: React.FC<RelatedListProps> = ({
15151515
// value may itself be falsy. Cleared by every re-read (the fetch effect's
15161516
// first statement) and by Retry.
15171517
const [loadFailure, setLoadFailure] = React.useState<{ error: unknown } | null>(null);
1518+
// objectstack#36822 — whether `relatedData` holds the answer of a read this
1519+
// list actually issued, so the badge may keep drawing its count while a
1520+
// later read is in flight. Set only where an issued read resolves, cleared
1521+
// where one rejects (the rejection empties the rows, and 0 is not their
1522+
// count). A path that reads nothing (no scope yet, a refused filter, no
1523+
// adapter) never sets it, so the first real read still draws no count; one
1524+
// that empties the rows without reading (no scope, no adapter) clears it,
1525+
// so the next parent's read draws no count either.
1526+
const [countKnown, setCountKnown] = React.useState(false);
15181527
// Per-lookup-field cache of resolved labels: fieldName -> Map<id, label>
15191528
const [lookupLabels, setLookupLabels] = React.useState<Record<string, Record<string, string>>>({});
15201529
const { t } = useDetailTranslation();
@@ -2249,6 +2258,8 @@ export const RelatedList: React.FC<RelatedListProps> = ({
22492258
setRelatedData([]);
22502259
setTotal(null);
22512260
setHasMore(false);
2261+
// Nothing is read, and the emptied rows hold no count (objectstack#36822).
2262+
setCountKnown(false);
22522263
setLoading(false);
22532264
return;
22542265
}
@@ -2280,13 +2291,15 @@ export const RelatedList: React.FC<RelatedListProps> = ({
22802291
setRelatedData(result.rows as any[]);
22812292
setTotal(result.total);
22822293
setHasMore(result.total > result.rows.length);
2294+
setCountKnown(true);
22832295
setLoading(false);
22842296
},
22852297
(err) => {
22862298
if (cancelled) return;
22872299
setRelatedData([]);
22882300
setTotal(null);
22892301
setHasMore(false);
2302+
setCountKnown(false);
22902303
setLoadFailure({ error: err });
22912304
setLoading(false);
22922305
},
@@ -2380,6 +2393,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
23802393
setTotal(null);
23812394
setHasMore(false);
23822395
}
2396+
setCountKnown(true);
23832397
setLoading(false);
23842398
}).catch((err) => {
23852399
console.error('Failed to fetch related data:', err);
@@ -2389,6 +2403,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
23892403
setRelatedData([]);
23902404
setTotal(null);
23912405
setHasMore(false);
2406+
setCountKnown(false);
23922407
setLoadFailure({ error: err });
23932408
setLoading(false);
23942409
});
@@ -2406,6 +2421,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
24062421
setRelatedData([]);
24072422
setTotal(null);
24082423
setHasMore(false);
2424+
setCountKnown(false);
24092425
setLoading(false);
24102426
} else if (referenceFieldIsMultiValue) {
24112427
// The same refusal as the arm above, one cause earlier: this path's
@@ -2432,6 +2448,7 @@ export const RelatedList: React.FC<RelatedListProps> = ({
24322448
setRelatedData([]);
24332449
setTotal(null);
24342450
setHasMore(false);
2451+
setCountKnown(false);
24352452
setLoading(false);
24362453
} else {
24372454
const qs = new URLSearchParams({
@@ -2451,13 +2468,15 @@ export const RelatedList: React.FC<RelatedListProps> = ({
24512468
setRelatedData(items);
24522469
setTotal(null);
24532470
setHasMore(false);
2471+
setCountKnown(true);
24542472
})
24552473
.catch(err => {
24562474
console.error('Failed to fetch related data:', err);
24572475
if (cancelled) return;
24582476
setRelatedData([]);
24592477
setTotal(null);
24602478
setHasMore(false);
2479+
setCountKnown(false);
24612480
setLoadFailure({ error: err });
24622481
})
24632482
.finally(() => { if (!cancelled) setLoading(false); });
@@ -4032,7 +4051,11 @@ export const RelatedList: React.FC<RelatedListProps> = ({
40324051
// the 0 an empty first read holds: while the first rows are in flight (or a
40334052
// card out of view has not asked) and no count answers for them, the badge
40344053
// waits. A refetch over rows already drawn keeps their count.
4035-
const countPending = !countAnswers && loading && relatedData.length === 0 && !(windowed && total != null);
4054+
// objectstack#36822 — and so does a refetch over a list that already
4055+
// answered 0: once a read has answered, the count is known (`countKnown`),
4056+
// so a later read in flight keeps drawing the last answer.
4057+
const countPending =
4058+
!countAnswers && loading && !countKnown && relatedData.length === 0 && !(windowed && total != null);
40364059
// In windowed mode an empty page beyond page 0 is a transient overflow (the
40374060
// step-back effect above corrects it), not an empty collection.
40384061
// A failed read is never empty: it draws its own line (objectstack#26415).

‎ui/packages/plugin-detail/src/__tests__/RelatedList.countPending-36235.test.tsx‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,166 @@ describe('a related list\'s count before it is known (objectstack#36235)', () =>
134134
await waitFor(() => expect(cardCount()).toBe('2'));
135135
});
136136
});
137+
138+
/**
139+
* objectstack#36822 — a refetch over a list whose count is already known keeps
140+
* drawing that count. A list that answered 0 used to blank its badge for the
141+
* length of the refetch, because an empty list in flight read as a first read.
142+
*/
143+
describe('a related list\'s count across a refetch (objectstack#36822)', () => {
144+
/** Every badge reading the DOM goes through, from now until `stop()`. */
145+
function watchBadge() {
146+
const seen: Array<string | null> = [headBadge()];
147+
const observer = new MutationObserver(() => seen.push(headBadge()));
148+
observer.observe(document.body, { subtree: true, childList: true, characterData: true, attributes: true });
149+
return { seen, stop: () => observer.disconnect() };
150+
}
151+
152+
const refetch = () =>
153+
act(async () => {
154+
window.dispatchEvent(new CustomEvent('objectui:related-changed', { detail: { objectName: 'contact' } }));
155+
});
156+
157+
it('a list that answered 0 keeps drawing 0 through a refetch that answers no rows', async () => {
158+
const read = deferredFind();
159+
renderList(read.find);
160+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(1));
161+
await read.answer([]);
162+
await waitFor(() => expect(headBadge()).toBe('0'));
163+
164+
const watch = watchBadge();
165+
await refetch();
166+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(2));
167+
await flush();
168+
// The refetch is held in flight here: the badge still reads 0.
169+
expect(headBadge()).toBe('0');
170+
await read.answer([]);
171+
await flush();
172+
watch.stop();
173+
expect(headBadge()).toBe('0');
174+
expect(watch.seen.length).toBeGreaterThan(1);
175+
expect(watch.seen.filter((v) => v !== '0')).toEqual([]);
176+
});
177+
178+
it('a list that answered rows draws their count through a refetch, then the 0 it answers', async () => {
179+
const read = deferredFind();
180+
renderList(read.find);
181+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(1));
182+
await read.answer(ROWS);
183+
await waitFor(() => expect(headBadge()).toBe('2'));
184+
185+
const watch = watchBadge();
186+
await refetch();
187+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(2));
188+
await flush();
189+
expect(headBadge()).toBe('2');
190+
await read.answer([]);
191+
await waitFor(() => expect(headBadge()).toBe('0'));
192+
watch.stop();
193+
expect(watch.seen.filter((v) => v !== '2' && v !== '0')).toEqual([]);
194+
});
195+
196+
/** A `find` whose current read is answered or refused by hand. */
197+
function heldFind() {
198+
let settle = { resolve: (_: unknown) => {}, reject: (_: unknown) => {} };
199+
const find = vi.fn(
200+
() => new Promise((resolve, reject) => {
201+
settle = { resolve, reject };
202+
}),
203+
);
204+
return {
205+
find,
206+
answer: (rows: unknown[]) => act(async () => { settle.resolve({ data: rows, total: rows.length }); }),
207+
fail: () => act(async () => { settle.reject(new Error('network down')); }),
208+
};
209+
}
210+
211+
it('a read that fails after rows were known draws no count while Retry reads, never a false 0', async () => {
212+
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
213+
const read = heldFind();
214+
renderList(read.find);
215+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(1));
216+
await read.answer(ROWS);
217+
await waitFor(() => expect(headBadge()).toBe('2'));
218+
219+
await refetch();
220+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(2));
221+
await read.fail();
222+
const retry = await waitFor(() => {
223+
const button = document.querySelector('[data-testid="related-list-load-failed-retry"]');
224+
expect(button).not.toBeNull();
225+
return button as HTMLElement;
226+
});
227+
await act(async () => { retry.click(); });
228+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(3));
229+
await flush();
230+
// Retry's read is held in flight: the failed read emptied the rows, so
231+
// their count is not known and the badge waits.
232+
expect(document.querySelector('[data-related-list-head]')).not.toBeNull();
233+
expect(headBadge()).toBeNull();
234+
await read.answer([]);
235+
await waitFor(() => expect(headBadge()).toBe('0'));
236+
quiet.mockRestore();
237+
});
238+
239+
it('a list with no scope on its first render draws no count during its first real read', async () => {
240+
const read = heldFind();
241+
const list = (parentId: string | null) => (
242+
<RelatedList
243+
title="Contacts"
244+
type="table"
245+
api="contact"
246+
objectName="contact"
247+
referenceField="account"
248+
parentId={parentId as any}
249+
columns={[{ accessorKey: 'name', header: 'Name' }]}
250+
dataSource={{ find: read.find } as any}
251+
/>
252+
);
253+
// The parent is still loading: no scope, so nothing is read.
254+
const view = render(list(null));
255+
await flush();
256+
expect(read.find).not.toHaveBeenCalled();
257+
// The parent lands: the first real read is held in flight.
258+
view.rerender(list('ACC-1'));
259+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(1));
260+
await flush();
261+
expect(document.querySelector('[data-related-list-head]')).not.toBeNull();
262+
expect(headBadge()).toBeNull();
263+
await read.answer(ROWS);
264+
await waitFor(() => expect(headBadge()).toBe('2'));
265+
});
266+
267+
it('a list whose parent reloads through no scope draws no count during the new parent\'s read', async () => {
268+
const read = heldFind();
269+
const list = (parentId: string | null) => (
270+
<RelatedList
271+
title="Contacts"
272+
type="table"
273+
api="contact"
274+
objectName="contact"
275+
referenceField="account"
276+
parentId={parentId as any}
277+
columns={[{ accessorKey: 'name', header: 'Name' }]}
278+
dataSource={{ find: read.find } as any}
279+
/>
280+
);
281+
const view = render(list('ACC-1'));
282+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(1));
283+
await read.answer(ROWS);
284+
await waitFor(() => expect(headBadge()).toBe('2'));
285+
// The parent reloads: no scope, so nothing is read and the rows empty.
286+
view.rerender(list(null));
287+
await flush();
288+
expect(read.find).toHaveBeenCalledTimes(1);
289+
// The new parent lands: its read is held in flight, and the emptied rows
290+
// are no count of it.
291+
view.rerender(list('ACC-2'));
292+
await waitFor(() => expect(read.find).toHaveBeenCalledTimes(2));
293+
await flush();
294+
expect(document.querySelector('[data-related-list-head]')).not.toBeNull();
295+
expect(headBadge()).toBeNull();
296+
await read.answer([{ id: 'c3', name: 'Linus' }]);
297+
await waitFor(() => expect(headBadge()).toBe('1'));
298+
});
299+
});

0 commit comments

Comments
 (0)