2024-06-04 21:40:54 +08:00
|
|
|
import { useMutation, useQuery } from '@apollo/client';
|
2024-06-06 04:41:23 +08:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2024-03-07 01:28:18 +08:00
|
|
|
import { Session } from 'meteor/session';
|
|
|
|
import {
|
|
|
|
getUserCurrent,
|
|
|
|
GetUserCurrentResponse,
|
|
|
|
getUserInfo,
|
|
|
|
GetUserInfoResponse,
|
|
|
|
userJoinMutation,
|
|
|
|
} from './queries';
|
|
|
|
import { setAuthData } from '/imports/ui/core/local-states/useAuthData';
|
2024-05-01 20:39:03 +08:00
|
|
|
import MeetingEndedContainer from '../../meeting-ended/component';
|
2024-03-07 01:28:18 +08:00
|
|
|
import { setUserDataToSessionStorage } from './service';
|
|
|
|
import { LoadingContext } from '../../common/loading-screen/loading-screen-HOC/component';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '/imports/ui/core/hooks/useDeduplicatedSubscription';
|
2024-05-30 04:43:17 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import deviceInfo from '/imports/utils/deviceInfo';
|
2024-06-06 04:41:23 +08:00
|
|
|
import GuestWaitContainer, { GUEST_STATUSES } from '../guest-wait/component';
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
const connectionTimeout = 60000;
|
2024-06-06 04:41:23 +08:00
|
|
|
const MESSAGE_TIMEOUT = 3000;
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
interface PresenceManagerContainerProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PresenceManagerProps extends PresenceManagerContainerProps {
|
|
|
|
authToken: string;
|
|
|
|
logoutUrl: string;
|
|
|
|
meetingId: string;
|
|
|
|
meetingName: string;
|
|
|
|
userName: string;
|
|
|
|
extId: string;
|
|
|
|
userId: string;
|
|
|
|
joinErrorCode: string;
|
|
|
|
joinErrorMessage: string;
|
|
|
|
joined: boolean;
|
|
|
|
meetingEnded: boolean;
|
|
|
|
endedReasonCode: string;
|
|
|
|
endedBy: string;
|
|
|
|
ejectReasonCode: string;
|
|
|
|
bannerColor: string;
|
|
|
|
bannerText: string;
|
2024-05-03 20:31:18 +08:00
|
|
|
customLogoUrl: string;
|
2024-04-05 21:54:08 +08:00
|
|
|
loggedOut: boolean;
|
2024-06-06 04:41:23 +08:00
|
|
|
guestStatus: string;
|
|
|
|
guestLobbyMessage: string | null;
|
|
|
|
positionInWaitingQueue: number | null;
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const PresenceManager: React.FC<PresenceManagerProps> = ({
|
|
|
|
authToken,
|
|
|
|
children,
|
|
|
|
logoutUrl,
|
|
|
|
meetingId,
|
|
|
|
meetingName,
|
|
|
|
userName,
|
|
|
|
extId,
|
|
|
|
userId,
|
|
|
|
joinErrorCode,
|
|
|
|
joinErrorMessage,
|
|
|
|
joined,
|
|
|
|
meetingEnded,
|
|
|
|
endedReasonCode,
|
|
|
|
endedBy,
|
|
|
|
ejectReasonCode,
|
|
|
|
bannerColor,
|
|
|
|
bannerText,
|
2024-05-03 20:31:18 +08:00
|
|
|
customLogoUrl,
|
2024-04-05 21:54:08 +08:00
|
|
|
loggedOut,
|
2024-06-06 04:41:23 +08:00
|
|
|
guestLobbyMessage,
|
|
|
|
guestStatus,
|
|
|
|
positionInWaitingQueue,
|
2024-03-07 01:28:18 +08:00
|
|
|
}) => {
|
|
|
|
const [allowToRender, setAllowToRender] = React.useState(false);
|
|
|
|
const [dispatchUserJoin] = useMutation(userJoinMutation);
|
|
|
|
const timeoutRef = React.useRef<ReturnType<typeof setTimeout>>();
|
|
|
|
const loadingContextInfo = useContext(LoadingContext);
|
2024-06-06 04:41:23 +08:00
|
|
|
const [isGuestAllowed, setIsGuestAllowed] = useState(guestStatus === GUEST_STATUSES.ALLOW);
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-06 04:41:23 +08:00
|
|
|
const allowed = guestStatus === GUEST_STATUSES.ALLOW;
|
|
|
|
if (allowed) {
|
|
|
|
setTimeout(() => {
|
|
|
|
setIsGuestAllowed(true);
|
|
|
|
}, MESSAGE_TIMEOUT);
|
|
|
|
} else {
|
|
|
|
setIsGuestAllowed(false);
|
|
|
|
}
|
|
|
|
}, [guestStatus]);
|
2024-03-07 01:28:18 +08:00
|
|
|
|
2024-06-06 04:41:23 +08:00
|
|
|
useEffect(() => {
|
2024-03-07 01:28:18 +08:00
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
const sessionToken = urlParams.get('sessionToken') as string;
|
|
|
|
setAuthData({
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
authToken,
|
|
|
|
logoutUrl,
|
|
|
|
sessionToken,
|
|
|
|
userName,
|
|
|
|
extId,
|
|
|
|
meetingName,
|
|
|
|
});
|
|
|
|
setUserDataToSessionStorage({
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
authToken,
|
|
|
|
logoutUrl,
|
|
|
|
sessionToken,
|
|
|
|
userName,
|
|
|
|
extId,
|
|
|
|
meetingName,
|
2024-05-03 20:31:18 +08:00
|
|
|
customLogoUrl,
|
2024-03-07 01:28:18 +08:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2024-06-06 04:41:23 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (isGuestAllowed) {
|
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
loadingContextInfo.setLoading(false, '');
|
|
|
|
throw new Error('Authentication timeout');
|
|
|
|
}, connectionTimeout);
|
|
|
|
}
|
|
|
|
}, [isGuestAllowed]);
|
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (bannerColor || bannerText) {
|
|
|
|
Session.set('bannerText', bannerText);
|
|
|
|
Session.set('bannerColor', bannerColor);
|
|
|
|
}
|
|
|
|
}, [bannerColor, bannerText]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-06 04:41:23 +08:00
|
|
|
if (authToken && !joined && isGuestAllowed) {
|
2024-03-07 01:28:18 +08:00
|
|
|
dispatchUserJoin({
|
|
|
|
variables: {
|
|
|
|
authToken,
|
|
|
|
clientType: 'HTML5',
|
2024-05-30 04:43:17 +08:00
|
|
|
clientIsMobile: deviceInfo.isMobile,
|
2024-03-07 01:28:18 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-06-06 04:41:23 +08:00
|
|
|
}, [joined, authToken, isGuestAllowed]);
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (joined) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
2024-04-25 23:48:48 +08:00
|
|
|
setAllowToRender(true);
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
|
|
|
}, [joined]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (joinErrorCode) {
|
|
|
|
loadingContextInfo.setLoading(false, '');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[joinErrorCode, joinErrorMessage]);
|
|
|
|
|
2024-04-05 21:54:08 +08:00
|
|
|
const errorCode = loggedOut ? 'user_logged_out_reason' : joinErrorCode || ejectReasonCode;
|
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
return (
|
|
|
|
<>
|
2024-04-05 21:54:08 +08:00
|
|
|
{allowToRender && !(meetingEnded || joinErrorCode || ejectReasonCode || loggedOut) ? children : null}
|
2024-03-07 01:28:18 +08:00
|
|
|
{
|
2024-04-05 21:54:08 +08:00
|
|
|
meetingEnded || joinErrorCode || ejectReasonCode || loggedOut
|
2024-03-07 01:28:18 +08:00
|
|
|
? (
|
|
|
|
<MeetingEndedContainer
|
|
|
|
meetingEndedCode={endedReasonCode}
|
|
|
|
endedBy={endedBy}
|
2024-04-05 21:54:08 +08:00
|
|
|
joinErrorCode={errorCode}
|
2024-03-07 01:28:18 +08:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
2024-06-06 04:41:23 +08:00
|
|
|
{
|
|
|
|
!isGuestAllowed && !(meetingEnded || joinErrorCode || ejectReasonCode || loggedOut)
|
|
|
|
? (
|
|
|
|
<GuestWaitContainer
|
|
|
|
guestLobbyMessage={guestLobbyMessage}
|
|
|
|
guestStatus={guestStatus}
|
|
|
|
logoutUrl={logoutUrl}
|
|
|
|
positionInWaitingQueue={positionInWaitingQueue}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
}
|
2024-03-07 01:28:18 +08:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PresenceManagerContainer: React.FC<PresenceManagerContainerProps> = ({ children }) => {
|
2024-06-04 21:40:54 +08:00
|
|
|
const { loading, error, data } = useDeduplicatedSubscription<GetUserCurrentResponse>(getUserCurrent);
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
loading: userInfoLoading,
|
|
|
|
error: userInfoError,
|
|
|
|
data: userInfoData,
|
|
|
|
} = useQuery<GetUserInfoResponse>(getUserInfo);
|
|
|
|
|
|
|
|
const loadingContextInfo = useContext(LoadingContext);
|
|
|
|
if (loading || userInfoLoading) return null;
|
|
|
|
if (error || userInfoError) {
|
|
|
|
loadingContextInfo.setLoading(false, '');
|
2024-05-30 04:43:17 +08:00
|
|
|
logger.debug(`Error on user authentication: ${error}`);
|
|
|
|
throw new Error('Error on user authentication');
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data || data.user_current.length === 0) return null;
|
|
|
|
if (!userInfoData
|
|
|
|
|| userInfoData.meeting.length === 0
|
|
|
|
|| userInfoData.user_current.length === 0) return null;
|
|
|
|
const {
|
|
|
|
authToken,
|
|
|
|
joinErrorCode,
|
|
|
|
joinErrorMessage,
|
|
|
|
joined,
|
|
|
|
ejectReasonCode,
|
|
|
|
meeting,
|
2024-04-05 21:54:08 +08:00
|
|
|
loggedOut,
|
2024-06-06 04:41:23 +08:00
|
|
|
guestStatusDetails,
|
|
|
|
guestStatus,
|
2024-03-07 01:28:18 +08:00
|
|
|
} = data.user_current[0];
|
|
|
|
const {
|
|
|
|
logoutUrl,
|
|
|
|
meetingId,
|
|
|
|
name: meetingName,
|
|
|
|
bannerColor,
|
|
|
|
bannerText,
|
2024-05-03 20:31:18 +08:00
|
|
|
customLogoUrl,
|
2024-03-07 01:28:18 +08:00
|
|
|
} = userInfoData.meeting[0];
|
|
|
|
const { extId, name: userName, userId } = userInfoData.user_current[0];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PresenceManager
|
|
|
|
authToken={authToken}
|
|
|
|
logoutUrl={logoutUrl}
|
|
|
|
meetingId={meetingId}
|
|
|
|
meetingName={meetingName}
|
|
|
|
userName={userName}
|
|
|
|
extId={extId}
|
|
|
|
userId={userId}
|
|
|
|
joined={joined}
|
|
|
|
joinErrorCode={joinErrorCode}
|
|
|
|
joinErrorMessage={joinErrorMessage}
|
|
|
|
meetingEnded={meeting.ended}
|
|
|
|
endedReasonCode={meeting.endedReasonCode}
|
2024-03-07 21:28:34 +08:00
|
|
|
endedBy={meeting.endedByUserName}
|
2024-03-07 01:28:18 +08:00
|
|
|
ejectReasonCode={ejectReasonCode}
|
|
|
|
bannerColor={bannerColor}
|
|
|
|
bannerText={bannerText}
|
2024-04-05 21:54:08 +08:00
|
|
|
loggedOut={loggedOut}
|
2024-05-03 20:31:18 +08:00
|
|
|
customLogoUrl={customLogoUrl}
|
2024-06-06 04:41:23 +08:00
|
|
|
guestLobbyMessage={guestStatusDetails?.guestLobbyMessage ?? null}
|
|
|
|
positionInWaitingQueue={guestStatusDetails?.positionInWaitingQueue ?? null}
|
|
|
|
guestStatus={guestStatus}
|
2024-03-07 01:28:18 +08:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</PresenceManager>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PresenceManagerContainer;
|