2024-01-16 01:12:39 +08:00
|
|
|
import { PluginLoaderManagerProps } from './types';
|
2023-08-16 06:31:11 +08:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
2024-01-16 01:12:39 +08:00
|
|
|
const PluginLoaderManager = (props: PluginLoaderManagerProps) => {
|
2023-09-05 20:49:55 +08:00
|
|
|
const {
|
|
|
|
uuid,
|
|
|
|
containerRef,
|
|
|
|
loadedPlugins,
|
|
|
|
setLastLoadedPlugin,
|
|
|
|
pluginConfig: plugin,
|
|
|
|
} = props;
|
2023-08-16 06:31:11 +08:00
|
|
|
|
2023-09-05 20:49:55 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!plugin || !containerRef) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-16 06:31:11 +08:00
|
|
|
|
2023-09-05 20:49:55 +08:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.id = uuid;
|
|
|
|
containerRef.current?.appendChild(div);
|
|
|
|
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.onload = () => {
|
|
|
|
loadedPlugins.current += 1;
|
|
|
|
setLastLoadedPlugin(script);
|
|
|
|
logger.info(`Loaded plugin ${plugin.name}`);
|
|
|
|
};
|
|
|
|
script.onerror = (err) => {
|
2023-09-19 19:39:52 +08:00
|
|
|
logger.error(`Error when loading plugin ${plugin.name}, error: `, err);
|
2023-09-05 20:49:55 +08:00
|
|
|
};
|
|
|
|
script.src = plugin.url;
|
|
|
|
script.setAttribute('uuid', div.id);
|
|
|
|
script.setAttribute('pluginName', plugin.name);
|
|
|
|
document.head.appendChild(script);
|
|
|
|
}, [plugin, containerRef]);
|
|
|
|
return null;
|
|
|
|
};
|
2023-08-16 06:31:11 +08:00
|
|
|
|
2024-01-16 01:12:39 +08:00
|
|
|
export default PluginLoaderManager;
|