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
57 changes: 27 additions & 30 deletions apps/meteor/client/providers/CallProvider/CallProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 { useVoipClient } from './hooks/useVoipClient';
Expand All @@ -44,6 +44,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 +57,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();
tiagoevanp marked this conversation as resolved.
Show resolved Hide resolved
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 +256,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,29 +335,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, closeRoom, openWrapUpModal, visitorEndpoint, voipEndpoint]);

return (
<CallContext.Provider value={contextValue}>
Expand Down