-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(openapi): revise default version logic (#2180)
* feat: implement find-default-version * fix: use findDefaultVersion * chore: clean up unused imports * fix: avoid potential undefined in static props * chore: rm unused find-latest-stable-version * style: clearer distinction in comment
- Loading branch information
Showing
4 changed files
with
43 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/views/open-api-docs-view/utils/find-default-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { OpenApiDocsVersionData } from '../types' | ||
import { sortDateVersionData } from './sort-date-version-data' | ||
|
||
/** | ||
* Given an array of version data, | ||
* Return the default version that should be shown. | ||
* | ||
* - If there is exactly one version, we treat it as the default version. | ||
* - If there are multiple versions, but no `stable` version, we'll show | ||
* the very latest version as the default version (even if not `stable`). | ||
* - If there are multiple versions, and at least one `stable` version, | ||
* we'll show the latest `stable` version as the default version. | ||
* | ||
* Note: only supports date-based version formats, for example "2023-01-15". | ||
* We'd need to update the sort logic in order to support other formats. | ||
*/ | ||
export function findDefaultVersion( | ||
versionData: OpenApiDocsVersionData[] | ||
): OpenApiDocsVersionData { | ||
// If we have exactly one version, we'll show that as the default. | ||
if (versionData.length === 1) { | ||
return versionData[0] | ||
} | ||
// Sort versions in descending order | ||
const versionsDescending = sortDateVersionData(versionData) | ||
// Ideally, we'll show the latest 'stable' release as the default. | ||
const latestStableVersion = versionsDescending.find( | ||
(v) => v.releaseStage === 'stable' | ||
) | ||
// Fall back to the latest version (any stage!) if there's no 'stable' version | ||
const defaultVersion = latestStableVersion || versionsDescending[0] | ||
// Return the default version | ||
return defaultVersion | ||
} |
29 changes: 0 additions & 29 deletions
29
src/views/open-api-docs-view/utils/find-latest-stable-version.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e94df40
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dev-portal – ./
dev-portal-hashicorp.vercel.app
docs.hashicorp.com
dev-portal-git-main-hashicorp.vercel.app
developer.hashicorp.com