|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + Button, |
| 5 | + ButtonGroup, |
| 6 | + DropdownMenu, |
| 7 | + DropdownMenuItem, |
| 8 | + ToggleChevron, |
| 9 | +} from '@/components/primitives'; |
| 10 | +import { tString, useLanguage } from '@/intl/client'; |
| 11 | +import { tcls } from '@/lib/tailwind'; |
| 12 | +import { Icon, type IconName } from '@gitbook/icons'; |
| 13 | +import React from 'react'; |
| 14 | + |
| 15 | +const OPEN_IN_AI_PROVIDERS = ['claude', 'chatgpt', 'cursor'] as const; |
| 16 | +type AIProviders = (typeof OPEN_IN_AI_PROVIDERS)[number]; |
| 17 | + |
| 18 | +export function PromptClient(props: { |
| 19 | + contentIcon: IconName | null; |
| 20 | + description: string; |
| 21 | + prompt: string; |
| 22 | + openInAIProviders: boolean; |
| 23 | +}) { |
| 24 | + const { contentIcon, description, prompt, openInAIProviders } = props; |
| 25 | + const language = useLanguage(); |
| 26 | + const promptId = React.useId(); |
| 27 | + const [open, setOpen] = React.useState(false); |
| 28 | + const [headerHasFocus, setHeaderHasFocus] = React.useState(false); |
| 29 | + return ( |
| 30 | + <> |
| 31 | + <div className="group/prompt-header relative flex min-h-9 flex-row items-center justify-between gap-4 px-3 py-2"> |
| 32 | + <button |
| 33 | + type="button" |
| 34 | + aria-controls={promptId} |
| 35 | + aria-expanded={open} |
| 36 | + aria-label={tString(language, 'view')} |
| 37 | + className={tcls( |
| 38 | + 'absolute inset-0 z-10 cursor-pointer outline-hidden', |
| 39 | + 'focus-visible:ring-2 focus-visible:ring-primary-hover' |
| 40 | + )} |
| 41 | + disabled={!prompt} |
| 42 | + onBlur={() => setHeaderHasFocus(false)} |
| 43 | + onClick={() => setOpen((prev) => !prev)} |
| 44 | + onFocus={() => setHeaderHasFocus(true)} |
| 45 | + /> |
| 46 | + <div className="pointer-events-none relative z-0 flex min-w-0 flex-row items-center gap-2 text-tint-strong"> |
| 47 | + <PromptDisclosureIcon |
| 48 | + contentIcon={contentIcon} |
| 49 | + headerHasFocus={headerHasFocus} |
| 50 | + open={open} |
| 51 | + /> |
| 52 | + <span className="min-w-0 truncate">{description}</span> |
| 53 | + </div> |
| 54 | + <PromptActions prompt={prompt} openInAIProviders={openInAIProviders} /> |
| 55 | + </div> |
| 56 | + {open ? ( |
| 57 | + <div id={promptId} className="border-tint-subtle border-t bg-tint-base"> |
| 58 | + <pre className="overflow-auto p-4 text-sm text-tint-strong"> |
| 59 | + <code className="language-markdown whitespace-pre-wrap font-mono"> |
| 60 | + {prompt} |
| 61 | + </code> |
| 62 | + </pre> |
| 63 | + </div> |
| 64 | + ) : null} |
| 65 | + </> |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +function PromptDisclosureIcon(props: { |
| 70 | + contentIcon: IconName | null; |
| 71 | + headerHasFocus: boolean; |
| 72 | + open: boolean; |
| 73 | +}) { |
| 74 | + const { contentIcon, headerHasFocus, open } = props; |
| 75 | + return ( |
| 76 | + <span className="relative flex size-4 shrink-0 items-center justify-center"> |
| 77 | + {contentIcon ? ( |
| 78 | + <> |
| 79 | + <span |
| 80 | + className={tcls( |
| 81 | + 'flex items-center transition-opacity duration-150 group-hover/prompt-header:opacity-0', |
| 82 | + headerHasFocus && 'opacity-0' |
| 83 | + )} |
| 84 | + > |
| 85 | + <Icon icon={contentIcon} className="size-4 shrink-0" /> |
| 86 | + </span> |
| 87 | + <span |
| 88 | + className={tcls( |
| 89 | + 'absolute inset-0 flex items-center justify-center text-tint-subtle opacity-0 transition-opacity duration-150 group-hover/prompt-header:opacity-100', |
| 90 | + headerHasFocus && 'opacity-100' |
| 91 | + )} |
| 92 | + > |
| 93 | + <ToggleChevron open={open} orientation="right-to-down" className="size-3" /> |
| 94 | + </span> |
| 95 | + </> |
| 96 | + ) : ( |
| 97 | + <ToggleChevron |
| 98 | + open={open} |
| 99 | + orientation="right-to-down" |
| 100 | + className="size-3 text-tint-subtle" |
| 101 | + /> |
| 102 | + )} |
| 103 | + </span> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function PromptActions(props: { prompt: string; openInAIProviders: boolean }) { |
| 108 | + const { prompt, openInAIProviders } = props; |
| 109 | + |
| 110 | + return ( |
| 111 | + <ButtonGroup className="relative z-20 shrink-0 overflow-visible"> |
| 112 | + <CopyPromptButton prompt={prompt} /> |
| 113 | + {openInAIProviders ? <OpenPromptDropdown prompt={prompt} /> : null} |
| 114 | + </ButtonGroup> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +// time in milliseconds to show the "Copied" message after copying a prompt |
| 119 | +const COPIED_MESSAGE_DURATION = 1000; |
| 120 | + |
| 121 | +function CopyPromptButton(props: { prompt: string }) { |
| 122 | + const { prompt } = props; |
| 123 | + const language = useLanguage(); |
| 124 | + const [copied, setCopied] = React.useState(false); |
| 125 | + |
| 126 | + React.useEffect(() => { |
| 127 | + if (!copied) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + const timeout = setTimeout(() => { |
| 132 | + setCopied(false); |
| 133 | + }, COPIED_MESSAGE_DURATION); |
| 134 | + |
| 135 | + return () => { |
| 136 | + clearTimeout(timeout); |
| 137 | + }; |
| 138 | + }, [copied]); |
| 139 | + |
| 140 | + return ( |
| 141 | + <Button |
| 142 | + variant="secondary" |
| 143 | + size="xsmall" |
| 144 | + icon={copied ? 'check' : 'copy'} |
| 145 | + label={copied ? tString(language, 'code_copied') : tString(language, 'prompt_copy')} |
| 146 | + className="bg-tint-base" |
| 147 | + disabled={!prompt} |
| 148 | + onClick={() => { |
| 149 | + navigator.clipboard.writeText(prompt); |
| 150 | + setCopied(true); |
| 151 | + }} |
| 152 | + /> |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +function OpenPromptDropdown(props: { prompt: string }) { |
| 157 | + const { prompt } = props; |
| 158 | + const language = useLanguage(); |
| 159 | + |
| 160 | + return ( |
| 161 | + <DropdownMenu |
| 162 | + align="end" |
| 163 | + className="!min-w-48 max-w-max" |
| 164 | + button={ |
| 165 | + <Button |
| 166 | + icon={<ToggleChevron className="size-text-sm" />} |
| 167 | + label={tString(language, 'open')} |
| 168 | + iconOnly |
| 169 | + size="xsmall" |
| 170 | + variant="secondary" |
| 171 | + className="bg-tint-base" |
| 172 | + disabled={!prompt} |
| 173 | + /> |
| 174 | + } |
| 175 | + > |
| 176 | + {OPEN_IN_AI_PROVIDERS.map((provider) => { |
| 177 | + const definition = getPromptOpenActionDefinition(provider, prompt); |
| 178 | + |
| 179 | + return ( |
| 180 | + <DropdownMenuItem |
| 181 | + key={provider} |
| 182 | + href={definition.href} |
| 183 | + target="_blank" |
| 184 | + leadingIcon={definition.icon} |
| 185 | + > |
| 186 | + {tString(language, 'open_in', definition.label)} |
| 187 | + </DropdownMenuItem> |
| 188 | + ); |
| 189 | + })} |
| 190 | + </DropdownMenu> |
| 191 | + ); |
| 192 | +} |
| 193 | + |
| 194 | +function getPromptOpenActionDefinition( |
| 195 | + action: AIProviders, |
| 196 | + prompt: string |
| 197 | +): { href: string; icon: IconName; label: string } { |
| 198 | + const encodedPrompt = encodeURIComponent(prompt); |
| 199 | + |
| 200 | + switch (action) { |
| 201 | + case 'cursor': |
| 202 | + return { |
| 203 | + href: `${CURSOR_PROMPT_URL}?text=${encodedPrompt}`, |
| 204 | + icon: 'cursor', |
| 205 | + label: 'Cursor', |
| 206 | + }; |
| 207 | + case 'claude': |
| 208 | + return { |
| 209 | + href: `${CLAUDE_PROMPT_URL}?q=${encodedPrompt}`, |
| 210 | + icon: 'claude', |
| 211 | + label: 'Claude', |
| 212 | + }; |
| 213 | + case 'chatgpt': |
| 214 | + return { |
| 215 | + href: `${CHATGPT_PROMPT_URL}?q=${encodedPrompt}`, |
| 216 | + icon: 'chatgpt', |
| 217 | + label: 'ChatGPT', |
| 218 | + }; |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +const CLAUDE_PROMPT_URL = 'https://claude.ai/new'; |
| 223 | +const CHATGPT_PROMPT_URL = 'https://chat.openai.com/'; |
| 224 | +const CURSOR_PROMPT_URL = 'https://cursor.com/link/prompt'; |
0 commit comments