2024-05-01 19:38:12 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2024-04-20 04:34:43 +08:00
|
|
|
import { useSubscription } from '@apollo/client';
|
2024-05-02 03:48:12 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2024-04-20 04:34:43 +08:00
|
|
|
import {
|
|
|
|
VIDEO_STREAMS_SUBSCRIPTION,
|
|
|
|
VideoStreamsResponse,
|
|
|
|
} from './queries';
|
|
|
|
import { setStreams } from './state';
|
2024-05-01 19:38:12 +08:00
|
|
|
import { AdapterProps } from '../../components-data/graphqlToMiniMongoAdapterManager/component';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-01 19:38:12 +08:00
|
|
|
const VideoStreamAdapter: React.FC<AdapterProps> = ({
|
|
|
|
onReady,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ready = useRef(false);
|
2024-04-20 04:34:43 +08:00
|
|
|
const { data, loading, error } = useSubscription<VideoStreamsResponse>(VIDEO_STREAMS_SUBSCRIPTION);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-02 03:48:12 +08:00
|
|
|
if (loading) return;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
logger.error(`Video streams subscription failed. ${error.name}: ${error.message}`, error);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
setStreams([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const streams = data.user_camera.map(({ streamId, user, voice }) => ({
|
|
|
|
stream: streamId,
|
|
|
|
deviceId: streamId.split('_')[2],
|
|
|
|
userId: user.userId,
|
|
|
|
name: user.name,
|
|
|
|
sortName: user.nameSortable,
|
|
|
|
pin: user.pinned,
|
|
|
|
floor: voice?.floor || false,
|
|
|
|
lastFloorTime: voice?.lastFloorTime || '0',
|
2024-05-02 03:48:12 +08:00
|
|
|
isModerator: user.isModerator,
|
|
|
|
type: 'stream' as const,
|
2024-04-20 04:34:43 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
setStreams(streams);
|
|
|
|
}, [data]);
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
useEffect(() => {
|
2024-05-01 19:38:12 +08:00
|
|
|
if (!ready.current && !loading) {
|
|
|
|
ready.current = true;
|
|
|
|
onReady('VideoStreamAdapter');
|
|
|
|
}
|
2024-05-02 03:48:12 +08:00
|
|
|
}, [loading]);
|
2024-05-01 19:38:12 +08:00
|
|
|
|
|
|
|
return children;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default VideoStreamAdapter;
|