Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 89 additions & 96 deletions packages/api-explorer/src/ApiExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,8 @@ import { funFetch, fallbackFetch, OAuthScene } from '@looker/run-it'
import { FirstPage } from '@styled-icons/material/FirstPage'
import { LastPage } from '@styled-icons/material/LastPage'

import { LodeContext, defaultLodeContextValue } from './context'
import type { IApixEnvAdaptor } from './utils'
import {
getLoded,
oAuthPath,
registerEnvAdaptor,
unregisterEnvAdaptor,
} from './utils'
import { oAuthPath, registerEnvAdaptor, unregisterEnvAdaptor } from './utils'
import {
Header,
SideNav,
Expand All @@ -63,13 +57,17 @@ import {
import { specReducer, initDefaultSpecState, updateSpecApi } from './reducers'
import { AppRouter } from './routes'
import { apixFilesHost } from './utils/lodeUtils'
import { useActions, useSettingsStoreState } from './state'

import {
useSettingActions,
useSettingStoreState,
useLodeActions,
useLodesStoreState,
} from './state'
export interface ApiExplorerProps {
specs: SpecList
envAdaptor: IApixEnvAdaptor
setVersionsUrl: RunItSetter
exampleLodeUrl?: string
examplesLodeUrl?: string
declarationsLodeUrl?: string
headless?: boolean
}
Expand All @@ -80,12 +78,14 @@ const ApiExplorer: FC<ApiExplorerProps> = ({
specs,
envAdaptor,
setVersionsUrl,
exampleLodeUrl = 'https://raw.githubusercontent.com/looker-open-source/sdk-codegen/main/examplesIndex.json',
examplesLodeUrl = 'https://raw.githubusercontent.com/looker-open-source/sdk-codegen/main/examplesIndex.json',
declarationsLodeUrl = `${apixFilesHost}/declarationsIndex.json`,
headless = false,
}) => {
const { initialized } = useSettingsStoreState()
const { initAction } = useActions()
const { initialized } = useSettingStoreState()
useLodesStoreState()
const { initLodesAction } = useLodeActions()
const { initSettingsAction } = useSettingActions()
const location = useLocation()
const oauthReturn = location.pathname === `/${oAuthPath}`
const [specState, specDispatch] = useReducer(
Expand All @@ -94,8 +94,6 @@ const ApiExplorer: FC<ApiExplorerProps> = ({
)
const { spec } = specState

const [lode, setLode] = useState(defaultLodeContextValue)

const [hasNavigation, setHasNavigation] = useState(true)
const toggleNavigation = (target?: boolean) =>
setHasNavigation(target || !hasNavigation)
Expand All @@ -108,7 +106,8 @@ const ApiExplorer: FC<ApiExplorerProps> = ({

useEffect(() => {
registerEnvAdaptor(envAdaptor)
initAction()
initSettingsAction()
initLodesAction({ examplesLodeUrl, declarationsLodeUrl })

return () => unregisterEnvAdaptor()
}, [])
Expand Down Expand Up @@ -144,10 +143,6 @@ const ApiExplorer: FC<ApiExplorerProps> = ({
}
}, [spec, location])

useEffect(() => {
getLoded(exampleLodeUrl, declarationsLodeUrl).then((resp) => setLode(resp))
}, [exampleLodeUrl, declarationsLodeUrl])

const themeOverrides = envAdaptor.themeOverrides()

return (
Expand All @@ -160,87 +155,85 @@ const ApiExplorer: FC<ApiExplorerProps> = ({
<Loader message="Initializing" themeOverrides={themeOverrides} />
) : (
<ErrorBoundary logError={envAdaptor.logError.bind(envAdaptor)}>
<LodeContext.Provider value={{ ...lode }}>
<Page style={{ overflow: 'hidden' }}>
{!headless && (
<Header
specs={specs}
spec={spec}
specDispatch={specDispatch}
toggleNavigation={toggleNavigation}
/>
)}
<Layout hasAside height="100%">
<AsideBorder
borderRight
isOpen={hasNavigation}
headless={headless}
>
{headless && (
<>
<Space
alignItems="center"
py="u3"
px={hasNavigation ? 'u5' : '0'}
justifyContent={
hasNavigation ? 'space-between' : 'center'
}
>
{hasNavigation && (
<Heading
as="h2"
fontSize="xsmall"
fontWeight="bold"
color="text2"
>
API DOCUMENTATION
</Heading>
)}
<IconButton
size="xsmall"
shape="round"
icon={hasNavigation ? <FirstPage /> : <LastPage />}
label={HEADER_TOGGLE_LABEL}
onClick={() => toggleNavigation()}
/>
</Space>
<Page style={{ overflow: 'hidden' }}>
{!headless && (
<Header
specs={specs}
spec={spec}
specDispatch={specDispatch}
toggleNavigation={toggleNavigation}
/>
)}
<Layout hasAside height="100%">
<AsideBorder
borderRight
isOpen={hasNavigation}
headless={headless}
>
{headless && (
<>
<Space
alignItems="center"
py="u3"
px={hasNavigation ? 'u5' : '0'}
justifyContent={
hasNavigation ? 'space-between' : 'center'
}
>
{hasNavigation && (
<>
<Divider mb="u3" appearance="light" />
<SelectorContainer
ml="large"
mr="large"
specs={specs}
spec={spec}
specDispatch={specDispatch}
/>
</>
<Heading
as="h2"
fontSize="xsmall"
fontWeight="bold"
color="text2"
>
API DOCUMENTATION
</Heading>
)}
</>
)}
{hasNavigation && (
<SideNav
headless={headless}
specs={specs}
spec={spec}
specDispatch={specDispatch}
/>
)}
</AsideBorder>
{oauthReturn && <OAuthScene />}
{!oauthReturn && spec.api && (
<AppRouter
api={spec.api}
specKey={spec.key}
<IconButton
size="xsmall"
shape="round"
icon={hasNavigation ? <FirstPage /> : <LastPage />}
label={HEADER_TOGGLE_LABEL}
onClick={() => toggleNavigation()}
/>
</Space>
{hasNavigation && (
<>
<Divider mb="u3" appearance="light" />
<SelectorContainer
ml="large"
mr="large"
specs={specs}
spec={spec}
specDispatch={specDispatch}
/>
</>
)}
</>
)}
{hasNavigation && (
<SideNav
headless={headless}
specs={specs}
toggleNavigation={toggleNavigation}
envAdaptor={envAdaptor}
setVersionsUrl={setVersionsUrl}
spec={spec}
specDispatch={specDispatch}
/>
)}
</Layout>
</Page>
</LodeContext.Provider>
</AsideBorder>
{oauthReturn && <OAuthScene />}
{!oauthReturn && spec.api && (
<AppRouter
api={spec.api}
specKey={spec.key}
specs={specs}
toggleNavigation={toggleNavigation}
envAdaptor={envAdaptor}
setVersionsUrl={setVersionsUrl}
/>
)}
</Layout>
</Page>
</ErrorBoundary>
)}
</ComponentsProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import React from 'react'
import { screen } from '@testing-library/react'
import { findExampleLanguages } from '@looker/sdk-codegen'

import { renderWithReduxProviderAndLode } from '../../test-utils'
import { renderWithLode } from '../../test-utils'
import { api, examples } from '../../test-data'
import { DocSdkUsage } from './DocSdkUsage'
import {
Expand All @@ -47,7 +47,7 @@ describe('DocSdkUsage', () => {
method.operationId,
1
)
renderWithReduxProviderAndLode(<DocSdkUsage method={method} />, examples)
renderWithLode(<DocSdkUsage method={method} />, examples)
expect(screen.getAllByRole('link')).toHaveLength(PER_PAGE_COUNT)

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

*/
import type { FC } from 'react'
import React, { useContext, useState, useEffect } from 'react'
import React, { useState, useEffect } from 'react'
import {
Box,
Card,
Expand All @@ -41,8 +41,7 @@ import { CollapserCard } from '@looker/run-it'
import { InsertDriveFile } from '@styled-icons/material-outlined/InsertDriveFile'
import { useSelector } from 'react-redux'

import { selectSdkLanguage } from '../../state'
import { LodeContext } from '../../context'
import { selectSdkLanguage, selectExamplesLode } from '../../state'
import {
exampleColumns,
EMPTY_STRING,
Expand All @@ -61,15 +60,16 @@ interface DocSdkUsageProps {
* links to the source files
*/
export const DocSdkUsage: FC<DocSdkUsageProps> = ({ method }) => {
const { examples } = useContext(LodeContext)
const examples = useSelector(selectExamplesLode)
const sdkLanguage = useSelector(selectSdkLanguage)
let languages = findExampleLanguages(examples, method.name)
const [page, setPage] = useState(1)

useEffect(() => {
setPage(1)
}, [method])

if (!examples) return <></>
let languages = findExampleLanguages(examples, method.name)
if (languages.length === 0) return <></>

languages = sortLanguagesByPreference(languages, sdkLanguage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@

*/
import type { FC } from 'react'
import React, { useContext } from 'react'
import React from 'react'
import type { IMethod, IType } from '@looker/sdk-codegen'
import { findDeclaration } from '@looker/sdk-codegen'
import { Icon, Link, Tooltip } from '@looker/components'
import { IdeFileDocument } from '@looker/icons'
import { useSelector } from 'react-redux'

import { LodeContext } from '../../context'
import { selectDeclarationsLode } from '../../state'

interface DocSourceProps {
method?: IMethod
type?: IType
}

export const DocSource: FC<DocSourceProps> = ({ method, type }) => {
const { declarations } = useContext(LodeContext)
const declarations = useSelector(selectDeclarationsLode)
let sourceLink
let declaration
if (declarations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import userEvent from '@testing-library/user-event'
import { codeGenerators } from '@looker/sdk-codegen'
import * as reactRedux from 'react-redux'

import { defaultSettingsState, slice as settingsSlice } from '../../state'
import { defaultSettingsState, settingsSlice } from '../../state'
import {
registerTestEnvAdaptor,
renderWithReduxProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import { Select } from '@looker/components'
import { useSelector } from 'react-redux'
import type { SelectOptionProps } from '@looker/components'

import { useActions, selectSdkLanguage } from '../../state'
import { useSettingActions, selectSdkLanguage } from '../../state'

/**
* Allows the user to select their preferred SDK language
*/
export const SdkLanguageSelector: FC = () => {
const { setSdkLanguageAction } = useActions()
const { setSdkLanguageAction } = useSettingActions()
const selectedSdkLanguage = useSelector(selectSdkLanguage)

const allSdkLanguages: SelectOptionProps[] = codeGenerators.map((gen) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/api-explorer/src/components/SideNav/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { useSelector } from 'react-redux'
import type { SpecAction } from '../../reducers'
import { useWindowSize } from '../../utils'
import { HEADER_REM } from '../Header'
import { selectSearchCriteria, useActions } from '../../state'
import { selectSearchCriteria, useSettingActions } from '../../state'
import { SideNavMethodTags } from './SideNavMethodTags'
import { SideNavTypeTags } from './SideNavTypeTags'
import { useDebounce, countMethods, countTypes } from './searchUtils'
Expand Down Expand Up @@ -100,7 +100,7 @@ export const SideNav: FC<SideNavProps> = ({ headless = false, spec }) => {
}
const tabs = useTabs({ defaultIndex, onChange: onTabChange })
const searchCriteria = useSelector(selectSearchCriteria)
const { setSearchPatternAction } = useActions()
const { setSearchPatternAction } = useSettingActions()

const [pattern, setSearchPattern] = useState('')
const debouncedPattern = useDebounce(pattern, 250)
Expand Down
1 change: 1 addition & 0 deletions packages/api-explorer/src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
*/
export * from './store'
export * from './settings'
export * from './lodes'
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
SOFTWARE.

*/
export { LodeContext, defaultLodeContextValue } from './lode'
export * from './selectors'
export * from './slice'
Loading