6
6
MethodPaths ,
7
7
} from "../models/operations/getcodesamples.js" ;
8
8
import { OperationId } from "../types/custom.js" ;
9
- import { useCodeSampleState } from "./code-sample.state.js" ;
9
+ import { getMethodPath , useCodeSampleState } from "./code-sample.state.js" ;
10
10
import classes from "./code-sample.styles.js" ;
11
11
import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
12
12
import codehikeTheme from "./codehike/theme.js" ;
@@ -20,15 +20,15 @@ import {
20
20
} from "./titles.js" ;
21
21
import { prettyLanguageName } from "./utils.js" ;
22
22
import { Selector } from "./selector" ;
23
+ import { UsageSnippet } from "../models/components" ;
23
24
24
25
export type CodeSamplesViewerProps = {
25
26
/** Whether the code snippet should be copyable. */
26
27
copyable ?: boolean ;
27
28
28
29
/** Default language to show in the code playground. If not found in the snippets, the first one will be used. */
29
30
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
+
32
32
/**
33
33
* The color mode for the code playground. If "system", the component will
34
34
* detect the system color scheme automagically.
@@ -40,12 +40,13 @@ export type CodeSamplesViewerProps = {
40
40
* A component to render as the snippet title in the upper-right corner of
41
41
* the component. Receives data about the selected code sample. The library
42
42
* comes pre-packaged with some sensible options.
43
+ * If set to false, no title bar will be shown.
43
44
*
44
45
* @see CodeSampleTitle
45
46
* @see CodeSampleFilenameTitle
46
47
* @default CodeSampleMethodTitle
47
48
*/
48
- title ?: CodeSampleTitleComponent | React . ReactNode | string | "none" ;
49
+ title ?: CodeSampleTitleComponent | React . ReactNode | string | false ;
49
50
/** The operations to get code samples for. If only one is provided, no selector will be shown.
50
51
* Can be queried by either operationId or method+path.
51
52
*/
@@ -68,7 +69,6 @@ export function CodeSamplesViewer({
68
69
theme = "system" ,
69
70
title = CodeSampleFilenameTitle ,
70
71
defaultLanguage,
71
- defaultOperation,
72
72
operations,
73
73
copyable,
74
74
client : clientProp ,
@@ -93,7 +93,7 @@ export function CodeSamplesViewer({
93
93
// On mount, select the defaults
94
94
useEffect ( ( ) => {
95
95
if ( ! state . snippets || state . status !== "success" ) return ;
96
- selectSnippet ( { language : defaultLanguage , operationId : defaultOperation } ) ;
96
+ selectSnippet ( { language : defaultLanguage } ) ;
97
97
} , [ state . status ] ) ;
98
98
99
99
const systemColorMode = useSystemColorMode ( ) ;
@@ -110,12 +110,28 @@ export function CodeSamplesViewer({
110
110
] ;
111
111
} , [ state . snippets ] ) ;
112
112
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
+ ) ;
117
131
} , [ state . snippets ] ) ;
118
132
133
+ const operationIds = Object . keys ( operationIdToMethodAndPath ) ;
134
+
119
135
return (
120
136
< LazyMotion strict features = { domMax } >
121
137
< div
@@ -128,7 +144,7 @@ export function CodeSamplesViewer({
128
144
} }
129
145
className = { `${ classes . root } ${ className ?? "" } ` }
130
146
>
131
- { title !== "none" && (
147
+ { title !== false && (
132
148
< div className = { classes . heading } >
133
149
< CodeSampleTitle
134
150
component = { title }
@@ -143,9 +159,13 @@ export function CodeSamplesViewer({
143
159
) }
144
160
{ state . status === "success" && operationIds . length > 1 && (
145
161
< Selector
146
- value = { state . selectedSnippet ?. raw . operationId }
162
+ value = { getOperationKey ( state . selectedSnippet ?. raw ) }
147
163
values = { operationIds }
148
- onChange = { ( operationId ) => selectSnippet ( { operationId } ) }
164
+ onChange = { ( operationId : string ) =>
165
+ selectSnippet ( {
166
+ methodPath : operationIdToMethodAndPath [ operationId ] ,
167
+ } )
168
+ }
149
169
className = { classes . selector }
150
170
/>
151
171
) }
@@ -155,7 +175,7 @@ export function CodeSamplesViewer({
155
175
state . selectedSnippet ?. raw . language ,
156
176
) }
157
177
values = { languages }
158
- onChange = { ( language ) => selectSnippet ( { language } ) }
178
+ onChange = { ( language : string ) => selectSnippet ( { language } ) }
159
179
className = { classes . selector }
160
180
/>
161
181
) }
0 commit comments