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

Fixes #37454 - Allow js to be loaded late for react_component #10167

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
43 changes: 15 additions & 28 deletions webpack/assets/javascripts/react_app/common/AwaitedMount.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import PropTypes from 'prop-types';
import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } from 'react';
import store from '../redux';
import componentRegistry from '../components/componentRegistry';

// Mounts a component after all plugins have been imported to make sure that all plugins are available to the component
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really what it does? I see it's listening for the 'loadPlugin' event now, instead of 'loadJs'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadPlugin might happen after loadjs. example: user clicks a button, a tab appears, plugin has Deface::Override that calls loadPlugin

export const AwaitedMount = ({ component, data, flattenData }) => {
const [mounted, setMounted] = useState(false);
const [mountedComponent, setMountedComponent] = useState(null);
const [allPluginsImported, setAllPluginsImported] = useState(
window.allJsLoaded
);
async function mountComponent() {
const mountComponent = useCallback(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an async function? I don't see any await

if (componentRegistry.registry[component]) {
setMounted(true);
setMountedComponent(
Expand All @@ -20,32 +17,22 @@ export const AwaitedMount = ({ component, data, flattenData }) => {
flattenData,
})
);
} else if (allPluginsImported) {
const awaitedComponent = componentRegistry.markup(component, {
data,
store,
flattenData,
});
setMounted(true);
setMountedComponent(awaitedComponent);
} else {
// eslint-disable-next-line no-console
console.debug(
`Component not found: ${component}. The script for the component might not have been loaded yet.`
);
}
}
const updateAllPluginsImported = e => {
setAllPluginsImported(true);
};
useEffect(() => {
document.addEventListener('loadJS', updateAllPluginsImported);
return () => window.removeEventListener('loadJS', updateAllPluginsImported);
}, []);
}, [component, data, flattenData]);
useEffect(() => {
if (!mounted) mountComponent();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [allPluginsImported]);
document.addEventListener('loadPlugin', mountComponent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this is listening for all plugins' JS to be loaded and not just a single random plugin?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its listening to each plugin load, and then checking if the component is registered. if it is it loads it.

return () => {
window.removeEventListener('loadPlugin', mountComponent);
};
}, [mountComponent]);
useEffect(() => {
// Update the component if the data (props) change
if (allPluginsImported) mountComponent();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data]);
mountComponent();
}, [mountComponent]);
return mounted ? mountedComponent : null;
};

Expand Down
Loading