fix(voice): keep ringing WhatsApp calls alive across page refresh

Inverted the previous decision: only the active (accepted) call gets
terminated on pagehide, since its WebRTC session dies with the page and
genuinely can't be rejoined. Ringing calls have no WebRTC state yet —
they're just a row on the backend and a state on Meta — so killing them
on refresh would needlessly hang up on the customer when the agent
intended to recover their UI, not reject the call.

After this, the flow on refresh during a ringing inbound is:
  - beforeunload prompt fires (warning the agent)
  - pagehide does nothing for ringing calls; active calls still terminate
  - new page loads, conversation messages fetch
  - seedCallsFromHydratedMessages picks up status='ringing' voice_call
    messages and populates the calls store
  - FloatingCallWidget reappears with Accept; agent clicks; acceptIncomingCall
    fetches the SDP via /whatsapp_calls/:id and the call connects
This commit is contained in:
Tanmay Deep Sharma
2026-05-03 16:49:00 +07:00
parent 3c959f0604
commit 6179adac22
@@ -6,7 +6,6 @@ import { useCallsStore } from 'dashboard/stores/calls';
import {
useWhatsappCallSession,
sendWhatsappTerminateBeacon,
sendWhatsappCallBeacon,
cleanupWhatsappSession,
} from 'dashboard/composables/useWhatsappCallSession';
import { handleVoiceCallCreated } from 'dashboard/helper/voice';
@@ -70,15 +69,13 @@ export function useCallSession() {
});
};
// pagehide fires after the user confirms the prompt. Beacon a terminate for
// every WhatsApp call in the store — active OR ringing — since browser-direct
// WebRTC has no rejoin path, so any call that survives the close becomes
// permanently orphaned on Meta's side.
// pagehide fires after the user confirms the refresh prompt. Terminate the
// active call only — its WebRTC session dies with the page and can't be
// rejoined. Ringing calls intentionally stay alive on Meta so the agent can
// pick them up after the page reloads (FloatingCallWidget rehydrates them
// via seedCallsFromHydratedMessages once the conversation messages land).
const handlePageHide = () => {
sendWhatsappTerminateBeacon();
callsStore.calls
.filter(c => c.provider === 'whatsapp' && !c.isActive && c.callId)
.forEach(c => sendWhatsappCallBeacon(c.callId));
};
const handleTwilioDisconnected = () => callsStore.clearActiveCall();