|
| 1 | +'use client'; |
| 2 | +import { useAIChatController, useAIChatState } from '@/components/AI/useAIChat'; |
| 3 | +import AIChatIcon from '@/components/AIChat/AIChatIcon'; |
| 4 | +import { Button } from '@/components/primitives/Button'; |
| 5 | +import { DropdownMenu, DropdownMenuItem } from '@/components/primitives/DropdownMenu'; |
| 6 | +import { tString, useLanguage } from '@/intl/client'; |
| 7 | +import { Icon, type IconName, IconStyle } from '@gitbook/icons'; |
| 8 | +import { useEffect, useRef } from 'react'; |
| 9 | + |
| 10 | +type Action = { |
| 11 | + icon?: IconName | React.ReactNode; |
| 12 | + label: string; |
| 13 | + description?: string; |
| 14 | + /** |
| 15 | + * Whether the action is an external link. |
| 16 | + */ |
| 17 | + isExternal?: boolean; |
| 18 | + onClick?: () => void; |
| 19 | +}; |
| 20 | + |
| 21 | +export function AIActionsDropdown() { |
| 22 | + const chatController = useAIChatController(); |
| 23 | + const chat = useAIChatState(); |
| 24 | + const language = useLanguage(); |
| 25 | + const ref = useRef<HTMLDivElement>(null); |
| 26 | + |
| 27 | + const handleOpenAIAssistant = () => { |
| 28 | + // Open the chat if it's not already open |
| 29 | + if (!chat.opened) { |
| 30 | + chatController.open(); |
| 31 | + } |
| 32 | + |
| 33 | + // Send the "What is this page about?" message |
| 34 | + chatController.postMessage({ |
| 35 | + message: tString(language, 'ai_chat_suggested_questions_about_this_page'), |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + const handleCopyPage = async () => { |
| 40 | + const markdownUrl = `${window.location.href}.md`; |
| 41 | + |
| 42 | + // Get the page content |
| 43 | + const markdown = await fetch(markdownUrl).then((res) => res.text()); |
| 44 | + |
| 45 | + // Copy the markdown to the clipboard |
| 46 | + navigator.clipboard.writeText(markdown); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleViewAsMarkdown = () => { |
| 50 | + // Open the page in Markdown format |
| 51 | + const currentUrl = window.location.href; |
| 52 | + const markdownUrl = `${currentUrl}.md`; |
| 53 | + window.open(markdownUrl, '_blank'); |
| 54 | + }; |
| 55 | + |
| 56 | + const actions: Action[] = [ |
| 57 | + { |
| 58 | + icon: 'copy', |
| 59 | + label: 'Copy page', |
| 60 | + description: 'Copy the page content', |
| 61 | + onClick: handleCopyPage, |
| 62 | + }, |
| 63 | + { |
| 64 | + icon: 'markdown', |
| 65 | + label: 'View as Markdown', |
| 66 | + description: 'Open a Markdown version of this page', |
| 67 | + isExternal: true, |
| 68 | + onClick: handleViewAsMarkdown, |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + // Get the header width with title and check if there is enough space to show the dropdown |
| 73 | + useEffect(() => { |
| 74 | + const getHeaderAvailableSpace = () => { |
| 75 | + const header = document.getElementById('page-header'); |
| 76 | + const headerTitle = header?.getElementsByTagName('h1')[0]; |
| 77 | + |
| 78 | + return ( |
| 79 | + (header?.getBoundingClientRect().width ?? 0) - |
| 80 | + (headerTitle?.getBoundingClientRect().width ?? 0) |
| 81 | + ); |
| 82 | + }; |
| 83 | + |
| 84 | + const dropdownWidth = 202; |
| 85 | + |
| 86 | + window.addEventListener('resize', () => { |
| 87 | + const headerAvailableSpace = getHeaderAvailableSpace(); |
| 88 | + if (ref.current) { |
| 89 | + if (headerAvailableSpace <= dropdownWidth) { |
| 90 | + ref.current.classList.add('-mt-3'); |
| 91 | + ref.current.classList.remove('mt-3'); |
| 92 | + } else { |
| 93 | + ref.current.classList.remove('-mt-3'); |
| 94 | + ref.current.classList.add('mt-3'); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + window.addEventListener('load', () => { |
| 100 | + const headerAvailableSpace = getHeaderAvailableSpace(); |
| 101 | + if (ref.current) { |
| 102 | + if (headerAvailableSpace <= dropdownWidth) { |
| 103 | + ref.current.classList.add('-mt-3'); |
| 104 | + ref.current.classList.remove('mt-3'); |
| 105 | + } else { |
| 106 | + ref.current.classList.remove('-mt-3'); |
| 107 | + ref.current.classList.add('mt-3'); |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + }, []); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div ref={ref} className="hidden items-stretch justify-start md:flex"> |
| 115 | + <Button |
| 116 | + icon={<AIChatIcon className="size-3.5" />} |
| 117 | + size="small" |
| 118 | + variant="secondary" |
| 119 | + label="Ask Docs Assistant" |
| 120 | + className="hover:!scale-100 !shadow-none !rounded-r-none border-r-0 bg-tint-base text-sm" |
| 121 | + onClick={handleOpenAIAssistant} |
| 122 | + /> |
| 123 | + <DropdownMenu |
| 124 | + contentProps={{ |
| 125 | + align: 'end', |
| 126 | + }} |
| 127 | + button={ |
| 128 | + <Button |
| 129 | + icon="chevron-down" |
| 130 | + iconOnly |
| 131 | + size="small" |
| 132 | + variant="secondary" |
| 133 | + className="hover:!scale-100 !shadow-none !rounded-l-none bg-tint-base text-sm" |
| 134 | + /> |
| 135 | + } |
| 136 | + > |
| 137 | + {actions.map((action, index) => ( |
| 138 | + <DropdownMenuItem |
| 139 | + onClick={action.onClick} |
| 140 | + key={index} |
| 141 | + className="flex items-stretch gap-2 p-1.5" |
| 142 | + > |
| 143 | + {action.icon ? ( |
| 144 | + <div className="mt-0.5 flex size-3.5 items-center"> |
| 145 | + {typeof action.icon === 'string' ? ( |
| 146 | + <Icon |
| 147 | + icon={action.icon as IconName} |
| 148 | + iconStyle={IconStyle.Light} |
| 149 | + className="size-full" |
| 150 | + /> |
| 151 | + ) : ( |
| 152 | + action.icon |
| 153 | + )} |
| 154 | + </div> |
| 155 | + ) : null} |
| 156 | + <div className="flex flex-1 flex-col"> |
| 157 | + <span className="flex items-center gap-1.5 text-tint-strong"> |
| 158 | + <span className="truncate font-medium">{action.label}</span> |
| 159 | + {action.isExternal ? ( |
| 160 | + <Icon icon="arrow-up-right" className="size-3" /> |
| 161 | + ) : null} |
| 162 | + </span> |
| 163 | + {action.description && ( |
| 164 | + <span className="truncate text-tint text-xs"> |
| 165 | + {action.description} |
| 166 | + </span> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + </DropdownMenuItem> |
| 170 | + ))} |
| 171 | + </DropdownMenu> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +} |
0 commit comments