Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | 'use client'; |
| 2 | |
| 3 | import { useCallback, useEffect, useRef, useState } from 'react'; |
| 4 | import { Check, ChevronDown, Copy, ExternalLink, FileText } from 'lucide-react'; |
| 5 | |
| 6 | interface PageActionsProps { |
| 7 | path: string; |
| 8 | /** |
| 9 | * 'overlap-title' (default) places the button below the title on mobile, |
| 10 | * then lifts it to the right of `<DocsTitle>` on medium screens and up. |
| 11 | * 'inline' renders the button as a normal inline-block — use when the |
| 12 | * caller already controls layout (e.g. the Toolkits landing header). |
| 13 | */ |
| 14 | variant?: 'overlap-title' | 'inline'; |
| 15 | } |
| 16 | |
| 17 | const FALLBACK_ORIGIN = 'https://docs.composio.dev'; |
| 18 | |
| 19 | /** |
| 20 | * Single "Copy page" dropdown with three actions: |
| 21 | * - Copy page (fetches the .md and writes to clipboard) |
| 22 | * - Open in ChatGPT (deep link with a prefilled prompt) |
| 23 | * - Open in Claude (deep link with a prefilled prompt) |
| 24 | * |
| 25 | * Keeps its own row on narrow viewports so it cannot overlap the title text. |
| 26 | * On medium screens and up, a negative margin lifts it beside the title. |
| 27 | */ |
| 28 | export function PageActions({ path, variant = 'overlap-title' }: PageActionsProps) { |
| 29 | const [open, setOpen] = useState(false); |
| 30 | const [copied, setCopied] = useState(false); |
| 31 | const wrapperRef = useRef<HTMLDivElement>(null); |
| 32 | |
| 33 | // Close on outside click and Escape. |
| 34 | useEffect(() => { |
| 35 | if (!open) return; |
| 36 | function onClick(e: MouseEvent) { |
| 37 | if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) { |
| 38 | setOpen(false); |
| 39 | } |
| 40 | } |
| 41 | function onKey(e: KeyboardEvent) { |
| 42 | if (e.key === 'Escape') setOpen(false); |
| 43 | } |
| 44 | document.addEventListener('mousedown', onClick); |
| 45 | document.addEventListener('keydown', onKey); |
| 46 | return () => { |
| 47 | document.removeEventListener('mousedown', onClick); |
| 48 | document.removeEventListener('keydown', onKey); |
| 49 | }; |
| 50 | }, [open]); |
| 51 | |
| 52 | const absoluteMdUrl = |
| 53 | typeof window !== 'undefined' |
| 54 | ? `${window.location.origin}${path}.md` |
| 55 | : `${FALLBACK_ORIGIN}${path}.md`; |
| 56 | |
| 57 | const chatgptUrl = `https://chatgpt.com/?hints=search&q=${encodeURIComponent( |
| 58 | `Read ${absoluteMdUrl} so I can ask questions about it.`, |
| 59 | )}`; |
| 60 | const claudeUrl = `https://claude.ai/new?q=${encodeURIComponent( |
| 61 | `Read ${absoluteMdUrl} so I can ask questions about it.`, |
| 62 | )}`; |
| 63 | |
| 64 | const handleCopy = useCallback(async () => { |
| 65 | try { |
| 66 | const response = await fetch(`${path}.md`); |
| 67 | if (!response.ok) throw new Error('Failed to fetch'); |
| 68 | const markdown = await response.text(); |
| 69 | await navigator.clipboard.writeText(markdown); |
| 70 | setCopied(true); |
| 71 | setTimeout(() => setCopied(false), 1500); |
| 72 | setTimeout(() => setOpen(false), 400); |
| 73 | } catch { |
| 74 | // best-effort; leave menu open so user can retry |
| 75 | } |
| 76 | }, [path]); |
| 77 | |
| 78 | return ( |
| 79 | <div className={variant === 'overlap-title' ? 'not-prose md:-mt-12 mb-2 flex justify-end pointer-events-none' : 'not-prose inline-block'}> |
| 80 | <div ref={wrapperRef} className="relative pointer-events-auto"> |
| 81 | <button |
| 82 | type="button" |
| 83 | onClick={() => setOpen((v) => !v)} |
| 84 | aria-haspopup="menu" |
| 85 | aria-expanded={open} |
| 86 | className="inline-flex items-center gap-1.5 px-2.5 py-1.5 text-xs font-medium |
| 87 | text-fd-muted-foreground hover:text-fd-foreground |
| 88 | bg-fd-secondary/50 hover:bg-fd-secondary |
| 89 | border border-fd-border/60 |
| 90 | transition-colors duration-150 ease-out |
| 91 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring focus-visible:ring-offset-2 focus-visible:ring-offset-fd-background |
| 92 | touch-manipulation" |
| 93 | style={{ borderRadius: 0 }} |
| 94 | > |
| 95 | <Copy className="size-3.5" aria-hidden="true" /> |
| 96 | <span>Copy page</span> |
| 97 | <ChevronDown |
| 98 | className={`size-3.5 transition-transform duration-150 ${open ? 'rotate-180' : ''}`} |
| 99 | aria-hidden="true" |
| 100 | /> |
| 101 | </button> |
| 102 | |
| 103 | {open && ( |
| 104 | <div |
| 105 | role="menu" |
| 106 | className="absolute right-0 z-50 mt-1 w-[280px] border border-fd-border bg-fd-popover text-fd-popover-foreground shadow-lg" |
| 107 | style={{ borderRadius: 0 }} |
| 108 | > |
| 109 | <MenuItem |
| 110 | icon={ |
| 111 | copied ? ( |
| 112 | <Check className="size-4 text-emerald-500" aria-hidden="true" /> |
| 113 | ) : ( |
| 114 | <Copy className="size-4" aria-hidden="true" /> |
| 115 | ) |
| 116 | } |
| 117 | title={copied ? 'Copied!' : 'Copy page'} |
| 118 | subtitle="Copy page as Markdown for LLMs" |
| 119 | onClick={handleCopy} |
| 120 | /> |
| 121 | <MenuItem |
| 122 | icon={<FileText className="size-4" aria-hidden="true" />} |
| 123 | external |
| 124 | title="View as Markdown" |
| 125 | subtitle="Open the raw .md source in a new tab" |
| 126 | href={`${path}.md`} |
| 127 | /> |
| 128 | <MenuItem |
| 129 | icon={<ChatGPTMark className="size-4" />} |
| 130 | external |
| 131 | title="Open in ChatGPT" |
| 132 | subtitle="Ask questions about this page" |
| 133 | href={chatgptUrl} |
| 134 | /> |
| 135 | <MenuItem |
| 136 | icon={<ClaudeMark className="size-4" />} |
| 137 | external |
| 138 | title="Open in Claude" |
| 139 | subtitle="Ask questions about this page" |
| 140 | href={claudeUrl} |
| 141 | /> |
| 142 | </div> |
| 143 | )} |
| 144 | </div> |
| 145 | </div> |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | interface MenuItemProps { |
| 150 | icon: React.ReactNode; |
| 151 | title: string; |
| 152 | subtitle: string; |
| 153 | href?: string; |
| 154 | external?: boolean; |
| 155 | onClick?: () => void; |
| 156 | } |
| 157 | |
| 158 | function MenuItem({ icon, title, subtitle, href, external, onClick }: MenuItemProps) { |
| 159 | const className = |
| 160 | 'flex w-full items-start gap-3 px-3 py-2.5 text-left hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:bg-fd-accent'; |
| 161 | |
| 162 | const content = ( |
| 163 | <> |
| 164 | <span className="mt-0.5 flex size-4 shrink-0 items-center justify-center text-fd-muted-foreground"> |
| 165 | {icon} |
| 166 | </span> |
| 167 | <span className="min-w-0 flex-1"> |
| 168 | <span className="flex items-center gap-1 text-sm font-medium text-fd-foreground"> |
| 169 | {title} |
| 170 | {external && ( |
| 171 | <ExternalLink className="size-3 text-fd-muted-foreground" aria-hidden="true" /> |
| 172 | )} |
| 173 | </span> |
| 174 | <span className="block text-xs text-fd-muted-foreground">{subtitle}</span> |
| 175 | </span> |
| 176 | </> |
| 177 | ); |
| 178 | |
| 179 | if (href) { |
| 180 | return ( |
| 181 | <a |
| 182 | href={href} |
| 183 | target="_blank" |
| 184 | rel="noopener noreferrer" |
| 185 | role="menuitem" |
| 186 | className={className} |
| 187 | > |
| 188 | {content} |
| 189 | </a> |
| 190 | ); |
| 191 | } |
| 192 | return ( |
| 193 | <button type="button" role="menuitem" onClick={onClick} className={className}> |
| 194 | {content} |
| 195 | </button> |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** Tiny inline OpenAI/ChatGPT mark (flower-gear), monochrome currentColor. */ |
| 200 | function ChatGPTMark({ className }: { className?: string }) { |
| 201 | return ( |
| 202 | <svg |
| 203 | className={className} |
| 204 | viewBox="0 0 24 24" |
| 205 | fill="currentColor" |
| 206 | aria-hidden="true" |
| 207 | xmlns="http://www.w3.org/2000/svg" |
| 208 | > |
| 209 | <path d="M22.282 9.821a5.985 5.985 0 0 0-.516-4.91 6.046 6.046 0 0 0-6.51-2.9A6.065 6.065 0 0 0 4.981 4.18a5.985 5.985 0 0 0-3.998 2.9 6.046 6.046 0 0 0 .743 7.097 5.98 5.98 0 0 0 .51 4.911 6.051 6.051 0 0 0 6.515 2.9A5.985 5.985 0 0 0 13.26 24a6.056 6.056 0 0 0 5.772-4.206 5.99 5.99 0 0 0 3.997-2.9 6.056 6.056 0 0 0-.747-7.073zM13.26 22.43a4.476 4.476 0 0 1-2.876-1.04l.141-.081 4.779-2.758a.795.795 0 0 0 .392-.681v-6.737l2.02 1.168a.071.071 0 0 1 .038.052v5.583a4.504 4.504 0 0 1-4.494 4.494zM3.6 18.304a4.47 4.47 0 0 1-.535-3.014l.142.085 4.783 2.759a.771.771 0 0 0 .78 0l5.843-3.369v2.332a.08.08 0 0 1-.033.062L9.74 19.95a4.5 4.5 0 0 1-6.14-1.646zM2.34 7.896a4.485 4.485 0 0 1 2.366-1.973V11.6a.766.766 0 0 0 .388.676l5.815 3.355-2.02 1.168a.077.077 0 0 1-.071 0l-4.83-2.786A4.504 4.504 0 0 1 2.34 7.872zm16.597 3.855-5.833-3.387L15.119 7.2a.076.076 0 0 1 .071 0l4.83 2.791a4.494 4.494 0 0 1-.676 8.105v-5.678a.79.79 0 0 0-.407-.667zm2.01-3.023-.141-.085-4.774-2.782a.776.776 0 0 0-.785 0L9.409 9.23V6.897a.066.066 0 0 1 .028-.061l4.83-2.787a4.5 4.5 0 0 1 6.68 4.66zm-12.64 4.135-2.02-1.164a.08.08 0 0 1-.038-.057V6.075a4.5 4.5 0 0 1 7.375-3.453l-.142.08-4.778 2.758a.795.795 0 0 0-.393.681zm1.097-2.365 2.602-1.5 2.607 1.5v2.999l-2.597 1.5-2.607-1.5z" /> |
| 210 | </svg> |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | function ClaudeMark({ className }: { className?: string }) { |
| 216 | return ( |
| 217 | <svg |
| 218 | className={className} |
| 219 | preserveAspectRatio="xMidYMid" |
| 220 | viewBox="0 0 256 257" |
| 221 | aria-hidden="true" |
| 222 | xmlns="http://www.w3.org/2000/svg" |
| 223 | > |
| 224 | <path |
| 225 | fill="#D97757" |
| 226 | d="m50.228 170.321 50.357-28.257.843-2.463-.843-1.361h-2.462l-8.426-.518-28.775-.778-24.952-1.037-24.175-1.296-6.092-1.297L0 125.796l.583-3.759 5.12-3.434 7.324.648 16.202 1.101 24.304 1.685 17.629 1.037 26.118 2.722h4.148l.583-1.685-1.426-1.037-1.101-1.037-25.147-17.045-27.22-18.017-14.258-10.37-7.713-5.25-3.888-4.925-1.685-10.758 7-7.713 9.397.649 2.398.648 9.527 7.323 20.35 15.75L94.817 91.9l3.889 3.24 1.555-1.102.195-.777-1.75-2.917-14.453-26.118-15.425-26.572-6.87-11.018-1.814-6.61c-.648-2.723-1.102-4.991-1.102-7.778l7.972-10.823L71.42 0 82.05 1.426l4.472 3.888 6.61 15.101 10.694 23.786 16.591 32.34 4.861 9.592 2.592 8.879.973 2.722h1.685v-1.556l1.36-18.211 2.528-22.36 2.463-28.776.843-8.1 4.018-9.722 7.971-5.25 6.222 2.981 5.12 7.324-.713 4.73-3.046 19.768-5.962 30.98-3.889 20.739h2.268l2.593-2.593 10.499-13.934 17.628-22.036 7.778-8.749 9.073-9.657 5.833-4.601h11.018l8.1 12.055-3.628 12.443-11.342 14.388-9.398 12.184-13.48 18.147-8.426 14.518.778 1.166 2.01-.194 30.46-6.481 16.462-2.982 19.637-3.37 8.88 4.148.971 4.213-3.5 8.62-20.998 5.184-24.628 4.926-36.682 8.685-.454.324.519.648 16.526 1.555 7.065.389h17.304l32.21 2.398 8.426 5.574 5.055 6.805-.843 5.184-12.962 6.611-17.498-4.148-40.83-9.721-14-3.5h-1.944v1.167l11.666 11.406 21.387 19.314 26.767 24.887 1.36 6.157-3.434 4.86-3.63-.518-23.526-17.693-9.073-7.972-20.545-17.304h-1.36v1.814l4.73 6.935 25.017 37.59 1.296 11.536-1.814 3.76-6.481 2.268-7.13-1.297-14.647-20.544-15.1-23.138-12.185-20.739-1.49.843-7.194 77.448-3.37 3.953-7.778 2.981-6.48-4.925-3.436-7.972 3.435-15.749 4.148-20.544 3.37-16.333 3.046-20.285 1.815-6.74-.13-.454-1.49.194-15.295 20.999-23.267 31.433-18.406 19.702-4.407 1.75-7.648-3.954.713-7.064 4.277-6.286 25.47-32.405 15.36-20.092 9.917-11.6-.065-1.686h-.583L44.07 198.125l-12.055 1.555-5.185-4.86.648-7.972 2.463-2.593 20.35-13.999-.064.065Z" |
| 227 | /> |
| 228 | </svg> |
| 229 | ); |
| 230 | } |