-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgenerate-resources-nav-items.ts
172 lines (159 loc) · 4.94 KB
/
generate-resources-nav-items.ts
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { ProductSlug } from 'types/products'
import { certificationProgramSlugMap } from 'views/certifications/content/utils/program-slug-map'
import {
VALID_EDITION_SLUGS_FOR_FILTERING,
VALID_PRODUCT_SLUGS_FOR_FILTERING,
} from 'views/tutorial-library/constants'
/**
* Note: these ResourceNav types could probably be abstracted up or lifted out,
* but for now, since the functions here had very little type information,
* felt pragmatic to declare them just for this file.
*/
type ResourceNavLink = {
title: string
href: string
}
type ResourceNavHeading = {
heading: string
}
type ResourceNavItem = ResourceNavLink | ResourceNavHeading
const DEFAULT_COMMUNITY_FORUM_LINK = 'https://discuss.hashicorp.com/'
const DEFAULT_GITHUB_LINK = 'https://github.com/hashicorp'
const DEFAULT_SUPPORT_LINK = 'https://www.hashicorp.com/customer-success'
const COMMUNITY_LINKS_BY_PRODUCT: {
[key in Exclude<
ProductSlug,
'well-architected-framework' | 'validated-patterns'
>]: string
} = {
boundary: 'https://discuss.hashicorp.com/c/boundary/50',
consul: 'https://discuss.hashicorp.com/c/consul/29',
hcp: 'https://discuss.hashicorp.com/c/hcp/54',
nomad: 'https://discuss.hashicorp.com/c/nomad/28',
packer: 'https://discuss.hashicorp.com/c/packer/23',
sentinel: 'https://discuss.hashicorp.com/c/sentinel/25',
terraform: 'https://discuss.hashicorp.com/c/terraform-core/27',
vagrant: 'https://discuss.hashicorp.com/c/vagrant/24',
vault: 'https://discuss.hashicorp.com/c/vault/30',
waypoint: 'https://discuss.hashicorp.com/c/waypoint/51',
}
const GITHUB_LINKS_BY_PRODUCT_SLUG: {
[key in Exclude<
ProductSlug,
'waypoint' | 'well-architected-framework' | 'validated-patterns'
>]: string
} = {
boundary: 'https://github.com/hashicorp/boundary',
consul: 'https://github.com/hashicorp/consul',
hcp: DEFAULT_GITHUB_LINK,
nomad: 'https://github.com/hashicorp/nomad',
packer: 'https://github.com/hashicorp/packer',
sentinel: DEFAULT_GITHUB_LINK,
terraform: 'https://github.com/hashicorp/terraform',
vagrant: 'https://github.com/hashicorp/vagrant',
vault: 'https://github.com/hashicorp/vault',
}
/**
* Generates additional sidebar nav items for the Resources section. If an
* `additional-sidebar-resources.json` file exists for a product in the
* `src/content` directory, and it has the correct data structure, the specified
* nav items will be appended to the Resources section.
*/
function generateAdditionalResources(
productSlug?: ProductSlug
): ResourceNavItem[] {
if (productSlug) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(`content/${productSlug}/additional-sidebar-resources.json`)
} catch {
return []
}
}
return []
}
/**
* Given a product slug,
* Return a link to the Tutorials Library with filters applied
* that correspond to that product.
*/
function getTutorialLibraryUrl(productSlug?: ProductSlug): string {
const baseUrl = '/tutorials/library'
if (!productSlug) {
return baseUrl
}
if (VALID_PRODUCT_SLUGS_FOR_FILTERING.includes(productSlug)) {
return `${baseUrl}/?product=${productSlug}`
} else if (VALID_EDITION_SLUGS_FOR_FILTERING.includes(productSlug)) {
return `${baseUrl}/?edition=${productSlug}`
} else {
return baseUrl
}
}
/**
* Given a product slug,
* Return an array of resource links.
*
* If we have a corresponding certification program page to link to,
* we return that single link item in an array.
*
* If the given product does not have a certifications page,
* we return an empty array.
*/
function getCertificationsLink(productSlug?: ProductSlug): ResourceNavLink[] {
// If this product does not have a certifications link, return an empty array
const programSlug = certificationProgramSlugMap[productSlug]
if (!programSlug) {
return []
}
// If this product does have a certifications link, return a single-item array
const link = {
title: 'Certifications',
href: `/certifications/${programSlug}`,
}
return [link]
}
/**
* Generates the sidebar nav items for the Resources section of the sidebar.
* Optionally accepts a Product slug for customization of links.
*/
function generateResourcesNavItems(
productSlug?: ProductSlug
): ResourceNavItem[] {
const additionalResources = generateAdditionalResources(productSlug)
return [
{ heading: 'Resources' },
{
// Add a "Tutorials" link for all products except Sentinel
title: 'Tutorial Library',
href: getTutorialLibraryUrl(productSlug),
},
...getCertificationsLink(productSlug),
{
title: 'Community Forum',
href: productSlug
? COMMUNITY_LINKS_BY_PRODUCT[productSlug]
: DEFAULT_COMMUNITY_FORUM_LINK,
},
{
title: 'Support',
href: DEFAULT_SUPPORT_LINK,
},
...(productSlug !== 'waypoint'
? [
{
title: 'GitHub',
href: productSlug
? GITHUB_LINKS_BY_PRODUCT_SLUG[productSlug]
: DEFAULT_GITHUB_LINK,
},
]
: []),
...additionalResources,
]
}
export { generateResourcesNavItems }