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

ArticleContext.tsx 1.7 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
  1. import { createContext, useContext } from 'react'
  2. export type LearningTrack = {
  3. trackName?: string
  4. prevGuide?: { href: string; title: string }
  5. nextGuide?: { href: string; title: string }
  6. }
  7. export type MiniTocItem = {
  8. indentationLevel: number
  9. platform: string
  10. contents: string
  11. }
  12. export type ArticleContextT = {
  13. title: string
  14. intro: string
  15. renderedPage: string
  16. miniTocItems: Array<MiniTocItem>
  17. contributor: { name: string; URL: string } | null
  18. permissions?: string
  19. includesPlatformSpecificContent: boolean
  20. defaultPlatform?: string
  21. product?: string
  22. currentLearningTrack?: LearningTrack
  23. }
  24. export const ArticleContext = createContext<ArticleContextT | null>(null)
  25. export const useArticleContext = (): ArticleContextT => {
  26. const context = useContext(ArticleContext)
  27. if (!context) {
  28. throw new Error('"useArticleContext" may only be used inside "ArticleContext.Provider"')
  29. }
  30. return context
  31. }
  32. export const getArticleContextFromRequest = (req: any): ArticleContextT => {
  33. const page = req.context.page
  34. return {
  35. title: page.titlePlainText,
  36. intro: page.introPlainText,
  37. renderedPage: req.context.renderedPage || '',
  38. miniTocItems:
  39. (req.context.miniTocItems || []).map((item: any) => {
  40. return {
  41. indentationLevel: item.indentationLevel || 0,
  42. platform: item.platform || '',
  43. contents: item.contents || '',
  44. }
  45. }) || [],
  46. contributor: page.contributor || null,
  47. permissions: page.permissions || '',
  48. includesPlatformSpecificContent: page.includesPlatformSpecificContent || false,
  49. defaultPlatform: page.defaultPlatform || '',
  50. product: page.product || '',
  51. currentLearningTrack: req.context.currentLearningTrack,
  52. }
  53. }
Tip!

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

Comments

Loading...