Fix window.GitBook.registerAssistant is being called twice (#4296)

This commit is contained in:
Zeno Kapitein
2026-06-10 17:54:38 +02:00
committed by GitHub
parent 6bd522b322
commit 7036b67307
2 changed files with 16 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Fix custom assistants (e.g. Kapa, Inkeep) being opened twice when clicking "Ask with …" in the search bar.
@@ -19,18 +19,23 @@ function useInitialAskBootstrap(props: {
isLoaded: boolean;
}) {
const { asEmbeddable, assistants, initialAsk, isLoaded } = props;
const handledInitialAskRef = React.useRef<string | null | undefined>(undefined);
const hasBootstrappedRef = React.useRef(false);
React.useEffect(() => {
if (asEmbeddable) return;
if (hasBootstrappedRef.current) return;
if (assistants.length === 0) return;
if (initialAsk === null) return;
if (handledInitialAskRef.current === initialAsk) return;
if (!isLoaded) return;
// For simplicity we're only triggering the first assistant.
if (isLoaded) {
// Mark bootstrap as done once assistants and the body are ready, even if
// there is no initial ask. Subsequent `ask` changes come from user
// interactions (e.g. clicking "Ask with …" in the search bar, which also
// calls `assistant.open` directly) and must not re-trigger this effect.
hasBootstrappedRef.current = true;
if (initialAsk !== null) {
// For simplicity we're only triggering the first assistant.
assistants[0]?.open(initialAsk || undefined);
handledInitialAskRef.current = initialAsk;
}
}, [asEmbeddable, assistants, initialAsk, isLoaded]);
}