1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/components/context/ArticleContext.tsx
Grace Park 884556eb02 Adding effectiveDate frontmatter property (#22317)
* adding effectiveDate frontmatter property

* Update components/article/ArticlePage.tsx

Co-authored-by: Peter Bengtsson <peterbe@github.com>

* adding validation for correct date

* update to dateTime

* update to year month day

* update error date validation

* moving validation to getArticleContextFromRequest and moving id to div

* remove enteredDate

* Update content/README.md

Co-authored-by: Laura Coursen <lecoursen@github.com>

Co-authored-by: Peter Bengtsson <peterbe@github.com>
Co-authored-by: Laura Coursen <lecoursen@github.com>
2021-10-20 19:40:13 +00:00

67 lines
1.8 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 = {
platform: string
contents: string
items?: MiniTocItem[]
}
export type ArticleContextT = {
title: string
intro: string
effectiveDate: 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
if (page.effectiveDate) {
if (isNaN(Date.parse(page.effectiveDate))) {
throw new Error(
'The "effectiveDate" frontmatter property is not valid. Please make sure it is YEAR-MONTH-DAY'
)
}
}
return {
title: page.titlePlainText,
intro: page.intro,
effectiveDate: page.effectiveDate || '',
renderedPage: req.context.renderedPage || '',
miniTocItems: req.context.miniTocItems || [],
contributor: page.contributor || null,
permissions: page.permissions || '',
includesPlatformSpecificContent: page.includesPlatformSpecificContent || false,
defaultPlatform: page.defaultPlatform || '',
product: page.product || '',
currentLearningTrack: req.context.currentLearningTrack,
}
}