Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

MainContext.tsx 5.8 KB

You have to be logged in to leave a comment. Sign In
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  1. import { createContext, useContext } from 'react'
  2. import pick from 'lodash/pick'
  3. import type { BreadcrumbT } from 'components/Breadcrumbs'
  4. import type { FeatureFlags } from 'components/hooks/useFeatureFlags'
  5. type ProductT = {
  6. external: boolean
  7. href: string
  8. id: string
  9. name: string
  10. versions?: Array<string>
  11. }
  12. type LanguageItem = {
  13. name: string
  14. nativeName: string
  15. code: string
  16. hreflang: string
  17. wip?: boolean
  18. }
  19. type VersionItem = {
  20. version: string
  21. versionTitle: string
  22. }
  23. export type CurrentProductTree = {
  24. page: {
  25. hidden?: boolean
  26. documentType: 'article' | 'mapTopic'
  27. title: string
  28. shortTitle: string
  29. }
  30. renderedShortTitle?: string
  31. renderedFullTitle: string
  32. href: string
  33. childPages: Array<CurrentProductTree>
  34. }
  35. type DataT = {
  36. ui: Record<string, any>
  37. reusables: {
  38. enterprise_deprecation: {
  39. version_was_deprecated: string
  40. version_will_be_deprecated: string
  41. deprecation_details: string
  42. isOldestReleaseDeprecated: boolean
  43. }
  44. policies: {
  45. translation: string
  46. }
  47. }
  48. variables: {
  49. release_candidate: { version: string }
  50. }
  51. }
  52. type EnterpriseServerReleases = {
  53. isOldestReleaseDeprecated: boolean
  54. oldestSupported: string
  55. nextDeprecationDate: string
  56. }
  57. export type MainContextT = {
  58. breadcrumbs: {
  59. product: BreadcrumbT
  60. category?: BreadcrumbT
  61. maptopic?: BreadcrumbT
  62. article?: BreadcrumbT
  63. }
  64. builtAssets: { main: { js: string } }
  65. expose: string
  66. activeProducts: Array<ProductT>
  67. currentProduct?: ProductT
  68. currentLayoutName: string
  69. isHomepageVersion: boolean
  70. data: DataT
  71. airGap?: boolean
  72. error: string
  73. currentCategory?: string
  74. relativePath?: string
  75. enterpriseServerReleases: EnterpriseServerReleases
  76. currentPathWithoutLanguage: string
  77. currentLanguage: string
  78. userLanguage: string
  79. languages: Record<string, LanguageItem>
  80. allVersions: Record<string, VersionItem>
  81. currentProductTree?: CurrentProductTree | null
  82. featureFlags: FeatureFlags
  83. page: {
  84. documentType: string
  85. languageVariants: Array<{ name: string; code: string; hreflang: string; href: string }>
  86. topics: Array<string>
  87. title: string
  88. fullTitle?: string
  89. introPlainText?: string
  90. hidden: boolean
  91. permalinks?: Array<{
  92. languageCode: string
  93. relativePath: string
  94. title: string
  95. pageVersionTitle: string
  96. pageVersion: string
  97. href: string
  98. }>
  99. }
  100. enterpriseServerVersions: Array<string>
  101. }
  102. export const getMainContextFromRequest = (req: any): MainContextT => {
  103. return {
  104. builtAssets: { main: { js: req.context.builtAssets.main.js } },
  105. expose: req.context.expose,
  106. breadcrumbs: req.context.breadcrumbs || {},
  107. activeProducts: req.context.activeProducts,
  108. currentProduct: req.context.productMap[req.context.currentProduct] || null,
  109. currentLayoutName: req.context.currentLayoutName,
  110. isHomepageVersion: req.context.currentVersion === 'homepage',
  111. error: req.context.error || '',
  112. data: {
  113. ui: req.context.site.data.ui,
  114. reusables: {
  115. enterprise_deprecation: req.context.site.data.reusables.enterprise_deprecation,
  116. policies: req.context.site.data.reusables.policies,
  117. },
  118. variables: {
  119. release_candidate: req.context.site.data.variables.release_candidate,
  120. },
  121. },
  122. airGap: req.context.AIRGAP || false,
  123. currentCategory: req.context.currentCategory || '',
  124. currentPathWithoutLanguage: req.context.currentPathWithoutLanguage,
  125. relativePath: req.context.page?.relativePath,
  126. page: {
  127. languageVariants: req.context.page.languageVariants,
  128. documentType: req.context.page.documentType,
  129. title: req.context.page.title,
  130. fullTitle: req.context.page.fullTitle,
  131. topics: req.context.page.topics || [],
  132. introPlainText: req.context.page?.introPlainText,
  133. permalinks: req.context.page?.permalinks.map((obj: any) =>
  134. pick(obj, [
  135. 'title',
  136. 'pageVersionTitle',
  137. 'pageVersion',
  138. 'href',
  139. 'relativePath',
  140. 'languageCode',
  141. ])
  142. ),
  143. hidden: req.context.page.hidden || false,
  144. },
  145. enterpriseServerReleases: pick(req.context.enterpriseServerReleases, [
  146. 'isOldestReleaseDeprecated',
  147. 'oldestSupported',
  148. 'nextDeprecationDate',
  149. ]),
  150. enterpriseServerVersions: req.context.enterpriseServerVersions,
  151. currentLanguage: req.context.currentLanguage,
  152. userLanguage: req.context.userLanguage || '',
  153. languages: Object.fromEntries(
  154. Object.entries(req.context.languages).map(([key, entry]: any) => {
  155. return [
  156. key,
  157. {
  158. name: entry.name,
  159. nativeName: entry.nativeName || '',
  160. code: entry.code,
  161. hreflang: entry.hreflang,
  162. wip: entry.wip || false,
  163. },
  164. ]
  165. })
  166. ),
  167. allVersions: req.context.allVersions,
  168. currentProductTree: req.context.currentProductTree
  169. ? getCurrentProductTree(req.context.currentProductTree)
  170. : null,
  171. featureFlags: {},
  172. }
  173. }
  174. // only pull things we need from the product tree, and make sure there are default values instead of `undefined`
  175. const getCurrentProductTree = (input: any): CurrentProductTree => {
  176. return {
  177. href: input.href,
  178. renderedShortTitle: input.renderedShortTitle || '',
  179. renderedFullTitle: input.renderedFullTitle || '',
  180. page: {
  181. hidden: input.page.hidden || false,
  182. documentType: input.page.documentType,
  183. title: input.page.title,
  184. shortTitle: input.page.shortTitle || '',
  185. },
  186. childPages: (input.childPages || []).map(getCurrentProductTree),
  187. }
  188. }
  189. export const MainContext = createContext<MainContextT | null>(null)
  190. export const useMainContext = (): MainContextT => {
  191. const context = useContext(MainContext)
  192. if (!context) {
  193. throw new Error('"useMainContext" may only be used inside "MainContext.Provider"')
  194. }
  195. return context
  196. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...