Merged in hotfix/2025-07-22-SocketProvider (pull request #2430)

Hotfix/2025 07 22 SocketProvider
This commit is contained in:
Patrick Fic
2025-07-22 21:24:59 +00:00

View File

@@ -32,7 +32,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
const socketRef = useRef(null);
const [clientId, setClientId] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const [socketInitialized, setSocketInitialized] = useState(false);
const notification = useNotification();
const userAssociationId = bodyshop?.associations?.[0]?.id;
const { t } = useTranslation();
@@ -148,13 +147,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
onError: (err) => console.error("MARK_ALL_NOTIFICATIONS_READ error:", err)
});
const checkAndReconnect = () => {
if (socketRef.current && !socketRef.current.connected) {
console.log("Attempting manual reconnect due to event trigger");
socketRef.current.connect();
}
};
useEffect(() => {
const initializeSocket = async (token) => {
if (!bodyshop || !bodyshop.id || socketRef.current) return;
@@ -166,14 +158,13 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
auth: { token, bodyshopId: bodyshop.id },
reconnectionAttempts: Infinity,
reconnectionDelay: 2000,
reconnectionDelayMax: 60000,
randomizationFactor: 0.5,
transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
rememberUpgrade: true
reconnectionDelayMax: 60000
// randomizationFactor: 0.5,
// transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
// rememberUpgrade: true
});
socketRef.current = socketInstance;
setSocketInitialized(true);
const handleBodyshopMessage = (message) => {
if (!message || !message.type) return;
@@ -261,7 +252,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
break;
}
};
const handleConnect = () => {
socketInstance.emit("join-bodyshop-room", bodyshop.id);
setClientId(socketInstance.id);
@@ -558,57 +549,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
t
]);
useEffect(() => {
if (!socketInitialized) return;
const onVisibilityChange = () => {
if (document.visibilityState === "visible") {
checkAndReconnect();
}
};
const onFocus = () => {
checkAndReconnect();
};
const onOnline = () => {
checkAndReconnect();
};
const onPageShow = (event) => {
if (event.persisted) {
checkAndReconnect();
}
};
document.addEventListener("visibilitychange", onVisibilityChange);
window.addEventListener("focus", onFocus);
window.addEventListener("online", onOnline);
window.addEventListener("pageshow", onPageShow);
// Sleep/wake detection using timer
let lastTime = Date.now();
const intervalMs = 1000; // Check every second
const thresholdMs = 2000; // If more than 2 seconds elapsed, assume sleep/wake
const sleepCheckInterval = setInterval(() => {
const currentTime = Date.now();
if (currentTime > lastTime + intervalMs + thresholdMs) {
console.log("Detected potential wake from sleep/hibernate");
checkAndReconnect();
}
lastTime = currentTime;
}, intervalMs);
return () => {
document.removeEventListener("visibilitychange", onVisibilityChange);
window.removeEventListener("focus", onFocus);
window.removeEventListener("online", onOnline);
window.removeEventListener("pageshow", onPageShow);
clearInterval(sleepCheckInterval);
};
}, [socketInitialized]);
return (
<SocketContext.Provider
value={{