1717 * under the License.
1818 */
1919
20- import React , { useState , useEffect } from 'react' ;
20+ import React , { useState , useRef , useEffect } from 'react' ;
2121import SwaggerUI from 'swagger-ui-react' ;
2222import 'swagger-ui-react/swagger-ui.css' ;
2323import useBaseUrl from '@docusaurus/useBaseUrl' ;
@@ -26,23 +26,20 @@ import styles from './styles.module.css';
2626export default function SwaggerUIComponent ( { spec, defaultServer } ) {
2727 const specUrl = useBaseUrl ( spec ) ;
2828 const [ serverUrl , setServerUrl ] = useState ( defaultServer || 'http://localhost:9888' ) ;
29- const [ specData , setSpecData ] = useState ( null ) ;
29+ const swaggerSystemRef = useRef ( null ) ;
3030
31+ // Update the server URL in Swagger whenever serverUrl changes
3132 useEffect ( ( ) => {
32- // Fetch the spec and modify the servers array
33- fetch ( specUrl )
34- . then ( response => response . text ( ) )
35- . then ( text => {
36- // Parse YAML to JSON (simple approach for this case)
37- // Since swagger-ui-react can handle YAML, we'll pass the URL
38- // but configure servers via the spec modification
39- return fetch ( specUrl ) . then ( r => r . json ( ) . catch ( ( ) => {
40- // If JSON parsing fails, it's YAML, use the URL directly
41- return null ;
42- } ) ) ;
43- } )
44- . catch ( ( ) => null ) ;
45- } , [ specUrl ] ) ;
33+ if ( swaggerSystemRef . current ) {
34+ const spec = swaggerSystemRef . current . getState ( ) . getIn ( [ 'spec' , 'json' ] ) ;
35+ if ( spec ) {
36+ swaggerSystemRef . current . specActions . updateJsonSpec ( {
37+ ...spec . toJS ( ) ,
38+ servers : [ { url : serverUrl , description : 'Configured Recon Server' } ]
39+ } ) ;
40+ }
41+ }
42+ } , [ serverUrl ] ) ;
4643
4744 return (
4845 < div className = { styles . swaggerWrapper } >
@@ -68,7 +65,9 @@ export default function SwaggerUIComponent({ spec, defaultServer }) {
6865 defaultModelsExpandDepth = { 1 }
6966 defaultModelExpandDepth = { 1 }
7067 onComplete = { ( system ) => {
71- // Override the servers in the spec with the user-configured URL
68+ // Store the system reference for later updates
69+ swaggerSystemRef . current = system ;
70+ // Set initial server URL
7271 const spec = system . getState ( ) . getIn ( [ 'spec' , 'json' ] ) ;
7372 if ( spec ) {
7473 system . specActions . updateJsonSpec ( {
0 commit comments