-
Notifications
You must be signed in to change notification settings - Fork 68.3k
Expand file tree
/
Copy pathPlatformPicker.tsx
More file actions
59 lines (51 loc) · 1.85 KB
/
Copy pathPlatformPicker.tsx
File metadata and controls
59 lines (51 loc) · 1.85 KB
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
import { useEffect, useState } from 'react'
import { useArticleContext } from '@/frame/components/context/ArticleContext'
import { parseUserAgent } from '@/events/components/user-agent'
import { InArticlePicker } from './InArticlePicker'
import { useSelection } from './SelectionContext'
import { OS_PREFERRED_COOKIE_NAME } from '@/frame/lib/constants'
const platformQueryKey = 'platform'
const platforms = [
{ value: 'mac', label: 'Mac' },
{ value: 'windows', label: 'Windows' },
{ value: 'linux', label: 'Linux' },
]
// Nota bene: platform === os
export const PlatformPicker = () => {
const { defaultPlatform, detectedPlatforms } = useArticleContext()
const { setPlatform } = useSelection()
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={OS_PREFERRED_COOKIE_NAME}
queryStringKey={platformQueryKey}
onValue={(value: string) => {
// Visibility is driven by React state via ToggleableContent/MiniTocs
// (#6619); the article body is React-owned on both the hast and string
// paths, so no imperative DOM mutation is needed.
setPlatform(value)
}}
preferenceName="os"
ariaLabel="Platform"
options={options}
/>
)
}