Skip to content

Commit

Permalink
make collection fetch theme optional for sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh committed Sep 11, 2024
1 parent ba911c2 commit 903686d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/lib/learn-client/api/collection/fetch-product-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
uuid,
ProductOption,
AllCollectionsProductOptions,
ThemeOption,
} from 'lib/learn-client/types'
import { get, toError } from '../../index'

Expand All @@ -27,18 +28,29 @@ export const PRODUCT_COLLECTION_API_ROUTE = (
* includes filtering for theme
*/
export async function fetchAllCollectionsByProduct(
product: AllCollectionsProductOptions
product: AllCollectionsProductOptions,
/**
* All `ProductOption` values except `sentinel` can be used as "theme" options.
* Theme is mainly used to add a product logo to various UI elements, and
* since Sentinel doesn't have a logo, it's not a valid theme option.
*
* Note: an alternative here might be to implement a `theme` option for
* Sentinel, and for now, set it to render a HashiCorp logo. This might
* be a more future-proof approach. This would require updates to `learn-api`:
* https://github.com/hashicorp/learn-api/blob/main/src/models/collection.ts#L17
*/
theme?: Exclude<ProductOption, 'sentinel'> | ThemeOption
): Promise<Collection[]> {
const baseUrl = PRODUCT_COLLECTION_API_ROUTE(product.slug)
let route = baseUrl

if (product.sidebarSort) {
const params = new URLSearchParams([
['topLevelCategorySort', 'true'],
['theme', product.slug],
])

route = baseUrl + `?${params.toString()}`
const params = []
params.push(['topLevelCategorySort', 'true'])
if (theme) {
params.push(['theme', theme])
}
route = baseUrl + `?${new URLSearchParams(params).toString()}`
}

const getProductCollectionsRes = await get(route)
Expand Down
15 changes: 13 additions & 2 deletions src/lib/learn-client/api/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ export async function getAllCollections(

// check if the product option is valid, i.e. not 'cloud' or 'hashicorp'
if (options?.product && themeIsProduct(options.product.slug)) {
const allCollections = await fetchAllCollectionsByProduct(options.product)

/**
* Sentinel cannot use "theme", as the `learn-api` doesn't support a
* `sentinel` theme value. We expect authors to use `theme: hashicorp`
* on Sentinel collections. We could provide "hashicorp" here instead
* of `null`, but that might result in unexpected filtering out if
* authors use a different `theme` for any Sentinel collection.
*/
const theme =
options.product.slug === 'sentinel' ? null : options.product.slug
const allCollections = await fetchAllCollectionsByProduct(
options.product,
theme
)
collections = [...allCollections]
} else {
const limit = options?.limit?.toString()
Expand Down

0 comments on commit 903686d

Please sign in to comment.