|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import React, { useState, useRef, useEffect } from 'react'; |
| 21 | +import SwaggerUI from 'swagger-ui-react'; |
| 22 | +import 'swagger-ui-react/swagger-ui.css'; |
| 23 | +import useBaseUrl from '@docusaurus/useBaseUrl'; |
| 24 | +import styles from './styles.module.css'; |
| 25 | + |
| 26 | +export default function SwaggerUIComponent({ spec, defaultServer }) { |
| 27 | + const specUrl = useBaseUrl(spec); |
| 28 | + const [serverUrl, setServerUrl] = useState(defaultServer || 'http://localhost:9888'); |
| 29 | + const [apiVersion, setApiVersion] = useState('v1'); |
| 30 | + const [appliedServerUrl, setAppliedServerUrl] = useState(defaultServer || 'http://localhost:9888'); |
| 31 | + const [appliedApiVersion, setAppliedApiVersion] = useState('v1'); |
| 32 | + const swaggerSystemRef = useRef(null); |
| 33 | + |
| 34 | + // Update the server URL in Swagger only when applied values change |
| 35 | + useEffect(() => { |
| 36 | + if (swaggerSystemRef.current) { |
| 37 | + const spec = swaggerSystemRef.current.getState().getIn(['spec', 'json']); |
| 38 | + if (spec) { |
| 39 | + // Construct full URL: {serverUrl}/api/{version}/ |
| 40 | + const baseUrl = appliedServerUrl.endsWith('/') ? appliedServerUrl.slice(0, -1) : appliedServerUrl; |
| 41 | + const fullServerUrl = `${baseUrl}/api/${appliedApiVersion}/`; |
| 42 | + swaggerSystemRef.current.specActions.updateJsonSpec({ |
| 43 | + ...spec.toJS(), |
| 44 | + servers: [{ url: fullServerUrl, description: 'Configured Recon Server' }] |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + }, [appliedServerUrl, appliedApiVersion]); |
| 49 | + |
| 50 | + const handleApply = () => { |
| 51 | + setAppliedServerUrl(serverUrl); |
| 52 | + setAppliedApiVersion(apiVersion); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className={styles.swaggerWrapper}> |
| 57 | + <div className={styles.serverConfig}> |
| 58 | + <label htmlFor="recon-server-url" className={styles.serverLabel}> |
| 59 | + Recon Server URL: |
| 60 | + </label> |
| 61 | + <input |
| 62 | + id="recon-server-url" |
| 63 | + type="text" |
| 64 | + value={serverUrl} |
| 65 | + onChange={(e) => setServerUrl(e.target.value)} |
| 66 | + placeholder="http://localhost:9888" |
| 67 | + className={styles.serverInput} |
| 68 | + /> |
| 69 | + <label htmlFor="recon-api-version" className={styles.serverLabel}> |
| 70 | + API Version: |
| 71 | + </label> |
| 72 | + <input |
| 73 | + id="recon-api-version" |
| 74 | + type="text" |
| 75 | + value={apiVersion} |
| 76 | + onChange={(e) => setApiVersion(e.target.value)} |
| 77 | + placeholder="v1" |
| 78 | + className={styles.versionInput} |
| 79 | + /> |
| 80 | + <button |
| 81 | + onClick={handleApply} |
| 82 | + className={styles.applyButton} |
| 83 | + type="button" |
| 84 | + > |
| 85 | + Apply |
| 86 | + </button> |
| 87 | + <span className={styles.serverHint}> |
| 88 | + (Default for Docker quick start. Change server/version and click Apply.) |
| 89 | + </span> |
| 90 | + </div> |
| 91 | + <SwaggerUI |
| 92 | + url={specUrl} |
| 93 | + docExpansion="list" |
| 94 | + defaultModelsExpandDepth={1} |
| 95 | + defaultModelExpandDepth={1} |
| 96 | + onComplete={(system) => { |
| 97 | + // Store the system reference for later updates |
| 98 | + swaggerSystemRef.current = system; |
| 99 | + // Set initial server URL with API version |
| 100 | + const spec = system.getState().getIn(['spec', 'json']); |
| 101 | + if (spec) { |
| 102 | + const baseUrl = appliedServerUrl.endsWith('/') ? appliedServerUrl.slice(0, -1) : appliedServerUrl; |
| 103 | + const fullServerUrl = `${baseUrl}/api/${appliedApiVersion}/`; |
| 104 | + system.specActions.updateJsonSpec({ |
| 105 | + ...spec.toJS(), |
| 106 | + servers: [{ url: fullServerUrl, description: 'Configured Recon Server' }] |
| 107 | + }); |
| 108 | + } |
| 109 | + }} |
| 110 | + /> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
0 commit comments