bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/audio/audio-graphql/hooks/useToggleVoice.ts
2024-01-29 11:05:39 -03:00

29 lines
1005 B
TypeScript

import { useCallback } from 'react';
import { useMutation, useSubscription } from '@apollo/client';
import Auth from '/imports/ui/services/auth';
import { USER_SET_MUTED } from '../mutations';
import { USER_MUTED, UserMutedResponse } from '../queries';
const useToggleVoice = () => {
const [userSetMuted] = useMutation(USER_SET_MUTED);
const { data: userMutedData } = useSubscription<UserMutedResponse>(USER_MUTED);
const toggleVoice = async (userId?: string | null, muted?: boolean | null) => {
let shouldMute = muted;
const userToMute = userId ?? Auth.userID;
if (muted === undefined || muted === null) {
const { user_voice } = userMutedData || {};
const userData = user_voice && user_voice.find((u) => u.userId === userToMute);
if (!userData) return;
shouldMute = !userData.muted;
}
userSetMuted({ variables: { muted: shouldMute, userId: userToMute } });
};
return useCallback(toggleVoice, [userMutedData]);
};
export default useToggleVoice;