1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/components/context/ArticleContext.tsx
Robert Sese 000799df17 Handle learning track paths from a different product (#21776)
* Add trackProduct property

* Fall back to learning track product URL param

* Add product URL param to learning track banner

* Add product URL param to featured track links

* Fix typo :(

* Add product URL param to learning track

* Add multi-product learning track tests

* Re-enable tests with a Code Security learning track

* Re-enable more tests with Code Security learning tracks

* Add more multi-product testing

* Update components/sublanding/LearningTrack.tsx

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
2021-09-30 21:17:02 +00:00

63 lines
1.7 KiB
TypeScript

import { createContext, useContext } from 'react'
export type LearningTrack = {
trackName?: string
trackProduct?: string
prevGuide?: { href: string; title: string }
nextGuide?: { href: string; title: string }
}
export type MiniTocItem = {
indentationLevel: number
platform: string
contents: string
}
export type ArticleContextT = {
title: string
intro: string
renderedPage: string
miniTocItems: Array<MiniTocItem>
contributor: { name: string; URL: string } | null
permissions?: string
includesPlatformSpecificContent: boolean
defaultPlatform?: string
product?: string
currentLearningTrack?: LearningTrack
}
export const ArticleContext = createContext<ArticleContextT | null>(null)
export const useArticleContext = (): ArticleContextT => {
const context = useContext(ArticleContext)
if (!context) {
throw new Error('"useArticleContext" may only be used inside "ArticleContext.Provider"')
}
return context
}
export const getArticleContextFromRequest = (req: any): ArticleContextT => {
const page = req.context.page
return {
title: page.titlePlainText,
intro: page.intro,
renderedPage: req.context.renderedPage || '',
miniTocItems:
(req.context.miniTocItems || []).map((item: any) => {
return {
indentationLevel: item.indentationLevel || 0,
platform: item.platform || '',
contents: item.contents || '',
}
}) || [],
contributor: page.contributor || null,
permissions: page.permissions || '',
includesPlatformSpecificContent: page.includesPlatformSpecificContent || false,
defaultPlatform: page.defaultPlatform || '',
product: page.product || '',
currentLearningTrack: req.context.currentLearningTrack,
}
}