16848: Adds test for idle timer reset via localStorage event.
[arvados-workbench2.git] / src / views-components / auto-logout / auto-logout.tsx
index 52a1950ae087b885b8201ed56268592e190c7483..f7e6f4b838d0082feff5c2bc8d9ff0f4d14c61cb 100644 (file)
@@ -24,12 +24,10 @@ interface AutoLogoutActionProps {
     doCloseWarn: () => void;
 }
 
-const mapStateToProps = (state: RootState, ownProps: any): AutoLogoutDataProps => {
-    return {
-        sessionIdleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
-        lastWarningDuration: ownProps.lastWarningDuration || 60,
-    };
-};
+const mapStateToProps = (state: RootState, ownProps: any): AutoLogoutDataProps => ({
+    sessionIdleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
+    lastWarningDuration: ownProps.lastWarningDuration || 60,
+});
 
 const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({
     doLogout: () => dispatch<any>(logout(true)),
@@ -39,34 +37,74 @@ const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({
     doCloseWarn: () => dispatch(snackbarActions.CLOSE_SNACKBAR()),
 });
 
-type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
+export type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
 
-export const AutoLogout = connect(mapStateToProps, mapDispatchToProps)(
-    (props: AutoLogoutProps) => {
-        let logoutTimer: NodeJS.Timer;
-        const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000 ;
+const debounce = (delay: number | undefined, fn: Function) => {
+    let timerId: number | null;
+    return (...args: any[]) => {
+        if (timerId) { clearTimeout(timerId); }
+        timerId = setTimeout(() => {
+            fn(...args);
+            timerId = null;
+        }, delay);
+    };
+};
 
-        const handleOnIdle = () => {
-            logoutTimer = setTimeout(
-                () => props.doLogout(), lastWarningDuration);
-            props.doWarn(
-                "Your session is about to be closed due to inactivity",
-                lastWarningDuration);
-        };
+export const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp';
+
+export const AutoLogoutComponent = (props: AutoLogoutProps) => {
+    let logoutTimer: NodeJS.Timer;
+    const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000;
 
-        const handleOnActive = () => {
-            clearTimeout(logoutTimer);
-            props.doCloseWarn();
+    // Runs once after render
+    React.useEffect(() => {
+        window.addEventListener('storage', handleStorageEvents);
+        // Component cleanup
+        return () => {
+            window.removeEventListener('storage', handleStorageEvents);
         };
+    }, []);
 
-        useIdleTimer({
-            timeout: (props.lastWarningDuration < props.sessionIdleTimeout)
-                ? 1000 * (props.sessionIdleTimeout - props.lastWarningDuration)
-                : 1,
-            onIdle: handleOnIdle,
-            onActive: handleOnActive,
-            debounce: 500
-        });
+    const handleStorageEvents = (e: StorageEvent) => {
+        if (e.key === LAST_ACTIVE_TIMESTAMP) {
+            // Other tab activity detected by a localStorage change event.
+            debounce(500, () => {
+                handleOnActive();
+                reset();
+            })();
+        }
+    };
 
-        return <span />;
+    const handleOnIdle = () => {
+        logoutTimer = setTimeout(
+            () => props.doLogout(), lastWarningDuration);
+        props.doWarn(
+            "Your session is about to be closed due to inactivity",
+            lastWarningDuration);
+    };
+
+    const handleOnActive = () => {
+        if (logoutTimer) { clearTimeout(logoutTimer); }
+        props.doCloseWarn();
+    };
+
+    const handleOnAction = () => {
+        // Notify the other tabs there was some activity.
+        const now = (new Date).getTime();
+        localStorage.setItem(LAST_ACTIVE_TIMESTAMP, now.toString());
+    };
+
+    const { reset } = useIdleTimer({
+        timeout: (props.lastWarningDuration < props.sessionIdleTimeout)
+            ? 1000 * (props.sessionIdleTimeout - props.lastWarningDuration)
+            : 1,
+        onIdle: handleOnIdle,
+        onActive: handleOnActive,
+        onAction: handleOnAction,
+        debounce: 500
     });
+
+    return <span />;
+};
+
+export const AutoLogout = connect(mapStateToProps, mapDispatchToProps)(AutoLogoutComponent);