-
Notifications
You must be signed in to change notification settings - Fork 68.3k
Expand file tree
/
Copy pathToolPicker.tsx
More file actions
53 lines (43 loc) · 1.94 KB
/
Copy pathToolPicker.tsx
File metadata and controls
53 lines (43 loc) · 1.94 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
import { useArticleContext } from '@/frame/components/context/ArticleContext'
import { InArticlePicker } from './InArticlePicker'
import { useSelection } from './SelectionContext'
import { TOOL_PREFERRED_COOKIE_NAME } from '@/frame/lib/constants'
// example: http://localhost:4000/en/codespaces/developing-in-codespaces/creating-a-codespace
// Nota bene: tool === application
// Nota bene: picker === switcher
function getDefaultTool(defaultTool: string | undefined, detectedTools: Array<string>): string {
// If there is a default tool and the tool is present on this page
if (defaultTool && detectedTools.includes(defaultTool)) return defaultTool
// Default to webui if present (this is generally the case where we show UI/CLI/Desktop info)
if (detectedTools.includes('webui')) return 'webui'
// Default to cli if present (this is generally the case where we show curl/CLI info)
if (detectedTools.includes('cli')) return 'cli'
// Otherwise, just choose the first detected tool
return detectedTools[0]
}
const toolQueryKey = 'tool'
export const ToolPicker = () => {
// allTools comes from the ArticleContext which contains the list of tools available
const { defaultTool, detectedTools, allTools } = useArticleContext()
const { setTool } = useSelection()
if (!detectedTools.length) return null
const options = detectedTools.map((value) => {
return { value, label: allTools[value] }
})
return (
<InArticlePicker
fallbackValue={getDefaultTool(defaultTool, detectedTools)}
cookieKey={TOOL_PREFERRED_COOKIE_NAME}
queryStringKey={toolQueryKey}
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.
setTool(value)
}}
preferenceName="application"
ariaLabel="Tool"
options={options}
/>
)
}