2023-09-13 22:31:20 +08:00
|
|
|
/* eslint-disable no-undef */
|
|
|
|
// Rule applied because EvenetListener is no undefined at all times.
|
2023-08-16 06:31:11 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
|
2023-09-13 22:31:20 +08:00
|
|
|
import CurrentPresentationHookContainer from './use-current-presentation/container';
|
|
|
|
import LoadedUserListHookContainer from './use-loaded-user-list/container';
|
2023-08-16 06:31:11 +08:00
|
|
|
|
2023-09-05 20:49:55 +08:00
|
|
|
const hooksMap:{
|
|
|
|
[key: string]: React.FunctionComponent
|
|
|
|
} = {
|
|
|
|
[PluginSdk.Internal.BbbHooks.UseCurrentPresentation]: CurrentPresentationHookContainer,
|
|
|
|
[PluginSdk.Internal.BbbHooks.UseLoadedUserList]: LoadedUserListHookContainer,
|
|
|
|
};
|
2023-08-16 06:31:11 +08:00
|
|
|
|
2023-09-11 21:12:37 +08:00
|
|
|
const PluginHooksHandlerContainer: React.FC = () => {
|
2023-09-05 20:49:55 +08:00
|
|
|
const [
|
|
|
|
hookUtilizationCount,
|
|
|
|
setHookUtilizationCount,
|
|
|
|
] = useState(new Map<string, number>());
|
2023-08-16 06:31:11 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-05 20:49:55 +08:00
|
|
|
const updateHookUsage = (hookName: string, delta: number):void => {
|
|
|
|
setHookUtilizationCount((mapObj) => {
|
|
|
|
const newMap = new Map<string, number>(mapObj.entries());
|
|
|
|
newMap.set(hookName, (mapObj.get(hookName) || 0) + delta);
|
|
|
|
return newMap;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-08-16 06:31:11 +08:00
|
|
|
const subscribeHandler: EventListener = (
|
|
|
|
(event: PluginSdk.CustomEventHookWrapper<void>) => {
|
2023-09-05 20:49:55 +08:00
|
|
|
updateHookUsage(event.detail.hook, 1);
|
2023-08-16 06:31:11 +08:00
|
|
|
}) as EventListener;
|
|
|
|
const unsubscribeHandler: EventListener = (
|
|
|
|
(event: PluginSdk.CustomEventHookWrapper<void>) => {
|
2023-09-05 20:49:55 +08:00
|
|
|
updateHookUsage(event.detail.hook, -1);
|
2023-08-16 06:31:11 +08:00
|
|
|
}) as EventListener;
|
|
|
|
|
|
|
|
window.addEventListener(PluginSdk.Internal.BbbHookEvents.Subscribe, subscribeHandler);
|
2023-09-05 20:49:55 +08:00
|
|
|
window.addEventListener(PluginSdk.Internal.BbbHookEvents.Unsubscribe, unsubscribeHandler);
|
2023-08-16 06:31:11 +08:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener(PluginSdk.Internal.BbbHookEvents.Subscribe, subscribeHandler);
|
|
|
|
window.removeEventListener(PluginSdk.Internal.BbbHookEvents.Unsubscribe, unsubscribeHandler);
|
2023-09-05 20:49:55 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-08-16 06:31:11 +08:00
|
|
|
return (
|
2023-09-11 21:12:37 +08:00
|
|
|
<>
|
|
|
|
{
|
|
|
|
Object.keys(hooksMap)
|
|
|
|
.filter((hookName: string) => hookUtilizationCount.get(hookName)
|
|
|
|
&& hookUtilizationCount.get(hookName)! > 0)
|
|
|
|
.map((hookName: string) => {
|
|
|
|
const HookComponent = hooksMap[hookName];
|
|
|
|
return <HookComponent key={hookName} />;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</>
|
2023-08-16 06:31:11 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-05 20:49:55 +08:00
|
|
|
export default PluginHooksHandlerContainer;
|