Skip to content

Commit

Permalink
[IMPROVE] Moving voip wrap up modal to EE
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva committed Jun 15, 2022
1 parent 9c8d28e commit 28408fc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
84 changes: 48 additions & 36 deletions apps/meteor/client/providers/CallProvider/CallProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ 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 { useHasLicense } from '../../../ee/client/hooks/useHasLicense';
import { WrapUpCallModal } from '../../../ee/client/omnichannel/components/voip/WrapUpCallModal';
import { CallContext, CallContextValue } from '../../contexts/CallContext';
import { imperativeModal } from '../../lib/imperativeModal';
import { roomCoordinator } from '../../lib/rooms/roomCoordinator';
Expand Down Expand Up @@ -49,19 +50,57 @@ export const CallProvider: FC = ({ children }) => {
const result = useVoipClient();
const user = useUser();
const homeRoute = useRoute('home');
const isEnterprise = useHasLicense('voip-enterprise');

const remoteAudioMediaRef = useRef<HTMLAudioElement>(null); // TODO: Create a dedicated file for the AUDIO and make the controls accessible

const [queueCounter, setQueueCounter] = useState(0);
const [queueName, setQueueName] = useState('');

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 [queueAggregator, setQueueAggregator] = useState<QueueAggregator>();
const [networkStatus, setNetworkStatus] = useState<NetworkState>('online');

const openRoom = (rid: IVoipRoom['_id']): void => {
roomCoordinator.openRouteLink('v', { rid });
};

const closeRoom = useCallback(
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 = result.voipClient?.getAggregator();

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

const openWrapUpModal = useCallback((): void => {
imperativeModal.open({ component: WrapUpCallModal });
}, []);

const [queueAggregator, setQueueAggregator] = useState<QueueAggregator>();
const handleWrapUp = useCallback(() => {
if (isEnterprise) {
openWrapUpModal();
return;
}

const [networkStatus, setNetworkStatus] = useState<NetworkState>('online');
closeRoom();
}, [isEnterprise, openWrapUpModal, closeRoom]);

useEffect(() => {
if (!result?.voipClient) {
Expand Down Expand Up @@ -131,12 +170,14 @@ export const CallProvider: FC = ({ children }) => {

const handleCallHangup = (_event: { roomId: string }): void => {
setQueueName(queueAggregator.getCurrentQueueName());
openWrapUpModal();

handleWrapUp();

dispatchEvent({ event: VoipClientEvents['VOIP-CALL-ENDED'], rid: _event.roomId });
};

return subscribeToNotifyUser(`${user._id}/call.hangup`, handleCallHangup);
}, [openWrapUpModal, queueAggregator, subscribeToNotifyUser, user, voipEnabled, dispatchEvent]);
}, [openWrapUpModal, queueAggregator, subscribeToNotifyUser, user, voipEnabled, dispatchEvent, handleWrapUp]);

useEffect(() => {
if (!result.voipClient) {
Expand Down Expand Up @@ -234,16 +275,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 });
};

const contextValue: CallContextValue = useMemo(() => {
if (!voipEnabled) {
return {
Expand Down Expand Up @@ -319,29 +350,10 @@ 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, openWrapUpModal, visitorEndpoint, voipEndpoint, closeRoom]);

return (
<CallContext.Provider value={contextValue}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useSetModal, useTranslation } from '@rocket.chat/ui-contexts';
import React, { ReactElement, useEffect } from 'react';
import { useForm, SubmitHandler } from 'react-hook-form';

import { useCallCloseRoom } from '../../../contexts/CallContext';
import Tags from '../../Omnichannel/Tags';
import Tags from '../../../../../client/components/Omnichannel/Tags';
import { useCallCloseRoom } from '../../../../../client/contexts/CallContext';

type WrapUpCallPayload = {
comment: string;
Expand Down Expand Up @@ -40,8 +40,6 @@ export const WrapUpCallModal = (): ReactElement => {
closeModal();
};

useEffect(() => closeRoom, [closeRoom]);

return (
<Modal is='form' onSubmit={handleSubmit(onSubmit)}>
<Modal.Header>
Expand Down

0 comments on commit 28408fc

Please sign in to comment.