2024-03-07 00:08:32 +08:00
|
|
|
import { useSubscription } from '@apollo/client';
|
2024-05-01 19:38:12 +08:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2024-03-07 00:08:32 +08:00
|
|
|
import VoiceUsers from '/imports/api/voice-users/';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import { UserVoiceStreamResponse, voiceUserStream } from './queries';
|
2024-05-01 19:38:12 +08:00
|
|
|
import { AdapterProps } from '../graphqlToMiniMongoAdapterManager/component';
|
2024-03-07 00:08:32 +08:00
|
|
|
|
2024-05-01 19:38:12 +08:00
|
|
|
const VoiceUserGrapQlMiniMongoAdapter: React.FC<AdapterProps> = ({
|
|
|
|
onReady,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ready = useRef(false);
|
2024-03-07 00:08:32 +08:00
|
|
|
const {
|
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
data,
|
|
|
|
} = useSubscription<UserVoiceStreamResponse>(voiceUserStream);
|
|
|
|
const [voiceUserDataSetted, setVoiceUserDataSetted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Error in VoiceUserGrapQlMiniMongoAdapter', error);
|
|
|
|
}
|
|
|
|
}, [error]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-07 22:56:38 +08:00
|
|
|
if (data && data.user_voice_mongodb_adapter_stream) {
|
|
|
|
const usersVoice = data.user_voice_mongodb_adapter_stream;
|
|
|
|
|
2024-03-07 00:08:32 +08:00
|
|
|
usersVoice.forEach((userVoice) => {
|
|
|
|
VoiceUsers.upsert({ userId: userVoice.userId }, userVoice);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (loading) {
|
2024-05-01 19:38:12 +08:00
|
|
|
// loading turns false only first audio join of the meeting, probably because it's a stream
|
|
|
|
if (!ready.current) {
|
|
|
|
ready.current = true;
|
|
|
|
onReady('VoiceUserGrapQlMiniMongoAdapter');
|
|
|
|
}
|
2024-03-07 00:08:32 +08:00
|
|
|
if (!voiceUserDataSetted) {
|
|
|
|
setVoiceUserDataSetted(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [loading]);
|
2024-05-01 19:38:12 +08:00
|
|
|
return children;
|
2024-03-07 00:08:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default VoiceUserGrapQlMiniMongoAdapter;
|