1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/components/context/ProductLandingContext.tsx
Robert Sese 33c05d81ce Change product landing page introLinks to a general map of link titles to links (#26360)
* Add cta button to frontmatter and context

* Add cta to Page

* Render cta button in product landing hero

* Handle external links

* Add simple unit test for cta link

* Address feedback

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

* Actually push condition update

* Show it's an external link

* Refactor FullLink so we use Link once

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

* Custom link can also be null

* Rename 'cta' to 'custom' and make it the last introLink

* Update tests with 'cta' to 'custom' change

* Filter once

* Revert "Filter once"

This reverts commit a3f9a8a06b505d77fed47ca96a66c187c86c3c91.

* Update introLinks to a map of titles and URLs

* No more custom introLink in LandingHero

* Simplify introLinks processing

* introLinks can also be null

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

* more concise

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

* No longer necessary with the a plain introLinks map

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

* '.entries()` is simpler

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

* 'link' could be false depending on what version you're on

* Update test for new introLinks

Co-authored-by: Peter Bengtsson <mail@peterbe.com>
2022-04-01 11:01:37 -05:00

161 lines
4.6 KiB
TypeScript

import { createContext, useContext } from 'react'
import pick from 'lodash/pick'
export type TocItem = {
fullPath: string
title: string
intro?: string
childTocItems?: Array<{
fullPath: string
title: string
}>
}
export type FeaturedLink = {
title: string
href: string
intro?: string
authors?: Array<string>
hideIntro?: boolean
date?: string
fullTitle?: string
}
export type CodeExample = {
title: string
description: string
languages: string // single comma separated string
href: string
tags: Array<string>
}
export type Product = {
title: string
href: string
}
export type ProductLandingContextT = {
title: string
introPlainText: string
shortTitle: string
intro: string
beta_product: boolean
product: Product
introLinks: Record<string, string> | null
product_video?: string
featuredLinks: Record<string, Array<FeaturedLink>>
productCodeExamples: Array<CodeExample>
productUserExamples: Array<{ username: string; description: string }>
productCommunityExamples: Array<{ repo: string; description: string }>
featuredArticles: Array<{
label: string // Guides
viewAllHref?: string // If provided, adds a "View All ->" to the header
viewAllTitleText?: string // Adds 'title' attribute text for the "View All" href
articles: Array<FeaturedLink>
}>
changelogUrl?: string
whatsNewChangelog?: Array<{ href: string; title: string; date: string }>
tocItems: Array<TocItem>
hasGuidesPage: boolean
releases: Array<{
version: string
firstPreviousRelease: string
secondPreviousRelease: string
patches: Array<{ date: string; version: string }>
}>
}
export const ProductLandingContext = createContext<ProductLandingContextT | null>(null)
export const useProductLandingContext = (): ProductLandingContextT => {
const context = useContext(ProductLandingContext)
if (!context) {
throw new Error(
'"useProductLandingContext" may only be used inside "ProductLandingContext.Provider"'
)
}
return context
}
export const getFeaturedLinksFromReq = (req: any): Record<string, Array<FeaturedLink>> => {
return Object.fromEntries(
Object.entries(req.context.featuredLinks || {}).map(([key, entries]) => {
return [
key,
((entries as Array<any>) || []).map((entry: any) => ({
href: entry.href,
title: entry.title,
intro: entry.intro || null,
authors: entry.page?.authors || [],
fullTitle: entry.fullTitle || null,
})),
]
})
)
}
export const getProductLandingContextFromRequest = (req: any): ProductLandingContextT => {
const productTree = req.context.currentProductTree
const page = req.context.page
const hasGuidesPage = (page.children || []).includes('/guides')
return {
...pick(page, [
'title',
'shortTitle',
'introPlainText',
'beta_product',
'intro',
'product_video',
]),
hasGuidesPage,
product: {
href: productTree.href,
title: productTree.renderedShortTitle || productTree.renderedFullTitle,
},
whatsNewChangelog: req.context.whatsNewChangelog || [],
changelogUrl: req.context.changelogUrl || [],
productCodeExamples: req.context.productCodeExamples || [],
productCommunityExamples: req.context.productCommunityExamples || [],
releases: req.context.releases || [],
productUserExamples: (req.context.productUserExamples || []).map(
({ user, description }: any) => ({
username: user,
description,
})
),
introLinks: page.introLinks || null,
featuredLinks: getFeaturedLinksFromReq(req),
tocItems: req.context.tocItems || [],
featuredArticles: Object.entries(req.context.featuredLinks || [])
.filter(([key]) => {
return key === 'guides' || key === 'popular' || key === 'videos'
})
.map(([key, links]: any) => {
return {
label:
key === 'popular' || key === 'videos'
? req.context.page.featuredLinks[key + 'Heading'] || req.context.site.data.ui.toc[key]
: req.context.site.data.ui.toc[key],
viewAllHref:
key === 'guides' && !req.context.currentCategory && hasGuidesPage
? `${req.context.currentPath}/guides`
: '',
articles: links.map((link: any) => {
return {
hideIntro: key === 'popular',
href: link.href,
title: link.title,
intro: link.intro || null,
authors: link.page?.authors || [],
fullTitle: link.fullTitle || null,
}
}),
}
}),
}
}