livstream-player
Version:
A lightweight livestream player SDK for React apps
36 lines (35 loc) • 1.44 kB
JavaScript
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { LAST_ACTIVE_TIME, MAX_ENABLE_INACTIVE_LOGOUT, } from '../constants/system.keys';
import { resetDataToken } from '../redux/socket';
export const useInActiveLogout = () => {
const dispatch = useDispatch();
const maxEnableInactiveLogoutTime = localStorage.getItem(MAX_ENABLE_INACTIVE_LOGOUT);
useEffect(() => {
function handleSaveLastActive() {
localStorage.setItem(LAST_ACTIVE_TIME, Math.floor(new Date().getTime() / 1000));
}
// Bind the event listener
document.addEventListener('mousemove', handleSaveLastActive);
return () => {
document.removeEventListener('mousemove', handleSaveLastActive);
};
}, []);
useEffect(() => {
let intervalCheckLogout = null;
if (maxEnableInactiveLogoutTime) {
intervalCheckLogout = setInterval(() => {
const now = Math.floor(new Date().getTime() / 1000);
const lastActiveTime = localStorage.getItem(LAST_ACTIVE_TIME);
if (now - lastActiveTime > maxEnableInactiveLogoutTime) {
resetDataToken();
}
}, 3000);
}
return () => {
if (intervalCheckLogout) {
clearInterval(intervalCheckLogout);
}
};
}, [dispatch, maxEnableInactiveLogoutTime]);
};