1
0
mirror of synced 2025-12-23 11:54:18 -05:00

refactor rest context provider to be generic (#29021)

This commit is contained in:
Rachael Sewell
2022-07-14 07:09:38 -07:00
committed by GitHub
parent d738183157
commit 26e4dbc4bf
5 changed files with 32 additions and 30 deletions

View File

@@ -1,26 +1,28 @@
import { createContext, useContext } from 'react'
import type { MiniTocItem } from 'components/context/ArticleContext'
export type RestContextT = {
export type AutomatedPageContextT = {
title: string
intro: string
renderedPage: string | JSX.Element[]
miniTocItems: Array<MiniTocItem>
}
export const RestContext = createContext<RestContextT | null>(null)
export const AutomatedPageContext = createContext<AutomatedPageContextT | null>(null)
export const useRestContext = (): RestContextT => {
const context = useContext(RestContext)
export const useAutomatedPageContext = (): AutomatedPageContextT => {
const context = useContext(AutomatedPageContext)
if (!context) {
throw new Error('"useRestContext" may only be used inside "RestContext.Provider"')
throw new Error(
'"useAutomatedPageContext" may only be used inside "AutomatedPageContext.Provider"'
)
}
return context
}
export const getRestContextFromRequest = (req: any): RestContextT => {
export const getAutomatedPageContextFromRequest = (req: any): AutomatedPageContextT => {
const page = req.context.page
return {

View File

@@ -8,7 +8,7 @@ import { MarkdownContent } from 'components/ui/MarkdownContent'
import { Lead } from 'components/ui/Lead'
import { RestOperation } from './RestOperation'
import styles from './RestOperation.module.scss'
import { useRestContext } from 'components/context/RestContext'
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
import { Operation } from './types'
const ClientSideHighlightJS = dynamic(() => import('components/article/ClientSideHighlightJS'), {
@@ -28,7 +28,7 @@ export type StructuredContentT = {
export const RestReferencePage = ({ restOperations }: StructuredContentT) => {
const { asPath } = useRouter()
const { title, intro, renderedPage } = useRestContext()
const { title, intro, renderedPage } = useAutomatedPageContext()
// We have some one-off redirects for rest api docs
// currently those are limited to the repos page, but
// that will grow soon as we restructure the rest api docs.

View File

@@ -7,7 +7,7 @@ import { ActionList } from '@primer/react'
import { Link } from 'components/Link'
import { ProductTreeNode } from 'components/context/MainContext'
import { EventType, sendEvent } from 'components/lib/events'
import { useRestContext } from 'components/context/RestContext'
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
import type { MiniTocItem } from 'components/context/ArticleContext'
import styles from './SidebarProduct.module.scss'
@@ -47,7 +47,7 @@ export const RestCollapsibleSection = (props: SectionProps) => {
router.asPath.includes('/rest/guides') ||
router.asPath.includes('/rest/overview')
? []
: useRestContext().miniTocItems
: useAutomatedPageContext().miniTocItems
useEffect(() => {
if (!currentAnchor) {