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" ;
710import classes from "./code-sample.styles.js" ;
8- import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
11+ import { CodeViewer , ErrorDisplay } from "./code-viewer.js" ;
912import 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" ;
1724
1825export type CodeSamplesViewerProps = {
1926 /** Whether the code snippet should be copyable. */
@@ -66,35 +73,35 @@ export type CodeSamplesViewerProps = {
6673} ;
6774
6875export 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 ) {
8087 const requestParams : GetCodeSamplesRequest = React . useMemo ( ( ) => {
8188 if ( typeof operations ?. [ 0 ] === "string" )
82- return { operationIds : operations as OperationId [ ] } ;
89+ return { operationIds : operations as OperationId [ ] } ;
8390 else if ( operations ?. [ 0 ] ?. method && operations [ 0 ] . path )
84- return { methodPaths : operations as MethodPaths [ ] } ;
91+ return { methodPaths : operations as MethodPaths [ ] } ;
8592
8693 return { } ;
8794 } , [ operations ] ) ;
8895
89- const { state, selectSnippet} = useCodeSampleState ( {
96+ const { state, selectSnippet } = useCodeSampleState ( {
9097 client : clientProp ,
9198 requestParams,
9299 } ) ;
93100
94101 // On mount, select the defaults
95102 useEffect ( ( ) => {
96103 if ( ! state . snippets || state . status !== "success" ) return ;
97- selectSnippet ( { language : defaultLanguage } ) ;
104+ selectSnippet ( { language : defaultLanguage } ) ;
98105 } , [ state . status ] ) ;
99106
100107 const systemColorMode = useSystemColorMode ( ) ;
@@ -106,13 +113,13 @@ export function CodeSamplesViewer({
106113 const languages : string [ ] = useMemo ( ( ) => {
107114 return [
108115 ...new Set (
109- state . snippets ?. map ( ( { raw} ) => prettyLanguageName ( raw . language ) ) ,
116+ state . snippets ?. map ( ( { raw } ) => prettyLanguageName ( raw . language ) ) ,
110117 ) ,
111118 ] ;
112119 } , [ state . snippets ] ) ;
113120
114121 const getOperationKey = ( snippet : UsageSnippet | undefined ) : string => {
115- let { operationId} = snippet ;
122+ let { operationId } = snippet ;
116123 const methodPathDisplay = getMethodPath ( snippet ) ;
117124 if ( ! operationId ) {
118125 operationId = methodPathDisplay ;
@@ -124,7 +131,7 @@ export function CodeSamplesViewer({
124131 // For the selector, we try to show operation ID but fall back on method+path if it's missing
125132 const operationIdToMethodAndPath : Record < string , string > = useMemo ( ( ) => {
126133 return Object . fromEntries (
127- state . snippets ?. map ( ( { raw} ) => [
134+ state . snippets ?. map ( ( { raw } ) => [
128135 getOperationKey ( raw ) ,
129136 getMethodPath ( raw ) ,
130137 ] ) ?? [ ] ,
@@ -171,10 +178,10 @@ export function CodeSamplesViewer({
171178 status = { state . status }
172179 data = { state . selectedSnippet ?. raw }
173180 />
174- < div style = { { display : "flex" , gap : "0.75rem" } } >
181+ < div style = { { display : "flex" , gap : "0.75rem" } } >
175182 { state . status === "loading" && (
176- < div style = { { width : "180px" } } >
177- < LanguageSelectorSkeleton />
183+ < div style = { { width : "180px" } } >
184+ < LanguageSelectorSkeleton />
178185 </ div >
179186 ) }
180187 { state . status === "success" && operationIds . length > 1 && (
@@ -195,7 +202,7 @@ export function CodeSamplesViewer({
195202 state . selectedSnippet ?. raw . language ,
196203 ) }
197204 values = { languages }
198- onChange = { ( language : string ) => selectSnippet ( { language} ) }
205+ onChange = { ( language : string ) => selectSnippet ( { language } ) }
199206 className = { classes . selector }
200207 />
201208 ) }
@@ -204,10 +211,10 @@ export function CodeSamplesViewer({
204211 ) }
205212 < div className = { classes . codeContainer } >
206213 { state . status === "success" && copyable && (
207- < CopyButton code = { state . selectedSnippet . code } />
214+ < CopyButton code = { state . selectedSnippet . code } />
208215 ) }
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 } /> }
211218 { state . status === "success" && (
212219 < CodeViewer
213220 status = { state . status }
0 commit comments