@@ -2,29 +2,35 @@ import React, { useState, useEffect } from "react";
22import { ipcRenderer } from "electron" ;
33import Swagger from "swagger-ui" ;
44import axios , { AxiosResponse } from "axios" ;
5+ import { isEmpty } from "lodash" ;
56
67import Titlebar from "@components/Titlebar" ;
78
89type Credentials = {
9- address : string ;
10- port : number ;
11- username : string ;
12- password : string ;
13- protocol : string ;
10+ address ? : string ;
11+ port ? : number ;
12+ username ? : string ;
13+ password ? : string ;
14+ protocol ? : string ;
1415} ;
1516type SwaggerUIPlugged = Swagger & {
1617 updateSpec : ( specUpdates : any ) => void ;
1718} ;
1819
1920const BASIC_AUTH = "BasicAuth" ;
2021
21- const Home = ( ) : JSX . Element => {
22- const [ swagger , SetSwagger ] = useState < SwaggerUIPlugged > ( ) ;
23- const [ credentials , SetCredentials ] = useState < Credentials > ( ) ;
24- const [ spec , SetSpec ] = useState < any > ( ) ;
22+ const Home : React . FC = ( ) => {
23+ const [ swagger , SetSwagger ] = useState < any > ( { } ) ;
24+ const [ credentials , SetCredentials ] = useState < Credentials > ( { } ) ;
25+ const [ spec , SetSpec ] = useState < any > ( { } ) ;
2526
2627 // Get spec.
2728 useEffect ( ( ) => {
29+ if ( ! isEmpty ( spec ) ) {
30+ console . log ( "spec is not empty returning" ) ;
31+ return ;
32+ }
33+ console . warn ( "spec is empty fetching" ) ;
2834 axios
2935 . get ( "https://www.mingweisamuel.com/lcu-schema/lcu/openapi.json" )
3036 . then ( ( res : AxiosResponse < any > ) => SetSpec ( res . data ) ) ;
@@ -34,14 +40,17 @@ const Home = (): JSX.Element => {
3440 useEffect ( ( ) => {
3541 ipcRenderer . on ( "credentialspass" , ( _event , newCredentials ) => {
3642 console . log ( `FE received credentials: ${ JSON . stringify ( newCredentials ) } ` ) ;
37- SetCredentials ( newCredentials ) ;
43+ if ( isEmpty ( credentials ) ) {
44+ SetCredentials ( newCredentials ) ;
45+ }
3846 } ) ;
3947 ipcRenderer . send ( "fe-ready" ) ;
4048 } , [ ] ) ;
4149
4250 // Setup Swagger UI.
4351 useEffect ( ( ) => {
4452 const swaggerInst = Swagger ( {
53+ syntaxHighlight : false ,
4554 dom_id : "#swagger" ,
4655 spec : {
4756 openapi : "3.0.0" ,
@@ -63,8 +72,13 @@ const Home = (): JSX.Element => {
6372 operationsSorter : "alpha" ,
6473 tagsSorter : "alpha" ,
6574 docExpansion : "none" ,
66- defaultModelExpandDepth : 1 ,
75+ defaultModelExpandDepth : 10 ,
76+ maxDisplayedTags : 100 ,
77+ tryItOutEnabled : true ,
6778 displayRequestDuration : true ,
79+ pluginsOptions : {
80+ pluginLoadType : "chain" ,
81+ } ,
6882 filter : "" ,
6983 deepLinking : false , // @ts -ignore
7084 requestInterceptor : ( request : any ) => {
@@ -77,8 +91,9 @@ const Home = (): JSX.Element => {
7791 spec : {
7892 wrapSelectors : {
7993 allowTryItOutFor : ( ) => ( ) => {
80- const jsonSpec = system . getState ( ) . toJSON ( ) . spec . json ;
81- return jsonSpec . servers . length > 0 ;
94+ // const jsonSpec = system.getState().toJSON().spec.json;
95+ console . log ( "rerendering" ) ;
96+ return true ;
8297 } ,
8398 } ,
8499 } ,
@@ -97,7 +112,12 @@ const Home = (): JSX.Element => {
97112 ] ,
98113 onComplete : ( ) => {
99114 console . log ( "Swagger UI loading complete." ) ;
100- SetSwagger ( swaggerInst ) ;
115+ if ( isEmpty ( swagger ) ) {
116+ console . log ( "swagger is empty applying instance" ) ;
117+ SetSwagger ( swaggerInst ) ;
118+ } else {
119+ console . log ( "swagger instance is present" ) ;
120+ }
101121 } ,
102122 } ) as SwaggerUIPlugged ;
103123 } , [ ] ) ;
@@ -116,6 +136,11 @@ const Home = (): JSX.Element => {
116136
117137 // Update credentials/port from connector.
118138 if ( credentials != null ) {
139+ if ( isEmpty ( swagger ) ) {
140+ console . log ( "swager is empty and credentials arent null" ) ;
141+ return ;
142+ }
143+ console . log ( "updating spec..." ) ;
119144 swagger . updateSpec ( {
120145 servers : [
121146 {
@@ -130,13 +155,18 @@ const Home = (): JSX.Element => {
130155 credentials . password
131156 ) ;
132157 } else {
158+ if ( isEmpty ( swagger ) ) {
159+ return ;
160+ }
161+ console . log ( "updating spec" ) ;
133162 swagger . updateSpec ( {
134163 servers : [ ] ,
135164 } ) ;
136165 }
137166
138167 // Update OpenAPI spec lcu-schema.
139168 if ( spec != null ) {
169+ console . log ( "updating spec" ) ;
140170 swagger . updateSpec ( spec ) ;
141171 }
142172 } , [ swagger , credentials , spec ] ) ;
0 commit comments