1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/components/context/PlaygroundContext.tsx
Mike Surowiec 06d8f81401 Two-pane Experiment (#21092)
* pull changes from docs-playground

* cleanup, add callout banner

* cleanup linting and test fixes

* add discussion link

Co-authored-by: James M. Greene <JamesMGreene@github.com>
2021-08-26 14:19:40 -04:00

69 lines
2.1 KiB
TypeScript

import React, { createContext, useContext, useState } from 'react'
import { CodeLanguage, PlaygroundArticleT } from 'components/playground/types'
import { useRouter } from 'next/router'
import jsArticle from 'components/playground/content/building-and-testing/nodejs'
import pyArticle from 'components/playground/content/building-and-testing/python'
const articles = [jsArticle, pyArticle]
const articlesByLangId = articles.reduce((obj, item) => {
obj[item.codeLanguageId] = item
return obj
}, {} as Record<string, PlaygroundArticleT | undefined>)
const codeLanguages: Array<CodeLanguage> = [
{
id: 'nodejs',
label: 'Node.js',
},
{
id: 'py',
label: 'Python',
},
]
type PlaygroundContextT = {
activeSectionIndex: number
setActiveSectionIndex: (sectionIndex: number) => void
scrollToSection: number | undefined
setScrollToSection: (sectionIndex?: number) => void
codeLanguages: Array<CodeLanguage>
currentLanguage: CodeLanguage
article: PlaygroundArticleT | undefined
}
export const PlaygroundContext = createContext<PlaygroundContextT | null>(null)
export const usePlaygroundContext = (): PlaygroundContextT => {
const context = useContext(PlaygroundContext)
if (!context) {
throw new Error('"usePlaygroundContext" may only be used inside "PlaygroundContext.Provider"')
}
return context
}
export const PlaygroundContextProvider = (props: { children: React.ReactNode }) => {
const router = useRouter()
const [activeSectionIndex, setActiveSectionIndex] = useState(0)
const [scrollToSection, setScrollToSection] = useState<number>()
const { langId } = router.query
const currentLanguage = codeLanguages.find(({ id }) => id === langId) || codeLanguages[0]
const availableLanguages = codeLanguages.filter(({ id }) => !!articlesByLangId[id])
const article = articlesByLangId[currentLanguage.id]
const context = {
activeSectionIndex,
setActiveSectionIndex,
scrollToSection,
setScrollToSection,
currentLanguage,
codeLanguages: availableLanguages,
article,
}
return <PlaygroundContext.Provider value={context}>{props.children}</PlaygroundContext.Provider>
}