-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathArticleContext.tsx
More file actions
110 lines (97 loc) · 3.7 KB
/
ArticleContext.tsx
File metadata and controls
110 lines (97 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { SupportPortalVaIframeProps } from '@/frame/components/article/SupportPortalVaIframe'
import { createContext, useContext } from 'react'
import type { JourneyContext } from '@/journeys/lib/journey-path-resolver'
export type LearningTrack = {
trackTitle: string
trackName: string
trackProduct: string
prevGuide?: { href: string; title: string }
nextGuide?: { href: string; title: string }
numberOfGuides: number
currentGuideIndex: number
}
export type MiniTocItem = {
platform?: string
contents: {
href: string
title: string
}
items?: MiniTocItem[]
}
export type ArticleContextT = {
title: string
intro: string
effectiveDate: string
renderedPage: string | JSX.Element[]
miniTocItems: Array<MiniTocItem>
permissions?: string
includesPlatformSpecificContent: boolean
includesToolSpecificContent: boolean
defaultPlatform?: string
defaultTool?: string
product?: string
productVideoUrl?: string
currentLearningTrack?: LearningTrack
currentJourneyTrack?: JourneyContext
detectedPlatforms: Array<string>
detectedTools: Array<string>
allTools: Record<string, string>
supportPortalVaIframeProps: SupportPortalVaIframeProps
currentLayout?: string
}
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
}
const PagePathToVaFlowMapping: Record<string, string> = {
'content/account-and-profile/setting-up-and-managing-your-github-profile/managing-contribution-settings-on-your-profile/why-are-my-contributions-not-showing-up-on-my-profile.md':
'contribution_troubleshooting',
'content/authentication/securing-your-account-with-two-factor-authentication-2fa/recovering-your-account-if-you-lose-your-2fa-credentials.md':
'2fa',
'content/pages/getting-started-with-github-pages/securing-your-github-pages-site-with-https.md':
'pages_ssl_check',
}
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',
)
}
}
const supportPortalUrl =
process.env.NODE_ENV === 'production'
? 'https://support.github.com'
: // Assume that a developer is not testing the VA iframe locally if this env var is not set
process.env.SUPPORT_PORTAL_URL || ''
const supportPortalVaIframeProps = {
supportPortalUrl,
vaFlowUrlParameter: PagePathToVaFlowMapping[req.context.page.fullPath] || '',
}
return {
title: page.title,
intro: page.intro,
effectiveDate: page.effectiveDate || '',
renderedPage: req.context.renderedPage || '',
miniTocItems: req.context.miniTocItems || [],
permissions: page.permissions || '',
includesPlatformSpecificContent: page.includesPlatformSpecificContent || false,
includesToolSpecificContent: page.includesToolSpecificContent || false,
defaultPlatform: page.defaultPlatform || '',
defaultTool: page.defaultTool || '',
product: page.product || '',
productVideoUrl: page.product_video || '',
currentLearningTrack: req.context.currentLearningTrack,
currentJourneyTrack: req.context.currentJourneyTrack,
detectedPlatforms: page.detectedPlatforms || [],
detectedTools: page.detectedTools || [],
allTools: page.allToolsParsed || [], // this is set at the page level, see lib/page.js
supportPortalVaIframeProps,
currentLayout: req.context.currentLayoutName,
}
}