2626 * entry.
2727 */
2828
29- import { lazy , Suspense , type ComponentProps } from 'react' ;
29+ import { lazy , Suspense , useState , type ComponentProps } from 'react' ;
3030import type {
3131 HeaderHighlight as HeaderHighlightComponent ,
3232 RelatedList as RelatedListComponent ,
@@ -35,31 +35,51 @@ import type {
3535type DetailEntry = typeof import ( '@object-ui/plugin-detail' ) ;
3636
3737let 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. */
4042export 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
4957const HeaderHighlightChunk = lazy ( ( ) => preloadDetailEntry ( ) . then ( ( m ) => ( { default : m . HeaderHighlight } ) ) ) ;
5058const 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+ */
5364export 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+ */
6280export 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 } />
0 commit comments