|
| 1 | +import { createContext, useContext } from 'react' |
| 2 | + |
| 3 | +import type { BreadcrumbT } from 'components/Breadcrumbs' |
| 4 | + |
| 5 | +type ProductT = { |
| 6 | + external: boolean |
| 7 | + href: string |
| 8 | + id: string |
| 9 | + name: string |
| 10 | +} |
| 11 | +type DataT = { |
| 12 | + ui: Record<string, Record<string, string>> |
| 13 | + reusables: { |
| 14 | + enterprise_deprecation: { |
| 15 | + version_was_deprecated: string |
| 16 | + version_will_be_deprecated: string |
| 17 | + deprecation_details: string |
| 18 | + isOldestReleaseDeprecated: boolean |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | +type EnterpriseServerReleases = { |
| 23 | + isOldestReleaseDeprecated: boolean |
| 24 | + oldestSupported: string |
| 25 | + nextDeprecationDate: string |
| 26 | +} |
| 27 | +export type MainContextT = { |
| 28 | + breadcrumbs?: Record<string, BreadcrumbT> |
| 29 | + builtAssets: { main: { css: string; js: string } } |
| 30 | + expose: string |
| 31 | + activeProducts: Array<ProductT> |
| 32 | + currentProduct: ProductT |
| 33 | + currentLayoutName: string |
| 34 | + data: DataT |
| 35 | + airGap?: boolean |
| 36 | + currentCategory?: string |
| 37 | + relativePath?: string |
| 38 | + enterpriseServerReleases: EnterpriseServerReleases |
| 39 | +} |
| 40 | + |
| 41 | +export const getMainContextFromRequest = (req: any): MainContextT => { |
| 42 | + return { |
| 43 | + builtAssets: req.context.builtAssets, |
| 44 | + expose: req.context.expose, |
| 45 | + breadcrumbs: req.context.breadcrumbs, |
| 46 | + activeProducts: req.context.activeProducts, |
| 47 | + currentProduct: req.context.productMap[req.context.currentProduct], |
| 48 | + currentLayoutName: req.context.currentLayoutName, |
| 49 | + data: { |
| 50 | + ui: req.context.site.data.ui, |
| 51 | + reusables: { |
| 52 | + enterprise_deprecation: req.context.site.data.reusables.enterprise_deprecation, |
| 53 | + }, |
| 54 | + }, |
| 55 | + airGap: req.context.AIRGAP || false, |
| 56 | + currentCategory: req.context.currentCategory || '', |
| 57 | + relativePath: req.context.page.relativePath, |
| 58 | + enterpriseServerReleases: req.context.enterpriseServerReleases, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export const MainContext = createContext<MainContextT | null>(null) |
| 63 | + |
| 64 | +export const useMainContext = (): MainContextT => { |
| 65 | + const context = useContext(MainContext) |
| 66 | + |
| 67 | + if (!context) { |
| 68 | + throw new Error('"useMainContext" may only be used inside "MainContext.Provider"') |
| 69 | + } |
| 70 | + |
| 71 | + return context |
| 72 | +} |
0 commit comments