@@ -82,28 +82,33 @@ async function readShareableObjects(doFetch: ApiFetch): Promise<ShareableObjectH
8282 return listOf ( await res . json ( ) ) ;
8383}
8484
85+ /** What the door answered through one fetch function: its list, or `null` for no list. */
86+ interface DoorAnswer {
87+ from : ApiFetch ;
88+ objects : ShareableObjectHeader [ ] | null ;
89+ }
90+
8591function useShareableObjects ( doFetch : ApiFetch | undefined , enabled : boolean ) : ShareableCatalog {
86- const [ catalog , setCatalog ] = React . useState < ShareableCatalog > ( doFetch && enabled ? LOADING : FALLBACK ) ;
92+ const [ answer , setAnswer ] = React . useState < DoorAnswer | null > ( null ) ;
8793 React . useEffect ( ( ) => {
88- if ( ! doFetch || ! enabled ) {
89- setCatalog ( FALLBACK ) ;
90- return ;
91- }
94+ if ( ! doFetch || ! enabled ) return ;
9295 let cancelled = false ;
93- setCatalog ( LOADING ) ;
9496 readShareableObjects ( doFetch ) . then (
9597 ( objects ) => {
96- if ( ! cancelled ) setCatalog ( objects ? { status : 'listed' , objects } : FALLBACK ) ;
98+ if ( ! cancelled ) setAnswer ( { from : doFetch , objects } ) ;
9799 } ,
98100 ( ) => {
99- if ( ! cancelled ) setCatalog ( FALLBACK ) ;
101+ if ( ! cancelled ) setAnswer ( { from : doFetch , objects : null } ) ;
100102 } ,
101103 ) ;
102104 return ( ) => {
103105 cancelled = true ;
104106 } ;
105107 } , [ doFetch , enabled ] ) ;
106- return catalog ;
108+ if ( ! doFetch || ! enabled ) return FALLBACK ;
109+ // An answer read through another fetch function is not this one's.
110+ if ( ! answer || answer . from !== doFetch ) return LOADING ;
111+ return answer . objects ? { status : 'listed' , objects : answer . objects } : FALLBACK ;
107112}
108113
109114export function ShareableObjectRefField ( props : FieldWidgetComponentProps < string > ) {
@@ -112,12 +117,14 @@ export function ShareableObjectRefField(props: FieldWidgetComponentProps<string>
112117 const { t } = useFieldTranslation ( ) ;
113118 const catalog = useShareableObjects ( ctx ?. apiFetch , ! readonly ) ;
114119
115- const options = React . useMemo ( ( ) => {
116- if ( catalog . status !== 'listed' ) return [ ] ;
117- const opts = catalog . objects . map ( ( o ) => ( { value : o . name , label : objectRefOptionLabel ( o . name , o . label ) } ) ) ;
118- if ( value && ! opts . some ( ( o ) => o . value === value ) ) opts . unshift ( { value, label : value } ) ;
119- return opts ;
120- } , [ catalog , value ] ) ;
120+ // The door's order, as given; a stored name it does not list leads.
121+ const options =
122+ catalog . status === 'listed'
123+ ? catalog . objects . map ( ( o ) => ( { value : o . name , label : objectRefOptionLabel ( o . name , o . label ) } ) )
124+ : [ ] ;
125+ if ( value && catalog . status === 'listed' && ! options . some ( ( o ) => o . value === value ) ) {
126+ options . unshift ( { value, label : value } ) ;
127+ }
121128
122129 if ( readonly || catalog . status === 'fallback' ) return < ObjectRefField { ...props } /> ;
123130
0 commit comments