The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/hooks/useFireGlobalEvent.ts

34 lines
1.2 KiB

import { useSetting } from '@rocket.chat/ui-contexts';
import { useMutation } from '@tanstack/react-query';
import { useEffect, useRef } from 'react';
import { fireGlobalEventBase } from '../lib/utils/fireGlobalEventBase';
const getScopeForEvent = (eventName: string, scope?: string) => (scope ? `${eventName}/${scope}` : eventName);
export const useFireGlobalEvent = (eventName: string, scope?: string) => {
const sendEnabled = useSetting('Iframe_Integration_send_enable');
const origin = useSetting('Iframe_Integration_send_target_origin');
const dispatchedRef = useRef({ scope: getScopeForEvent(eventName, scope), dispatched: false });
useEffect(() => {
const newScope = getScopeForEvent(eventName, scope);
if (dispatchedRef.current?.scope !== newScope) {
dispatchedRef.current = { scope: newScope, dispatched: false };
}
}, [scope, eventName]);
return useMutation({
mutationFn: async (data?: unknown) => {
if (scope && dispatchedRef.current.dispatched) {
return;
}
const postMessage = fireGlobalEventBase(eventName, data);
postMessage(sendEnabled as boolean, origin as string);
dispatchedRef.current.dispatched = true;
},
scope: scope ? { id: getScopeForEvent(eventName, scope) } : undefined,
});
};