|
| 1 | +/* |
| 2 | +
|
| 3 | + MIT License |
| 4 | +
|
| 5 | + Copyright (c) 2023 Looker Data Sciences, Inc. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | +
|
| 25 | + */ |
| 26 | +import React, { useState, useEffect } from 'react' |
| 27 | +import { ComponentsProvider } from '@looker/components' |
| 28 | +import { |
| 29 | + OAuthConfigForm, |
| 30 | + BrowserAdaptor, |
| 31 | + registerEnvAdaptor, |
| 32 | + OAuthScene, |
| 33 | + OAuthConfigProvider, |
| 34 | +} from '@looker/extension-utils' |
| 35 | +import { Provider } from 'react-redux' |
| 36 | +import { useLocation } from 'react-router' |
| 37 | +import type { IApiSettings } from '@looker/sdk-rtl' |
| 38 | +import { |
| 39 | + BrowserSession, |
| 40 | + BrowserTransport, |
| 41 | + DefaultSettings, |
| 42 | +} from '@looker/sdk-rtl' |
| 43 | +import { functionalSdk40 } from '@looker/sdk' |
| 44 | +import { store } from './state' |
| 45 | +import { Loader } from './components' |
| 46 | +import { EmbedPlayground } from '.' |
| 47 | + |
| 48 | +const ConfigKey = 'EPConfig' |
| 49 | + |
| 50 | +const OAuthClientId = 'looker.embed-playground' |
| 51 | + |
| 52 | +export const initSdk = () => { |
| 53 | + const settings = { |
| 54 | + ...DefaultSettings(), |
| 55 | + base_url: 'https://self-signed.looker.com:19999', |
| 56 | + agentTag: 'EmbedPlayground 0.1', |
| 57 | + } as IApiSettings |
| 58 | + |
| 59 | + const options = new OAuthConfigProvider(settings, OAuthClientId, ConfigKey) |
| 60 | + const transport = new BrowserTransport(options) |
| 61 | + const session = new BrowserSession(options, transport) |
| 62 | + const sdk = functionalSdk40(session) |
| 63 | + return sdk |
| 64 | +} |
| 65 | + |
| 66 | +export const StandaloneEmbedPlayground = () => { |
| 67 | + const location = useLocation() |
| 68 | + const oauthReturn = location.pathname === '/oauth' |
| 69 | + const [adaptor] = useState(new BrowserAdaptor(initSdk())) |
| 70 | + const sdk = adaptor.sdk |
| 71 | + const authIsConfigured = ( |
| 72 | + sdk.authSession.settings as OAuthConfigProvider |
| 73 | + ).authIsConfigured() |
| 74 | + const canLogin = |
| 75 | + !authIsConfigured && !sdk.authSession.isAuthenticated() && !oauthReturn |
| 76 | + |
| 77 | + useEffect(() => { |
| 78 | + const login = async () => await adaptor.login() |
| 79 | + if (canLogin) { |
| 80 | + login() |
| 81 | + } |
| 82 | + }, []) |
| 83 | + |
| 84 | + const { looker_url } = ( |
| 85 | + sdk.authSession.settings as OAuthConfigProvider |
| 86 | + ).getStoredConfig() |
| 87 | + registerEnvAdaptor(adaptor) |
| 88 | + |
| 89 | + const themeOverrides = adaptor.themeOverrides() |
| 90 | + |
| 91 | + if (!authIsConfigured) { |
| 92 | + return ( |
| 93 | + <Provider store={store}> |
| 94 | + <ComponentsProvider |
| 95 | + loadGoogleFonts={themeOverrides.loadGoogleFonts} |
| 96 | + themeCustomizations={themeOverrides.themeCustomizations} |
| 97 | + > |
| 98 | + <OAuthConfigForm |
| 99 | + configKey="EPConfig" |
| 100 | + clientId={OAuthClientId} |
| 101 | + clientLabel="Embed Playground" |
| 102 | + /> |
| 103 | + </ComponentsProvider> |
| 104 | + </Provider> |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + if (canLogin) { |
| 109 | + return ( |
| 110 | + <Provider store={store}> |
| 111 | + <Loader |
| 112 | + themeOverrides={adaptor.themeOverrides()} |
| 113 | + message={`Configuration found. Logging into ${looker_url}`} |
| 114 | + /> |
| 115 | + </Provider> |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <> |
| 121 | + {oauthReturn ? ( |
| 122 | + <OAuthScene adaptor={adaptor} /> |
| 123 | + ) : ( |
| 124 | + <EmbedPlayground adaptor={adaptor} /> |
| 125 | + )} |
| 126 | + </> |
| 127 | + ) |
| 128 | +} |
0 commit comments