Skip to content

Commit bf8e34a

Browse files
committed
Add customizable suggested questions
1 parent 2aa4f2e commit bf8e34a

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

.changeset/ready-aliens-lose.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Add customizable suggested questions

packages/gitbook/src/components/AI/useAI.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useSearch } from '../Search/useSearch';
1515
// Unify assistants configuration context with the assistants hook in one place
1616
export type AIConfig = {
1717
aiMode: CustomizationAIMode;
18+
suggestions?: string[];
1819
trademark: boolean;
1920
};
2021

@@ -49,8 +50,11 @@ export type Assistant = Omit<GitBookAssistant, 'icon'> & {
4950
const AIContext = React.createContext<AIConfig | null>(null);
5051

5152
export function AIContextProvider(props: React.PropsWithChildren<AIConfig>): React.ReactElement {
52-
const { aiMode, trademark, children } = props;
53-
const value = React.useMemo(() => ({ aiMode, trademark }), [aiMode, trademark]);
53+
const { aiMode, trademark, suggestions, children } = props;
54+
const value = React.useMemo(
55+
() => ({ aiMode, trademark, suggestions }),
56+
[aiMode, trademark, suggestions]
57+
);
5458
return <AIContext.Provider value={value}>{children}</AIContext.Provider>;
5559
}
5660

packages/gitbook/src/components/AIChat/AIChat.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useHotkeys } from 'react-hotkeys-hook';
88
import {
99
type AIChatController,
1010
type AIChatState,
11+
useAI,
1112
useAIChatController,
1213
useAIChatState,
1314
} from '../AI';
@@ -32,6 +33,9 @@ import AIChatSuggestedQuestions from './AIChatSuggestedQuestions';
3233
export function AIChat(props: { trademark: boolean }) {
3334
const { trademark } = props;
3435

36+
const {
37+
config: { suggestions },
38+
} = useAI();
3539
const language = useLanguage();
3640
const chat = useAIChatState();
3741
const chatController = useAIChatController();
@@ -96,7 +100,12 @@ export function AIChat(props: { trademark: boolean }) {
96100
</EmbeddableFrameButtons>
97101
</EmbeddableFrameHeader>
98102
<EmbeddableFrameBody>
99-
<AIChatBody chatController={chatController} chat={chat} trademark={trademark} />
103+
<AIChatBody
104+
chatController={chatController}
105+
chat={chat}
106+
trademark={trademark}
107+
suggestions={suggestions}
108+
/>
100109
</EmbeddableFrameBody>
101110
</EmbeddableFrame>
102111
</div>

packages/gitbook/src/components/Embeddable/EmbeddableRootLayout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export async function EmbeddableRootLayout({
3535
>
3636
<AIContextProvider
3737
aiMode={CustomizationAIMode.Assistant}
38+
suggestions={context.customization.ai?.suggestions}
3839
trademark={context.customization.trademark.enabled}
3940
>
4041
<SpaceLayoutServerContext

packages/gitbook/src/components/Search/SearchContainer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function SearchContainer(props: SearchContainerProps) {
6464
siteSpaceIds,
6565
} = props;
6666

67-
const { assistants } = useAI();
67+
const { assistants, config } = useAI();
6868

6969
const [state, setSearchState] = useSearch();
7070
const searchAsk = useSearchAskState();
@@ -187,7 +187,8 @@ export function SearchContainer(props: SearchContainerProps) {
187187
siteSpaceId,
188188
siteSpaceIds,
189189
scope: state?.scope ?? 'default',
190-
withAI: withAI,
190+
withAI,
191+
suggestions: config.suggestions,
191192
});
192193
const searchValue = state?.query ?? (withSearchAI || !withAI ? state?.ask : null) ?? '';
193194

packages/gitbook/src/components/Search/useSearchResults.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export function useSearchResults(props: {
3636
siteSpaceIds: string[];
3737
scope: SearchScope;
3838
withAI: boolean;
39+
suggestions?: string[];
3940
}) {
40-
const { disabled, query, siteSpaceId, siteSpaceIds, scope } = props;
41+
const { disabled, query, siteSpaceId, siteSpaceIds, scope, suggestions } = props;
4142

4243
const trackEvent = useTrackEvent();
4344

@@ -79,6 +80,22 @@ export function useSearchResults(props: {
7980
const questions = new Set<string>();
8081
const recommendedQuestions: ResultType[] = [];
8182

83+
if (suggestions && suggestions.length > 0) {
84+
suggestions.forEach((question) => {
85+
questions.add(question);
86+
});
87+
setResultsState({
88+
results: suggestions.map((question, index) => ({
89+
type: 'recommended-question',
90+
id: `recommended-question-${index}`,
91+
question,
92+
})),
93+
fetching: false,
94+
error: false,
95+
});
96+
return;
97+
}
98+
8299
const timeout = setTimeout(async () => {
83100
if (cancelled) {
84101
return;

packages/gitbook/src/components/SiteLayout/SiteLayout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export async function SiteLayout(props: {
5656
>
5757
<AIContextProvider
5858
aiMode={customization.ai?.mode}
59+
suggestions={context.customization.ai?.suggestions}
5960
trademark={customization.trademark.enabled}
6061
>
6162
<SpaceLayout

0 commit comments

Comments
 (0)