2024-05-01 19:38:12 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2024-06-08 04:41:36 +08:00
|
|
|
import { throttle } from 'radash';
|
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-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '/imports/ui/core/hooks/useDeduplicatedSubscription';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-06-08 04:41:36 +08:00
|
|
|
const throttledSetStreams = throttle({ interval: 500 }, setStreams);
|
|
|
|
|
2024-05-01 19:38:12 +08:00
|
|
|
const VideoStreamAdapter: React.FC<AdapterProps> = ({
|
|
|
|
onReady,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ready = useRef(false);
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data, loading, error } = useDeduplicatedSubscription<VideoStreamsResponse>(VIDEO_STREAMS_SUBSCRIPTION);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (!data) {
|
2024-06-08 04:41:36 +08:00
|
|
|
throttledSetStreams([]);
|
2024-04-20 04:34:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const streams = data.user_camera.map(({ streamId, user, voice }) => ({
|
|
|
|
stream: streamId,
|
2024-05-08 05:05:36 +08:00
|
|
|
deviceId: streamId.split('_')[3],
|
2024-04-20 04:34:43 +08:00
|
|
|
name: user.name,
|
2024-05-06 21:45:59 +08:00
|
|
|
nameSortable: user.nameSortable,
|
2024-06-08 04:41:36 +08:00
|
|
|
userId: user.userId,
|
|
|
|
user,
|
|
|
|
floor: voice?.floor ?? false,
|
|
|
|
lastFloorTime: voice?.lastFloorTime ?? '0',
|
2024-05-02 03:48:12 +08:00
|
|
|
type: 'stream' as const,
|
2024-04-20 04:34:43 +08:00
|
|
|
}));
|
|
|
|
|
2024-06-08 04:41:36 +08:00
|
|
|
throttledSetStreams(streams);
|
2024-04-20 04:34:43 +08:00
|
|
|
}, [data]);
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
useEffect(() => {
|
2024-06-04 21:40:54 +08:00
|
|
|
if (!ready.current) {
|
2024-05-01 19:38:12 +08:00
|
|
|
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;
|