1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/components/context/TocLandingContext.tsx
Mike Surowiec e0d9a061a3 React category / sub-landing page (#19635)
Re-work routes to use GlobalPage, implements TocLanding, bundle of minor cleanup / fixes
2021-06-02 16:28:39 +00:00

34 lines
802 B
TypeScript

import { createContext, useContext } from 'react'
export type TocItem = {
fullPath: string
title: string
intro?: string
}
export type TocLandingContextT = {
title: string
introPlainText: string
tocItems: Array<TocItem>
}
export const TocLandingContext = createContext<TocLandingContextT | null>(null)
export const useTocLandingContext = (): TocLandingContextT => {
const context = useContext(TocLandingContext)
if (!context) {
throw new Error('"useTocLandingContext" may only be used inside "TocLandingContext.Provider"')
}
return context
}
export const getTocLandingContextFromRequest = (req: any): TocLandingContextT => {
return {
title: req.context.page.title,
introPlainText: req.context.page.introPlainText,
tocItems: req.context.tocItems || [],
}
}