-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAPI V2 - implement landing content (#2606)
* port over components from existing view * implement landing-content to match upstream * add demo data to preview
- Loading branch information
Showing
15 changed files
with
641 additions
and
46 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
16 changes: 16 additions & 0 deletions
16
...-view-v2/components/landing-content/components/description-mdx/description-mdx.module.css
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,16 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
.root { | ||
composes: hds-typography-body-300 from global; | ||
|
||
& > *:first-child { | ||
margin-top: 0; | ||
} | ||
|
||
& > *:last-child { | ||
margin-bottom: 0; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ews/open-api-docs-view-v2/components/landing-content/components/description-mdx/index.tsx
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,60 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { MDXRemote, MDXRemoteSerializeResult } from 'lib/next-mdx-remote' | ||
import { | ||
MdxA, | ||
MdxOrderedList, | ||
MdxUnorderedList, | ||
MdxListItem, | ||
MdxTable, | ||
MdxH1, | ||
MdxH2, | ||
MdxH3, | ||
MdxH4, | ||
MdxH5, | ||
MdxH6, | ||
MdxP, | ||
MdxInlineCode, | ||
MdxBlockquote, | ||
MdxPre, | ||
} from 'components/dev-dot-content/mdx-components' | ||
import s from './description-mdx.module.css' | ||
|
||
type MdxComponents = Record<string, (props: unknown) => JSX.Element> | ||
|
||
const DEFAULT_MDX_COMPONENTS: MdxComponents = { | ||
a: MdxA, | ||
blockquote: MdxBlockquote, | ||
h1: MdxH1, | ||
h2: MdxH2, | ||
h3: MdxH3, | ||
h4: MdxH4, | ||
h5: MdxH5, | ||
h6: MdxH6, | ||
inlineCode: MdxInlineCode, | ||
li: MdxListItem, | ||
ol: MdxOrderedList, | ||
p: MdxP, | ||
pre: MdxPre, | ||
table: MdxTable, | ||
ul: MdxUnorderedList, | ||
} | ||
|
||
/** | ||
* Renders CommonMark-compliant markdown content using our established set | ||
* of MDX custom components, via next-mdx-remote. | ||
*/ | ||
export function DescriptionMdx({ | ||
mdxRemoteProps, | ||
}: { | ||
mdxRemoteProps: MDXRemoteSerializeResult | ||
}) { | ||
return ( | ||
<div className={s.root}> | ||
<MDXRemote {...mdxRemoteProps} components={DEFAULT_MDX_COMPONENTS} /> | ||
</div> | ||
) | ||
} |
44 changes: 44 additions & 0 deletions
44
src/views/open-api-docs-view-v2/components/landing-content/components/status/index.tsx
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,44 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
// Third-party | ||
import { IconExternalLink16 } from '@hashicorp/flight-icons/svg-react/external-link-16' | ||
// Components | ||
import ServiceStatusBadge from 'components/service-status-badge' | ||
import StandaloneLink from 'components/standalone-link' | ||
// Local | ||
import { useServiceStatus } from './utils/use-service-status' | ||
// Types | ||
import { StatusIndicatorConfig } from 'views/open-api-docs-view/types' | ||
// Styles | ||
import s from './status.module.css' | ||
|
||
/** | ||
* Displays a `ServiceStatusBadge` with data from the provided `endpointUrl`, | ||
* alongside an external link to the provided `pageUrl`. | ||
* | ||
* We expect the `endpointUrl` to be a status-page component data URL, like: | ||
* - https://status.hashicorp.com/api/v2/components/{componentId}.json | ||
* | ||
* We expect the `pageUrl` to be a browser-friendly status page URL, such as: | ||
* - https://status.hashicorp.com | ||
*/ | ||
export function Status({ endpointUrl, pageUrl }: StatusIndicatorConfig) { | ||
const status = useServiceStatus(endpointUrl) | ||
return ( | ||
<div className={s.wrapper}> | ||
<ServiceStatusBadge status={status} /> | ||
<StandaloneLink | ||
text="Status" | ||
icon={<IconExternalLink16 />} | ||
iconPosition="trailing" | ||
color="secondary" | ||
href={pageUrl} | ||
size="small" | ||
opensInNewTab | ||
/> | ||
</div> | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
...iews/open-api-docs-view-v2/components/landing-content/components/status/status.module.css
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,9 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
.wrapper { | ||
display: flex; | ||
gap: 8px; | ||
} |
71 changes: 71 additions & 0 deletions
71
...api-docs-view-v2/components/landing-content/components/status/utils/use-service-status.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,71 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { useEffect, useState } from 'react' | ||
import { ServiceStatus } from 'components/service-status-badge' | ||
|
||
/** | ||
* Hook to use the StatusPage service status at a given endpoint. | ||
*/ | ||
export function useServiceStatus(endpointUrl: string) { | ||
const [status, setStatus] = useState<ServiceStatus>('loading') | ||
|
||
useEffect(() => { | ||
const asyncEffect = async () => { | ||
setStatus(await fetchServiceStatus(endpointUrl)) | ||
} | ||
asyncEffect() | ||
}, [endpointUrl]) | ||
|
||
return status | ||
} | ||
|
||
/** | ||
* Fetches the service status from a given StatusPage component | ||
* endpoint URL. URLs are expected to be something like: | ||
* https://status.hashicorp.com/api/v2/components/{componentId}.json | ||
* | ||
* If the retrieved data does not match the expected shape | ||
*/ | ||
async function fetchServiceStatus(url: string): Promise<ServiceStatus> { | ||
let status: ServiceStatus | ||
try { | ||
const data = (await fetchJson(url)) as ExpectedStatusPageData | ||
status = data.component?.status | ||
if (typeof status !== 'string') { | ||
throw new Error( | ||
`In the "useServiceStatus" hook, the status data did not match expected shape. Please ensure GET requests to the endpoint ${url} yield data with a string at "responseData.component.status".` | ||
) | ||
} | ||
} catch (e) { | ||
console.error(`Failed to parse valid status page data from ${url}.`) | ||
console.error(e) | ||
// Return 'unknown' as a fallback. | ||
status = 'unknown' | ||
} | ||
return status | ||
} | ||
|
||
/** | ||
* The shape of data we expect to receive from a provided `endpointUrl`. | ||
*/ | ||
interface ExpectedStatusPageData { | ||
component: { | ||
status: ServiceStatus | ||
} | ||
} | ||
|
||
/** | ||
* Fetch JSON data from a provided URL. | ||
*/ | ||
async function fetchJson(url: string) { | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
throw new Error( | ||
`HTTP error when fetching from ${url}. Status: ${response.status}` | ||
) | ||
} | ||
return await response.json() | ||
} |
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
22 changes: 0 additions & 22 deletions
22
src/views/open-api-docs-view-v2/components/landing-content/server.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.