2023-08-16 06:31:11 +08:00
|
|
|
import React, { useEffect, useRef, useState, useMemo } from 'react';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
2023-09-06 21:38:22 +08:00
|
|
|
import * as uuid from 'uuid';
|
2023-08-16 06:31:11 +08:00
|
|
|
import PluginHooksHandlerContainer from './plugin-hooks-handler/container';
|
|
|
|
import PluginsEngineComponent from './component';
|
2023-09-05 20:49:55 +08:00
|
|
|
import { PluginConfig, EffectivePluginConfig } from './types';
|
2023-08-16 06:31:11 +08:00
|
|
|
import PluginLoaderContainer from './plugin-loader/container';
|
|
|
|
import PluginProvidedStateContainer from './plugin-provided-state/container';
|
|
|
|
|
2023-09-06 21:38:22 +08:00
|
|
|
// @ts-ignore - temporary, while meteor exists in the project
|
2023-08-16 06:31:11 +08:00
|
|
|
const PLUGINS_CONFIG = Meteor.settings.public.plugins;
|
|
|
|
|
|
|
|
const PluginsEngineContainer = () => {
|
|
|
|
// If there is no plugin to load, the engine simply returns null
|
|
|
|
if (!PLUGINS_CONFIG) return null;
|
|
|
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const [lastLoadedPlugin, setLastLoadedPlugin] = useState<HTMLScriptElement | undefined>();
|
|
|
|
const loadedPlugins = useRef<number>(0);
|
|
|
|
|
|
|
|
const effectivePluginsConfig: EffectivePluginConfig[] = useMemo<EffectivePluginConfig[]>(
|
2023-09-06 21:38:22 +08:00
|
|
|
() => PLUGINS_CONFIG.map((p: PluginConfig) => ({
|
2023-08-16 06:31:11 +08:00
|
|
|
...p,
|
|
|
|
uuid: uuid.v4(),
|
2023-09-06 21:38:22 +08:00
|
|
|
} as EffectivePluginConfig)), [
|
|
|
|
PLUGINS_CONFIG,
|
|
|
|
],
|
|
|
|
);
|
2023-08-16 06:31:11 +08:00
|
|
|
|
|
|
|
const totalNumberOfPlugins = PLUGINS_CONFIG?.length;
|
|
|
|
window.React = React;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
logger.info(`${loadedPlugins.current}/${totalNumberOfPlugins} plugins loaded`);
|
|
|
|
},
|
|
|
|
[loadedPlugins.current, lastLoadedPlugin]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PluginsEngineComponent
|
|
|
|
{...{
|
2023-09-06 21:38:22 +08:00
|
|
|
containerRef,
|
2023-08-16 06:31:11 +08:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{
|
|
|
|
effectivePluginsConfig.map((effectivePluginConfig: EffectivePluginConfig) => {
|
|
|
|
const uuid = effectivePluginConfig.uuid;
|
|
|
|
return (
|
|
|
|
<div key={uuid}>
|
2023-09-06 21:38:22 +08:00
|
|
|
<PluginLoaderContainer
|
2023-08-16 06:31:11 +08:00
|
|
|
{...{
|
|
|
|
uuid,
|
2023-09-06 21:38:22 +08:00
|
|
|
containerRef,
|
|
|
|
loadedPlugins,
|
2023-08-16 06:31:11 +08:00
|
|
|
setLastLoadedPlugin,
|
2023-09-06 21:38:22 +08:00
|
|
|
pluginConfig: effectivePluginConfig,
|
2023-08-16 06:31:11 +08:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<PluginProvidedStateContainer
|
|
|
|
{...{
|
|
|
|
uuid,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
<PluginHooksHandlerContainer />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PluginsEngineContainer;
|