-
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.
feat(openapi): support versioned docs (#2177)
* fix: width style issue with version switcher * feat: add versionSwitcherSlot in OpenApiOverview * feat: implement get-version-switcher-props * feat: generate versionSwitcherProps, pass to view * chore: rename prop for clarity * feat: add NoIndexTagIfVersioned * refactor: rethink and clarify missing versionData * fix: consolidated ApiDocsVersionData type use
- Loading branch information
Showing
10 changed files
with
188 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
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
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
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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
src/views/open-api-docs-view/utils/get-version-switcher-props.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,69 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
// Types | ||
import type { VersionSwitcherProps } from 'components/version-switcher' | ||
import type { ApiDocsVersionData } from 'lib/api-docs/types' | ||
|
||
/** | ||
* Given version data and other OpenAPI docs details, | ||
* Return version switcher dropdown props for use in `OpenApiDocsView`. | ||
* | ||
* Note: If there is only one version, we return null. | ||
*/ | ||
export function getVersionSwitcherProps({ | ||
projectName, | ||
versionData, | ||
targetVersion, | ||
defaultVersion, | ||
basePath, | ||
}: { | ||
projectName: string | ||
versionData: ApiDocsVersionData[] | ||
targetVersion: ApiDocsVersionData | ||
defaultVersion: ApiDocsVersionData | ||
basePath: string | ||
}): VersionSwitcherProps | null { | ||
// Return null early if we only have one version | ||
if (versionData.length === 1) { | ||
return null | ||
} | ||
|
||
// Otherwise, we have multiple versions, we need to build dropdown props | ||
const label = projectName | ||
|
||
// Each version becomes an option in the dropdown | ||
const options = versionData.map( | ||
({ versionId, releaseStage }: ApiDocsVersionData) => { | ||
/** | ||
* Determine the version label suffix to show. | ||
* - Default case is to show the version only, no (suffix) | ||
* - If this is the default version, show 'latest'. For information on | ||
* what "default version" means, see `findDefaultVersion`. | ||
* - If we have a defined releaseStage that isn't 'stable', show it | ||
*/ | ||
const isLatest = versionId === defaultVersion.versionId | ||
let versionLabelSuffix = '' | ||
if (isLatest) { | ||
versionLabelSuffix = ' (latest)' | ||
} else if (releaseStage && releaseStage !== 'stable') { | ||
versionLabelSuffix = ` (${releaseStage})` | ||
} | ||
// Construct the label to show in the dropdown | ||
const label = versionId + versionLabelSuffix | ||
// Construct the aria-label for the version dropdown. | ||
const ariaLabel = `Choose a version of the API docs for ${projectName}. Currently viewing version ${label}.` | ||
// Construct the `href` for this version, which is special if latest | ||
const href = isLatest ? basePath : `${basePath}/${versionId}` | ||
// Mark this version option as selected if it's the current option | ||
const isSelected = versionId === targetVersion.versionId | ||
// Return all the props | ||
return { ariaLabel, href, isLatest, isSelected, label } | ||
} | ||
) | ||
|
||
// Return the dropdown props | ||
return { label, options } | ||
} |
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
a8b442c
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-git-main-hashicorp.vercel.app
docs.hashicorp.com
dev-portal-hashicorp.vercel.app
developer.hashicorp.com