'use client'; import clsx from 'classnames'; import type { Key } from 'react-aria'; import { OpenAPISelect, OpenAPISelectItem, useSelectState } from './OpenAPISelect'; import { StaticSection } from './StaticSection'; import { createStateKey, getStatusCodeClassName } from './utils'; type OpenAPIResponseExampleItem = OpenAPISelectItem & { statusCode: string; body: React.ReactNode; }; /** * Get the state of the response examples select. */ export function useResponseExamplesState(initialKey: Key = 'default') { return useSelectState(getResponseExampleStateKey(), initialKey); } export function OpenAPIResponseExampleContent(props: { items: OpenAPIResponseExampleItem[]; selectIcon?: React.ReactNode; }) { const { items, selectIcon } = props; return ( } className="openapi-response-examples" > ); } function OpenAPIResponseExampleHeader(props: { items: OpenAPIResponseExampleItem[]; selectIcon?: React.ReactNode; }) { const { items, selectIcon } = props; if (items.length === 1) { const item = items[0]; if (!item) { return null; } return ( ); } return ( {items.map((item) => ( ))} ); } function OpenAPIResponseExampleItem(props: { item: OpenAPIResponseExampleItem; }) { const { item } = props; return ( <> {item.statusCode} {item.label} ); } function OpenAPIResponseExampleBody(props: { items: OpenAPIResponseExampleItem[]; }) { const { items } = props; const state = useResponseExamplesState(items[0]?.key); const selectedItem = items.find((item) => item.key === state.key) ?? items[0]; if (!selectedItem) { return null; } return
{selectedItem.body}
; } /** * Return the state key for the response examples. Not scoped to a block so the selected response * stays in sync across every operation on the page (like the code sample language selector). */ function getResponseExampleStateKey() { return createStateKey('openapi-responses'); }