Skip to content
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

Jwesson/plugin profile page #866

Merged
merged 10 commits into from
Oct 12, 2023
290 changes: 200 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
],
"dependencies": {
"@edx/brand": "npm:@edx/[email protected]",
"@edx/frontend-component-footer": "12.2.1",
"@edx/frontend-component-header": "4.6.1",
"@edx/frontend-component-footer": "12.3.0",
"@edx/frontend-component-header": "4.7.1",
"@edx/frontend-platform": "5.4.0",
"@edx/paragon": "^20.44.0",
"@fortawesome/fontawesome-svg-core": "1.2.36",
Expand All @@ -49,6 +49,7 @@
"prop-types": "15.8.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-error-boundary": "^4.0.11",
"react-helmet": "6.1.0",
"react-redux": "7.2.9",
"react-router": "6.16.0",
Expand All @@ -72,7 +73,7 @@
"@wojtekmaj/enzyme-adapter-react-17": "0.8.0",
"codecov": "3.8.3",
"enzyme": "3.11.0",
"glob": "10.3.7",
"glob": "10.3.10",
"react-test-renderer": "17.0.2",
"reactifex": "1.1.1",
"redux-mock-store": "1.5.4"
Expand Down
92 changes: 92 additions & 0 deletions plugins/Plugin.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use client';

import React, {
useEffect, useMemo, useState,
} from 'react';
import PropTypes from 'prop-types';
import { ErrorBoundary } from 'react-error-boundary';
import { logError } from '@edx/frontend-platform/logging';
import {
dispatchMountedEvent, dispatchReadyEvent, dispatchUnmountedEvent, useHostEvent,
} from './data/hooks';
import { PLUGIN_RESIZE } from './data/constants';

// see example-plugin-app/src/PluginOne.jsx for example of customizing errorFallback
function errorFallbackDefault() {
return (
<div>
<h2>
Oops! An error occurred. Please refresh the screen to try again.
</h2>
</div>
);
}

export default function Plugin({

Check failure on line 25 in plugins/Plugin.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function

Check failure on line 25 in plugins/Plugin.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function
children, className, style, ready, errorFallbackProp,
}) {
const [dimensions, setDimensions] = useState({
width: null,
height: null,
});

const finalStyle = useMemo(() => ({
...dimensions,
...style,
}), [dimensions, style]);

const errorFallback = errorFallbackProp || errorFallbackDefault;

// Error logging function
// Need to confirm: When an error is caught here, the logging will be sent to the child MFE's logging service
const logErrorToService = (error, info) => {
logError(error, { stack: info.componentStack });
};

useHostEvent(PLUGIN_RESIZE, ({ payload }) => {
setDimensions({
width: payload.width,
height: payload.height,
});
});

useEffect(() => {
dispatchMountedEvent();

return () => {
dispatchUnmountedEvent();
};
}, []);

useEffect(() => {
if (ready) {
dispatchReadyEvent();
}
}, [ready]);

return (
<div className={className} style={finalStyle}>
<ErrorBoundary
FallbackComponent={errorFallback}
onError={logErrorToService}
>
{children}
</ErrorBoundary>
</div>
);
}

Plugin.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
errorFallbackProp: PropTypes.func,
ready: PropTypes.bool,
style: PropTypes.object, // eslint-disable-line
};

Plugin.defaultProps = {
className: null,
errorFallbackProp: null,
style: {},
ready: true,
};
41 changes: 41 additions & 0 deletions plugins/PluginContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';

import React from 'react';

// eslint-disable-next-line import/no-extraneous-dependencies
import PluginContainerIframe from './PluginContainerIframe';

import {
IFRAME_PLUGIN,
} from './data/constants';
import { pluginConfigShape } from './data/shapes';

export default function PluginContainer({ config, ...props }) {

Check failure on line 13 in plugins/PluginContainer.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function

Check failure on line 13 in plugins/PluginContainer.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function
if (config === null) {
return null;
}

// this will allow for future plugin types to be inserted in the PluginErrorBoundary
let renderer = null;
switch (config.type) {
case IFRAME_PLUGIN:
renderer = (
<PluginContainerIframe config={config} {...props} />
);
break;
// istanbul ignore next: default isn't meaningful, just satisfying linter
default:
}

return (
renderer
);
}

PluginContainer.propTypes = {
config: pluginConfigShape,
};

PluginContainer.defaultProps = {
config: null,
};
98 changes: 98 additions & 0 deletions plugins/PluginContainerIframe.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, {
useEffect, useState,
} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import {
PLUGIN_MOUNTED,
PLUGIN_READY,
PLUGIN_RESIZE,
} from './data/constants';
import {
dispatchPluginEvent,
useElementSize,
usePluginEvent,
} from './data/hooks';
import { pluginConfigShape } from './data/shapes';

/**
* Feature policy for iframe, allowing access to certain courseware-related media.
*
* We must use the wildcard (*) origin for each feature, as courseware content
* may be embedded in external iframes. Notably, xblock-lti-consumer is a popular
* block that iframes external course content.

* This policy was selected in conference with the edX Security Working Group.
* Changes to it should be vetted by them ([email protected]).
*/
export const IFRAME_FEATURE_POLICY = (
'fullscreen; microphone *; camera *; midi *; geolocation *; encrypted-media *'
);

export default function PluginContainerIframe({

Check failure on line 33 in plugins/PluginContainerIframe.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function

Check failure on line 33 in plugins/PluginContainerIframe.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Function component is not an arrow function
config, fallback, className, ...props
}) {
const { url } = config;
const { title, scrolling } = props;
const [mounted, setMounted] = useState(false);
const [ready, setReady] = useState(false);

const [iframeRef, iframeElement, width, height] = useElementSize();

useEffect(() => {
if (mounted) {
dispatchPluginEvent(iframeElement, {
type: PLUGIN_RESIZE,
payload: {
width,
height,
},
}, url);
}
}, [iframeElement, mounted, width, height, url]);

usePluginEvent(iframeElement, PLUGIN_MOUNTED, () => {
setMounted(true);
});

usePluginEvent(iframeElement, PLUGIN_READY, () => {
setReady(true);
});

return (
<>
<iframe
ref={iframeRef}
title={title}
src={url}
allow={IFRAME_FEATURE_POLICY}
scrolling={scrolling}
referrerPolicy="origin" // The sent referrer will be limited to the origin of the referring page: its scheme, host, and port.
className={classNames(
'border border-0',
{ 'd-none': !ready },
className,
)}
{...props}
/>
{!ready && fallback}
</>
);
}

PluginContainerIframe.propTypes = {
config: pluginConfigShape,
fallback: PropTypes.node,
scrolling: PropTypes.oneOf(['auto', 'yes', 'no']),
title: PropTypes.string,
className: PropTypes.string,
};

PluginContainerIframe.defaultProps = {
config: null,
fallback: null,
scrolling: 'auto',
title: null,
className: null,
};
44 changes: 44 additions & 0 deletions plugins/PluginErrorBoundary.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';

Check failure on line 3 in plugins/PluginErrorBoundary.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'react-intl' should be listed in the project's dependencies. Run 'npm i -S react-intl' to add it

Check failure on line 3 in plugins/PluginErrorBoundary.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'react-intl' should be listed in the project's dependencies. Run 'npm i -S react-intl' to add it

import { logError } from '@edx/frontend-platform/logging';

export default class PluginErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}

static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}

componentDidCatch(error, info) {
logError(error, { stack: info.componentStack });
}

render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<FormattedMessage
id="plugin.load.failure.text"
defaultMessage="This content failed to load."
description="error message when an unexpected error occurs"
/>
);
}

return this.props.children;
}
}

PluginErrorBoundary.propTypes = {
children: PropTypes.node,
};

PluginErrorBoundary.defaultProps = {
children: null,
};
74 changes: 74 additions & 0 deletions plugins/PluginSlot.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { forwardRef } from 'react';

import classNames from 'classnames';
import { Spinner } from '@edx/paragon';
import PropTypes from 'prop-types';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';

// import { usePluginSlot } from './data/hooks';
import PluginContainer from './PluginContainer';

Check failure on line 9 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'PluginContainer' is defined but never used

Check failure on line 9 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'PluginContainer' is defined but never used

const PluginSlot = forwardRef(({
as, id, intl, pluginProps, children, ...props
}, ref) => {
/* the plugins below are obtained by the id passed into PluginSlot by the Host MFE. See example/src/PluginsPage.jsx
for an example of how PluginSlot is populated, and example/src/index.jsx for a dummy JS config that holds all plugins
*/
// const { plugins, keepDefault } = usePluginSlot(id);

const { fallback } = pluginProps;

// TODO: Add internationalization to the "Loading" text on the spinner.
let finalFallback = (
<div className={classNames(pluginProps.className, 'd-flex justify-content-center align-items-center')}>
<Spinner animation="border" screenReaderText="Loading" />
</div>
);
if (fallback !== undefined) {
finalFallback = fallback;

Check failure on line 28 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'finalFallback' is assigned a value but never used

Check failure on line 28 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

'finalFallback' is assigned a value but never used
}

let finalChildren = [];
// if (plugins.length > 0) {
// if (keepDefault) {

Check failure on line 33 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4

Check failure on line 33 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4
// finalChildren.push(children);

Check failure on line 34 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4

Check failure on line 34 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4
// }

Check failure on line 35 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4

Check failure on line 35 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4
// plugins.forEach((pluginConfig) => {

Check failure on line 36 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4

Check failure on line 36 in plugins/PluginSlot.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Expected indentation of 2 spaces but found 4
// finalChildren.push(
// <PluginContainer
// key={pluginConfig.url}
// config={pluginConfig}
// fallback={finalFallback}
// {...pluginProps}
// />,
// );
// });
// } else {
finalChildren = children;
// }

return React.createElement(
as,
{
...props,
ref,
},
finalChildren,
);
});

export default injectIntl(PluginSlot);

PluginSlot.propTypes = {
as: PropTypes.elementType,
children: PropTypes.node,
id: PropTypes.string.isRequired,
intl: intlShape.isRequired,
pluginProps: PropTypes.object, // eslint-disable-line
};

PluginSlot.defaultProps = {
as: 'div',
children: null,
pluginProps: {},
};
8 changes: 8 additions & 0 deletions plugins/data/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// TODO: We expect other plugin types to be added here, such as LTI_PLUGIN and BUILD_TIME_PLUGIN.
export const IFRAME_PLUGIN = 'IFRAME_PLUGIN'; // loads iframe at the URL, rather than loading a JS file.

// Plugin lifecycle events
export const PLUGIN_MOUNTED = 'PLUGIN_MOUNTED';
export const PLUGIN_READY = 'PLUGIN_READY';
export const PLUGIN_UNMOUNTED = 'PLUGIN_UNMOUNTED';
export const PLUGIN_RESIZE = 'PLUGIN_RESIZE';
Loading
Loading