Files
chatwoot/app/javascript/dashboard/store/modules/calls.js
Sojan Jose fb4042c863 refactor(voice): use Twilio-native statuses and remove legacy fallbacks
- Drop FE normalizeStatus and BE STATUS_MAPPING
- Update bubbles/preview and store to use Twilio status values
- DRY ActionCable incoming-call payload builder
- Use server-provided conference_sid for client connect
- VoiceAPI: standardized returns, accept store, simplify disconnects
- Remove legacy/fallback comments introduced in this PR
2025-08-20 13:38:50 +02:00

105 lines
2.6 KiB
JavaScript

import VoiceAPI from 'dashboard/api/channels/voice';
import { TERMINAL_STATUSES } from 'dashboard/helper/voice';
const state = {
activeCall: null,
incomingCall: null,
};
const getters = {
getActiveCall: $state => $state.activeCall,
hasActiveCall: $state => !!$state.activeCall,
getIncomingCall: $state => $state.incomingCall,
hasIncomingCall: $state => !!$state.incomingCall,
};
const actions = {
handleCallStatusChanged(
{ state: currentState, dispatch },
{ callSid, status }
) {
const isActiveCall = callSid === currentState.activeCall?.callSid;
const isIncomingCall = callSid === currentState.incomingCall?.callSid;
const terminalStatuses = [...TERMINAL_STATUSES];
if (terminalStatuses.includes(status)) {
if (isActiveCall) {
dispatch('clearActiveCall');
} else if (isIncomingCall) {
dispatch('clearIncomingCall');
}
}
},
setActiveCall({ commit, dispatch, state: currentState }, callData) {
if (!callData?.callSid) return;
if (callData.status && TERMINAL_STATUSES.includes(callData.status)) {
if (callData.callSid === currentState.activeCall?.callSid) {
dispatch('clearActiveCall');
}
return;
}
commit('SET_ACTIVE_CALL', callData);
},
clearActiveCall({ commit }) {
// End the WebRTC connection before clearing the call state
try {
VoiceAPI.endClientCall();
} catch (error) {
// Error ending client call during clearActiveCall
}
commit('CLEAR_ACTIVE_CALL');
},
setIncomingCall({ commit, state: currentState }, callData) {
if (!callData?.callSid) return;
if (currentState.activeCall?.callSid === callData.callSid) return;
if (currentState.incomingCall?.callSid === callData.callSid) return;
commit('SET_INCOMING_CALL', { ...callData, receivedAt: Date.now() });
},
clearIncomingCall({ commit }) {
commit('CLEAR_INCOMING_CALL');
},
acceptIncomingCall({ commit, state: currentState }) {
const incomingCall = currentState.incomingCall;
if (!incomingCall) return;
commit('SET_ACTIVE_CALL', {
...incomingCall,
isJoined: true,
startedAt: Date.now(),
});
commit('CLEAR_INCOMING_CALL');
},
};
const mutations = {
SET_ACTIVE_CALL($state, callData) {
$state.activeCall = callData;
},
CLEAR_ACTIVE_CALL($state) {
$state.activeCall = null;
},
SET_INCOMING_CALL($state, callData) {
$state.incomingCall = callData;
},
CLEAR_INCOMING_CALL($state) {
$state.incomingCall = null;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};