@@ -16,20 +16,14 @@ import CodeTabs from "@theme/ApiExplorer/CodeTabs";
16
16
import { useTypedSelector } from "@theme/ApiItem/hooks" ;
17
17
import merge from "lodash/merge" ;
18
18
19
- export interface Language {
20
- highlight : string ;
21
- language : string ;
22
- logoClass : string ;
23
- variant : string ;
24
- variants : string [ ] ;
25
- options : { [ key : string ] : boolean } ;
26
- source ?: string ;
27
- }
19
+ import { CodeSample , Language } from "./code-snippets-types" ;
20
+ import { mergeCodeSampleLanguage } from "./languages" ;
28
21
29
22
export const languageSet : Language [ ] = [
30
23
{
31
24
highlight : "bash" ,
32
25
language : "curl" ,
26
+ codeSampleLanguage : "Shell" ,
33
27
logoClass : "bash" ,
34
28
options : {
35
29
longFormat : false ,
@@ -42,6 +36,7 @@ export const languageSet: Language[] = [
42
36
{
43
37
highlight : "python" ,
44
38
language : "python" ,
39
+ codeSampleLanguage : "Python" ,
45
40
logoClass : "python" ,
46
41
options : {
47
42
followRedirect : true ,
@@ -53,6 +48,7 @@ export const languageSet: Language[] = [
53
48
{
54
49
highlight : "go" ,
55
50
language : "go" ,
51
+ codeSampleLanguage : "Go" ,
56
52
logoClass : "go" ,
57
53
options : {
58
54
followRedirect : true ,
@@ -64,18 +60,20 @@ export const languageSet: Language[] = [
64
60
{
65
61
highlight : "javascript" ,
66
62
language : "nodejs" ,
63
+ codeSampleLanguage : "JavaScript" ,
67
64
logoClass : "nodejs" ,
68
65
options : {
69
66
ES6_enabled : true ,
70
67
followRedirect : true ,
71
68
trimRequestBody : true ,
72
69
} ,
73
70
variant : "axios" ,
74
- variants : [ "axios" , "native" , "request" , "unirest" ] ,
71
+ variants : [ "axios" , "native" ] ,
75
72
} ,
76
73
{
77
74
highlight : "ruby" ,
78
75
language : "ruby" ,
76
+ codeSampleLanguage : "Ruby" ,
79
77
logoClass : "ruby" ,
80
78
options : {
81
79
followRedirect : true ,
@@ -87,6 +85,7 @@ export const languageSet: Language[] = [
87
85
{
88
86
highlight : "csharp" ,
89
87
language : "csharp" ,
88
+ codeSampleLanguage : "C#" ,
90
89
logoClass : "csharp" ,
91
90
options : {
92
91
followRedirect : true ,
@@ -98,6 +97,7 @@ export const languageSet: Language[] = [
98
97
{
99
98
highlight : "php" ,
100
99
language : "php" ,
100
+ codeSampleLanguage : "PHP" ,
101
101
logoClass : "php" ,
102
102
options : {
103
103
followRedirect : true ,
@@ -109,6 +109,7 @@ export const languageSet: Language[] = [
109
109
{
110
110
highlight : "java" ,
111
111
language : "java" ,
112
+ codeSampleLanguage : "Java" ,
112
113
logoClass : "java" ,
113
114
options : {
114
115
followRedirect : true ,
@@ -120,6 +121,7 @@ export const languageSet: Language[] = [
120
121
{
121
122
highlight : "powershell" ,
122
123
language : "powershell" ,
124
+ codeSampleLanguage : "PowerShell" ,
123
125
logoClass : "powershell" ,
124
126
options : {
125
127
followRedirect : true ,
@@ -132,10 +134,10 @@ export const languageSet: Language[] = [
132
134
133
135
export interface Props {
134
136
postman : sdk . Request ;
135
- codeSamples : any ; // TODO: Type this...
137
+ codeSamples : CodeSample [ ] ;
136
138
}
137
139
138
- function CodeTab ( { children, hidden, className, onClick } : any ) : JSX . Element {
140
+ function CodeTab ( { children, hidden, className } : any ) : JSX . Element {
139
141
return (
140
142
< div role = "tabpanel" className = { className } { ...{ hidden } } >
141
143
{ children }
@@ -165,7 +167,6 @@ function CodeSnippets({ postman, codeSamples }: Props) {
165
167
const langs = [
166
168
...( ( siteConfig ?. themeConfig ?. languageTabs as Language [ ] | undefined ) ??
167
169
languageSet ) ,
168
- ...codeSamples ,
169
170
] ;
170
171
171
172
// Filter languageSet by user-defined langs
@@ -176,14 +177,18 @@ function CodeSnippets({ postman, codeSamples }: Props) {
176
177
} ) ;
177
178
178
179
// Merge user-defined langs into languageSet
179
- const mergedLangs = merge ( filteredLanguageSet , langs ) ;
180
+ const mergedLangs = mergeCodeSampleLanguage (
181
+ merge ( filteredLanguageSet , langs ) ,
182
+ codeSamples
183
+ ) ;
180
184
181
185
// Read defaultLang from localStorage
182
186
const defaultLang : Language [ ] = mergedLangs . filter (
183
187
( lang ) =>
184
188
lang . language === localStorage . getItem ( "docusaurus.tab.code-samples" )
185
189
) ;
186
- const [ selectedVariant , setSelectedVariant ] = useState ( ) ;
190
+ const [ selectedVariant , setSelectedVariant ] = useState < string | undefined > ( ) ;
191
+ const [ selectedSample , setSelectedSample ] = useState < string | undefined > ( ) ;
187
192
const [ language , setLanguage ] = useState ( ( ) => {
188
193
// Return first index if only 1 user-defined language exists
189
194
if ( mergedLangs . length === 1 ) {
@@ -192,9 +197,23 @@ function CodeSnippets({ postman, codeSamples }: Props) {
192
197
// Fall back to language in localStorage or first user-defined language
193
198
return defaultLang [ 0 ] ?? mergedLangs [ 0 ] ;
194
199
} ) ;
195
- const [ codeText , setCodeText ] = useState ( "" ) ;
200
+ const [ codeText , setCodeText ] = useState < string > ( "" ) ;
201
+ const [ codeSampleCodeText , setCodeSampleCodeText ] = useState < string > ( "" ) ;
196
202
197
203
useEffect ( ( ) => {
204
+ // initial active language is custom code sample
205
+ if (
206
+ language &&
207
+ language . sample &&
208
+ language . samples &&
209
+ language . samplesSources
210
+ ) {
211
+ const sampleIndex = language . samples . findIndex (
212
+ ( smp ) => smp === language . sample
213
+ ) ;
214
+ setCodeSampleCodeText ( language . samplesSources [ sampleIndex ] ) ;
215
+ }
216
+
198
217
if ( language && ! ! language . options ) {
199
218
const postmanRequest = buildPostmanRequest ( postman , {
200
219
queryParams,
@@ -219,8 +238,6 @@ function CodeSnippets({ postman, codeSamples }: Props) {
219
238
setCodeText ( snippet ) ;
220
239
}
221
240
) ;
222
- } else if ( language && ! ! language . source ) {
223
- setCodeText ( language . source ) ;
224
241
} else if ( language && ! language . options ) {
225
242
const langSource = mergedLangs . filter (
226
243
( lang ) => lang . language === language . language
@@ -271,8 +288,8 @@ function CodeSnippets({ postman, codeSamples }: Props) {
271
288
auth ,
272
289
mergedLangs ,
273
290
] ) ;
274
-
275
- useEffect ( ( ) => {
291
+ // no dependencies was intentionlly set for this particular hook. it's safe as long as if conditions are set
292
+ useEffect ( function onSelectedVariantUpdate ( ) {
276
293
if ( selectedVariant && selectedVariant !== language . variant ) {
277
294
const postmanRequest = buildPostmanRequest ( postman , {
278
295
queryParams,
@@ -300,6 +317,22 @@ function CodeSnippets({ postman, codeSamples }: Props) {
300
317
}
301
318
} ) ;
302
319
320
+ // no dependencies was intentionlly set for this particular hook. it's safe as long as if conditions are set
321
+ // eslint-disable-next-line react-hooks/exhaustive-deps
322
+ useEffect ( function onSelectedSampleUpdate ( ) {
323
+ if (
324
+ language . samples &&
325
+ language . samplesSources &&
326
+ selectedSample &&
327
+ selectedSample !== language . sample
328
+ ) {
329
+ const sampleIndex = language . samples . findIndex (
330
+ ( smp ) => smp === selectedSample
331
+ ) ;
332
+ setCodeSampleCodeText ( language . samplesSources [ sampleIndex ] ) ;
333
+ }
334
+ } ) ;
335
+
303
336
if ( language === undefined ) {
304
337
return null ;
305
338
}
@@ -324,6 +357,46 @@ function CodeSnippets({ postman, codeSamples }: Props) {
324
357
className : `openapi-tabs__code-item--${ lang . logoClass } ` ,
325
358
} }
326
359
>
360
+ { lang . samples && (
361
+ < CodeTabs
362
+ className = "openapi-tabs__code-container-inner"
363
+ action = { {
364
+ setLanguage : setLanguage ,
365
+ setSelectedSample : setSelectedSample ,
366
+ } }
367
+ includeSample = { true }
368
+ currentLanguage = { lang . language }
369
+ defaultValue = { selectedSample }
370
+ lazy
371
+ >
372
+ { lang . samples . map ( ( sample , index ) => {
373
+ return (
374
+ < CodeTab
375
+ value = { sample }
376
+ label = {
377
+ lang . samplesLabels
378
+ ? lang . samplesLabels [ index ]
379
+ : sample
380
+ }
381
+ key = { `${ lang . language } -${ lang . sample } ` }
382
+ attributes = { {
383
+ className : `openapi-tabs__code-item--sample` ,
384
+ } }
385
+ >
386
+ { /* @ts -ignore */ }
387
+ < ApiCodeBlock
388
+ language = { lang . highlight }
389
+ className = "openapi-explorer__code-block"
390
+ showLineNumbers = { true }
391
+ >
392
+ { codeSampleCodeText }
393
+ </ ApiCodeBlock >
394
+ </ CodeTab >
395
+ ) ;
396
+ } ) }
397
+ </ CodeTabs >
398
+ ) }
399
+
327
400
< CodeTabs
328
401
className = "openapi-tabs__code-container-inner"
329
402
action = { {
@@ -335,7 +408,7 @@ function CodeSnippets({ postman, codeSamples }: Props) {
335
408
defaultValue = { selectedVariant }
336
409
lazy
337
410
>
338
- { lang . variants . map ( ( variant ) => {
411
+ { lang . variants . map ( ( variant , index ) => {
339
412
return (
340
413
< CodeTab
341
414
value = { variant . toLowerCase ( ) }
0 commit comments