fix: prevent StrictMode double-mount from killing SIP WebSocket connection

This commit is contained in:
2026-03-17 20:08:22 +05:30
parent b2cc3d7012
commit d54d54f5f3
3 changed files with 30 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
import { createContext, useContext, useEffect, type PropsWithChildren } from 'react';
import { createContext, useContext, useEffect, useRef, type PropsWithChildren } from 'react';
import { useSipPhone } from '@/hooks/use-sip-phone';
type SipContextType = ReturnType<typeof useSipPhone>;
@@ -7,10 +7,15 @@ const SipContext = createContext<SipContextType | null>(null);
export const SipProvider = ({ children }: PropsWithChildren) => {
const sipPhone = useSipPhone();
const hasConnected = useRef(false);
// Auto-connect on mount
// Auto-connect on mount — skip StrictMode double-fire
useEffect(() => {
sipPhone.connect();
if (!hasConnected.current) {
hasConnected.current = true;
sipPhone.connect();
}
// Do NOT disconnect on cleanup — the SIP connection should persist
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);