|
| 1 | +import { ErrorComponent } from '@/components/ErrorComponent'; |
| 2 | +import { Loading } from '@/components/Loading'; |
| 3 | +import { useInstanceClientIdParams } from '@/config/useInstanceClient'; |
| 4 | +import { plugins } from '@/features/instance/apis/plugins'; |
| 5 | +import { requestSnippets } from '@/features/instance/apis/requestSnippets'; |
| 6 | +import { getConfigurationQueryOptions } from '@/features/instance/operations/queries/getConfiguration'; |
| 7 | +import { getOpenAPIQueryOptions } from '@/features/instance/operations/queries/getOpenAPI'; |
| 8 | +import { getRegistrationInfoQueryOptions } from '@/features/instance/operations/queries/getRegistrationInfo'; |
| 9 | +import { wasAReleasedBeforeB } from '@/lib/string/wasAReleasedBeforeB'; |
| 10 | +import { useQuery } from '@tanstack/react-query'; |
| 11 | +import { useParams } from '@tanstack/react-router'; |
| 12 | +import SwaggerUI from 'swagger-ui-react'; |
| 13 | +import 'swagger-ui-react/swagger-ui.css'; |
| 14 | +import './swagger.css'; |
| 15 | + |
| 16 | +export function APIDocs() { |
| 17 | + const { instanceId, clusterId }: { instanceId?: string; clusterId?: string; } = useParams({ strict: false }); |
| 18 | + const operationsParams = useInstanceClientIdParams(); |
| 19 | + const { |
| 20 | + data: configurationInfo, |
| 21 | + isLoading: isLoadingConfiguration, |
| 22 | + } = useQuery(getConfigurationQueryOptions(operationsParams)); |
| 23 | + const { data: registrationInfo, isLoading: isLoadingRegistration } = useQuery( |
| 24 | + getRegistrationInfoQueryOptions(operationsParams), |
| 25 | + ); |
| 26 | + const { |
| 27 | + data: spec, |
| 28 | + isLoading: isLoadingDocs, |
| 29 | + error, |
| 30 | + } = useQuery(getOpenAPIQueryOptions(operationsParams)); |
| 31 | + |
| 32 | + if (isLoadingConfiguration || isLoadingRegistration || isLoadingDocs) { |
| 33 | + return <Loading centered={true} text="Looking up your instance configuration, one moment." />; |
| 34 | + } |
| 35 | + |
| 36 | + if (error) { |
| 37 | + if (registrationInfo?.version && !wasAReleasedBeforeB('4.7.0-beta.7', registrationInfo?.version)) { |
| 38 | + return <ErrorComponent |
| 39 | + title="API Docs Unavailable" |
| 40 | + error={{ |
| 41 | + message: `API Docs are only available starting in version '4.7.0-beta.7' of Harper, please update your version ${registrationInfo.version}!`, |
| 42 | + }} |
| 43 | + showReturnToHome={false} |
| 44 | + />; |
| 45 | + } |
| 46 | + |
| 47 | + return <ErrorComponent |
| 48 | + title="API Docs Unavailable" |
| 49 | + error={{ |
| 50 | + message: 'We weren\'t able to look up your docs. Please check the Network tab of your' + |
| 51 | + ' developer tools to see why the docs were not accessible to Studio.', |
| 52 | + }} |
| 53 | + showReturnToHome={false} |
| 54 | + />; |
| 55 | + } |
| 56 | + |
| 57 | + return (<> |
| 58 | + {configurationInfo?.http?.cors === false && (<ErrorComponent |
| 59 | + title="CORS Disabled: API Not Accessible" |
| 60 | + className="mt-0 mx-4 m-0" |
| 61 | + error={{ |
| 62 | + message: `This ${clusterId && !instanceId ? 'cluster' : 'instance'} has CORS disabled currently, so you won't be able to execute API requests from the browser.`, |
| 63 | + }} |
| 64 | + showReturnToHome={false} |
| 65 | + />)} |
| 66 | + <SwaggerUI |
| 67 | + spec={spec} |
| 68 | + persistAuthorization={true} |
| 69 | + requestSnippetsEnabled={true} |
| 70 | + requestSnippets={requestSnippets} |
| 71 | + plugins={plugins} |
| 72 | + tryItOutEnabled={true} |
| 73 | + /> |
| 74 | + </>); |
| 75 | +} |
0 commit comments