2023-10-27 03:59:05 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-11-29 02:31:28 +08:00
|
|
|
import { createChannelIdentifier } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/hooks';
|
|
|
|
import { DataChannelArguments } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/types';
|
|
|
|
import {
|
|
|
|
HookEventWrapper, UnsubscribedEventDetails, SubscribedEventDetails,
|
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/types';
|
|
|
|
import {
|
|
|
|
Hooks, HookEvents,
|
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/enum';
|
|
|
|
|
|
|
|
import { PluginDataChannelManagerContainerProps } from './types';
|
|
|
|
import { DataChannelItemManager } from './channel-manager/manager';
|
2023-10-27 03:59:05 +08:00
|
|
|
|
|
|
|
const PluginDataChannelManagerContainer: React.ElementType<PluginDataChannelManagerContainerProps> = ((
|
|
|
|
props: PluginDataChannelManagerContainerProps,
|
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
pluginApi,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { pluginName: pluginNameInUse } = pluginApi;
|
|
|
|
if (!pluginNameInUse) return (<></>);
|
|
|
|
const [
|
|
|
|
mapOfDataChannelInformation,
|
|
|
|
setMapOfDataChannelInformation,
|
|
|
|
] = useState(new Map<string, number>());
|
|
|
|
|
|
|
|
const updateHookUsage = (channelName: string, pluginName: string, deltaSubscribe: number) => {
|
|
|
|
setMapOfDataChannelInformation((
|
|
|
|
previousMap: Map<string, number>,
|
|
|
|
) => {
|
|
|
|
if (pluginName === pluginNameInUse) {
|
|
|
|
const newMap = new Map<string, number>(previousMap.entries());
|
|
|
|
newMap.set(channelName, (previousMap.get(channelName) || 0) + deltaSubscribe);
|
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
return previousMap;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-29 02:31:28 +08:00
|
|
|
// Alterar aqui
|
2023-10-27 03:59:05 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const subscribeHandler: EventListener = (
|
2023-11-29 02:31:28 +08:00
|
|
|
(event: HookEventWrapper<void>) => {
|
|
|
|
if (event.detail.hook === Hooks.DATA_CHANNEL) {
|
|
|
|
const eventDetails = event.detail as SubscribedEventDetails;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
|
|
|
if (hookArguments?.channelName && hookArguments?.pluginName) {
|
|
|
|
updateHookUsage(hookArguments.channelName, hookArguments.pluginName, 1);
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
const unsubscribeHandler: EventListener = (
|
2023-11-29 02:31:28 +08:00
|
|
|
(event: HookEventWrapper<void>) => {
|
|
|
|
if (event.detail.hook === Hooks.DATA_CHANNEL) {
|
|
|
|
const eventDetails = event.detail as UnsubscribedEventDetails;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
|
|
|
if (hookArguments?.channelName && hookArguments?.pluginName) {
|
|
|
|
updateHookUsage(hookArguments.channelName, hookArguments.pluginName, 1);
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
|
2023-11-29 02:31:28 +08:00
|
|
|
window.addEventListener(HookEvents.SUBSCRIBED, subscribeHandler);
|
|
|
|
window.addEventListener(HookEvents.UNSUBSCRIBED, unsubscribeHandler);
|
2023-10-27 03:59:05 +08:00
|
|
|
return () => {
|
2023-11-29 02:31:28 +08:00
|
|
|
window.removeEventListener(HookEvents.SUBSCRIBED, subscribeHandler);
|
|
|
|
window.removeEventListener(HookEvents.UNSUBSCRIBED, unsubscribeHandler);
|
2023-10-27 03:59:05 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{
|
|
|
|
Array.from(mapOfDataChannelInformation.keys()).filter(
|
|
|
|
(keyChannelName: string) => mapOfDataChannelInformation.get(keyChannelName) !== undefined
|
|
|
|
&& mapOfDataChannelInformation.get(keyChannelName)! > 0,
|
|
|
|
).map((keyChannelName: string) => (
|
|
|
|
<DataChannelItemManager
|
|
|
|
{...{
|
2023-11-29 02:31:28 +08:00
|
|
|
key: createChannelIdentifier(keyChannelName, pluginNameInUse),
|
2023-10-27 03:59:05 +08:00
|
|
|
pluginName: pluginNameInUse,
|
|
|
|
channelName: keyChannelName,
|
2023-10-28 00:54:00 +08:00
|
|
|
pluginApi,
|
2023-10-27 03:59:05 +08:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}) as React.ElementType<PluginDataChannelManagerContainerProps>;
|
|
|
|
|
|
|
|
export default PluginDataChannelManagerContainer;
|