Skip to content

Commit 309f44a

Browse files
committed
refactor(fields): derive the shareable-object list state from the door's answer, with no synchronous setState in the effect
Claude-Session: https://claude.ai/code/session_01FEkQ5ntiCZev2cDL6r5VLw Co-authored-by: Claude <noreply@anthropic.com>
1 parent 417a139 commit 309f44a

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

‎ui/packages/fields/src/widgets/ShareableObjectRefField.tsx‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
8591
function 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

109114
export 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

Comments
 (0)