|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useAIChatController } from '@/components/AI/useAIChat'; |
| 4 | +import { useAIChatState } from '@/components/AI/useAIChat'; |
| 5 | +import { ChatGPTIcon } from '@/components/AIActions/assets/ChatGPTIcon'; |
| 6 | +import { ClaudeIcon } from '@/components/AIActions/assets/ClaudeIcon'; |
| 7 | +import { MarkdownIcon } from '@/components/AIActions/assets/MarkdownIcon'; |
| 8 | +import AIChatIcon from '@/components/AIChat/AIChatIcon'; |
| 9 | +import { Button } from '@/components/primitives/Button'; |
| 10 | +import { DropdownMenuItem } from '@/components/primitives/DropdownMenu'; |
| 11 | +import { tString, useLanguage } from '@/intl/client'; |
| 12 | +import type { TranslationLanguage } from '@/intl/translations'; |
| 13 | +import { Icon, type IconName, IconStyle } from '@gitbook/icons'; |
| 14 | +import assertNever from 'assert-never'; |
| 15 | +import type React from 'react'; |
| 16 | +import { useEffect, useRef } from 'react'; |
| 17 | +import { create } from 'zustand'; |
| 18 | + |
| 19 | +type AIActionType = 'button' | 'dropdown-menu-item'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Opens our AI Docs Assistant. |
| 23 | + */ |
| 24 | +export function OpenDocsAssistant(props: { type: AIActionType }) { |
| 25 | + const { type } = props; |
| 26 | + const chatController = useAIChatController(); |
| 27 | + const chat = useAIChatState(); |
| 28 | + const language = useLanguage(); |
| 29 | + |
| 30 | + return ( |
| 31 | + <AIActionWrapper |
| 32 | + type={type} |
| 33 | + icon={<AIChatIcon state={chat.loading ? 'thinking' : 'default'} />} |
| 34 | + label={tString(language, 'ai_chat_ask', tString(language, 'ai_chat_assistant_name'))} |
| 35 | + shortLabel={tString(language, 'ask')} |
| 36 | + description={tString( |
| 37 | + language, |
| 38 | + 'ai_chat_ask_about_page', |
| 39 | + tString(language, 'ai_chat_assistant_name') |
| 40 | + )} |
| 41 | + disabled={chat.loading} |
| 42 | + onClick={() => { |
| 43 | + // Open the chat if it's not already open |
| 44 | + if (!chat.opened) { |
| 45 | + chatController.open(); |
| 46 | + } |
| 47 | + |
| 48 | + // Send the "What is this page about?" message |
| 49 | + chatController.postMessage({ |
| 50 | + message: tString(language, 'ai_chat_suggested_questions_about_this_page'), |
| 51 | + }); |
| 52 | + }} |
| 53 | + /> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +// We need to store the copied state in a store to share the state between the |
| 58 | +// copy button and the dropdown menu item. |
| 59 | +const useCopiedStore = create<{ |
| 60 | + copied: boolean; |
| 61 | + setCopied: (copied: boolean) => void; |
| 62 | +}>((set) => ({ |
| 63 | + copied: false, |
| 64 | + setCopied: (copied: boolean) => set({ copied }), |
| 65 | +})); |
| 66 | + |
| 67 | +/** |
| 68 | + * Copies the markdown version of the page to the clipboard. |
| 69 | + */ |
| 70 | +export function CopyMarkdown(props: { |
| 71 | + markdown: string; |
| 72 | + type: AIActionType; |
| 73 | + isDefaultAction?: boolean; |
| 74 | +}) { |
| 75 | + const { markdown, type, isDefaultAction } = props; |
| 76 | + const language = useLanguage(); |
| 77 | + const { copied, setCopied } = useCopiedStore(); |
| 78 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 79 | + |
| 80 | + // Close the dropdown menu manually after the copy button is clicked |
| 81 | + const closeDropdownMenu = () => { |
| 82 | + const dropdownMenu = document.querySelector('div[data-radix-popper-content-wrapper]'); |
| 83 | + |
| 84 | + // Cancel if no dropdown menu is open |
| 85 | + if (!dropdownMenu) return; |
| 86 | + |
| 87 | + // Dispatch on `document` so that the event is captured by Radix's |
| 88 | + // dismissable-layer listener regardless of focus location. |
| 89 | + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); |
| 90 | + }; |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + return () => { |
| 94 | + if (timeoutRef.current) { |
| 95 | + clearTimeout(timeoutRef.current); |
| 96 | + } |
| 97 | + }; |
| 98 | + }, []); |
| 99 | + |
| 100 | + const onClick = (e: React.MouseEvent) => { |
| 101 | + // Prevent default behavior for non-default actions to avoid closing the dropdown. |
| 102 | + // This allows showing transient UI (e.g., a "copied" state) inside the menu item. |
| 103 | + // Default action buttons are excluded from this behavior. |
| 104 | + if (!isDefaultAction) { |
| 105 | + e.preventDefault(); |
| 106 | + } |
| 107 | + |
| 108 | + // Cancel any pending timeout |
| 109 | + if (timeoutRef.current) { |
| 110 | + clearTimeout(timeoutRef.current); |
| 111 | + } |
| 112 | + |
| 113 | + navigator.clipboard.writeText(markdown); |
| 114 | + |
| 115 | + setCopied(true); |
| 116 | + |
| 117 | + // Reset the copied state after 2 seconds |
| 118 | + timeoutRef.current = setTimeout(() => { |
| 119 | + // Close the dropdown menu if it's a dropdown menu item and not the default action |
| 120 | + if (type === 'dropdown-menu-item' && !isDefaultAction) { |
| 121 | + closeDropdownMenu(); |
| 122 | + } |
| 123 | + |
| 124 | + setCopied(false); |
| 125 | + }, 2000); |
| 126 | + }; |
| 127 | + |
| 128 | + return ( |
| 129 | + <AIActionWrapper |
| 130 | + type={type} |
| 131 | + icon={copied ? 'check' : 'copy'} |
| 132 | + label={copied ? tString(language, 'code_copied') : tString(language, 'copy_page')} |
| 133 | + description={tString(language, 'copy_page_markdown')} |
| 134 | + onClick={onClick} |
| 135 | + /> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Redirects to the markdown version of the page. |
| 141 | + */ |
| 142 | +export function ViewAsMarkdown(props: { markdownPageUrl: string; type: AIActionType }) { |
| 143 | + const { markdownPageUrl, type } = props; |
| 144 | + const language = useLanguage(); |
| 145 | + |
| 146 | + return ( |
| 147 | + <AIActionWrapper |
| 148 | + type={type} |
| 149 | + icon={<MarkdownIcon className="size-4 fill-current" />} |
| 150 | + label={tString(language, 'view_page_markdown')} |
| 151 | + description={tString(language, 'view_page_plaintext')} |
| 152 | + href={`${markdownPageUrl}.md`} |
| 153 | + /> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Open the page in a LLM with a pre-filled prompt. Either ChatGPT or Claude. |
| 159 | + */ |
| 160 | +export function OpenInLLM(props: { |
| 161 | + provider: 'chatgpt' | 'claude'; |
| 162 | + url: string; |
| 163 | + type: AIActionType; |
| 164 | +}) { |
| 165 | + const { provider, url, type } = props; |
| 166 | + const language = useLanguage(); |
| 167 | + |
| 168 | + const providerLabel = provider === 'chatgpt' ? 'ChatGPT' : 'Claude'; |
| 169 | + |
| 170 | + return ( |
| 171 | + <AIActionWrapper |
| 172 | + type={type} |
| 173 | + icon={ |
| 174 | + provider === 'chatgpt' ? ( |
| 175 | + <ChatGPTIcon className="size-3.5 fill-current" /> |
| 176 | + ) : ( |
| 177 | + <ClaudeIcon className="size-3.5 fill-current" /> |
| 178 | + ) |
| 179 | + } |
| 180 | + label={tString(language, 'open_in', providerLabel)} |
| 181 | + shortLabel={providerLabel} |
| 182 | + description={tString(language, 'ai_chat_ask_about_page', providerLabel)} |
| 183 | + href={getLLMURL(provider, url, language)} |
| 184 | + /> |
| 185 | + ); |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Wraps an action in a button (for the default action) or dropdown menu item. |
| 190 | + */ |
| 191 | +function AIActionWrapper(props: { |
| 192 | + type: AIActionType; |
| 193 | + icon: IconName | React.ReactNode; |
| 194 | + label: string; |
| 195 | + /** |
| 196 | + * The label to display in the button. If not provided, the `label` will be used. |
| 197 | + */ |
| 198 | + shortLabel?: string; |
| 199 | + onClick?: (e: React.MouseEvent) => void; |
| 200 | + description?: string; |
| 201 | + href?: string; |
| 202 | + disabled?: boolean; |
| 203 | +}) { |
| 204 | + const { type, icon, label, shortLabel, onClick, href, description, disabled } = props; |
| 205 | + |
| 206 | + if (type === 'button') { |
| 207 | + return ( |
| 208 | + <Button |
| 209 | + icon={icon} |
| 210 | + size="small" |
| 211 | + variant="secondary" |
| 212 | + label={shortLabel || label} |
| 213 | + className="hover:!scale-100 !shadow-none !rounded-r-none border-r-0 bg-tint-base text-sm" |
| 214 | + onClick={onClick} |
| 215 | + href={href} |
| 216 | + target={href ? '_blank' : undefined} |
| 217 | + disabled={disabled} |
| 218 | + /> |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + return ( |
| 223 | + <DropdownMenuItem |
| 224 | + className="flex items-stretch gap-2.5 p-2" |
| 225 | + href={href} |
| 226 | + target="_blank" |
| 227 | + onClick={onClick} |
| 228 | + disabled={disabled} |
| 229 | + > |
| 230 | + {icon ? ( |
| 231 | + <div className="flex size-5 items-center justify-center text-tint"> |
| 232 | + {typeof icon === 'string' ? ( |
| 233 | + <Icon |
| 234 | + icon={icon as IconName} |
| 235 | + iconStyle={IconStyle.Regular} |
| 236 | + className="size-4 fill-transparent stroke-current" |
| 237 | + /> |
| 238 | + ) : ( |
| 239 | + icon |
| 240 | + )} |
| 241 | + </div> |
| 242 | + ) : null} |
| 243 | + <div className="flex flex-1 flex-col gap-0.5"> |
| 244 | + <span className="flex items-center gap-2 text-tint-strong"> |
| 245 | + <span className="truncate font-medium text-sm">{label}</span> |
| 246 | + {href ? <Icon icon="arrow-up-right" className="size-3" /> : null} |
| 247 | + </span> |
| 248 | + {description && <span className="truncate text-tint text-xs">{description}</span>} |
| 249 | + </div> |
| 250 | + </DropdownMenuItem> |
| 251 | + ); |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Returns the URL to open the page in a LLM with a pre-filled prompt. |
| 256 | + */ |
| 257 | +function getLLMURL(provider: 'chatgpt' | 'claude', url: string, language: TranslationLanguage) { |
| 258 | + const prompt = encodeURIComponent(tString(language, 'open_in_llms_pre_prompt', url)); |
| 259 | + |
| 260 | + switch (provider) { |
| 261 | + case 'chatgpt': |
| 262 | + return `https://chat.openai.com/?q=${prompt}`; |
| 263 | + case 'claude': |
| 264 | + return `https://claude.ai/new?q=${prompt}`; |
| 265 | + default: |
| 266 | + assertNever(provider); |
| 267 | + } |
| 268 | +} |
0 commit comments