bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/components-data/userGrapQlMiniMongoAdapter/component.tsx

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-02-23 23:01:53 +08:00
import { useSubscription } from '@apollo/client';
import React, { useEffect, useRef, useState } from 'react';
2024-02-23 23:01:53 +08:00
import CURRENT_USER_SUBSCRIPTION from '/imports/ui/core/graphql/queries/currentUserSubscription';
import { User } from '/imports/ui/Types/user';
import Users from '/imports/api/users';
import logger from '/imports/startup/client/logger';
import { AdapterProps } from '../graphqlToMiniMongoAdapterManager/component';
2024-02-23 23:01:53 +08:00
interface UserCurrentResponse {
user_current: Array<User>;
}
const UserGrapQlMiniMongoAdapter: React.FC<AdapterProps> = ({
onReady,
children,
}) => {
const ready = useRef(false);
2024-02-23 23:01:53 +08:00
const {
error,
data,
} = useSubscription<UserCurrentResponse>(CURRENT_USER_SUBSCRIPTION);
const [userDataSetted, setUserDataSetted] = useState(false);
useEffect(() => {
if (error) {
logger.error('Error in UserGrapQlMiniMongoAdapter', error);
}
}, [error]);
useEffect(() => {
if (data && data.user_current) {
if (!ready.current) {
ready.current = true;
onReady('UserGrapQlMiniMongoAdapter');
}
2024-02-23 23:01:53 +08:00
const { userId } = data.user_current[0];
Users.upsert({ userId }, data.user_current[0]);
if (!userDataSetted) {
setUserDataSetted(true);
}
}
}, [data]);
return children;
2024-02-23 23:01:53 +08:00
};
export default UserGrapQlMiniMongoAdapter;