1
- import { domMax , LazyMotion } from "motion/react" ;
2
- import React , { useEffect , useMemo } from "react" ;
3
- import { SpeakeasyCodeSamplesCore } from "../core.js" ;
4
- import { GetCodeSamplesRequest , MethodPaths , } from "../models/operations/getcodesamples.js" ;
5
- import { OperationId } from "../types/custom.js" ;
6
- import { getMethodPath , useCodeSampleState } from "./code-sample.state.js" ;
1
+ import { domMax , LazyMotion } from "motion/react" ;
2
+ import React , { useEffect , useMemo } from "react" ;
3
+ import { SpeakeasyCodeSamplesCore } from "../core.js" ;
4
+ import {
5
+ GetCodeSamplesRequest ,
6
+ MethodPaths ,
7
+ } from "../models/operations/getcodesamples.js" ;
8
+ import { OperationId } from "../types/custom.js" ;
9
+ import { getMethodPath , useCodeSampleState } from "./code-sample.state.js" ;
7
10
import classes from "./code-sample.styles.js" ;
8
- import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
11
+ import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
9
12
import codehikeTheme from "./codehike/theme.js" ;
10
- import { CopyButton } from "./copy-button.js" ;
11
- import { LanguageSelectorSkeleton , LoadingSkeleton } from "./skeleton.js" ;
12
- import { getCssVars , useSystemColorMode } from "./styles.js" ;
13
- import { CodeSampleFilenameTitle , CodeSampleTitle , type CodeSampleTitleComponent , } from "./titles.js" ;
14
- import { prettyLanguageName } from "./utils.js" ;
15
- import { Selector } from "./selector" ;
16
- import { UsageSnippet } from "../models/components" ;
13
+ import { CopyButton } from "./copy-button.js" ;
14
+ import { LanguageSelectorSkeleton , LoadingSkeleton } from "./skeleton.js" ;
15
+ import { getCssVars , useSystemColorMode } from "./styles.js" ;
16
+ import {
17
+ CodeSampleFilenameTitle ,
18
+ CodeSampleTitle ,
19
+ type CodeSampleTitleComponent ,
20
+ } from "./titles.js" ;
21
+ import { prettyLanguageName } from "./utils.js" ;
22
+ import { Selector } from "./selector" ;
23
+ import { UsageSnippet } from "../models/components" ;
17
24
18
25
export type CodeSamplesViewerProps = {
19
26
/** Whether the code snippet should be copyable. */
@@ -66,35 +73,35 @@ export type CodeSamplesViewerProps = {
66
73
} ;
67
74
68
75
export function CodeSamplesViewer ( {
69
- theme = "system" ,
70
- title = CodeSampleFilenameTitle ,
71
- defaultLanguage,
72
- operations,
73
- copyable,
74
- client : clientProp ,
75
- style,
76
- codeWindowStyle,
77
- fixedHeight,
78
- className,
79
- } : CodeSamplesViewerProps ) {
76
+ theme = "system" ,
77
+ title = CodeSampleFilenameTitle ,
78
+ defaultLanguage,
79
+ operations,
80
+ copyable,
81
+ client : clientProp ,
82
+ style,
83
+ codeWindowStyle,
84
+ fixedHeight,
85
+ className,
86
+ } : CodeSamplesViewerProps ) {
80
87
const requestParams : GetCodeSamplesRequest = React . useMemo ( ( ) => {
81
88
if ( typeof operations ?. [ 0 ] === "string" )
82
- return { operationIds : operations as OperationId [ ] } ;
89
+ return { operationIds : operations as OperationId [ ] } ;
83
90
else if ( operations ?. [ 0 ] ?. method && operations [ 0 ] . path )
84
- return { methodPaths : operations as MethodPaths [ ] } ;
91
+ return { methodPaths : operations as MethodPaths [ ] } ;
85
92
86
93
return { } ;
87
94
} , [ operations ] ) ;
88
95
89
- const { state, selectSnippet} = useCodeSampleState ( {
96
+ const { state, selectSnippet } = useCodeSampleState ( {
90
97
client : clientProp ,
91
98
requestParams,
92
99
} ) ;
93
100
94
101
// On mount, select the defaults
95
102
useEffect ( ( ) => {
96
103
if ( ! state . snippets || state . status !== "success" ) return ;
97
- selectSnippet ( { language : defaultLanguage } ) ;
104
+ selectSnippet ( { language : defaultLanguage } ) ;
98
105
} , [ state . status ] ) ;
99
106
100
107
const systemColorMode = useSystemColorMode ( ) ;
@@ -106,13 +113,13 @@ export function CodeSamplesViewer({
106
113
const languages : string [ ] = useMemo ( ( ) => {
107
114
return [
108
115
...new Set (
109
- state . snippets ?. map ( ( { raw} ) => prettyLanguageName ( raw . language ) ) ,
116
+ state . snippets ?. map ( ( { raw } ) => prettyLanguageName ( raw . language ) ) ,
110
117
) ,
111
118
] ;
112
119
} , [ state . snippets ] ) ;
113
120
114
121
const getOperationKey = ( snippet : UsageSnippet | undefined ) : string => {
115
- let { operationId} = snippet ;
122
+ let { operationId } = snippet ;
116
123
const methodPathDisplay = getMethodPath ( snippet ) ;
117
124
if ( ! operationId ) {
118
125
operationId = methodPathDisplay ;
@@ -124,7 +131,7 @@ export function CodeSamplesViewer({
124
131
// For the selector, we try to show operation ID but fall back on method+path if it's missing
125
132
const operationIdToMethodAndPath : Record < string , string > = useMemo ( ( ) => {
126
133
return Object . fromEntries (
127
- state . snippets ?. map ( ( { raw} ) => [
134
+ state . snippets ?. map ( ( { raw } ) => [
128
135
getOperationKey ( raw ) ,
129
136
getMethodPath ( raw ) ,
130
137
] ) ?? [ ] ,
@@ -171,10 +178,10 @@ export function CodeSamplesViewer({
171
178
status = { state . status }
172
179
data = { state . selectedSnippet ?. raw }
173
180
/>
174
- < div style = { { display : "flex" , gap : "0.75rem" } } >
181
+ < div style = { { display : "flex" , gap : "0.75rem" } } >
175
182
{ state . status === "loading" && (
176
- < div style = { { width : "180px" } } >
177
- < LanguageSelectorSkeleton />
183
+ < div style = { { width : "180px" } } >
184
+ < LanguageSelectorSkeleton />
178
185
</ div >
179
186
) }
180
187
{ state . status === "success" && operationIds . length > 1 && (
@@ -195,7 +202,7 @@ export function CodeSamplesViewer({
195
202
state . selectedSnippet ?. raw . language ,
196
203
) }
197
204
values = { languages }
198
- onChange = { ( language : string ) => selectSnippet ( { language} ) }
205
+ onChange = { ( language : string ) => selectSnippet ( { language } ) }
199
206
className = { classes . selector }
200
207
/>
201
208
) }
@@ -204,10 +211,10 @@ export function CodeSamplesViewer({
204
211
) }
205
212
< div className = { classes . codeContainer } >
206
213
{ state . status === "success" && copyable && (
207
- < CopyButton code = { state . selectedSnippet . code } />
214
+ < CopyButton code = { state . selectedSnippet . code } />
208
215
) }
209
- { state . status === "loading" && < LoadingSkeleton /> }
210
- { state . status === "error" && < ErrorDisplay error = { state . error } /> }
216
+ { state . status === "loading" && < LoadingSkeleton /> }
217
+ { state . status === "error" && < ErrorDisplay error = { state . error } /> }
211
218
{ state . status === "success" && (
212
219
< CodeViewer
213
220
status = { state . status }
0 commit comments