|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import Cookies from 'js-cookie' |
| 3 | +import { SubNav, TabNav, UnderlineNav } from '@primer/components' |
| 4 | +import { sendEvent, EventType } from 'components/lib/events' |
| 5 | + |
| 6 | +import { useArticleContext } from 'components/context/ArticleContext' |
| 7 | +import parseUserAgent from 'components/lib/user-agent' |
| 8 | + |
| 9 | +const platforms = [ |
| 10 | + { id: 'mac', label: 'Mac' }, |
| 11 | + { id: 'windows', label: 'Windows' }, |
| 12 | + { id: 'linux', label: 'Linux' }, |
| 13 | +] |
| 14 | + |
| 15 | +// Imperatively modify article content to show only the selected platform |
| 16 | +// find all platform-specific *block* elements and hide or show as appropriate |
| 17 | +// example: {% mac } block content {% mac %} |
| 18 | +function showPlatformSpecificContent(platform: string) { |
| 19 | + const markdowns = Array.from(document.querySelectorAll<HTMLElement>('.extended-markdown')) |
| 20 | + markdowns |
| 21 | + .filter((el) => platforms.some((platform) => el.classList.contains(platform.id))) |
| 22 | + .forEach((el) => { |
| 23 | + el.style.display = el.classList.contains(platform) ? '' : 'none' |
| 24 | + }) |
| 25 | + |
| 26 | + // find all platform-specific *inline* elements and hide or show as appropriate |
| 27 | + // example: <span class="platform-mac">inline content</span> |
| 28 | + const platformEls = Array.from( |
| 29 | + document.querySelectorAll<HTMLElement>( |
| 30 | + platforms.map((platform) => `.platform-${platform.id}`).join(', ') |
| 31 | + ) |
| 32 | + ) |
| 33 | + platformEls.forEach((el) => { |
| 34 | + el.style.display = el.classList.contains(`platform-${platform}`) ? '' : 'none' |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +// uses the order of the supportedPlatforms array to |
| 39 | +// determine the default platform |
| 40 | +const getFallbackPlatform = (detectedPlatforms: Array<string>): string => { |
| 41 | + const foundPlatform = platforms.find((platform) => detectedPlatforms.includes(platform.id)) |
| 42 | + return foundPlatform?.id || 'linux' |
| 43 | +} |
| 44 | + |
| 45 | +type Props = { |
| 46 | + variant?: 'subnav' | 'tabnav' | 'underlinenav' |
| 47 | +} |
| 48 | +export const PlatformPicker = ({ variant = 'subnav' }: Props) => { |
| 49 | + const { defaultPlatform, detectedPlatforms } = useArticleContext() |
| 50 | + const [currentPlatform, setCurrentPlatform] = useState(defaultPlatform || '') |
| 51 | + |
| 52 | + // Run on mount for client-side only features |
| 53 | + useEffect(() => { |
| 54 | + let userAgent = parseUserAgent().os |
| 55 | + if (userAgent === 'ios') { |
| 56 | + userAgent = 'mac' |
| 57 | + } |
| 58 | + |
| 59 | + setCurrentPlatform(defaultPlatform || Cookies.get('osPreferred') || userAgent || 'linux') |
| 60 | + }, []) |
| 61 | + |
| 62 | + // Make sure we've always selected a platform that exists in the article |
| 63 | + useEffect(() => { |
| 64 | + // Only check *after* current platform has been determined |
| 65 | + if (currentPlatform && !detectedPlatforms.includes(currentPlatform)) { |
| 66 | + setCurrentPlatform(getFallbackPlatform(detectedPlatforms)) |
| 67 | + } |
| 68 | + }, [currentPlatform, detectedPlatforms.join(',')]) |
| 69 | + |
| 70 | + const onClickPlatform = (platform: string) => { |
| 71 | + setCurrentPlatform(platform) |
| 72 | + |
| 73 | + // imperatively modify the article content |
| 74 | + showPlatformSpecificContent(platform) |
| 75 | + |
| 76 | + sendEvent({ |
| 77 | + type: EventType.preference, |
| 78 | + preference_name: 'os', |
| 79 | + preference_value: platform, |
| 80 | + }) |
| 81 | + |
| 82 | + Cookies.set('osPreferred', platform, { |
| 83 | + sameSite: 'strict', |
| 84 | + secure: true, |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + // only show platforms that are in the current article |
| 89 | + const platformOptions = platforms.filter((platform) => detectedPlatforms.includes(platform.id)) |
| 90 | + |
| 91 | + const sharedContainerProps = { |
| 92 | + 'data-testid': 'platform-picker', |
| 93 | + 'aria-label': 'Platform picker', |
| 94 | + 'data-default-platform': defaultPlatform, |
| 95 | + className: 'mb-4', |
| 96 | + } |
| 97 | + |
| 98 | + if (variant === 'subnav') { |
| 99 | + return ( |
| 100 | + <SubNav {...sharedContainerProps}> |
| 101 | + <SubNav.Links> |
| 102 | + {platformOptions.map((option) => { |
| 103 | + return ( |
| 104 | + <SubNav.Link |
| 105 | + key={option.id} |
| 106 | + data-platform={option.id} |
| 107 | + as="button" |
| 108 | + selected={option.id === currentPlatform} |
| 109 | + onClick={() => { |
| 110 | + onClickPlatform(option.id) |
| 111 | + }} |
| 112 | + > |
| 113 | + {option.label} |
| 114 | + </SubNav.Link> |
| 115 | + ) |
| 116 | + })} |
| 117 | + </SubNav.Links> |
| 118 | + </SubNav> |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + if (variant === 'underlinenav') { |
| 123 | + return ( |
| 124 | + <UnderlineNav {...sharedContainerProps}> |
| 125 | + {platformOptions.map((option) => { |
| 126 | + return ( |
| 127 | + <UnderlineNav.Link |
| 128 | + key={option.id} |
| 129 | + data-platform={option.id} |
| 130 | + as="button" |
| 131 | + selected={option.id === currentPlatform} |
| 132 | + onClick={() => { |
| 133 | + onClickPlatform(option.id) |
| 134 | + }} |
| 135 | + > |
| 136 | + {option.label} |
| 137 | + </UnderlineNav.Link> |
| 138 | + ) |
| 139 | + })} |
| 140 | + </UnderlineNav> |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + return ( |
| 145 | + <TabNav {...sharedContainerProps}> |
| 146 | + {platformOptions.map((option) => { |
| 147 | + return ( |
| 148 | + <TabNav.Link |
| 149 | + key={option.id} |
| 150 | + data-platform={option.id} |
| 151 | + as="button" |
| 152 | + selected={option.id === currentPlatform} |
| 153 | + onClick={() => { |
| 154 | + onClickPlatform(option.id) |
| 155 | + }} |
| 156 | + > |
| 157 | + {option.label} |
| 158 | + </TabNav.Link> |
| 159 | + ) |
| 160 | + })} |
| 161 | + </TabNav> |
| 162 | + ) |
| 163 | +} |
0 commit comments