UNPKG

@nethesis/phone-island

Version:
3 lines (2 loc) 25.8 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../node_modules/tslib/tslib.es6.js"),n=require("react");require("../node_modules/react-redux/es/index.js");var t=require("../node_modules/socket.io-client/build/esm/index.js"),r=require("./RestAPI.js"),i=require("../lib/phone/conversation.js"),o=require("../services/phonebook.js"),u=require("../services/user.js"),c=require("../static/busy_ringtone.js"),a=require("../utils/customHooks/useEventListener.js"),s=require("../utils/genericFunctions/eventDispatch.js"),l=require("../utils/genericFunctions/withTimeout.js"),d=require("../utils/genericFunctions/summaryEvents.js"),v=require("../store/index.js");require("../lib/webrtc/janus.js"),require("../node_modules/webrtc-adapter/src/js/adapter_core.js");var p=require("../lib/devices/devices.js"),m=require("../lib/user/default_device.js"),f=require("../utils/genericFunctions/isEmpty.js"),g=require("../utils/streaming/getStreamingSourceId.js"),h=require("../events/SocketEvents.js"),b=require("../utils/genericFunctions/timestamp.js"),k=require("../lib/user/extensions.js"),S=require("../utils/streaming/isFromStreaming.js"),q=require("../lib/phone/queue.js"),y=require("../lib/phone/trunk.js"),w=require("../node_modules/react-redux/es/hooks/useDispatch.js");function C(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var _=C(n);exports.Socket=function(C){var N=C.hostName,I=C.username,T=C.authToken,j=C.reload,U=C.reloadedCallback,D=C.children,x=C.uaType,E=w.useDispatch(),P=n.useRef(),O=n.useRef(),F=n.useRef(!1),A=n.useRef(0),V=n.useRef(null),M=n.useRef(new Map),R=n.useRef(new Map),L=n.useRef(new Set),Q=n.useRef({linkedid:null,uniqueid:null});a.useEventListener("phone-island-start-transcription",(function(e){if(O.current){var n=(null==e?void 0:e.linkedid)||(null==e?void 0:e.linkedId)||(null==e?void 0:e.uniqueid)||(null==e?void 0:e.uniqueId)||null,t=(null==e?void 0:e.uniqueid)||(null==e?void 0:e.uniqueId)||n;if(!n&&!t)return;Q.current={linkedid:n,uniqueid:t},O.current.emit("start_transcription",{linkedid:n,uniqueid:t})}})),a.useEventListener("phone-island-stop-transcription",(function(e){if(O.current){var n=(null==e?void 0:e.linkedid)||(null==e?void 0:e.linkedId)||(null==e?void 0:e.uniqueid)||(null==e?void 0:e.uniqueId)||null,t=(null==e?void 0:e.uniqueid)||(null==e?void 0:e.uniqueId)||n;if(!n&&!t)return;Q.current={linkedid:null,uniqueid:null},O.current.emit("stop_transcription",{linkedid:n,uniqueid:t})}})),a.useEventListener("phone-island-current-user-queue-call-waiting",(function(e){E.currentCall.updateCurrentCall({throughQueue:!0,queueId:(null==e?void 0:e.queueId)||"",queueName:(null==e?void 0:e.queueName)||"",queueNumber:(null==e?void 0:e.queueNumber)||"",queuePosition:(null==e?void 0:e.queuePosition)||"",queueWaitingTime:(null==e?void 0:e.queueWaitingTime)||0})})),a.useEventListener("phone-island-current-user-queue-call-connected",(function(e){E.currentCall.updateCurrentCall({throughQueue:!0,queueId:(null==e?void 0:e.queueId)||"",queueName:(null==e?void 0:e.queueName)||"",queueNumber:(null==e?void 0:e.queueNumber)||"",queuePosition:(null==e?void 0:e.queuePosition)||"",queueWaitingTime:(null==e?void 0:e.queueWaitingTime)||0})}));return n.useEffect((function(){var n,a=function(e){if(e.counterpartNum&&S.isFromStreaming(e.counterpartNum)){E.island.setIsFromStreaming(!0),E.currentCall.updateCurrentCall({streamingSourceNumber:e.counterpartNum});var n=g.getStreamingSourceId(e.counterpartNum);n&&u.subscribe({id:n}).catch((function(e){return console.error("Error subscribing to streaming source:",e)}))}},w=function(e){return(e||"").replace(/\D/g,"")},C=function(e){var n=i.getDisplayName(e),t=((null==e?void 0:e.counterpartNum)||"").trim();return n&&n!==t?n:(t?R.current.get(w(t)):void 0)||n},_=function(e){return q.getCurrentCallQueueContext(e,v.store.getState().queue)},j=function(e,n){var t=v.store.getState().currentCall,r=(null==n?void 0:n.queueId)||t.queueId||"",i=(null==n?void 0:n.queueName)||t.queueName||"",o=(null==n?void 0:n.queueNumber)||t.queueNumber||"";return{conversationId:e.id,linkedId:e.linkedId,uniqueId:e.uniqueId,ownerExtension:e.owner,number:"".concat(e.counterpartNum||""),queueId:r,queueName:i,queueNumber:o,queuePosition:(null==n?void 0:n.queuePosition)||"",queueWaitingTime:(null==n?void 0:n.queueWaitingTime)||0}},U=function(e){var n,t,r,i;if(e){var o=_(e),u=j(e,o);if(Boolean(u.queueId||u.queueName||u.queueNumber))if(e.connected)((null===(r=V.current)||void 0===r?void 0:r.conversationId)!==e.id||"connected"!==(null===(i=V.current)||void 0===i?void 0:i.phase))&&(h.dispatchCurrentUserQueueCallConnected(u),V.current={conversationId:e.id,phase:"connected"});else((null===(n=V.current)||void 0===n?void 0:n.conversationId)!==e.id||"waiting"!==(null===(t=V.current)||void 0===t?void 0:t.phase))&&(h.dispatchCurrentUserQueueCallWaiting(u),V.current={conversationId:e.id,phase:"waiting"});else V.current=null}else V.current=null},D=function(e){return(null==e?void 0:e.linkedId)||(null==e?void 0:e.linkedid)||""},W=function(e){return(null==e?void 0:e.uniqueId)||(null==e?void 0:e.uniqueid)||""},B=function(e){return!!(null==e?void 0:e.connected)&&!!D(e)&&!!W(e)},K=function(n){var t=v.store.getState().currentUser.conversations,r=n?null==t?void 0:t[n]:void 0,i=function(e){var n,t=null,r=Object.entries(e||{}).filter((function(e){var n=e[1];return n&&Object.keys(n).length>0}));return r.forEach((function(e){var r,i,o=e[0],u=e[1];if(!t)return t=u,void(n=o);var c=B(t),a=B(u);if(a&&!c)return t=u,void(n=o);if(a===c){var s=!!t.connected,l=!!u.connected;if(l&&!s)return t=u,void(n=o);var d=null!==(r=t.startTime)&&void 0!==r?r:0,v=null!==(i=u.startTime)&&void 0!==i?i:0;l===s&&v>d&&(t=u,n=o)}})),{conversation:t,key:n,count:r.length}}(r);if(B(i.conversation))return e.__assign(e.__assign({},i),{source:"live",cacheAgeMs:void 0,hasConversationForCaller:!!r,liveConversationKeys:Object.keys(r||{})});var o=function(n){if(!n)return null;var t=M.current.get(n);if(!t)return null;var r=Date.now()-t.cachedAt;return r>6e5?(M.current.delete(n),null):e.__assign(e.__assign({},t),{cacheAgeMs:r})}(n);return o&&B(o.conversation)?{conversation:o.conversation,key:o.conversation.id,count:i.count,source:"cache",cacheAgeMs:o.cacheAgeMs,hasConversationForCaller:!!r,liveConversationKeys:Object.keys(r||{})}:e.__assign(e.__assign({},i),{source:i.conversation?"live_invalid":"none",cacheAgeMs:null==o?void 0:o.cacheAgeMs,hasConversationForCaller:!!r,liveConversationKeys:Object.keys(r||{})})},z=function(n,t){var r=v.store.getState().currentCall,c=r.transferring,l=r.transferSwitching,d=r.transferCalls,p=v.store.getState().island.view;if(Object.keys(t).length>0){if(n.status){var f=v.store.getState().users.extensions,g=v.store.getState().currentUser.default_device,h=v.store.getState().currentUser,q=h.endpoints,N=h.username,I=v.store.getState().currentCall;I.incoming,I.outgoing;var T=function(){if(!f||!N)return!1;var e=Object.values(f).filter((function(e){return(null==e?void 0:e.username)===N}));return null==e?void 0:e.some((function(e){var n=null==q?void 0:q.extension.find((function(n){return n.id===(null==e?void 0:e.exten)}));return"nethlink"===(null==n?void 0:n.type)&&"offline"!==(null==e?void 0:e.status)}))};switch(n.status){case"ringing":if(function(e){var n=v.store.getState().currentCall,t=n.accepted,r=n.ownerExtension;return t&&!!r&&!!e&&r!==e}(t.owner))break;var D=_(t),P=j(t,D);U(t),a(t),function(e){if(e&&"in"===e.direction){var n=(e.counterpartNum||"").trim();if(!(n.length<3)){var t=(e.counterpartName||"").trim();if(!t||"<unknown>"===t||t===n){var r=v.store.getState().users.extensions;if(!(r&&r[n]||S.isFromStreaming(n))){var i=w(n);R.current.has(i)||e.id&&L.current.has(e.id)||(e.id&&(L.current.size>500&&L.current.clear(),L.current.add(e.id)),o.searchPhonebook(1,n,"").then((function(e){var n=((null==e?void 0:e.rows)||[]).find((function(e){return[e.extension,e.workphone,e.homephone,e.cellphone,e.fax].some((function(e){var n=w(e);return n.length>0&&(n===i||n.endsWith(i)||i.endsWith(n))}))})),t=((null==n?void 0:n.name)||(null==n?void 0:n.company)||"").trim();if(t){R.current.set(i,t);var r=v.store.getState().currentCall;w(r.number)===i&&E.currentCall.updateCurrentCall({displayName:t})}})).catch((function(){})))}}}}}(t),("mobile"===x&&T()||"desktop"===x&&("webrtc"===(null==g?void 0:g.type)||void 0===(null==g?void 0:g.type)&&!T()||!T()&&"physical"===(null==g?void 0:g.type)))&&(E.currentCall.checkIncomingUpdatePlay({conversationId:t.id,displayName:C(t),number:"".concat(t.counterpartNum),incomingSocket:!0,incoming:!0,throughQueue:Boolean(P.queueId||P.queueName||P.queueNumber),queueId:P.queueId,queueName:P.queueName,queueNumber:P.queueNumber,queuePosition:P.queuePosition,queueWaitingTime:P.queueWaitingTime,username:"".concat(f&&f[t.counterpartNum]&&f[t.counterpartNum].username)||"",ownerExtension:t.owner}),v.store.dispatch.island.setIslandView("call")),F.current||(F.current=!0,u.getCurrentUserInfo().then((function(n){n&&(E.currentUser.updateCurrentUser(n),s.eventDispatch("phone-island-user-informations-update",e.__assign({},n)),n.settings&&n.settings.open_param_url?E.paramUrl.setOpenParamUrlType(n.settings.open_param_url):E.paramUrl.setOpenParamUrlType("never"))})).catch((function(e){console.error("Error getting current user info:",e)})).finally((function(){setTimeout((function(){F.current=!1}),100)})));var O=v.store.getState().paramUrl.openParamUrlType,A=v.store.getState().island.urlOpened;if("ringing"===O&&!A){var V=y.isFromTrunk(t.counterpartNum);v.store.dispatch.paramUrl.setThroughTrunk(V),v.store.dispatch.island.setUrlOpened(!1),s.eventDispatch("phone-island-url-parameter-opened",{counterpartNum:t.counterpartNum,counterpartName:C(t),owner:t.owner,uniqueId:t.uniqueId,linkedId:t.linkedId,throughQueue:t.throughQueue,throughTrunk:V,direction:t.direction,connected:t.connected})}break;case"busy":if(U(t),a(t),"mobile"===x&&T()||"desktop"===x&&("webrtc"===(null==g?void 0:g.type)||void 0===(null==g?void 0:g.type)&&!T()||!T()&&"physical"===(null==g?void 0:g.type))){if(t&&t.connected)E.currentCall.updateCurrentCall({conversationId:t.id,displayName:C(t),number:"".concat(t.counterpartNum),ownerExtension:t.owner,username:"".concat(f&&f[t.counterpartNum]&&f[t.counterpartNum].username)||"",chDest:(null==t?void 0:t.chDest)||{},chSource:(null==t?void 0:t.chSource)||{}}),E.currentCall.checkAcceptedUpdate({acceptedSocket:!0}),E.currentCall.addTransferCalls({type:"transferred",displayName:C(t),number:"".concat(t.counterpartNum),startTime:"".concat(b.getTimestampInSeconds())}),v.store.getState().island.isFromStreaming&&"out"===t.direction&&setTimeout((function(){E.island.setIslandView("streamingAnswer")}),200),m.isPhysical()&&function(e){E.currentCall.updateCurrentCall({conversationId:e.id,accepted:!0,incoming:"in"!==e.direction&&void 0}),s.eventDispatch("phone-island-call-answered",{}),v.store.dispatch.player.stopAudioPlayer(),v.store.dispatch.player.setAudioPlayerLoop(!1)}(t),"call"===p&&c&&E.currentCall.updateCurrentCall({transferring:!1});if(d.length>1)E.currentCall.deleteTransferCalls();else if(t&&!t.connected){if(c&&!l){var M=d.find((function(e){return e.number===t.counterpartNum}));!t.connected&&M&&(E.currentCall.updateCurrentCall({transferring:!1}),s.eventDispatch("phone-island-call-transfer-failed",{}),E.currentCall.updateTransferSwitching(!1))}"REC"===(null==t?void 0:t.counterpartName)&&E.physicalRecorder.setRecordingTempVariable(!0)}t&&!t.connected&&"out"===t.direction&&E.currentCall.checkOutgoingUpdate({outgoingSocket:!0,outgoing:"REC"!==(null==t?void 0:t.counterpartName),displayName:i.getDisplayName(t),number:"".concat(null==t?void 0:t.counterpartNum),username:"".concat(f&&f[null==t?void 0:t.counterpartNum]&&f[null==t?void 0:t.counterpartNum].username)||""})}break;case"onhold":U(t);var Q=t.counterpartName,W=t.counterpartNum,B=t.startTime;c&&W&&Q&&"<unknown>"!==Q&&(E.currentCall.addTransferCalls({type:"destination",displayName:i.getDisplayName(t),number:W,startTime:"".concat(b.getTimestampInSeconds())}),E.currentCall.updateCurrentCall({displayName:i.getDisplayName(t),number:W,startTime:"".concat(B/1e3),conversationId:t.id}),E.island.setIslandView("call"));break;case"busy_ringing":s.eventDispatch("phone-island-call-ringing",{})}}}else"online"==n.status&&k.userTotallyFree()&&(U(null),E.player.stopAudioPlayer(),E.currentCall.reset(),E.listen.reset(),E.physicalRecorder.setRecordingTempVariable(!1),E.island.setIsFromStreaming(!1))};return n={upgrade:!1,transports:["websocket"],reconnection:!0,reconnectionDelay:2e3},"new"===r.getApiMode(I)&&(n.path="/api/ws"),O.current=t.io("https://"+N,n),E.websocket.update({socket:O.current}),O.current.on("connect",(function(){console.debug("Socket connected sid: ".concat(O.current.id)),s.eventDispatch("phone-island-socket-connected",{})})),O.current.on("disconnect",(function(e){console.debug("Socket disconnect - reason: ".concat(e)),P.current&&(clearInterval(P.current),P.current=null),e.includes("server disconnect")?s.eventDispatch("phone-island-server-disconnected",{}):s.eventDispatch("phone-island-socket-disconnected",{})})),O.current.io.on("error",(function(e){console.debug("Socket error: ",e)})),O.current.on("connect_error",(function(e){console.debug("Socket connect_error: ",e)})),O.current.io.on("reconnect",(function(e){A.current=0,s.eventDispatch("phone-island-socket-reconnected",{}),console.debug("Socket reconnect attemp ".concat(e," (sid: ").concat(O.current.id,")"))})),O.current.io.on("reconnect_attempt",(function(e){console.debug("Socket reconnect_attempt ".concat(e))})),O.current.io.on("reconnect_error",(function(e){console.debug("Socket reconnect_error: ",e)})),O.current.io.on("reconnect_failed",(function(){console.debug("Socket reconnect_failed")})),O.current.on("connect",(function(){console.debug("Socket on: "+N+" is connected!"),O.current.emit("login",{accessKeyId:"".concat(I),token:T,uaType:x})})),O.current.on("authe_ok",(function(){console.debug("Socket authentication success!"),s.eventDispatch("phone-island-socket-authorized",{}),P.current&&clearInterval(P.current),P.current=setInterval((function(){O.current.volatile.emit("ping",l.withTimeout((function(){A.current=0,setTimeout((function(){E.alerts.removeAlert("socket_down"),s.eventDispatch("phone-island-alert-removed",{type:"socket_down"}),s.eventDispatch("phone-island-socket-disconnected-popup-close",{})}),0)}),(function(){A.current++,console.debug("Socket ping timeout (".concat(A.current,"/").concat(3,"), connected: ").concat(O.current.connected)),setTimeout((function(){var e,n,t=O.current.connected&&A.current>=3;if(!O.current.connected||t){var r=v.store.getState().webrtc.sipcall,i=v.store.getState().currentCall,o=i.accepted,u=i.outgoing,c=null===(n=null===(e=null==r?void 0:r.webrtcStuff)||void 0===e?void 0:e.pc)||void 0===n?void 0:n.iceConnectionState;if((o||u)&&("connected"===c||"completed"===c))return void console.debug("Socket unreachable but active call with ICE connected - skipping socket_down alert",{iceState:c,accepted:o,outgoing:u,isStaleConnection:t,timestamp:(new Date).toISOString()});t&&(console.warn("Stale socket connection detected - forcing reconnection"),O.current.disconnect()),E.alerts.setAlert("socket_down"),s.eventDispatch("phone-island-socket-disconnected-popup-open",{}),console.error("Socket is unreachable!")}}),0)}),5e3))}),5e3)})),O.current.on("userMainPresenceUpdate",(function(n){v.store.dispatch.users.updateEndpointMainPresence(e.__assign({},n.mainPresence)),h.dispatchMainPresence(n)})),O.current.on("extenHangup",(function(e){var n=v.store.getState().currentUser,t=n.endpoints,r=n.username,i=v.store.getState().conference,o=i.isActive,u=i.conferenceStartedFrom,a=v.store.getState().island,l=a.view,d=a.previewCallFromMobileOrNethlink,p=(null==t?void 0:t.extension)||[],m=p.map((function(e){return e.id})),f=null==e?void 0:e.callerNum;!m.includes(f)&&m.includes(null==e?void 0:e.channelExten)&&(f=null==e?void 0:e.channelExten);var g=p.find((function(n){return n.id===e.callerNum})),h=null==g?void 0:g.type,b=K(f).conversation,k=D(b),S=W(b),q=(null==b?void 0:b.connected)||!1;if(S&&k&&q&&(s.eventDispatch("phone-island-summary-call-check",{linkedid:k,uniqueid:S}),f&&M.current.delete(f)),"normal_clearing"===e.cause&&("physical"===h||"mobile"===h)||"normal_clearing"===e.cause&&("webrtc"===h||"nethlink"===h)&&d||"user_busy"===(null==e?void 0:e.cause)||"not_defined"===(null==e?void 0:e.cause)||"call_rejected"===(null==e?void 0:e.cause)||"interworking"===(null==e?void 0:e.cause)?(setTimeout((function(){v.store.dispatch.island.toggleAvoidToShow(!1),v.store.dispatch.island.setPreviewCallFromMobileOrNethlink(!1)}),500),o&&u!==r&&v.store.dispatch.conference.resetConference()):"normal_circuit_congestion"===(null==e?void 0:e.cause)&&o&&u===r?setTimeout((function(){s.eventDispatch("phone-island-view-changed",{viewType:"waitingConference"})}),800):"normal_clearing"!==e.cause&&"user_busy"!==(null==e?void 0:e.cause)&&"not_defined"!==(null==e?void 0:e.cause)&&"call_rejected"!==(null==e?void 0:e.cause)||"webrtc"!==h&&"nethlink"!==h||!o||u===r||v.store.dispatch.conference.resetConference(),("normal_clearing"===e.cause&&("webrtc"===h||"nethlink"===h)||"call_rejected"===(null==e?void 0:e.cause))&&o&&u===r){var y=v.store.getState().conference,w=y.usersList,C=y.pendingUsers,_=w&&Object.keys(w).length>0,N=C&&Object.keys(C).length>0;_||N?setTimeout((function(){s.eventDispatch("phone-island-view-changed",{viewType:"waitingConference"})}),800):v.store.dispatch.conference.resetConference()}if("interworking"===(null==e?void 0:e.cause)&&o&&u===r&&"waitingConference"!==l&&setTimeout((function(){s.eventDispatch("phone-island-view-changed",{viewType:"waitingConference"})}),800),"user_busy"===(null==e?void 0:e.cause)){var I=v.store.getState().currentUser,T=I.endpoints,j=I.username,U=((null==T?void 0:T.extension)||[]).map((function(e){return e.id})),x=v.store.getState().currentCall,E=x.incoming,P=x.accepted,O=x.transferring,F=v.store.getState().conference,A=F.isActive,V=F.conferenceStartedFrom;!(E&&e.callerNum&&U.includes(e.callerNum))&&(!P||A||O)&&(v.store.dispatch.island.setOperatorBusyActive({callerNumber:e.callerNum||"Unknown"}),setTimeout((function(){v.store.dispatch.player.stopAudioPlayer()}),4e3),setTimeout((function(){v.store.dispatch.player.updateStartAudioPlayer({src:c.default,loop:!0}),v.store.dispatch.island.setIslandView("operatorBusy")}),600),A&&V===j&&setTimeout((function(){s.eventDispatch("phone-island-view-changed",{viewType:"waitingConference"})}),4e3))}if("subscriber_absent"===(null==e?void 0:e.cause)){var R=v.store.getState().conference,L=R.isActive,Q=R.conferenceStartedFrom,B=v.store.getState().currentUser.username;if(L&&Q===B){var z=v.store.getState().conference;w=z.usersList,C=z.pendingUsers,_=w&&Object.keys(w).length>0,N=C&&Object.keys(C).length>0,_||N?(setTimeout((function(){s.eventDispatch("phone-island-view-changed",{viewType:"waitingConference"})}),800),C&&C[e.callerNum]&&v.store.dispatch.conference.removePendingUser(e.callerNum)):v.store.dispatch.conference.resetConference()}}})),O.current.on("extenConnected",(function(e){var n=v.store.getState().currentUser,t=n.default_device,r=n.endpoints,i=((null==r?void 0:r.extension)||[]).find((function(n){return n.id===e.extenConnected})),o=null==i?void 0:i.type;(("webrtc"===(null==t?void 0:t.type)||"nethlink"===(null==t?void 0:t.type))&&o&&("mobile"===o||"physical"===o)||"physical"===(null==t?void 0:t.type)&&o&&"physical"!==o)&&(v.store.dispatch.island.toggleAvoidToShow(!0),v.store.dispatch.island.setPreviewCallFromMobileOrNethlink(!0),s.eventDispatch("phone-island-call-answered",{extensionType:o}))})),O.current.on("extenUpdate",(function(e){var n,t,r;E.users.updateExtension(e);var i=v.store.getState().users.extensions,o={},u=v.store.getState().currentUser;for(var c in i){var a=i[c].username,s=i[c].exten;o[a]||(o[a]=[]),o[a].push(s)}var l,d,p=o[e.username],m=Object.keys(e.conversations||{}),g=e.username===I,b=(null===(n=e.conversations)||void 0===n?void 0:n[m[0]])||{};g&&(l=e.exten,d=e.conversations,l&&Object.values(d||{}).forEach((function(e){B(e)&&M.current.set(l,{conversation:e,cachedAt:Date.now()})})));var k=!1;if((null==e?void 0:e.username)===I&&!f.isEmpty(b)&&(null==b?void 0:b.owner)){var S=null===(r=null===(t=null==u?void 0:u.endpoints)||void 0===t?void 0:t.extension)||void 0===r?void 0:r.find((function(e){return e.id===b.owner}));S&&"mobile"===S.type&&(k=!0)}if(h.dispatchExtensions(e),f.isEmpty(b)){var q=null==p?void 0:p.some((function(e){var n,t=null===(n=i[e])||void 0===n?void 0:n.conversations;return!f.isEmpty(t)}));q||h.dispatchConversations(e)}else k||h.dispatchConversations(e);k&&"busy"===(null==e?void 0:e.status)&&(null==e?void 0:e.username)===I&&(v.store.dispatch.island.toggleAvoidToShow(!0),v.store.dispatch.island.setPreviewCallFromMobileOrNethlink(!0)),e.username===I&&(z(e,b),E.currentUser.updateConversations(e))})),O.current.on("queueUpdate",(function(e){E.queue.updateQueue(e);var n,t,r,i=(n=v.store.getState().currentUser.conversations,t=v.store.getState().currentCall.conversationId,r=null,Object.values(n||{}).forEach((function(e){Object.values(e||{}).forEach((function(e){(null==r?void 0:r.id)&&r.id===t||(t&&e.id===t||!r||e.connected&&!r.connected||e.connected===r.connected&&e.startTime>r.startTime)&&(r=e)}))})),r);i&&U(i),h.dispatchQueueUpdate(e)})),O.current.on("queueMemberUpdate",(function(e){E.queue.updateQueueMember(e),h.dispatchQueueMemberUpdate(e)})),O.current.on("takeOver",(function(){h.dispatchAlreadyLogin()})),O.current.on("serverReloaded",(function(){h.dispatchServerReload()})),O.current.on("parkingUpdate",(function(e){h.dispatchParkingUpdate(e)})),O.current.on("actionNethLink",(function(e,n){h.dispatchUrlCall(e,n)})),O.current.on("satellite/summary",(function(e){var n=(null==e?void 0:e.uniqueid)||(null==e?void 0:e.uniqueId),t=(null==e?void 0:e.linkedid)||(null==e?void 0:e.linkedId);n&&d.dispatchSummaryReady({linkedid:t,uniqueid:n,display_name:null==e?void 0:e.display_name,display_number:null==e?void 0:e.display_number,source:"socket"})})),O.current.on("message",(function(e){switch(e.message){case"videoCallStart":var n=v.store.getState(),t=n.currentCall,r=t.incoming,i=t.outgoing,o=t.accepted,u=n.currentUser.default_device,c=n.island,a=c.isFromStreaming,s=c.view;("webrtc"===(null==u?void 0:u.type)||"nethlink"===(null==u?void 0:u.type))&&(r||i||o)&&!a&&"video"!==s&&(E.island.toggleSideViewVisible(!1),E.island.toggleTranscriptionViewVisible(!1),E.island.setIslandView("video")),h.dispatchVideoCallStarted({initiator:"remote",callUser:e.callUser,destUser:e.destUser});break;case"screenSharingStart":E.island.toggleSideViewVisible(!1),E.island.toggleTranscriptionViewVisible(!1),E.island.setIslandView("video"),E.screenShare.update({isJoiningScreenShare:!0,room:e.roomId});break;case"screenSharingStop":E.island.toggleSideViewVisible(!1),E.island.toggleTranscriptionViewVisible(!1),E.island.setIslandView("video"),E.screenShare.update({isLeavingScreenShare:!0});break;default:console.warn("Socket: unknown message type ",e.message)}})),O.current.on("updateDefaultDevice",(function(n){h.dispatchDefaultDeviceUpdate(n);var t=v.store.getState().users.extensions,r=v.store.getState().currentUser.endpoints;if(t&&r){var i=Object.values(t).filter((function(e){return(null==e?void 0:e.exten)===n}));if(0!==i.length){var o=i[0],u=r.extension.find((function(e){return e.id===o.exten}));u&&(o=e.__assign(e.__assign({},o),{type:u.type})),v.store.dispatch.currentUser.updateCurrentDefaultDevice(o),p.checkMediaPermissions()}}})),O.current.on("confBridgeUpdate",(function(n){if(n&&(null==n?void 0:n.users)){var t=null==n?void 0:n.id,r=null==n?void 0:n.users,i=v.store.getState().conference.usersList,o=e.__assign({},r);i&&Object.keys(o).forEach((function(n){i[n]&&(o[n]=e.__assign(e.__assign({},o[n]),{muted:i[n].muted}))})),v.store.dispatch.conference.updateConferenceUsersList(o),v.store.dispatch.conference.updateConferenceId(t)}})),O.current.on("confBridgeEnd",(function(e){e&&(null==e?void 0:e.id)&&(v.store.dispatch.conference.resetConference(),s.eventDispatch("phone-island-conference-finished",{}))})),O.current.on("callWebrtc",(function(e){s.eventDispatch("phone-island-call-start",{number:e})})),O.current.on("newVoiceMessageCounter",(function(e){s.eventDispatch("phone-island-voicemail-received",{voicemailInfo:e})})),O.current.on("streamingSourceUpdate",(function(e){s.eventDispatch("phone-island-streaming-information-received",{res:e});var n=e.streaming||e.res&&e.res.streaming;if(n){var t=n.source,r=n.image;if(t&&r){v.store.getState().island.isFromStreaming;var i=v.store.getState().currentCall.streamingSourceNumber;g.getStreamingSourceId(i),E.streaming.updateSourceImage({source:t,image:r})}}})),O.current.on("satellite/transcription",(function(e){var n=Q.current,t=null==e?void 0:e.uniqueid,r=null==e?void 0:e.linkedid;if(n.linkedid&&t){var i=function(e){return null!=e&&(e===n.linkedid||e===n.uniqueid)};if(!i(t)&&!i(r))return}s.eventDispatch("phone-island-conversation-transcription",e)})),function(){clearInterval(P.current),R.current.clear(),L.current.clear(),O.current.close()}}),[N,I,T,x,E]),n.useEffect((function(){var e;if(j){console.info("Socket reload requested");var n=v.store.getState().alerts.data,t=v.store.getState().island.forceReload;(null===(e=n.socket_down)||void 0===e?void 0:e.active)||!1||t?(console.info(t?"Force reload requested, performing Socket reconnection":"Socket down detected (alert active), performing reconnection"),t&&v.store.dispatch.island.setForceReload(!1),P.current&&(clearInterval(P.current),P.current=null),setTimeout((function(){O.current.disconnect(),O.current.connect(),U()}),100)):(console.info("Socket already connected (no alert active), skipping reconnection"),U())}}),[j]),_.default.createElement(_.default.Fragment,null,D)}; //# sourceMappingURL=Socket.js.map