bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/audio/audio-graphql/hooks/useToggleVoice.ts

29 lines
1005 B
TypeScript
Raw Normal View History

2024-01-29 20:49:40 +08:00
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;