mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 10:33:22 +00:00
Fix window.GitBook.registerAssistant is being called twice (#4296)
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user