Merge branch 'hotfix/2025-07-22-SocketProvider' into feature/IO-3255-simplified-part-management
This commit is contained in:
@@ -32,7 +32,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
|||||||
const socketRef = useRef(null);
|
const socketRef = useRef(null);
|
||||||
const [clientId, setClientId] = useState(null);
|
const [clientId, setClientId] = useState(null);
|
||||||
const [isConnected, setIsConnected] = useState(false);
|
const [isConnected, setIsConnected] = useState(false);
|
||||||
const [socketInitialized, setSocketInitialized] = useState(false);
|
|
||||||
const notification = useNotification();
|
const notification = useNotification();
|
||||||
const userAssociationId = bodyshop?.associations?.[0]?.id;
|
const userAssociationId = bodyshop?.associations?.[0]?.id;
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -148,13 +147,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
|||||||
onError: (err) => console.error("MARK_ALL_NOTIFICATIONS_READ error:", err)
|
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(() => {
|
useEffect(() => {
|
||||||
const initializeSocket = async (token) => {
|
const initializeSocket = async (token) => {
|
||||||
if (!bodyshop || !bodyshop.id || socketRef.current) return;
|
if (!bodyshop || !bodyshop.id || socketRef.current) return;
|
||||||
@@ -173,7 +165,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socketRef.current = socketInstance;
|
socketRef.current = socketInstance;
|
||||||
setSocketInitialized(true);
|
|
||||||
|
|
||||||
const handleBodyshopMessage = (message) => {
|
const handleBodyshopMessage = (message) => {
|
||||||
if (!message || !message.type) return;
|
if (!message || !message.type) return;
|
||||||
@@ -261,7 +252,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleConnect = () => {
|
const handleConnect = () => {
|
||||||
socketInstance.emit("join-bodyshop-room", bodyshop.id);
|
socketInstance.emit("join-bodyshop-room", bodyshop.id);
|
||||||
setClientId(socketInstance.id);
|
setClientId(socketInstance.id);
|
||||||
@@ -558,57 +549,6 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
|||||||
t
|
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 (
|
return (
|
||||||
<SocketContext.Provider
|
<SocketContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
|||||||
Reference in New Issue
Block a user