1
0
mirror of synced 2025-12-30 12:02:01 -05:00
Files
docs/components/context/AutomatedPageContext.tsx
Robert Sese b2e5d14036 Automate + Reactify webhooks page (#29534)
Co-authored-by: Rachael Sewell <rachmari@github.com>
Co-authored-by: Peter Bengtsson <mail@peterbe.com>
Co-authored-by: Joe Oak <41307427+joeoak@users.noreply.github.com>
Co-authored-by: Sarah Edwards <skedwards88@github.com>
Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: Peter Bengtsson <peterbe@github.com>
2022-11-15 22:12:16 +00:00

37 lines
982 B
TypeScript

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