|
1 | | -import { FileIcon } from '@primer/octicons-react' |
2 | | -import { Button } from '@primer/react' |
| 1 | +import { useCallback } from 'react' |
| 2 | +import { |
| 3 | + CopyIcon, |
| 4 | + CopilotIcon, |
| 5 | + FileIcon, |
| 6 | + LinkExternalIcon, |
| 7 | + TriangleDownIcon, |
| 8 | +} from '@primer/octicons-react' |
| 9 | +import { ActionList, ActionMenu, Button, ButtonGroup, VisuallyHidden } from '@primer/react' |
| 10 | +import { announce } from '@primer/live-region-element' |
| 11 | +import { MARKDOWN_SOURCE_MENU_EVENT_GROUP } from '@/events/components/event-groups' |
3 | 12 | import { sendEvent } from '@/events/components/events' |
4 | 13 | import { EventType } from '@/events/types' |
5 | 14 | import { useTranslation } from '@/languages/components/useTranslation' |
6 | 15 | import cx from 'classnames' |
7 | 16 | import styles from './ViewMarkdownButton.module.scss' |
8 | 17 |
|
9 | | -interface ViewMarkdownButtonProps { |
| 18 | +interface CopyMarkdownMenuProps { |
10 | 19 | currentPath: string |
11 | 20 | } |
12 | 21 |
|
13 | | -export const ViewMarkdownButton = ({ currentPath }: ViewMarkdownButtonProps) => { |
| 22 | +export const CopyMarkdownMenu = ({ currentPath }: CopyMarkdownMenuProps) => { |
14 | 23 | const { t } = useTranslation('pages') |
15 | 24 |
|
16 | 25 | const encodedPath = encodeURIComponent(currentPath).replace(/%2F/g, '/').replace(/%40/g, '@') |
17 | 26 | const markdownUrl = `/api/article/body?pathname=${encodedPath}` |
18 | 27 |
|
19 | | - const handleClick = () => { |
| 28 | + const docsUrl = `https://docs.github.com${encodedPath}` |
| 29 | + const copilotPrompt = `I need help with this GitHub Docs page: ${docsUrl}.md` |
| 30 | + const copilotUrl = `https://github.com/copilot?prompt=${encodeURIComponent(copilotPrompt)}` |
| 31 | + |
| 32 | + const handleViewClick = useCallback(() => { |
20 | 33 | sendEvent({ |
21 | 34 | type: EventType.link, |
22 | 35 | link_url: `${window.location.origin}${markdownUrl}`, |
23 | 36 | link_samesite: false, |
24 | | - link_container: 'view-markdown-button', |
| 37 | + link_container: 'markdown-source-menu', |
| 38 | + eventGroupKey: MARKDOWN_SOURCE_MENU_EVENT_GROUP, |
| 39 | + }) |
| 40 | + }, [markdownUrl]) |
| 41 | + |
| 42 | + const handleCopilotClick = useCallback(() => { |
| 43 | + sendEvent({ |
| 44 | + type: EventType.link, |
| 45 | + link_url: copilotUrl, |
| 46 | + link_samesite: false, |
| 47 | + link_container: 'markdown-source-menu', |
| 48 | + eventGroupKey: MARKDOWN_SOURCE_MENU_EVENT_GROUP, |
25 | 49 | }) |
26 | | - window.open(markdownUrl, '_blank') |
27 | | - } |
| 50 | + }, [copilotUrl]) |
| 51 | + |
| 52 | + const handleCopyClick = useCallback(async () => { |
| 53 | + sendEvent({ |
| 54 | + type: EventType.clipboard, |
| 55 | + clipboard_operation: 'copy', |
| 56 | + clipboard_target: markdownUrl, |
| 57 | + eventGroupKey: MARKDOWN_SOURCE_MENU_EVENT_GROUP, |
| 58 | + }) |
| 59 | + try { |
| 60 | + const res = await fetch(markdownUrl) |
| 61 | + if (!res.ok) { |
| 62 | + throw new Error(`Failed to fetch: ${res.status}`) |
| 63 | + } |
| 64 | + const text = await res.text() |
| 65 | + await navigator.clipboard.writeText(text) |
| 66 | + announce(t('copied')) |
| 67 | + } catch { |
| 68 | + // Fallback: open in new tab if fetch or clipboard fails |
| 69 | + window.open(markdownUrl, '_blank') |
| 70 | + } |
| 71 | + }, [markdownUrl, t]) |
28 | 72 |
|
29 | 73 | return ( |
30 | 74 | <div className="mb-3 ml-3"> |
31 | | - <Button |
32 | | - onClick={handleClick} |
33 | | - variant="default" |
34 | | - className={cx( |
35 | | - 'd-inline-flex flex-items-center border text-decoration-none color-fg-default', |
36 | | - styles.button, |
37 | | - )} |
38 | | - aria-label={t('view_page_as_markdown')} |
39 | | - > |
40 | | - <FileIcon size={12} className="mr-1" aria-hidden="true" /> |
41 | | - {t('view_page_as_markdown')} |
42 | | - </Button> |
| 75 | + <ButtonGroup> |
| 76 | + <Button |
| 77 | + variant="default" |
| 78 | + className={cx( |
| 79 | + 'd-inline-flex flex-items-center text-decoration-none color-fg-default', |
| 80 | + styles.button, |
| 81 | + )} |
| 82 | + onClick={handleCopyClick} |
| 83 | + > |
| 84 | + <CopyIcon size={12} className="mr-1" aria-hidden="true" /> |
| 85 | + {t('copy_as_markdown')} |
| 86 | + </Button> |
| 87 | + <ActionMenu> |
| 88 | + <ActionMenu.Button |
| 89 | + aria-label={t('more_markdown_options')} |
| 90 | + icon={TriangleDownIcon} |
| 91 | + className={styles.button} |
| 92 | + /> |
| 93 | + <ActionMenu.Overlay align="start"> |
| 94 | + <ActionList> |
| 95 | + <ActionList.Item onSelect={handleCopyClick}> |
| 96 | + <ActionList.LeadingVisual> |
| 97 | + <CopyIcon size={16} /> |
| 98 | + </ActionList.LeadingVisual> |
| 99 | + {t('copy_as_markdown')} |
| 100 | + <ActionList.Description variant="block"> |
| 101 | + {t('copy_as_markdown_desc')} |
| 102 | + </ActionList.Description> |
| 103 | + </ActionList.Item> |
| 104 | + <ActionList.LinkItem |
| 105 | + href={markdownUrl} |
| 106 | + target="_blank" |
| 107 | + rel="noopener noreferrer" |
| 108 | + onClick={handleViewClick} |
| 109 | + > |
| 110 | + <ActionList.LeadingVisual> |
| 111 | + <FileIcon size={16} /> |
| 112 | + </ActionList.LeadingVisual> |
| 113 | + {t('view_as_markdown')} |
| 114 | + <VisuallyHidden>{t('opens_in_new_tab')}</VisuallyHidden> |
| 115 | + <ActionList.Description variant="block"> |
| 116 | + {t('view_as_markdown_desc')} |
| 117 | + </ActionList.Description> |
| 118 | + <ActionList.TrailingVisual> |
| 119 | + <LinkExternalIcon size={16} aria-hidden="true" /> |
| 120 | + </ActionList.TrailingVisual> |
| 121 | + </ActionList.LinkItem> |
| 122 | + <ActionList.LinkItem |
| 123 | + href={copilotUrl} |
| 124 | + target="_blank" |
| 125 | + rel="noopener noreferrer" |
| 126 | + onClick={handleCopilotClick} |
| 127 | + > |
| 128 | + <ActionList.LeadingVisual> |
| 129 | + <CopilotIcon size={16} /> |
| 130 | + </ActionList.LeadingVisual> |
| 131 | + {t('ask_copilot')} |
| 132 | + <VisuallyHidden>{t('opens_in_new_tab')}</VisuallyHidden> |
| 133 | + <ActionList.Description variant="block"> |
| 134 | + {t('ask_copilot_desc')} |
| 135 | + </ActionList.Description> |
| 136 | + <ActionList.TrailingVisual> |
| 137 | + <LinkExternalIcon size={16} aria-hidden="true" /> |
| 138 | + </ActionList.TrailingVisual> |
| 139 | + </ActionList.LinkItem> |
| 140 | + </ActionList> |
| 141 | + </ActionMenu.Overlay> |
| 142 | + </ActionMenu> |
| 143 | + </ButtonGroup> |
43 | 144 | </div> |
44 | 145 | ) |
45 | 146 | } |
| 147 | + |
| 148 | +/** @deprecated Use CopyMarkdownMenu instead */ |
| 149 | +export const ViewMarkdownButton = CopyMarkdownMenu |
0 commit comments