Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 533x 533x 533x 533x 40x 40x 40x 40x 40x 40x 40x 20x 40x 40x 40x 40x 10x 10x 10x 10x 10x 40x | import { useEffect, useRef, useCallback } from "react";
import NiceModal from "@ebay/nice-modal-react";
import Cookies from "js-cookie";
import { SESSION_EXPIRED_MODAL } from "modals/modal_names";
import { useAppSelector } from "@redux/hooks";
import { selectAuth } from "@redux/slices/auth/auth";
import { sessionWorkerManager } from "./sessionWorkerManager";
const SESSION_DURATION = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const SESSION_STORAGE_KEY = "session_start_time";
// Utility function to check if session has expired
export const isSessionExpired = (): boolean => {
const sessionStartTime = localStorage.getItem(SESSION_STORAGE_KEY);
if (!sessionStartTime) {
return false;
}
const elapsedTime = Date.now() - parseInt(sessionStartTime);
return elapsedTime >= SESSION_DURATION;
};
export const useSessionTimeoutWithWorker = () => {
const isAuthenticated = useAppSelector(selectAuth);
const hasShownModalRef = useRef(false);
const cleanupRef = useRef<(() => void) | null>(null);
const showSessionExpiredModal = useCallback(() => {
if (hasShownModalRef.current) return;
hasShownModalRef.current = true;
localStorage.removeItem(SESSION_STORAGE_KEY);
NiceModal.show(SESSION_EXPIRED_MODAL);
}, []);
const handleWorkerMessage = useCallback((type: string) => {
switch (type) {
case "SESSION_EXPIRED":
console.log('🚨 Session expired from singleton worker');
showSessionExpiredModal();
break;
case "WORKER_ERROR":
console.error("Session worker error from singleton");
break;
default:
break;
}
}, [showSessionExpiredModal]);
const startSessionMonitoring = useCallback(() => {
hasShownModalRef.current = false;
if (isSessionExpired()) {
showSessionExpiredModal();
return;
}
// Clean up any existing listener first
if (cleanupRef.current) {
cleanupRef.current();
}
// Add listener to singleton worker manager
const cleanup = sessionWorkerManager.addListener(handleWorkerMessage);
cleanupRef.current = cleanup;
// Start monitoring
sessionWorkerManager.startMonitoring();
}, [handleWorkerMessage, showSessionExpiredModal]);
const clearSessionTimer = () => {
Iif (cleanupRef.current) {
console.log('⏹️ Stopping monitoring from singleton worker');
cleanupRef.current();
cleanupRef.current = null;
}
};
const clearSessionData = () => {
clearSessionTimer();
localStorage.removeItem(SESSION_STORAGE_KEY);
};
const setSessionStartTime = () => {
const timestamp = Date.now().toString();
localStorage.setItem(SESSION_STORAGE_KEY, timestamp);
startSessionMonitoring();
};
const resetSessionTimer = () => {
localStorage.setItem(SESSION_STORAGE_KEY, Date.now().toString());
startSessionMonitoring();
};
useEffect(() => {
const userCookie = Cookies.get("user");
Iif (isAuthenticated && userCookie) {
const sessionStartTime = localStorage.getItem(SESSION_STORAGE_KEY);
if (sessionStartTime) {
startSessionMonitoring();
}
} else {
clearSessionTimer();
}
return () => {
clearSessionTimer();
};
}, [isAuthenticated, startSessionMonitoring]);
return {
setSessionStartTime,
resetSessionTimer,
clearSessionTimer,
clearSessionData,
};
};
|