1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/components/context/TocLandingContext.tsx
Mike Surowiec ae3d8916c6 Cleanup featuredLinks usage, fix insights page, better ArticleList (#19872)
* cleanup featuredLinks usage, fix insights page, better ArticleList component
2021-06-15 18:22:43 +00:00

45 lines
1.3 KiB
TypeScript

import pick from 'lodash/pick'
import { createContext, useContext } from 'react'
import { FeaturedLink, getFeaturedLinksFromReq } from './ProductLandingContext'
export type TocItem = {
fullPath: string
title: string
intro?: string
}
export type TocLandingContextT = {
title: string
introPlainText: string
productCallout: string
tocItems: Array<TocItem>
variant?: 'compact' | 'expanded'
featuredLinks: Record<string, Array<FeaturedLink>>
}
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,
productCallout: req.context.page.product || '',
introPlainText: req.context.page.introPlainText,
tocItems: (req.context.genericTocFlat || req.context.genericTocNested || []).map((obj: any) =>
pick(obj, ['fullPath', 'title', 'intro'])
),
variant: req.context.genericTocFlat ? 'expanded' : 'compact',
featuredLinks: getFeaturedLinksFromReq(req),
}
}