rebreak-monorepo/apps/rebreak-native/hooks/useCallKeepEvents.ts
chahinebrini 5531ef5419 fix(calls): foreground call screen no longer disappears after few seconds
Root cause: iOS CallKit auto-dismisses incoming-call UI after ~5s when the
app is in foreground (because AppDelegate.didReceiveIncomingPush MUST call
reportNewIncomingCall — Apple requirement). That CallKit dismiss fires an
endCall event which our useCallKeepEvents.onEnd translated to declineCall,
unmounting the in-app /call screen before the user could tap accept/decline.

Fixes:
- useCallKeepEvents.onEnd: ignore CallKit endCall when iOS app is foreground
  AND status==='incoming' (in-app UI is authoritative there). Comment with
  big warning not to remove this again.
- call.tsx closeScreen: replace('/') instead of router.back() to avoid
  GO_BACK action errors when navigation stack is inconsistent after long
  calls (manifested as wrap-jsx.js crash in react-native-css-interop).
- useIncomingCalls: log CANCEL receive events for future diagnostics.
- call.ts: clog hangup/declineCall/closeScreen with reason+status for trace.

Verified: foreground call screen stays up the full UNANSWERED_MS (35s) and
caller-side hangup('unanswered') correctly triggers iPhone closeScreen via
cancel-broadcast.
2026-06-04 21:48:34 +02:00

121 lines
5.0 KiB
TypeScript

/**
* Bridge zwischen CallKit/ConnectionService-Events und unserer Call-Store.
*
* Wenn der User in der nativen Call-UI Accept/Reject/Hangup tippt, kommt das
* NICHT über unser React-UI rein — sondern via RNCallKeep-Events. Wir
* übersetzen die in store-Actions.
*
* Wird einmal app-weit im _layout.tsx aufgerufen.
*/
import { useEffect } from 'react';
import { AppState, Platform } from 'react-native';
import { useRouter } from 'expo-router';
import RNCallKeep from 'react-native-callkeep';
import { useCallStore } from '../stores/call';
import { setupCallKeep } from '../lib/callkit';
// VoIP-PushKit (iOS only) — Payload-Empfang um peer-Info in den Store zu
// hydrieren, BEVOR User in der CallKit-UI auf "Annehmen" tippt.
let RNVoipPushNotification: any = null;
if (Platform.OS === 'ios') {
try {
RNVoipPushNotification = require('react-native-voip-push-notification').default;
} catch {
// ungelinkt — skip
}
}
export function useCallKeepEvents() {
const router = useRouter();
useEffect(() => {
void setupCallKeep();
// VoIP-Push-Payload (iOS) → Store hydrieren mit caller-Info, damit
// acceptCall() den richtigen peer kennt. Der CallKit-UI-Show ist bereits
// in AppDelegate.swift (reportNewIncomingCall) erfolgt.
const onVoipNotification = (payload: any) => {
console.log('[voip] incoming push payload', payload);
const st = useCallStore.getState();
const callId = String(payload?.callId ?? '');
const from = payload?.from;
if (!callId || !from?.id) return;
// receiveIncoming dedupliziert via status-Check → safe doppelt zu rufen.
st.receiveIncoming(callId, {
id: String(from.id),
nickname: String(from.nickname ?? payload?.callerName ?? 'ReBreak'),
avatar: from.avatar ?? null,
});
};
if (RNVoipPushNotification) {
RNVoipPushNotification.addEventListener('notification', onVoipNotification);
// didLoadWithEvents wird beim Cold-Start gefeuert mit den queued Events,
// die vor dem ersten addEventListener-Call eingegangen sind.
RNVoipPushNotification.addEventListener('didLoadWithEvents', (events: any[]) => {
for (const ev of events ?? []) {
if (ev?.name === 'RNVoipPushRemoteNotificationReceivedEvent') {
onVoipNotification(ev?.data);
}
}
});
}
// User tippt "Annehmen" in der CallKit-/ConnectionService-UI
const onAnswer = ({ callUUID }: { callUUID: string }) => {
console.log('[callkeep] answer', callUUID, 'appState=', AppState.currentState);
const st = useCallStore.getState();
if (st.status !== 'incoming') return;
// Call-Screen \u00f6ffnen und Accept-Flow triggern.
router.push('/call');
void st.acceptCall();
};
// User tippt "Ablehnen" oder "Auflegen" in der nativen UI
const onEnd = ({ callUUID }: { callUUID: string }) => {
const appState = AppState.currentState;
const st = useCallStore.getState();
console.log('[callkeep] end', callUUID, 'appState=', appState, 'storeStatus=', st.status);
if (st.status === 'idle' || st.status === 'ended') return;
// iOS-Foreground: AppDelegate.didReceiveIncomingPush MUSS reportNewIncomingCall
// aufrufen (Apple-Pflicht, sonst killt iOS die App). Im Foreground bedient
// sich der User aber an unserem In-App /call-Screen mit Accept/Decline-
// Buttons. CallKit's Auto-Dismiss-Timer (~5s im Foreground!) darf den
// User-Flow NICHT unterbrechen — sonst verschwindet der /call-Screen nach
// 1-5s ohne dass der User irgendwas getippt hat.
// (Empirisch verifiziert: dieser early-return ist genau der Fix für das
// "1-3s disappear"-Bug. Bitte nicht erneut entfernen.)
if (Platform.OS === 'ios' && appState === 'active' && st.status === 'incoming') {
console.log('[callkeep] endCall IGNORED (iOS foreground incoming — in-app UI is authoritative)');
return;
}
if (st.status === 'incoming') {
st.declineCall();
} else {
st.hangup('ended');
}
};
// User mutet/unmutet über die native UI
const onMuted = ({ muted }: { muted: boolean; callUUID: string }) => {
const st = useCallStore.getState();
if (st.muted !== muted) st.toggleMute();
};
RNCallKeep.addEventListener('answerCall', onAnswer);
RNCallKeep.addEventListener('endCall', onEnd);
RNCallKeep.addEventListener('didPerformSetMutedCallAction', onMuted);
// didActivateAudioSession kommt nach CallKit-Audio-Activation — wir nutzen
// das (noch) nicht aktiv, weil WebRTC + InCallManager das selber regeln.
return () => {
RNCallKeep.removeEventListener('answerCall');
RNCallKeep.removeEventListener('endCall');
RNCallKeep.removeEventListener('didPerformSetMutedCallAction');
if (RNVoipPushNotification) {
RNVoipPushNotification.removeEventListener('notification');
RNVoipPushNotification.removeEventListener('didLoadWithEvents');
}
};
}, [router]);
}