2024-04-24 05:17:32 +08:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { useMutation } from '@apollo/client';
|
|
|
|
import * as PluginSdk from 'bigbluebutton-html-plugin-sdk';
|
|
|
|
|
|
|
|
import {
|
|
|
|
DataChannelArguments,
|
|
|
|
PushEntryFunction, ObjectTo, ToRole, ToUserId,
|
2024-06-10 22:32:48 +08:00
|
|
|
ReplaceEntryFunctionArguments,
|
2024-04-24 05:17:32 +08:00
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/types';
|
|
|
|
import { DataChannelHooks } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/enums';
|
|
|
|
import { HookEvents } from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/enum';
|
|
|
|
import { HookEventWrapper, UpdatedEventDetails } from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/types';
|
|
|
|
|
2024-06-10 22:32:48 +08:00
|
|
|
import {
|
|
|
|
PLUGIN_DATA_CHANNEL_DELETE_MUTATION, PLUGIN_DATA_CHANNEL_PUSH_MUTATION,
|
|
|
|
PLUGIN_DATA_CHANNEL_REPLACE_MUTATION, PLUGIN_DATA_CHANNEL_RESET_MUTATION,
|
|
|
|
} from '../mutations';
|
2024-04-24 05:17:32 +08:00
|
|
|
|
|
|
|
export interface DataChannelItemManagerWriterProps {
|
|
|
|
pluginName: string;
|
|
|
|
channelName: string;
|
|
|
|
subChannelName: string;
|
|
|
|
pluginApi: PluginSdk.PluginApi;
|
|
|
|
dataChannelIdentifier: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MutationVariables {
|
|
|
|
pluginName: string,
|
|
|
|
channelName: string,
|
2024-05-23 04:34:43 +08:00
|
|
|
payloadJson: object,
|
2024-04-24 05:17:32 +08:00
|
|
|
toRoles: PluginSdk.DataChannelPushEntryFunctionUserRole[],
|
|
|
|
toUserIds: string[],
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SubscriptionVariables {
|
|
|
|
subChannelName: string;
|
|
|
|
pluginName: string;
|
|
|
|
channelName: string;
|
|
|
|
createdAt?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DataChannelItemManagerWriter: React.ElementType<DataChannelItemManagerWriterProps> = (
|
|
|
|
props: DataChannelItemManagerWriterProps,
|
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
pluginName,
|
|
|
|
channelName,
|
|
|
|
pluginApi,
|
|
|
|
subChannelName,
|
|
|
|
dataChannelIdentifier,
|
|
|
|
} = props;
|
|
|
|
|
2024-06-10 22:32:48 +08:00
|
|
|
const [pushEntryFunctionPluginDataChannel] = useMutation(PLUGIN_DATA_CHANNEL_PUSH_MUTATION);
|
|
|
|
const [deleteEntryFunctionPluginDataChannel] = useMutation(PLUGIN_DATA_CHANNEL_DELETE_MUTATION);
|
|
|
|
const [resetFunctionPluginDataChannel] = useMutation(PLUGIN_DATA_CHANNEL_RESET_MUTATION);
|
|
|
|
const [replaceEntryFunctionPluginDataChannel] = useMutation(PLUGIN_DATA_CHANNEL_REPLACE_MUTATION);
|
2024-04-24 05:17:32 +08:00
|
|
|
|
|
|
|
const useDataChannelHandlerFunction = ((msg: object, objectsTo?: ObjectTo[]) => {
|
|
|
|
const argumentsOfPushEntryFunction = {
|
|
|
|
variables: {
|
|
|
|
pluginName,
|
|
|
|
channelName,
|
|
|
|
subChannelName,
|
2024-05-23 04:34:43 +08:00
|
|
|
payloadJson: msg,
|
2024-04-24 05:17:32 +08:00
|
|
|
toRoles: [],
|
|
|
|
toUserIds: [],
|
|
|
|
} as MutationVariables,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (objectsTo) {
|
|
|
|
const rolesTo: PluginSdk.DataChannelPushEntryFunctionUserRole[] = objectsTo.filter((
|
|
|
|
object: ObjectTo,
|
|
|
|
) => 'role' in object).map(
|
|
|
|
(object: ObjectTo) => {
|
|
|
|
const toRole = object as ToRole;
|
|
|
|
return toRole.role;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const usersTo = objectsTo.filter((
|
|
|
|
object: ObjectTo,
|
|
|
|
) => 'userId' in object).map(
|
|
|
|
(object: ObjectTo) => {
|
|
|
|
const toUserId = object as ToUserId;
|
|
|
|
return toUserId.userId;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (rolesTo.length > 0) argumentsOfPushEntryFunction.variables.toRoles = rolesTo;
|
|
|
|
if (usersTo.length > 0) argumentsOfPushEntryFunction.variables.toUserIds = usersTo;
|
|
|
|
}
|
2024-06-10 22:32:48 +08:00
|
|
|
pushEntryFunctionPluginDataChannel(argumentsOfPushEntryFunction);
|
2024-04-24 05:17:32 +08:00
|
|
|
}) as PushEntryFunction;
|
|
|
|
|
|
|
|
pluginApi.mapOfPushEntryFunctions[dataChannelIdentifier] = useDataChannelHandlerFunction;
|
|
|
|
window.dispatchEvent(new Event(`${dataChannelIdentifier}::pushEntryFunction`));
|
|
|
|
|
|
|
|
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;
|
2024-06-10 22:32:48 +08:00
|
|
|
deleteEntryFunctionPluginDataChannel({
|
2024-04-24 05:17:32 +08:00
|
|
|
variables: {
|
|
|
|
pluginName: hookArguments?.pluginName,
|
|
|
|
channelName: hookArguments?.channelName,
|
2024-06-10 22:32:48 +08:00
|
|
|
entryId: eventDetails.data,
|
2024-04-24 05:17:32 +08:00
|
|
|
subChannelName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_RESET) {
|
|
|
|
const eventDetails = event.detail as UpdatedEventDetails<void>;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
2024-06-10 22:32:48 +08:00
|
|
|
resetFunctionPluginDataChannel({
|
2024-04-24 05:17:32 +08:00
|
|
|
variables: {
|
|
|
|
pluginName: hookArguments?.pluginName,
|
|
|
|
channelName: hookArguments?.channelName,
|
|
|
|
subChannelName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
|
2024-06-10 22:32:48 +08:00
|
|
|
const replaceEntryHandler: EventListener = (
|
|
|
|
(event: HookEventWrapper<void>) => {
|
|
|
|
if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_REPLACE) {
|
|
|
|
const eventDetails = event.detail as UpdatedEventDetails<ReplaceEntryFunctionArguments<object>>;
|
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments | undefined;
|
|
|
|
replaceEntryFunctionPluginDataChannel({
|
|
|
|
variables: {
|
|
|
|
pluginName: hookArguments?.pluginName,
|
|
|
|
channelName: hookArguments?.channelName,
|
|
|
|
subChannelName: hookArguments?.subChannelName,
|
|
|
|
entryId: eventDetails.data.entryId,
|
|
|
|
payloadJson: eventDetails.data.payloadJson,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
|
2024-04-24 05:17:32 +08:00
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener(HookEvents.UPDATED, deleteOrResetHandler);
|
2024-06-10 22:32:48 +08:00
|
|
|
window.addEventListener(HookEvents.UPDATED, replaceEntryHandler);
|
2024-04-24 05:17:32 +08:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener(HookEvents.UPDATED, deleteOrResetHandler);
|
2024-06-10 22:32:48 +08:00
|
|
|
window.removeEventListener(HookEvents.UPDATED, replaceEntryHandler);
|
2024-04-24 05:17:32 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DataChannelItemManagerWriter;
|