2023-10-27 03:59:05 +08:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { useMutation, useSubscription } from '@apollo/client';
|
|
|
|
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
|
|
|
|
|
2024-01-31 05:38:09 +08:00
|
|
|
import { createChannelIdentifier } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/utils';
|
2023-11-29 02:31:28 +08:00
|
|
|
import {
|
2024-01-31 05:38:09 +08:00
|
|
|
DataChannelArguments,
|
2023-11-29 02:31:28 +08:00
|
|
|
DispatcherFunction, ObjectTo, ToRole, ToUserId,
|
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/types';
|
2024-01-16 01:12:39 +08:00
|
|
|
import { DataChannelHooks } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/enums';
|
2024-01-31 05:38:09 +08:00
|
|
|
import { HookEvents } from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/enum';
|
|
|
|
import { HookEventWrapper, UpdatedEventDetails } from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/types';
|
2023-11-29 02:31:28 +08:00
|
|
|
|
2024-01-31 05:38:09 +08:00
|
|
|
import PLUGIN_DATA_CHANNEL_FETCH_QUERY from '../queries';
|
|
|
|
import { PLUGIN_DATA_CHANNEL_DELETE_MUTATION, PLUGIN_DATA_CHANNEL_DISPATCH_MUTATION, PLUGIN_DATA_CHANNEL_RESET_MUTATION } from '../mutation';
|
2023-10-27 03:59:05 +08:00
|
|
|
|
|
|
|
export interface DataChannelItemManagerProps {
|
|
|
|
pluginName: string;
|
|
|
|
channelName: string;
|
2023-10-28 00:54:00 +08:00
|
|
|
pluginApi: PluginSdk.PluginApi
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MutationVariables {
|
|
|
|
pluginName: string,
|
|
|
|
dataChannel: string,
|
|
|
|
payloadJson: string,
|
2023-11-29 02:31:28 +08:00
|
|
|
toRoles: PluginSdk.DataChannelDispatcherUserRole[],
|
2023-10-27 03:59:05 +08:00
|
|
|
toUserIds: string[],
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DataChannelItemManager: React.ElementType<DataChannelItemManagerProps> = (
|
|
|
|
props: DataChannelItemManagerProps,
|
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
pluginName,
|
|
|
|
channelName,
|
2023-10-28 00:54:00 +08:00
|
|
|
pluginApi,
|
2023-10-27 03:59:05 +08:00
|
|
|
} = props;
|
2023-11-29 02:31:28 +08:00
|
|
|
const pluginIdentifier = createChannelIdentifier(channelName, pluginName);
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2023-11-29 02:31:28 +08:00
|
|
|
const dataChannelIdentifier = createChannelIdentifier(channelName, pluginName);
|
2024-01-31 05:38:09 +08:00
|
|
|
const [dispatchPluginDataChannelMessage] = useMutation(PLUGIN_DATA_CHANNEL_DISPATCH_MUTATION);
|
|
|
|
const [deletePluginDataChannelMessage] = useMutation(PLUGIN_DATA_CHANNEL_DELETE_MUTATION);
|
|
|
|
const [resetPluginDataChannelMessage] = useMutation(PLUGIN_DATA_CHANNEL_RESET_MUTATION);
|
2023-10-27 03:59:05 +08:00
|
|
|
|
|
|
|
const data = useSubscription(PLUGIN_DATA_CHANNEL_FETCH_QUERY, {
|
|
|
|
variables: {
|
|
|
|
pluginName,
|
|
|
|
channelName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-29 02:31:28 +08:00
|
|
|
const useDataChannelHandlerFunction = ((msg: object, objectsTo?: ObjectTo[]) => {
|
2023-10-27 03:59:05 +08:00
|
|
|
const argumentsOfDispatcher = {
|
|
|
|
variables: {
|
|
|
|
pluginName,
|
|
|
|
dataChannel: channelName,
|
|
|
|
payloadJson: JSON.stringify(msg),
|
|
|
|
toRoles: [],
|
|
|
|
toUserIds: [],
|
|
|
|
} as MutationVariables,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (objectsTo) {
|
2023-11-29 02:31:28 +08:00
|
|
|
const rolesTo: PluginSdk.DataChannelDispatcherUserRole[] = objectsTo.filter((
|
|
|
|
object: ObjectTo,
|
2023-10-27 03:59:05 +08:00
|
|
|
) => 'role' in object).map(
|
2023-11-29 02:31:28 +08:00
|
|
|
(object: ObjectTo) => {
|
|
|
|
const toRole = object as ToRole;
|
2023-10-27 03:59:05 +08:00
|
|
|
return toRole.role;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const usersTo = objectsTo.filter((
|
2023-11-29 02:31:28 +08:00
|
|
|
object: ObjectTo,
|
2023-10-27 03:59:05 +08:00
|
|
|
) => 'userId' in object).map(
|
2023-11-29 02:31:28 +08:00
|
|
|
(object: ObjectTo) => {
|
|
|
|
const toUserId = object as ToUserId;
|
2023-10-27 03:59:05 +08:00
|
|
|
return toUserId.userId;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (rolesTo.length > 0) argumentsOfDispatcher.variables.toRoles = rolesTo;
|
|
|
|
if (usersTo.length > 0) argumentsOfDispatcher.variables.toUserIds = usersTo;
|
|
|
|
}
|
|
|
|
dispatchPluginDataChannelMessage(argumentsOfDispatcher);
|
2023-11-29 02:31:28 +08:00
|
|
|
}) as DispatcherFunction;
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2023-10-28 00:54:00 +08:00
|
|
|
pluginApi.mapOfDispatchers[pluginIdentifier] = useDataChannelHandlerFunction;
|
|
|
|
window.dispatchEvent(new Event(`${pluginIdentifier}::dispatcherFunction`));
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2024-01-31 05:38:09 +08:00
|
|
|
const deleteOrResetHandler: EventListener = (
|
|
|
|
(event: HookEventWrapper<void>) => {
|
|
|
|
if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_DELETE) {
|
|
|
|
const eventDetails = event.detail as UpdatedEventDetails<string>;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
|
|
|
deletePluginDataChannelMessage({
|
|
|
|
variables: {
|
|
|
|
pluginName: hookArguments?.pluginName,
|
|
|
|
dataChannel: hookArguments?.channelName,
|
|
|
|
messageId: eventDetails.data,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_RESET) {
|
|
|
|
const eventDetails = event.detail as UpdatedEventDetails<void>;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
|
|
|
resetPluginDataChannelMessage({
|
|
|
|
variables: {
|
|
|
|
pluginName: hookArguments?.pluginName,
|
|
|
|
dataChannel: hookArguments?.channelName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
|
2023-10-27 03:59:05 +08:00
|
|
|
useEffect(() => {
|
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent(dataChannelIdentifier, {
|
2024-01-31 05:38:09 +08:00
|
|
|
detail: { hook: DataChannelHooks.DATA_CHANNEL_BUILDER, data },
|
2023-10-27 03:59:05 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}, [data]);
|
2024-01-31 05:38:09 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener(HookEvents.UPDATED, deleteOrResetHandler);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener(HookEvents.UPDATED, deleteOrResetHandler);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-10-27 03:59:05 +08:00
|
|
|
return null;
|
|
|
|
};
|