66 MethodPaths ,
77} from "../models/operations/getcodesamples.js" ;
88import { OperationId } from "../types/custom.js" ;
9- import { useCodeSampleState } from "./code-sample.state.js" ;
9+ import { getMethodPath , useCodeSampleState } from "./code-sample.state.js" ;
1010import classes from "./code-sample.styles.js" ;
1111import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
1212import codehikeTheme from "./codehike/theme.js" ;
@@ -20,15 +20,15 @@ import {
2020} from "./titles.js" ;
2121import { prettyLanguageName } from "./utils.js" ;
2222import { Selector } from "./selector" ;
23+ import { UsageSnippet } from "../models/components" ;
2324
2425export type CodeSamplesViewerProps = {
2526 /** Whether the code snippet should be copyable. */
2627 copyable ?: boolean ;
2728
2829 /** Default language to show in the code playground. If not found in the snippets, the first one will be used. */
2930 defaultLanguage ?: string ;
30- /** Default operation to show in the code playground. If not found in the snippets, the first one will be used. */
31- defaultOperation ?: string ;
31+
3232 /**
3333 * The color mode for the code playground. If "system", the component will
3434 * detect the system color scheme automagically.
@@ -40,12 +40,13 @@ export type CodeSamplesViewerProps = {
4040 * A component to render as the snippet title in the upper-right corner of
4141 * the component. Receives data about the selected code sample. The library
4242 * comes pre-packaged with some sensible options.
43+ * If set to false, no title bar will be shown.
4344 *
4445 * @see CodeSampleTitle
4546 * @see CodeSampleFilenameTitle
4647 * @default CodeSampleMethodTitle
4748 */
48- title ?: CodeSampleTitleComponent | React . ReactNode | string | "none" ;
49+ title ?: CodeSampleTitleComponent | React . ReactNode | string | false ;
4950 /** The operations to get code samples for. If only one is provided, no selector will be shown.
5051 * Can be queried by either operationId or method+path.
5152 */
@@ -68,7 +69,6 @@ export function CodeSamplesViewer({
6869 theme = "system" ,
6970 title = CodeSampleFilenameTitle ,
7071 defaultLanguage,
71- defaultOperation,
7272 operations,
7373 copyable,
7474 client : clientProp ,
@@ -93,7 +93,7 @@ export function CodeSamplesViewer({
9393 // On mount, select the defaults
9494 useEffect ( ( ) => {
9595 if ( ! state . snippets || state . status !== "success" ) return ;
96- selectSnippet ( { language : defaultLanguage , operationId : defaultOperation } ) ;
96+ selectSnippet ( { language : defaultLanguage } ) ;
9797 } , [ state . status ] ) ;
9898
9999 const systemColorMode = useSystemColorMode ( ) ;
@@ -110,12 +110,28 @@ export function CodeSamplesViewer({
110110 ] ;
111111 } , [ state . snippets ] ) ;
112112
113- const operationIds : string [ ] = useMemo ( ( ) => {
114- return [
115- ...new Set ( state . snippets ?. map ( ( { raw } ) => raw . operationId ) ?? [ ] ) ,
116- ] ;
113+ const getOperationKey = ( snippet : UsageSnippet | undefined ) : string => {
114+ let { operationId } = snippet ;
115+ const methodPathDisplay = getMethodPath ( snippet ) ;
116+ if ( ! operationId ) {
117+ operationId = methodPathDisplay ;
118+ }
119+ return operationId ;
120+ } ;
121+
122+ // We need this methodAndPath stuff because not all snippets will have operation ids
123+ // For the selector, we try to show operation ID but fall back on method+path if it's missing
124+ const operationIdToMethodAndPath : Record < string , string > = useMemo ( ( ) => {
125+ return Object . fromEntries (
126+ state . snippets ?. map ( ( { raw } ) => [
127+ getOperationKey ( raw ) ,
128+ getMethodPath ( raw ) ,
129+ ] ) ?? [ ] ,
130+ ) ;
117131 } , [ state . snippets ] ) ;
118132
133+ const operationIds = Object . keys ( operationIdToMethodAndPath ) ;
134+
119135 return (
120136 < LazyMotion strict features = { domMax } >
121137 < div
@@ -128,7 +144,7 @@ export function CodeSamplesViewer({
128144 } }
129145 className = { `${ classes . root } ${ className ?? "" } ` }
130146 >
131- { title !== "none" && (
147+ { title !== false && (
132148 < div className = { classes . heading } >
133149 < CodeSampleTitle
134150 component = { title }
@@ -143,9 +159,13 @@ export function CodeSamplesViewer({
143159 ) }
144160 { state . status === "success" && operationIds . length > 1 && (
145161 < Selector
146- value = { state . selectedSnippet ?. raw . operationId }
162+ value = { getOperationKey ( state . selectedSnippet ?. raw ) }
147163 values = { operationIds }
148- onChange = { ( operationId ) => selectSnippet ( { operationId } ) }
164+ onChange = { ( operationId : string ) =>
165+ selectSnippet ( {
166+ methodPath : operationIdToMethodAndPath [ operationId ] ,
167+ } )
168+ }
149169 className = { classes . selector }
150170 />
151171 ) }
@@ -155,7 +175,7 @@ export function CodeSamplesViewer({
155175 state . selectedSnippet ?. raw . language ,
156176 ) }
157177 values = { languages }
158- onChange = { ( language ) => selectSnippet ( { language } ) }
178+ onChange = { ( language : string ) => selectSnippet ( { language } ) }
159179 className = { classes . selector }
160180 />
161181 ) }
0 commit comments