forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionPicker.tsx
126 lines (120 loc) · 4.53 KB
/
VersionPicker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { useRouter } from 'next/router'
import cx from 'classnames'
import { Dropdown, Heading, Details, Box, Text, useDetails } from '@primer/components'
import { ArrowRightIcon, ChevronDownIcon } from '@primer/octicons-react'
import { Link } from 'components/Link'
import { useMainContext } from 'components/context/MainContext'
import { useVersion } from 'components/hooks/useVersion'
import { useTranslation } from 'components/hooks/useTranslation'
type Props = {
hideLabel?: boolean
variant?: 'default' | 'compact' | 'inline'
popoverVariant?: 'inline' | 'dropdown'
}
export const VersionPicker = ({ variant = 'default', popoverVariant, hideLabel }: Props) => {
const router = useRouter()
const { currentVersion } = useVersion()
const { allVersions, page, enterpriseServerVersions } = useMainContext()
const { getDetailsProps, setOpen } = useDetails({ closeOnOutsideClick: true })
const { t } = useTranslation('pages')
if (page.permalinks && page.permalinks.length <= 1) {
return null
}
return (
<>
{!hideLabel && (
<Heading as="span" fontSize={1} className="d-none d-xl-inline-block mb-1">
{t('article_version')}
</Heading>
)}
<div>
<Details
{...getDetailsProps()}
className={cx(
'position-relative details-reset',
variant === 'inline' ? 'd-block' : 'd-inline-block'
)}
data-testid="article-version-picker"
>
{(variant === 'compact' || variant === 'inline') && (
<summary
className="d-block btn btn-invisible color-text-primary"
aria-haspopup="true"
aria-label="Toggle version list"
>
{variant === 'inline' ? (
<div className="d-flex flex-items-center flex-justify-between">
<Text>{allVersions[currentVersion].versionTitle}</Text>
<ChevronDownIcon size={24} className="arrow ml-md-1" />
</div>
) : (
<>
<Text>{allVersions[currentVersion].versionTitle}</Text>
<Dropdown.Caret />
</>
)}
</summary>
)}
{variant === 'default' && (
<summary aria-haspopup="true" className="btn btn-sm">
<Text>{allVersions[currentVersion].versionTitle}</Text>
<Dropdown.Caret />
</summary>
)}
{popoverVariant === 'inline' ? (
<Box py="2">
{(page.permalinks || []).map((permalink) => {
return (
<Dropdown.Item key={permalink.href} onClick={() => setOpen(false)}>
<Link href={permalink.href}>{permalink.pageVersionTitle}</Link>
</Dropdown.Item>
)
})}
<Box mt={1}>
<Link
onClick={() => {
setOpen(false)
}}
href={`/${router.locale}/${enterpriseServerVersions[0]}/admin/all-releases`}
className="f6 no-underline color-text-tertiary pl-3 pr-2 no-wrap"
>
{t('all_enterprise_releases')}{' '}
<ArrowRightIcon verticalAlign="middle" size={15} className="mr-2" />
</Link>
</Box>
</Box>
) : (
<Dropdown.Menu direction="sw" style={{ width: 'unset' }}>
{(page.permalinks || []).map((permalink) => {
return (
<Dropdown.Item key={permalink.href} onClick={() => setOpen(false)}>
<Link href={permalink.href}>{permalink.pageVersionTitle}</Link>
</Dropdown.Item>
)
})}
<Box
borderColor="border.default"
borderTopWidth={1}
borderTopStyle="solid"
mt={2}
pt={2}
pb={1}
>
<Link
onClick={() => {
setOpen(false)
}}
href={`/${router.locale}/${enterpriseServerVersions[0]}/admin/all-releases`}
className="f6 no-underline color-text-tertiary pl-3 pr-2 no-wrap"
>
{t('all_enterprise_releases')}{' '}
<ArrowRightIcon verticalAlign="middle" size={15} className="mr-2" />
</Link>
</Box>
</Dropdown.Menu>
)}
</Details>
</div>
</>
)
}