mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
use a single instance for swagger ui (#5440)
This commit is contained in:
parent
4e05bcf488
commit
321584cdeb
@ -1,46 +1,40 @@
|
|||||||
import 'swagger-ui-dist/swagger-ui.css';
|
import 'swagger-ui-dist/swagger-ui.css';
|
||||||
|
|
||||||
import React, { FC, useEffect, useRef } from 'react';
|
import React, { FC, useEffect, useRef } from 'react';
|
||||||
import { SwaggerConfigs, SwaggerUIBundle as createSwaggerUI } from 'swagger-ui-dist';
|
import { Spec, SwaggerConfigs, SwaggerUIBundle as createSwaggerUI } from 'swagger-ui-dist';
|
||||||
|
|
||||||
export const SwaggerUI: FC<SwaggerConfigs> = props => {
|
// Keep an instance of swagger-ui because it doesn't have a teardown method
|
||||||
const swaggerUIRef = useRef<SwaggerConfigs | null>(null);
|
let SwaggerUIInstance: SwaggerConfigs | null = null;
|
||||||
const domNodeRef = useRef(null);
|
|
||||||
|
export const SwaggerUI: FC<{
|
||||||
|
spec: Spec;
|
||||||
|
supportedSubmitMethods: string[];
|
||||||
|
}> = ({
|
||||||
|
spec,
|
||||||
|
supportedSubmitMethods,
|
||||||
|
}) => {
|
||||||
|
const domNodeRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
swaggerUIRef.current = createSwaggerUI({
|
const swaggerUI = SwaggerUIInstance;
|
||||||
...props,
|
|
||||||
domNode: domNodeRef.current,
|
|
||||||
});
|
|
||||||
}, [props]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const swaggerUI = swaggerUIRef?.current;
|
|
||||||
|
|
||||||
if (swaggerUI) {
|
if (swaggerUI) {
|
||||||
const prevStateUrl = swaggerUIRef?.current?.specSelectors.url();
|
|
||||||
if (props.url !== prevStateUrl) {
|
|
||||||
// flush current content
|
|
||||||
swaggerUI.specActions.updateSpec('');
|
|
||||||
|
|
||||||
if (props.url) {
|
|
||||||
// update the internal URL
|
|
||||||
swaggerUI.specActions.updateUrl(props.url);
|
|
||||||
// trigger remote definition fetch
|
|
||||||
swaggerUI.system.specActions.download(props.url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const prevStateSpec = swaggerUI.specSelectors.specStr();
|
const prevStateSpec = swaggerUI.specSelectors.specStr();
|
||||||
if (props.spec && (props.spec !== prevStateSpec)) {
|
if (spec && (spec !== prevStateSpec)) {
|
||||||
if (typeof props.spec === 'object') {
|
if (typeof spec === 'object') {
|
||||||
swaggerUI.specActions.updateSpec(JSON.stringify(props.spec));
|
swaggerUI.specActions.updateSpec(JSON.stringify(spec));
|
||||||
} else {
|
} else {
|
||||||
swaggerUI.specActions.updateSpec(props.spec);
|
swaggerUI.specActions.updateSpec(spec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
SwaggerUIInstance = createSwaggerUI({
|
||||||
|
domNode: domNodeRef.current,
|
||||||
|
spec,
|
||||||
|
supportedSubmitMethods,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, [props.spec, props.url]);
|
}, [supportedSubmitMethods, spec]);
|
||||||
|
|
||||||
return <div ref={domNodeRef} />;
|
return <div ref={domNodeRef} />;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user