forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformPicker.tsx
74 lines (64 loc) · 2.4 KB
/
PlatformPicker.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
import { useEffect, useState } from 'react'
import { useArticleContext } from 'components/context/ArticleContext'
import { parseUserAgent } from 'components/lib/user-agent'
import { InArticlePicker } from './InArticlePicker'
const platformQueryKey = 'platform'
const platforms = [
{ value: 'mac', label: 'Mac' },
{ value: 'windows', label: 'Windows' },
{ value: 'linux', label: 'Linux' },
]
// Nota bene: platform === os
// Imperatively modify article content to show only the selected platform
// find all platform-specific *block* elements and hide or show as appropriate
// example: {% mac %} block content {% endmac %}
function showPlatformSpecificContent(platform: string) {
const markdowns = Array.from(document.querySelectorAll<HTMLElement>('.extended-markdown'))
markdowns
.filter((el) => platforms.some((platform) => el.classList.contains(platform.value)))
.forEach((el) => {
el.style.display = el.classList.contains(platform) ? '' : 'none'
})
// find all platform-specific *inline* elements and hide or show as appropriate
// example: <span class="platform-mac">inline content</span>
const platformEls = Array.from(
document.querySelectorAll<HTMLElement>(
platforms.map((platform) => `.platform-${platform.value}`).join(', ')
)
)
platformEls.forEach((el) => {
el.style.display = el.classList.contains(`platform-${platform}`) ? '' : 'none'
})
}
export const PlatformPicker = () => {
const { defaultPlatform, detectedPlatforms } = useArticleContext()
const [defaultUA, setDefaultUA] = useState('')
useEffect(() => {
let userAgent = parseUserAgent().os
if (userAgent === 'ios') {
userAgent = 'mac'
}
setDefaultUA(userAgent)
}, [])
// Defensively, just in case some article happens to have an array
// but for some reasons, it might be empty, let's not have a picker
// at all.
if (!detectedPlatforms.length) return null
const options = platforms.filter((platform) => detectedPlatforms.includes(platform.value))
return (
<InArticlePicker
defaultValue={defaultPlatform}
fallbackValue={
detectedPlatforms.includes(defaultUA)
? defaultUA
: detectedPlatforms[detectedPlatforms.length - 1]
}
cookieKey="osPreferred"
queryStringKey={platformQueryKey}
onValue={showPlatformSpecificContent}
preferenceName="os"
ariaLabel="Platform"
options={options}
/>
)
}