-
Notifications
You must be signed in to change notification settings - Fork 60
load scalprum #998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load scalprum #998
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||||||||||||||||||||||||
| import React, { useState, createContext, useCallback } from 'react'; | ||||||||||||||||||||||||||||
| import PropTypes from 'prop-types'; | ||||||||||||||||||||||||||||
| import { ScalprumProvider } from '@scalprum/react-core'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export const ScalprumContext = createContext(null); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||
| Wrap your component with ScalprumContextWrapper: | ||||||||||||||||||||||||||||
| <ScalprumContextWrapper> | ||||||||||||||||||||||||||||
| <WrappedComponent /> | ||||||||||||||||||||||||||||
| </ScalprumContextWrapper> | ||||||||||||||||||||||||||||
| inside the WrappedComponent (not the component that loads the wrapper), use useEffect and setConfig: | ||||||||||||||||||||||||||||
| const { config, setConfig } = React.useContext(ScalprumContext); | ||||||||||||||||||||||||||||
| const path = 'apps/landing' | ||||||||||||||||||||||||||||
| const scope = 'landing' | ||||||||||||||||||||||||||||
| const module = './AnsibleWidget' | ||||||||||||||||||||||||||||
| const newConfig = { | ||||||||||||||||||||||||||||
| [scope]: { | ||||||||||||||||||||||||||||
| name: scope, | ||||||||||||||||||||||||||||
| manifestLocation: `${window.location.origin}/scalprum/${cdnPath}/fed-mods.json`, | ||||||||||||||||||||||||||||
| cdnPath: `${window.location.origin}/scalprum/${cdnPath}/`, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||
| setConfig(newConfig) | ||||||||||||||||||||||||||||
| },[]) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Load ScalprumComponent conditionally to the config being set | ||||||||||||||||||||||||||||
| {config[scope] && ( | ||||||||||||||||||||||||||||
| <ScalprumComponent scope={scope} module={module}` /> | ||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export const ScalprumContextWrapper = ({ children }) => { | ||||||||||||||||||||||||||||
| const [config, setConfig] = useState({}); | ||||||||||||||||||||||||||||
| const contextData = { | ||||||||||||||||||||||||||||
| config, | ||||||||||||||||||||||||||||
| setConfig: useCallback( | ||||||||||||||||||||||||||||
| newConfig => setConfig(prev => ({ ...prev, ...newConfig })), | ||||||||||||||||||||||||||||
| [] | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const mockUser = { | ||||||||||||||||||||||||||||
| entitlements: {}, | ||||||||||||||||||||||||||||
| identity: { | ||||||||||||||||||||||||||||
| account_number: 'string', | ||||||||||||||||||||||||||||
| org_id: 'string', | ||||||||||||||||||||||||||||
| internal: { | ||||||||||||||||||||||||||||
| org_id: 'string', | ||||||||||||||||||||||||||||
| account_id: 'string', | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| type: 'string', | ||||||||||||||||||||||||||||
| user: { | ||||||||||||||||||||||||||||
| username: 'string', | ||||||||||||||||||||||||||||
| email: 'string', | ||||||||||||||||||||||||||||
| first_name: 'string', | ||||||||||||||||||||||||||||
| last_name: 'string', | ||||||||||||||||||||||||||||
| is_active: 'boolean', | ||||||||||||||||||||||||||||
| is_internal: 'boolean', | ||||||||||||||||||||||||||||
| is_org_admin: 'boolean', | ||||||||||||||||||||||||||||
| locale: 'string', | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| if (Object.keys(config).length) | ||||||||||||||||||||||||||||
|
MariaAga marked this conversation as resolved.
|
||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||
| <ScalprumContext.Provider value={contextData}> | ||||||||||||||||||||||||||||
| <ScalprumProvider | ||||||||||||||||||||||||||||
| pluginSDKOptions={{ | ||||||||||||||||||||||||||||
| pluginLoaderOptions: { | ||||||||||||||||||||||||||||
| transformPluginManifest: manifest => { | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| manifest.baseURL === 'auto' && | ||||||||||||||||||||||||||||
| config[manifest.name]?.cdnPath | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| const _cdnPath = config[manifest.name]?.cdnPath; | ||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||
| ...manifest, | ||||||||||||||||||||||||||||
| baseURL: _cdnPath, | ||||||||||||||||||||||||||||
| loadScripts: manifest.loadScripts.map( | ||||||||||||||||||||||||||||
| script => `${_cdnPath}${script}` | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
|
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): transformPluginManifest mutates loadScripts URLs but assumes all scripts are relative. This logic may fail if loadScripts includes absolute URLs. Please ensure only relative paths are prefixed, or clearly document the assumption that all scripts are relative.
Suggested change
|
||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return manifest; | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||
| api={{ | ||||||||||||||||||||||||||||
| chrome: { | ||||||||||||||||||||||||||||
| isBeta: () => false, | ||||||||||||||||||||||||||||
| on: () => {}, | ||||||||||||||||||||||||||||
| auth: { | ||||||||||||||||||||||||||||
| getUser: () => Promise.resolve(mockUser), | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||
|
MariaAga marked this conversation as resolved.
|
||||||||||||||||||||||||||||
| config={config} | ||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||
| {children} | ||||||||||||||||||||||||||||
| </ScalprumProvider> | ||||||||||||||||||||||||||||
| </ScalprumContext.Provider> | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||
| <ScalprumContext.Provider value={contextData}> | ||||||||||||||||||||||||||||
| {children} | ||||||||||||||||||||||||||||
| </ScalprumContext.Provider> | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ScalprumContextWrapper.propTypes = { | ||||||||||||||||||||||||||||
| children: PropTypes.node.isRequired, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.