Skip to content

Commit 5e9a221

Browse files
josephaxisabryans99jkaster
authored
chore: move specs into redux and cleanup oauth related code (#976)
* replace runItSettings and configurator with OAuthConfigProvider * move specs to redux and cleanup oauth related code - Work around non-reentrant code - Cleanup OAuthScene and more tweaks to initSpecs - Better memory handling - chore: clean up extension-utils package.json - feat: move specs to redux * fix: type and diff scene related issues * fix: stop diff links from reloading * fix: DiffScene back/forward browser button behaviour DiffScene did not handle changes in route when back or forward button pressed. This change has the react router driving the changes to state. If diff changes, history is updated. History updates state. When back/forward button pressed, history is update and again history updates state. * fix: route from diff page Setting of current spec has been hoisted to ApiExplorer. ApiExplorer monitors the current route and sets the current spec if route is different from current spec. Added a check in the rendering to make sure that the current spec is set correctly before it allows the router component to render. Renders the loading component instead which I think is okay but may need to be revisited. ApiSpecSelector has been modified to ONLY change the route. This means that if a component needs to switch spec all it needs to do is modify the route (diff component showing detail for example). * fix: initial load issue Co-authored-by: Bryn Ryans <bnryans@google.com> Co-authored-by: John Kaster <kaster@google.com>
1 parent 571a817 commit 5e9a221

File tree

82 files changed

+1227
-1432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1227
-1432
lines changed

examples/access-token-server/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"create-status-json": "ts-node script/create_status_json.ts"
1212
},
1313
"dependencies": {
14-
"@looker/sdk": "^21.14.0",
14+
"@looker/sdk": "^21.20.1",
1515
"@looker/sdk-rtl": "^21.0.20",
16-
"@looker/sdk-node": "^21.14.0",
16+
"@looker/sdk-node": "^21.20.1",
1717
"body-parser": "^1.19.0",
1818
"crypto-js": "^4.0.0",
1919
"dotenv": "^8.2.0",
@@ -35,4 +35,4 @@
3535
"ts-node": "^10.2.1",
3636
"typescript": "^4.4.3"
3737
}
38-
}
38+
}

examples/access-token-server/yarn.lock

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@
502502
"@types/yargs" "^15.0.0"
503503
chalk "^4.0.0"
504504

505-
"@looker/sdk-node@^21.14.0":
506-
version "21.14.0"
507-
resolved "https://registry.yarnpkg.com/@looker/sdk-node/-/sdk-node-21.14.0.tgz#2a7a6d8995f85832cef87165bc99e51041e8d75a"
508-
integrity sha512-JoN7o/WdbMiz3IYRkNlcD0zuGGH4jv73ngz4IrljX7uAKDZmxRaZwQnPvFpN6CF5BDXRCLhfMaA0yCwBYV1xig==
505+
"@looker/sdk-node@^22.0.0":
506+
version "21.20.1"
507+
resolved "https://registry.yarnpkg.com/@looker/sdk-node/-/sdk-node-21.20.1.tgz#4cef072397ad32ec8d0628bae36c461c8e091d3b"
508+
integrity sha512-0xi2uSfGslB8KdXAaG2kVWv9suzgg5yiBOqzpKQaOeLRZY2rZAx8nn3FDNKzk15ZZAG/zwXNGmYblJny24AMLQ==
509509
dependencies:
510-
"@looker/sdk" "^21.14.0"
511-
"@looker/sdk-rtl" "^21.0.20"
510+
"@looker/sdk" "^21.20.1"
511+
"@looker/sdk-rtl" "^21.3.1"
512512
ini "^1.3.8"
513513
readable-stream "^3.4.0"
514514
request "^2.88.0"
@@ -524,12 +524,22 @@
524524
request "^2.88.0"
525525
request-promise-native "^1.0.8"
526526

527-
"@looker/sdk@^21.14.0":
528-
version "21.14.0"
529-
resolved "https://registry.yarnpkg.com/@looker/sdk/-/sdk-21.14.0.tgz#56d6da1eb1ea65e145e25291f09160cc283f9e4f"
530-
integrity sha512-0/oh+95HYNG9VoqzKRim6z7bPic9ZgvlnDoBpQ1OZDLuufAWImZPiIwn2qBZESpCEgq1Sj3f2zdD+oqrLfCLIQ==
527+
"@looker/sdk-rtl@^21.3.1":
528+
version "21.3.1"
529+
resolved "https://registry.yarnpkg.com/@looker/sdk-rtl/-/sdk-rtl-21.3.1.tgz#ff860249959ccf12a9f48d3c79267113e61724d1"
530+
integrity sha512-i4SKNTtpNgNz7hLYMJ2W8xOrgQI81gw674q8pgDw1dFWuUA3yq4nTvuR9CFnStYCIoY/1ar2WcTkWp0YSlFzTw==
531531
dependencies:
532-
"@looker/sdk-rtl" "^21.0.20"
532+
ini "^1.3.8"
533+
readable-stream "^3.4.0"
534+
request "^2.88.0"
535+
request-promise-native "^1.0.8"
536+
537+
"@looker/sdk@^21.20.1", "@looker/sdk@^22.0.0":
538+
version "21.20.1"
539+
resolved "https://registry.yarnpkg.com/@looker/sdk/-/sdk-21.20.1.tgz#3d257309c2fe5ad1246e01837ea71aeeb7f4df22"
540+
integrity sha512-t54yXAMeJAIeq7uUc1Oa3SdFsE3zvQaL9S28ebXy9TrljbnHG4FsVMacfq0XQGYPDzYUIoEySShWcBOsFZ+eyw==
541+
dependencies:
542+
"@looker/sdk-rtl" "^21.3.1"
533543
ini "^1.3.8"
534544
readable-stream "^3.4.0"
535545
request "^2.88.0"

packages/api-explorer/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"devDependencies": {
3535
"@looker/components-test-utils": "^1.5.5",
3636
"@looker/sdk-codegen-scripts": "^21.2.1",
37-
"@looker/sdk-node": "^21.20.1",
37+
"@looker/sdk-node": "^22.0.0",
3838
"@styled-icons/styled-icon": "^10.6.3",
3939
"@testing-library/jest-dom": "^5.11.6",
4040
"@testing-library/react": "^11.2.2",
@@ -75,9 +75,9 @@
7575
"@looker/icons": "^1.5.3",
7676
"@looker/redux": "0.0.0",
7777
"@looker/run-it": "^0.9.26",
78-
"@looker/sdk": "^21.20.1",
79-
"@looker/sdk-codegen": "^21.3.1",
80-
"@looker/sdk-rtl": "^21.3.1",
78+
"@looker/sdk": "^22.0.0",
79+
"@looker/sdk-codegen": "^21.3.2",
80+
"@looker/sdk-rtl": "^21.3.2",
8181
"@reduxjs/toolkit": "^1.6.2",
8282
"@styled-icons/material": "^10.28.0",
8383
"@styled-icons/material-outlined": "^10.28.0",

packages/api-explorer/src/ApiExplorer.tsx

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
*/
2626

2727
import type { FC } from 'react'
28-
import React, { useReducer, useState, useEffect, useCallback } from 'react'
29-
import { useLocation } from 'react-router'
28+
import React, { useState, useEffect, useCallback } from 'react'
3029
import styled, { createGlobalStyle } from 'styled-components'
3130
import {
3231
Aside,
@@ -38,41 +37,41 @@ import {
3837
Page,
3938
Space,
4039
} from '@looker/components'
41-
import type { SpecList } from '@looker/sdk-codegen'
42-
import type { RunItSetter } from '@looker/run-it'
43-
import { funFetch, fallbackFetch, OAuthScene } from '@looker/run-it'
4440
import { FirstPage } from '@styled-icons/material/FirstPage'
4541
import { LastPage } from '@styled-icons/material/LastPage'
46-
47-
import type { IEnvironmentAdaptor } from '@looker/extension-utils'
4842
import {
4943
registerEnvAdaptor,
5044
unregisterEnvAdaptor,
5145
} from '@looker/extension-utils'
52-
import { oAuthPath } from './utils'
46+
import { useSelector } from 'react-redux'
47+
import { useLocation } from 'react-router-dom'
48+
49+
import type { IApixAdaptor } from './utils'
5350
import {
5451
Header,
5552
SideNav,
5653
ErrorBoundary,
57-
Loader,
5854
SelectorContainer,
5955
HEADER_TOGGLE_LABEL,
56+
Loader,
6057
Banner,
6158
} from './components'
62-
import { specReducer, initDefaultSpecState, updateSpecApi } from './reducers'
6359
import { AppRouter } from './routes'
6460
import { apixFilesHost } from './utils/lodeUtils'
6561
import {
6662
useSettingActions,
6763
useSettingStoreState,
6864
useLodeActions,
6965
useLodesStoreState,
66+
useSpecActions,
67+
useSpecStoreState,
68+
selectSpecs,
69+
selectCurrentSpec,
7070
} from './state'
71+
import { getSpecKey, diffPath } from './utils'
7172

7273
export interface ApiExplorerProps {
73-
specs: SpecList
74-
adaptor: IEnvironmentAdaptor
75-
setVersionsUrl: RunItSetter
74+
adaptor: IApixAdaptor
7675
examplesLodeUrl?: string
7776
declarationsLodeUrl?: string
7877
headless?: boolean
@@ -81,25 +80,21 @@ export interface ApiExplorerProps {
8180
const BodyOverride = createGlobalStyle` html { height: 100%; overflow: hidden; } `
8281

8382
export const ApiExplorer: FC<ApiExplorerProps> = ({
84-
specs,
8583
adaptor,
86-
setVersionsUrl,
8784
examplesLodeUrl = 'https://raw.githubusercontent.com/looker-open-source/sdk-codegen/main/examplesIndex.json',
8885
declarationsLodeUrl = `${apixFilesHost}/declarationsIndex.json`,
8986
headless = false,
9087
}) => {
91-
const { initialized } = useSettingStoreState()
88+
useSettingStoreState()
9289
useLodesStoreState()
90+
const { working, description } = useSpecStoreState()
91+
const specs = useSelector(selectSpecs)
92+
const spec = useSelector(selectCurrentSpec)
9393
const { initLodesAction } = useLodeActions()
9494
const { initSettingsAction } = useSettingActions()
95-
const location = useLocation()
96-
const oauthReturn = location.pathname === `/${oAuthPath}`
97-
const [specState, specDispatch] = useReducer(
98-
specReducer,
99-
initDefaultSpecState(specs, location)
100-
)
101-
const { spec } = specState
95+
const { initSpecsAction, setCurrentSpecAction } = useSpecActions()
10296

97+
const location = useLocation()
10398
const [hasNavigation, setHasNavigation] = useState(true)
10499
const toggleNavigation = (target?: boolean) =>
105100
setHasNavigation(target || !hasNavigation)
@@ -110,14 +105,24 @@ export const ApiExplorer: FC<ApiExplorerProps> = ({
110105
}
111106
}, [])
112107

108+
registerEnvAdaptor(adaptor)
109+
113110
useEffect(() => {
114-
registerEnvAdaptor(adaptor)
115111
initSettingsAction()
116112
initLodesAction({ examplesLodeUrl, declarationsLodeUrl })
117113

114+
const specKey = getSpecKey(location)
115+
initSpecsAction({ specKey })
118116
return () => unregisterEnvAdaptor()
119117
}, [])
120118

119+
useEffect(() => {
120+
const maybeSpec = location.pathname?.split('/')[1]
121+
if (spec && maybeSpec && maybeSpec !== diffPath && maybeSpec !== spec.key) {
122+
setCurrentSpecAction({ currentSpecKey: maybeSpec })
123+
}
124+
}, [location.pathname, spec])
125+
121126
useEffect(() => {
122127
if (headless) {
123128
window.addEventListener('message', hasNavigationToggle)
@@ -129,47 +134,27 @@ export const ApiExplorer: FC<ApiExplorerProps> = ({
129134
}
130135
}, [headless, hasNavigationToggle])
131136

132-
useEffect(() => {
133-
const loadSpec = async () => {
134-
if (!spec.api) {
135-
try {
136-
const newSpec = { ...spec }
137-
const api = await fallbackFetch(newSpec, funFetch)
138-
if (api) {
139-
spec.api = api
140-
specDispatch(updateSpecApi(spec.key, api))
141-
}
142-
} catch (error) {
143-
console.error(error)
144-
}
145-
}
146-
}
147-
if (!oauthReturn) {
148-
loadSpec()
149-
}
150-
}, [spec, location])
151-
152137
const themeOverrides = adaptor.themeOverrides()
153138

139+
let neededSpec = location.pathname?.split('/')[1]
140+
if (!neededSpec || neededSpec === diffPath) {
141+
neededSpec = spec?.key
142+
}
143+
154144
return (
155145
<>
156146
<ComponentsProvider
157147
loadGoogleFonts={themeOverrides.loadGoogleFonts}
158148
themeCustomizations={themeOverrides.themeCustomizations}
159149
>
160-
{!initialized ? (
161-
<Loader message="Initializing" themeOverrides={themeOverrides} />
150+
{working || !neededSpec || neededSpec !== spec.key ? (
151+
<Loader message={description} themeOverrides={themeOverrides} />
162152
) : (
163153
<ErrorBoundary logError={adaptor.logError.bind(adaptor)}>
164154
<Banner adaptor={adaptor} specs={specs} />
165155
<Page style={{ overflow: 'hidden' }}>
166156
{!headless && (
167-
<Header
168-
specs={specs}
169-
spec={spec}
170-
specDispatch={specDispatch}
171-
toggleNavigation={toggleNavigation}
172-
/>
157+
<Header spec={spec} toggleNavigation={toggleNavigation} />
173158
)}
174159
<Layout hasAside height="100%">
175160
<AsideBorder
@@ -209,36 +194,22 @@ export const ApiExplorer: FC<ApiExplorerProps> = ({
209194
<>
210195
<Divider mb="u3" appearance="light" />
211196
<SelectorContainer
197+
spec={spec}
212198
ml="large"
213199
mr="large"
214-
specs={specs}
215-
spec={spec}
216-
specDispatch={specDispatch}
217200
/>
218201
</>
219202
)}
220203
</>
221204
)}
222-
{hasNavigation && (
223-
<SideNav
224-
headless={headless}
225-
specs={specs}
226-
spec={spec}
227-
specDispatch={specDispatch}
228-
/>
229-
)}
205+
{hasNavigation && <SideNav headless={headless} spec={spec} />}
230206
</AsideBorder>
231-
{oauthReturn && <OAuthScene />}
232-
{!oauthReturn && spec.api && (
233-
<AppRouter
234-
api={spec.api}
235-
specKey={spec.key}
236-
specs={specs}
237-
toggleNavigation={toggleNavigation}
238-
adaptor={adaptor}
239-
setVersionsUrl={setVersionsUrl}
240-
/>
241-
)}
207+
<AppRouter
208+
specKey={spec.key}
209+
api={spec.api!}
210+
specs={specs}
211+
toggleNavigation={toggleNavigation}
212+
/>
242213
</Layout>
243214
</Page>
244215
</ErrorBoundary>

0 commit comments

Comments
 (0)