| | import { useEffect, useState } from 'react' |
| |
|
| | import { useArticleContext } from '@/frame/components/context/ArticleContext' |
| | import { parseUserAgent } from '@/events/components/user-agent' |
| | import { InArticlePicker } from './InArticlePicker' |
| |
|
| | const platformQueryKey = 'platform' |
| | const platforms = [ |
| | { value: 'mac', label: 'Mac' }, |
| | { value: 'windows', label: 'Windows' }, |
| | { value: 'linux', label: 'Linux' }, |
| | ] |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | function showPlatformSpecificContent(platform: string) { |
| | const markdowns = Array.from(document.querySelectorAll<HTMLElement>('.ghd-tool')) |
| | const platformMarkdowns = markdowns.filter((xel) => |
| | platforms.some((platformValue) => xel.classList.contains(platformValue.value)), |
| | ) |
| | for (const el of platformMarkdowns) { |
| | el.style.display = el.classList.contains(platform) ? '' : 'none' |
| |
|
| | |
| | |
| | |
| | |
| | if (el.tagName === 'A' && el.parentElement && el.parentElement.tagName === 'LI') { |
| | el.parentElement.style.display = el.classList.contains(platform) ? '' : 'none' |
| | } |
| | } |
| |
|
| | |
| | |
| | const platformEls = Array.from( |
| | document.querySelectorAll<HTMLElement>( |
| | platforms.map((platformOption) => `.platform-${platformOption.value}`).join(', '), |
| | ), |
| | ) |
| | for (const el of platformEls) { |
| | 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) |
| | }, []) |
| |
|
| | |
| | |
| | |
| | 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} |
| | /> |
| | ) |
| | } |
| |
|