Skip to content

Commit feb24f5

Browse files
committed
fix(app-shell): a detail surface mounted after the entry loaded draws without suspending
Claude-Session: https://claude.ai/code/session_01FEkQ5kt9AD1TPLy7Y9oa9D Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f3214b2 commit feb24f5

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* entry.
2727
*/
2828

29-
import { lazy, Suspense, type ComponentProps } from 'react';
29+
import { lazy, Suspense, useState, type ComponentProps } from 'react';
3030
import type {
3131
HeaderHighlight as HeaderHighlightComponent,
3232
RelatedList as RelatedListComponent,
@@ -35,31 +35,51 @@ import type {
3535
type DetailEntry = typeof import('@object-ui/plugin-detail');
3636

3737
let detailEntry: Promise<DetailEntry> | undefined;
38+
/** The entry once it has loaded: a surface mounted after that renders it without suspending. */
39+
let loadedEntry: DetailEntry | undefined;
3840

3941
/** Starts loading the detail entry (once); the lazy components below share it. */
4042
export function preloadDetailEntry(): Promise<DetailEntry> {
4143
// A failed load is forgotten, so the next draw asks again.
42-
detailEntry ??= import('@object-ui/plugin-detail').catch((error: unknown) => {
43-
detailEntry = undefined;
44-
throw error;
45-
});
44+
detailEntry ??= import('@object-ui/plugin-detail').then(
45+
(entry) => {
46+
loadedEntry = entry;
47+
return entry;
48+
},
49+
(error: unknown) => {
50+
detailEntry = undefined;
51+
throw error;
52+
},
53+
);
4654
return detailEntry;
4755
}
4856

4957
const HeaderHighlightChunk = lazy(() => preloadDetailEntry().then((m) => ({ default: m.HeaderHighlight })));
5058
const RelatedListChunk = lazy(() => preloadDetailEntry().then((m) => ({ default: m.RelatedList })));
5159

52-
/** `HeaderHighlight`, loaded on first draw. Nothing shows until the detail chunk is in. */
60+
/**
61+
* `HeaderHighlight`, loaded on first draw. Nothing shows until the detail chunk
62+
* is in; once it is, the component draws in the same render.
63+
*/
5364
export function LazyHeaderHighlight(props: ComponentProps<typeof HeaderHighlightComponent>) {
65+
// Decided once per mount, so a draw never switches between the two element
66+
// types (which would remount the component and drop its state).
67+
const [entry] = useState(() => loadedEntry);
68+
if (entry) return <entry.HeaderHighlight {...props} />;
5469
return (
5570
<Suspense fallback={null}>
5671
<HeaderHighlightChunk {...props} />
5772
</Suspense>
5873
);
5974
}
6075

61-
/** `RelatedList`, loaded on first draw. Nothing shows until the detail chunk is in. */
76+
/**
77+
* `RelatedList`, loaded on first draw. Nothing shows until the detail chunk is
78+
* in; once it is, the component draws in the same render.
79+
*/
6280
export function LazyRelatedList(props: ComponentProps<typeof RelatedListComponent>) {
81+
const [entry] = useState(() => loadedEntry);
82+
if (entry) return <entry.RelatedList {...props} />;
6383
return (
6484
<Suspense fallback={null}>
6585
<RelatedListChunk {...props} />

‎ui/packages/app-shell/src/views/record-preview/RecordPreviewCard.test.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import { PermissionProvider } from '@object-ui/permissions';
3333
import { createI18n, I18nProvider } from '@object-ui/i18n';
3434
import { MetadataProvider } from '../../providers/MetadataProvider';
3535
import { RecordPreviewCard, type RecordPreviewSource } from './RecordPreviewCard';
36+
// The key fields draw through `lazy()` (`lazyDetailSurfaces.tsx`): load the
37+
// detail entry at module scope, so its cost is paid at import and never inside
38+
// a test's waits (ui/AGENTS.md, lazy boundaries in tests).
39+
import '@object-ui/plugin-detail';
3640

3741
const OBJECT = 'invoice_12029';
3842

@@ -136,7 +140,8 @@ describe('RecordPreviewCard — loading and readable (objectui#12029)', () => {
136140
expect(screen.getByTitle('Laptop refresh')).toHaveTextContent('Laptop refresh');
137141
expect(screen.queryByText('INV-0001')).toBeNull();
138142
expect(screen.getByText('Invoice')).toBeInTheDocument();
139-
expect(screen.getByText('Note')).toBeInTheDocument();
143+
// The key-field strip sits behind the detail entry's lazy boundary.
144+
expect(await screen.findByText('Note')).toBeInTheDocument();
140145
expect(screen.getByText('Quarterly batch')).toBeInTheDocument();
141146

142147
// One read, the record page's request shape: no reference field is shown,

0 commit comments

Comments
 (0)