Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Small fix on callProvider #25963

Merged
merged 12 commits into from
Jun 24, 2022
32 changes: 20 additions & 12 deletions apps/meteor/client/contexts/CallContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IVoipRoom } from '@rocket.chat/core-typings';
import { ICallerInfo, VoIpCallerInfo } from '@rocket.chat/core-typings';
import { createContext, useCallback, useContext } from 'react';
import { createContext, useCallback, useContext, useRef } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';

import { VoIPUser } from '../lib/voip/VoIPUser';
Expand Down Expand Up @@ -62,23 +62,19 @@ export const useIsCallEnabled = (): boolean => {
return enabled;
};

let callerInfo: VoIpCallerInfo;

export const useIsCallReady = (): boolean => {
const context = useContext(CallContext);

if (isCallContextReady(context)) {
callerInfo = context.voipClient.callerInfo;
}
const { ready } = useContext(CallContext);

return !!context.ready;
return !!ready;
};

export const useIsCallError = (): boolean => {
const context = useContext(CallContext);
return Boolean(isCallContextError(context));
};

export const useCallContext = (): CallContextValue => useContext(CallContext);

export const useCallActions = (): CallActionsType => {
const context = useContext(CallContext);

Expand All @@ -94,19 +90,30 @@ export const useCallerInfo = (): VoIpCallerInfo => {
if (!isCallContextReady(context)) {
throw new Error('useCallerInfo only if Calls are enabled and ready');
}

const { voipClient } = context;

const ref = useRef<VoIpCallerInfo>(voipClient.callerInfo);

const subscribe = useCallback(
(callback: () => void): (() => void) => {
voipClient.on('stateChanged', callback);
ref.current = voipClient.callerInfo;

const handleSubscribe = (): void => {
ref.current = voipClient.callerInfo;
callback();
};

voipClient.on('stateChanged', handleSubscribe);

return (): void => {
voipClient.off('stateChanged', callback);
voipClient.off('stateChanged', handleSubscribe);
};
},
[voipClient],
);

const getSnapshot = (): VoIpCallerInfo => callerInfo;
const getSnapshot = (): VoIpCallerInfo => ref.current;

return useSyncExternalStore(subscribe, getSnapshot);
};
Expand Down Expand Up @@ -147,6 +154,7 @@ export const useCallClient = (): VoIPUser => {
if (!isCallContextReady(context)) {
throw new Error('useClient only if Calls are enabled and ready');
}

return context.voipClient;
};

Expand Down
23 changes: 23 additions & 0 deletions apps/meteor/client/contexts/VoipAgentContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createContext, Dispatch, SetStateAction } from 'react';

export type VoipAgentContextValue = {
agentEnabled: boolean;
registered: boolean;
networkStatus: 'online' | 'offline';
voipButtonEnabled: boolean;
setAgentEnabled: Dispatch<SetStateAction<boolean>>;
setRegistered: Dispatch<SetStateAction<boolean>>;
setNetworkStatus: Dispatch<SetStateAction<'online' | 'offline'>>;
setVoipButtonEnabled: Dispatch<SetStateAction<boolean>>;
};

export const VoipAgentContext = createContext<VoipAgentContextValue>({
agentEnabled: false,
registered: false,
networkStatus: 'offline',
voipButtonEnabled: false,
setAgentEnabled: () => undefined,
setRegistered: () => undefined,
setNetworkStatus: () => undefined,
setVoipButtonEnabled: () => undefined,
});
2 changes: 1 addition & 1 deletion apps/meteor/client/lib/voip/VoIPUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ export class VoIPUser extends Emitter<VoipEvents> {
return this.queueInfo;
}

getRegistrarState(): string | undefined {
getRegisterState(): string | undefined {
tiagoevanp marked this conversation as resolved.
Show resolved Hide resolved
return this.registerer?.state.toString().toLocaleLowerCase();
}

Expand Down
64 changes: 32 additions & 32 deletions apps/meteor/client/providers/CallProvider/CallProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import { OutgoingByeRequest } from 'sip.js/lib/core';
import { CustomSounds } from '../../../app/custom-sounds/client';
import { getUserPreference } from '../../../app/utils/client';
import { WrapUpCallModal } from '../../components/voip/modal/WrapUpCallModal';
import { CallContext, CallContextValue, useCallCloseRoom } from '../../contexts/CallContext';
import { CallContext, CallContextValue } from '../../contexts/CallContext';
import { roomCoordinator } from '../../lib/rooms/roomCoordinator';
import { QueueAggregator } from '../../lib/voip/QueueAggregator';
import VoipAgentProvider from '../VoipAgentProvider';
import { useVoipClient } from './hooks/useVoipClient';

const startRingback = (user: IUser): void => {
Expand All @@ -44,6 +45,9 @@ export const CallProvider: FC = ({ children }) => {
const voipEnabled = useSetting('VoIP_Enabled');
const subscribeToNotifyUser = useStream('notify-user');
const dispatchEvent = useEndpoint('POST', '/v1/voip/events');
const visitorEndpoint = useEndpoint('POST', '/v1/livechat/visitor');
const voipEndpoint = useEndpoint('GET', '/v1/voip/room');
const voipCloseRoomEndpoint = useEndpoint('POST', '/v1/voip/room.close');
const setModal = useSetModal();

const result = useVoipClient();
Expand All @@ -54,10 +58,29 @@ export const CallProvider: FC = ({ children }) => {

const [queueCounter, setQueueCounter] = useState(0);
const [queueName, setQueueName] = useState('');
const [roomInfo, setRoomInfo] = useState<{ v: { token?: string }; rid: string }>();

const closeRoom = useCallback(
async (data): Promise<void> => {
roomInfo &&
(await voipCloseRoomEndpoint({
rid: roomInfo.rid,
token: roomInfo.v.token || '',
options: { comment: data?.comment, tags: data?.tags },
}));
homeRoute.push({});

const queueAggregator = result.voipClient?.getAggregator();
if (queueAggregator) {
queueAggregator.callEnded();
}
},
[homeRoute, result?.voipClient, roomInfo, voipCloseRoomEndpoint],
);

const openWrapUpModal = useCallback((): void => {
setModal(() => <WrapUpCallModal closeRoom={useCallCloseRoom} />);
}, [setModal]);
setModal(() => <WrapUpCallModal closeRoom={closeRoom} />);
}, [closeRoom, setModal]);

const [queueAggregator, setQueueAggregator] = useState<QueueAggregator>();

Expand Down Expand Up @@ -234,12 +257,6 @@ export const CallProvider: FC = ({ children }) => {
};
}, [onNetworkConnected, onNetworkDisconnected, result.voipClient]);

const visitorEndpoint = useEndpoint('POST', '/v1/livechat/visitor');
const voipEndpoint = useEndpoint('GET', '/v1/voip/room');
const voipCloseRoomEndpoint = useEndpoint('POST', '/v1/voip/room.close');

const [roomInfo, setRoomInfo] = useState<{ v: { token?: string }; rid: string }>();

const openRoom = (rid: IVoipRoom['_id']): void => {
roomCoordinator.openRouteLink('v', { rid });
};
Expand Down Expand Up @@ -319,34 +336,17 @@ export const CallProvider: FC = ({ children }) => {
}
return '';
},
closeRoom: async ({ comment, tags }: { comment?: string; tags?: string[] }): Promise<void> => {
roomInfo && (await voipCloseRoomEndpoint({ rid: roomInfo.rid, token: roomInfo.v.token || '', options: { comment, tags } }));
homeRoute.push({});
const queueAggregator = voipClient.getAggregator();
if (queueAggregator) {
queueAggregator.callEnded();
}
},
closeRoom,
openWrapUpModal,
};
}, [
voipEnabled,
user,
result,
roomInfo,
queueCounter,
queueName,
openWrapUpModal,
visitorEndpoint,
voipEndpoint,
voipCloseRoomEndpoint,
homeRoute,
]);
}, [voipEnabled, user, result, roomInfo, queueCounter, queueName, closeRoom, openWrapUpModal, visitorEndpoint, voipEndpoint]);

return (
<CallContext.Provider value={contextValue}>
{children}
{contextValue.enabled && createPortal(<audio ref={remoteAudioMediaRef} />, document.body)}
<VoipAgentProvider>
{children}
{contextValue.enabled && createPortal(<audio ref={remoteAudioMediaRef} />, document.body)}
</VoipAgentProvider>
</CallContext.Provider>
);
};
28 changes: 28 additions & 0 deletions apps/meteor/client/providers/VoipAgentProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { FC, useState } from 'react';

import { VoipAgentContext } from '../contexts/VoipAgentContext';

const VoipAgentProvider: FC = ({ children }) => {
const [agentEnabled, setAgentEnabled] = useState(false);
const [registered, setRegistered] = useState(false);
const [networkStatus, setNetworkStatus] = useState<'online' | 'offline'>('online');
const [voipButtonEnabled, setVoipButtonEnabled] = useState(false);

return (
<VoipAgentContext.Provider
children={children}
value={{
agentEnabled,
registered,
networkStatus,
voipButtonEnabled,
setAgentEnabled,
setRegistered,
setNetworkStatus,
setVoipButtonEnabled,
}}
/>
);
};

export default VoipAgentProvider;
2 changes: 0 additions & 2 deletions apps/meteor/client/sidebar/footer/voip/VoipFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ export const VoipFooter = ({
small
square
danger
primary
onClick={(e): unknown => {
e.stopPropagation();
toggleMic(false);
Expand All @@ -152,7 +151,6 @@ export const VoipFooter = ({
small
square
success
primary
onClick={async (): Promise<void> => {
callActions.pickUp();
const rid = await createRoom(caller);
Expand Down
Loading