Skip to content

Commit cfd59cc

Browse files
committed
Fixes + handle aiChat enabled or not
1 parent c265320 commit cfd59cc

File tree

7 files changed

+137
-97
lines changed

7 files changed

+137
-97
lines changed
Lines changed: 81 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use client';
22

33
import { useAIChatController, useAIChatState } from '@/components/AI/useAIChat';
4+
import { ChatGPTIcon } from '@/components/AIActions/assets/ChatGPTIcon';
5+
import { ClaudeIcon } from '@/components/AIActions/assets/ClaudeIcon';
6+
import { MarkdownIcon } from '@/components/AIActions/assets/MarkdownIcon';
47
import AIChatIcon from '@/components/AIChat/AIChatIcon';
58
import { Button } from '@/components/primitives/Button';
69
import { DropdownMenu, DropdownMenuItem } from '@/components/primitives/DropdownMenu';
710
import { tString, useLanguage } from '@/intl/client';
811
import { Icon, type IconName, IconStyle } from '@gitbook/icons';
9-
import { useRef } from 'react';
12+
import { useCallback, useMemo, useRef } from 'react';
1013

1114
type AIAction = {
1215
icon?: IconName | React.ReactNode;
@@ -22,15 +25,22 @@ type AIAction = {
2225
export function AIActionsDropdown(props: {
2326
markdown?: string;
2427
markdownPageUrl: string;
28+
/**
29+
* Whether to include the "Ask Docs Assistant" entry in the dropdown menu. This **does not**
30+
* affect the standalone assistant button rendered next to the dropdown.
31+
* Defaults to `false` to avoid duplicating the action unless explicitly requested.
32+
*/
33+
withAIChat?: boolean;
34+
pageURL: string;
2535
}) {
26-
const { markdown, markdownPageUrl } = props;
36+
const { markdown, markdownPageUrl, withAIChat, pageURL } = props;
2737

2838
const chatController = useAIChatController();
2939
const chat = useAIChatState();
3040
const language = useLanguage();
3141
const ref = useRef<HTMLDivElement>(null);
3242

33-
const handleOpenAIAssistant = () => {
43+
const handleDocsAssistant = useCallback(() => {
3444
// Open the chat if it's not already open
3545
if (!chat.opened) {
3646
chatController.open();
@@ -40,106 +50,74 @@ export function AIActionsDropdown(props: {
4050
chatController.postMessage({
4151
message: tString(language, 'ai_chat_suggested_questions_about_this_page'),
4252
});
43-
};
53+
}, [chat, chatController, language]);
4454

45-
const actions: AIAction[] = [
46-
{
47-
icon: <AIChatIcon />,
48-
label: 'Ask Docs Assistant',
49-
description: 'Ask our Docs Assistant about this page',
50-
onClick: handleOpenAIAssistant,
51-
},
52-
...(markdown
53-
? [
54-
{
55-
icon: 'copy',
56-
label: 'Copy for LLMs',
57-
description: 'Copy page as Markdown',
58-
onClick: () => {
59-
if (!markdown) return;
60-
navigator.clipboard.writeText(markdown);
55+
const actions: AIAction[] = useMemo(
56+
(): AIAction[] => [
57+
...(withAIChat
58+
? [
59+
{
60+
icon: <AIChatIcon />,
61+
label: 'Ask Docs Assistant',
62+
description: 'Ask our Docs Assistant about this page',
63+
onClick: handleDocsAssistant,
64+
},
65+
]
66+
: []),
67+
...(markdown
68+
? [
69+
{
70+
icon: 'copy',
71+
label: 'Copy for LLMs',
72+
description: 'Copy page as Markdown',
73+
onClick: () => {
74+
if (!markdown) return;
75+
navigator.clipboard.writeText(markdown);
76+
},
6177
},
62-
},
63-
{
64-
icon: (
65-
<svg
66-
xmlns="http://www.w3.org/2000/svg"
67-
viewBox="0 0 471 289.85"
68-
className="size-5 fill-current text-current"
69-
>
70-
<title>markdown icon</title>
71-
<path d="M437,289.85H34a34,34,0,0,1-34-34V34A34,34,0,0,1,34,0H437a34,34,0,0,1,34,34V255.88A34,34,0,0,1,437,289.85ZM34,22.64A11.34,11.34,0,0,0,22.64,34V255.88A11.34,11.34,0,0,0,34,267.2H437a11.34,11.34,0,0,0,11.33-11.32V34A11.34,11.34,0,0,0,437,22.64Z" />
72-
<path d="M67.93,221.91v-154h45.29l45.29,56.61L203.8,67.93h45.29v154H203.8V133.6l-45.29,56.61L113.22,133.6v88.31Zm283.06,0-67.94-74.72h45.29V67.93h45.29v79.26h45.29Z" />
73-
</svg>
74-
),
75-
label: 'View as Markdown',
76-
description: 'View this page as plain text',
77-
isExternal: true,
78-
onClick: () => {
79-
window.open(markdownPageUrl, '_blank');
78+
{
79+
icon: <MarkdownIcon className="size-5 fill-current text-current" />,
80+
label: 'View as Markdown',
81+
description: 'View this page as plain text',
82+
isExternal: true,
83+
onClick: () => {
84+
window.open(`${markdownPageUrl}.md`, '_blank');
85+
},
8086
},
81-
},
82-
]
83-
: []),
84-
{
85-
icon: (
86-
<svg
87-
xmlns="http://www.w3.org/2000/svg"
88-
viewBox="0 0 320 320"
89-
className="mt-0.5 fill-current"
90-
>
91-
<title>ChatGPT</title>
92-
<path d="m297.06 130.97c7.26-21.79 4.76-45.66-6.85-65.48-17.46-30.4-52.56-46.04-86.84-38.68-15.25-17.18-37.16-26.95-60.13-26.81-35.04-.08-66.13 22.48-76.91 55.82-22.51 4.61-41.94 18.7-53.31 38.67-17.59 30.32-13.58 68.54 9.92 94.54-7.26 21.79-4.76 45.66 6.85 65.48 17.46 30.4 52.56 46.04 86.84 38.68 15.24 17.18 37.16 26.95 60.13 26.8 35.06.09 66.16-22.49 76.94-55.86 22.51-4.61 41.94-18.7 53.31-38.67 17.57-30.32 13.55-68.51-9.94-94.51zm-120.28 168.11c-14.03.02-27.62-4.89-38.39-13.88.49-.26 1.34-.73 1.89-1.07l63.72-36.8c3.26-1.85 5.26-5.32 5.24-9.07v-89.83l26.93 15.55c.29.14.48.42.52.74v74.39c-.04 33.08-26.83 59.9-59.91 59.97zm-128.84-55.03c-7.03-12.14-9.56-26.37-7.15-40.18.47.28 1.3.79 1.89 1.13l63.72 36.8c3.23 1.89 7.23 1.89 10.47 0l77.79-44.92v31.1c.02.32-.13.63-.38.83l-64.41 37.19c-28.69 16.52-65.33 6.7-81.92-21.95zm-16.77-139.09c7-12.16 18.05-21.46 31.21-26.29 0 .55-.03 1.52-.03 2.2v73.61c-.02 3.74 1.98 7.21 5.23 9.06l77.79 44.91-26.93 15.55c-.27.18-.61.21-.91.08l-64.42-37.22c-28.63-16.58-38.45-53.21-21.95-81.89zm221.26 51.49-77.79-44.92 26.93-15.54c.27-.18.61-.21.91-.08l64.42 37.19c28.68 16.57 38.51 53.26 21.94 81.94-7.01 12.14-18.05 21.44-31.2 26.28v-75.81c.03-3.74-1.96-7.2-5.2-9.06zm26.8-40.34c-.47-.29-1.3-.79-1.89-1.13l-63.72-36.8c-3.23-1.89-7.23-1.89-10.47 0l-77.79 44.92v-31.1c-.02-.32.13-.63.38-.83l64.41-37.16c28.69-16.55 65.37-6.7 81.91 22 6.99 12.12 9.52 26.31 7.15 40.1zm-168.51 55.43-26.94-15.55c-.29-.14-.48-.42-.52-.74v-74.39c.02-33.12 26.89-59.96 60.01-59.94 14.01 0 27.57 4.92 38.34 13.88-.49.26-1.33.73-1.89 1.07l-63.72 36.8c-3.26 1.85-5.26 5.31-5.24 9.06l-.04 89.79zm14.63-31.54 34.65-20.01 34.65 20v40.01l-34.65 20-34.65-20z" />
93-
</svg>
94-
),
95-
label: 'Open in ChatGPT',
96-
description: 'Ask ChatGPT about this page',
97-
isExternal: true,
98-
onClick: () => {
99-
window.open(
100-
`https://chatgpt.com/c/?prompt=${encodeURIComponent(markdown ?? '')}`,
101-
'_blank'
102-
);
87+
]
88+
: []),
89+
{
90+
icon: <ChatGPTIcon className="mt-0.5 fill-current" />,
91+
label: 'Open in ChatGPT',
92+
description: 'Ask ChatGPT about this page',
93+
isExternal: true,
94+
onClick: () => {
95+
window.open(getLLMURL('chatgpt', pageURL), '_blank');
96+
},
10397
},
104-
},
105-
{
106-
icon: (
107-
<svg
108-
viewBox="0 0 256 257"
109-
version="1.1"
110-
xmlns="http://www.w3.org/2000/svg"
111-
preserveAspectRatio="xMidYMid"
112-
className="mt-0.5"
113-
>
114-
<title>Claude</title>
115-
<g>
116-
<path
117-
d="M50.2278481,170.321013 L100.585316,142.063797 L101.427848,139.601013 L100.585316,138.24 L98.1225316,138.24 L89.6972152,137.721519 L60.921519,136.943797 L35.9696203,135.906835 L11.795443,134.610633 L5.70329114,133.31443 L0,125.796456 L0.583291139,122.037468 L5.70329114,118.602532 L13.0268354,119.250633 L29.2293671,120.352405 L53.5331646,122.037468 L71.161519,123.07443 L97.28,125.796456 L101.427848,125.796456 L102.011139,124.111392 L100.585316,123.07443 L99.4835443,122.037468 L74.3372152,104.992405 L47.116962,86.9751899 L32.8587342,76.6055696 L25.1463291,71.3559494 L21.2577215,66.4303797 L19.5726582,55.6718987 L26.5721519,47.9594937 L35.9696203,48.6075949 L38.3675949,49.2556962 L47.8946835,56.5792405 L68.2450633,72.3281013 L94.8172152,91.9007595 L98.7058228,95.1412658 L100.261266,94.0394937 L100.455696,93.2617722 L98.7058228,90.3453165 L84.2531646,64.2268354 L68.8283544,37.6546835 L61.958481,26.636962 L60.1437975,20.0263291 C59.4956962,17.3043038 59.0420253,15.0359494 59.0420253,12.2491139 L67.0136709,1.42582278 L71.4207595,-1.42108547e-14 L82.0496203,1.42582278 L86.521519,5.31443038 L93.1321519,20.4151899 L103.825823,44.2005063 L120.417215,76.5407595 L125.277975,86.1326582 L127.87038,95.0116456 L128.842532,97.7336709 L130.527595,97.7336709 L130.527595,96.1782278 L131.888608,77.9665823 L134.416203,55.6070886 L136.878987,26.8313924 L137.721519,18.7301266 L141.739747,9.00860759 L149.711392,3.75898734 L155.933165,6.74025316 L161.053165,14.0637975 L160.340253,18.7949367 L157.294177,38.5620253 L151.331646,69.5412658 L147.443038,90.2805063 L149.711392,90.2805063 L152.303797,87.6881013 L162.803038,73.7539241 L180.431392,51.718481 L188.208608,42.9691139 L197.282025,33.3124051 L203.114937,28.7108861 L214.132658,28.7108861 L222.233924,40.7655696 L218.604557,53.2091139 L207.262785,67.596962 L197.865316,79.7812658 L184.38481,97.9281013 L175.959494,112.44557 L176.737215,113.612152 L178.746329,113.417722 L209.207089,106.936709 L225.668861,103.955443 L245.306329,100.585316 L254.185316,104.733165 L255.157468,108.945823 L251.657722,117.56557 L230.659241,122.75038 L206.031392,127.675949 L169.348861,136.360506 L168.89519,136.684557 L169.413671,137.332658 L185.940253,138.888101 L193.004557,139.276962 L210.308861,139.276962 L242.519494,141.674937 L250.94481,147.248608 L256,154.053671 L255.157468,159.238481 L242.195443,165.849114 L224.696709,161.701266 L183.866329,151.979747 L169.867342,148.48 L167.923038,148.48 L167.923038,149.646582 L179.588861,161.053165 L200.976203,180.366582 L227.742785,205.253671 L229.103797,211.410633 L225.668861,216.271392 L222.039494,215.752911 L198.513418,198.059747 L189.44,190.088101 L168.89519,172.783797 L167.534177,172.783797 L167.534177,174.598481 L172.265316,181.533165 L197.282025,219.123038 L198.578228,230.659241 L196.763544,234.418228 L190.282532,236.686582 L183.153418,235.39038 L168.506329,214.84557 L153.40557,191.708354 L141.221266,170.969114 L139.730633,171.811646 L132.536709,249.259747 L129.166582,253.213165 L121.389367,256.19443 L114.908354,251.268861 L111.473418,243.297215 L114.908354,227.548354 L119.056203,207.003544 L122.426329,190.671392 L125.472405,170.385823 L127.287089,163.64557 L127.157468,163.191899 L125.666835,163.386329 L110.371646,184.38481 L87.1048101,215.817722 L68.6987342,235.52 L64.2916456,237.269873 L56.6440506,233.316456 L57.356962,226.252152 L61.6344304,219.96557 L87.1048101,187.560506 L102.46481,167.469367 L112.380759,155.868354 L112.315949,154.183291 L111.732658,154.183291 L44.0708861,198.124557 L32.0162025,199.68 L26.8313924,194.819241 L27.4794937,186.847595 L29.9422785,184.25519 L50.2926582,170.256203 L50.2278481,170.321013 Z"
118-
fill="currentColor"
119-
/>
120-
</g>
121-
</svg>
122-
),
123-
label: 'Open in Claude',
124-
description: 'Ask Claude about this page',
125-
isExternal: true,
126-
onClick: () =>
127-
window.open(
128-
`https://claude.ai/chat?prompt=${encodeURIComponent(markdown ?? '')}`,
129-
'_blank'
130-
),
131-
},
132-
];
98+
{
99+
icon: <ClaudeIcon className="mt-0.5" />,
100+
label: 'Open in Claude',
101+
description: 'Ask Claude about this page',
102+
isExternal: true,
103+
onClick: () => window.open(getLLMURL('claude', pageURL), '_blank'),
104+
},
105+
],
106+
[markdown, markdownPageUrl, pageURL, withAIChat, handleDocsAssistant]
107+
);
108+
109+
// The default action is Ask Docs Assistant if the AI assistant is enabled, otherwise View as Markdown
110+
const defaultAction = actions[0];
133111

134112
return (
135113
<div ref={ref} className="hidden h-fit items-stretch justify-start md:flex">
136114
<Button
137-
icon={<AIChatIcon className="size-3.5" />}
115+
icon={defaultAction.icon}
138116
size="small"
139117
variant="secondary"
140-
label="Ask Docs Assistant"
118+
label={defaultAction.label}
141119
className="hover:!scale-100 !shadow-none !rounded-r-none border-r-0 bg-tint-base text-sm"
142-
onClick={handleOpenAIAssistant}
120+
onClick={defaultAction.onClick}
143121
/>
144122
<DropdownMenu
145123
contentProps={{
@@ -193,3 +171,14 @@ export function AIActionsDropdown(props: {
193171
</div>
194172
);
195173
}
174+
175+
function getLLMURL(provider: 'chatgpt' | 'claude', url: string) {
176+
const prompt = encodeURIComponent(`Read ${url} and answer questions about the content.`);
177+
178+
switch (provider) {
179+
case 'chatgpt':
180+
return `https://chat.openai.com/?q=${prompt}`;
181+
case 'claude':
182+
return `https://claude.ai/new?q=${prompt}`;
183+
}
184+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function ChatGPTIcon(props: React.SVGProps<SVGSVGElement>) {
2+
return (
3+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 320" {...props}>
4+
<title>ChatGPT</title>
5+
<path d="m297.06 130.97c7.26-21.79 4.76-45.66-6.85-65.48-17.46-30.4-52.56-46.04-86.84-38.68-15.25-17.18-37.16-26.95-60.13-26.81-35.04-.08-66.13 22.48-76.91 55.82-22.51 4.61-41.94 18.7-53.31 38.67-17.59 30.32-13.58 68.54 9.92 94.54-7.26 21.79-4.76 45.66 6.85 65.48 17.46 30.4 52.56 46.04 86.84 38.68 15.24 17.18 37.16 26.95 60.13 26.8 35.06.09 66.16-22.49 76.94-55.86 22.51-4.61 41.94-18.7 53.31-38.67 17.57-30.32 13.55-68.51-9.94-94.51zm-120.28 168.11c-14.03.02-27.62-4.89-38.39-13.88.49-.26 1.34-.73 1.89-1.07l63.72-36.8c3.26-1.85 5.26-5.32 5.24-9.07v-89.83l26.93 15.55c.29.14.48.42.52.74v74.39c-.04 33.08-26.83 59.9-59.91 59.97zm-128.84-55.03c-7.03-12.14-9.56-26.37-7.15-40.18.47.28 1.3.79 1.89 1.13l63.72 36.8c3.23 1.89 7.23 1.89 10.47 0l77.79-44.92v31.1c.02.32-.13.63-.38.83l-64.41 37.19c-28.69 16.52-65.33 6.7-81.92-21.95zm-16.77-139.09c7-12.16 18.05-21.46 31.21-26.29 0 .55-.03 1.52-.03 2.2v73.61c-.02 3.74 1.98 7.21 5.23 9.06l77.79 44.91-26.93 15.55c-.27.18-.61.21-.91.08l-64.42-37.22c-28.63-16.58-38.45-53.21-21.95-81.89zm221.26 51.49-77.79-44.92 26.93-15.54c.27-.18.61-.21.91-.08l64.42 37.19c28.68 16.57 38.51 53.26 21.94 81.94-7.01 12.14-18.05 21.44-31.2 26.28v-75.81c.03-3.74-1.96-7.2-5.2-9.06zm26.8-40.34c-.47-.29-1.3-.79-1.89-1.13l-63.72-36.8c-3.23-1.89-7.23-1.89-10.47 0l-77.79 44.92v-31.1c-.02-.32.13-.63.38-.83l64.41-37.16c28.69-16.55 65.37-6.7 81.91 22 6.99 12.12 9.52 26.31 7.15 40.1zm-168.51 55.43-26.94-15.55c-.29-.14-.48-.42-.52-.74v-74.39c.02-33.12 26.89-59.96 60.01-59.94 14.01 0 27.57 4.92 38.34 13.88-.49.26-1.33.73-1.89 1.07l-63.72 36.8c-3.26 1.85-5.26 5.31-5.24 9.06l-.04 89.79zm14.63-31.54 34.65-20.01 34.65 20v40.01l-34.65 20-34.65-20z" />
6+
</svg>
7+
);
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function ClaudeIcon(props: React.SVGProps<SVGSVGElement>) {
2+
return (
3+
<svg
4+
viewBox="0 0 256 257"
5+
version="1.1"
6+
xmlns="http://www.w3.org/2000/svg"
7+
preserveAspectRatio="xMidYMid"
8+
{...props}
9+
>
10+
<title>Claude</title>
11+
<g>
12+
<path
13+
d="M50.2278481,170.321013 L100.585316,142.063797 L101.427848,139.601013 L100.585316,138.24 L98.1225316,138.24 L89.6972152,137.721519 L60.921519,136.943797 L35.9696203,135.906835 L11.795443,134.610633 L5.70329114,133.31443 L0,125.796456 L0.583291139,122.037468 L5.70329114,118.602532 L13.0268354,119.250633 L29.2293671,120.352405 L53.5331646,122.037468 L71.161519,123.07443 L97.28,125.796456 L101.427848,125.796456 L102.011139,124.111392 L100.585316,123.07443 L99.4835443,122.037468 L74.3372152,104.992405 L47.116962,86.9751899 L32.8587342,76.6055696 L25.1463291,71.3559494 L21.2577215,66.4303797 L19.5726582,55.6718987 L26.5721519,47.9594937 L35.9696203,48.6075949 L38.3675949,49.2556962 L47.8946835,56.5792405 L68.2450633,72.3281013 L94.8172152,91.9007595 L98.7058228,95.1412658 L100.261266,94.0394937 L100.455696,93.2617722 L98.7058228,90.3453165 L84.2531646,64.2268354 L68.8283544,37.6546835 L61.958481,26.636962 L60.1437975,20.0263291 C59.4956962,17.3043038 59.0420253,15.0359494 59.0420253,12.2491139 L67.0136709,1.42582278 L71.4207595,-1.42108547e-14 L82.0496203,1.42582278 L86.521519,5.31443038 L93.1321519,20.4151899 L103.825823,44.2005063 L120.417215,76.5407595 L125.277975,86.1326582 L127.87038,95.0116456 L128.842532,97.7336709 L130.527595,97.7336709 L130.527595,96.1782278 L131.888608,77.9665823 L134.416203,55.6070886 L136.878987,26.8313924 L137.721519,18.7301266 L141.739747,9.00860759 L149.711392,3.75898734 L155.933165,6.74025316 L161.053165,14.0637975 L160.340253,18.7949367 L157.294177,38.5620253 L151.331646,69.5412658 L147.443038,90.2805063 L149.711392,90.2805063 L152.303797,87.6881013 L162.803038,73.7539241 L180.431392,51.718481 L188.208608,42.9691139 L197.282025,33.3124051 L203.114937,28.7108861 L214.132658,28.7108861 L222.233924,40.7655696 L218.604557,53.2091139 L207.262785,67.596962 L197.865316,79.7812658 L184.38481,97.9281013 L175.959494,112.44557 L176.737215,113.612152 L178.746329,113.417722 L209.207089,106.936709 L225.668861,103.955443 L245.306329,100.585316 L254.185316,104.733165 L255.157468,108.945823 L251.657722,117.56557 L230.659241,122.75038 L206.031392,127.675949 L169.348861,136.360506 L168.89519,136.684557 L169.413671,137.332658 L185.940253,138.888101 L193.004557,139.276962 L210.308861,139.276962 L242.519494,141.674937 L250.94481,147.248608 L256,154.053671 L255.157468,159.238481 L242.195443,165.849114 L224.696709,161.701266 L183.866329,151.979747 L169.867342,148.48 L167.923038,148.48 L167.923038,149.646582 L179.588861,161.053165 L200.976203,180.366582 L227.742785,205.253671 L229.103797,211.410633 L225.668861,216.271392 L222.039494,215.752911 L198.513418,198.059747 L189.44,190.088101 L168.89519,172.783797 L167.534177,172.783797 L167.534177,174.598481 L172.265316,181.533165 L197.282025,219.123038 L198.578228,230.659241 L196.763544,234.418228 L190.282532,236.686582 L183.153418,235.39038 L168.506329,214.84557 L153.40557,191.708354 L141.221266,170.969114 L139.730633,171.811646 L132.536709,249.259747 L129.166582,253.213165 L121.389367,256.19443 L114.908354,251.268861 L111.473418,243.297215 L114.908354,227.548354 L119.056203,207.003544 L122.426329,190.671392 L125.472405,170.385823 L127.287089,163.64557 L127.157468,163.191899 L125.666835,163.386329 L110.371646,184.38481 L87.1048101,215.817722 L68.6987342,235.52 L64.2916456,237.269873 L56.6440506,233.316456 L57.356962,226.252152 L61.6344304,219.96557 L87.1048101,187.560506 L102.46481,167.469367 L112.380759,155.868354 L112.315949,154.183291 L111.732658,154.183291 L44.0708861,198.124557 L32.0162025,199.68 L26.8313924,194.819241 L27.4794937,186.847595 L29.9422785,184.25519 L50.2926582,170.256203 L50.2278481,170.321013 Z"
14+
fill="currentColor"
15+
/>
16+
</g>
17+
</svg>
18+
);
19+
}

0 commit comments

Comments
 (0)