17782: Additional imports and typing fixes.
[arvados-workbench2.git] / src / views-components / auto-logout / auto-logout.tsx
index 9ccf79a57f881f759e5c8af28d487682af4567dc..9a8299b25ca3b351f6c3f9ee161f9e6b67bb5d0c 100644 (file)
@@ -6,11 +6,11 @@ import { connect } from "react-redux";
 import { useIdleTimer } from "react-idle-timer";
 import { Dispatch } from "redux";
 
-import { RootState } from "~/store/store";
-import { SnackbarKind, snackbarActions } from "~/store/snackbar/snackbar-actions";
-import { logout } from "~/store/auth/auth-action";
+import { RootState } from "store/store";
+import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
+import { logout } from "store/auth/auth-action";
 import parse from "parse-duration";
-import * as React from "react";
+import React from "react";
 import { min } from "lodash";
 
 interface AutoLogoutDataProps {
@@ -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)),
@@ -42,7 +40,7 @@ const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({
 export type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
 
 const debounce = (delay: number | undefined, fn: Function) => {
-    let timerId: number | null;
+    let timerId: NodeJS.Timer | null;
     return (...args: any[]) => {
         if (timerId) { clearTimeout(timerId); }
         timerId = setTimeout(() => {
@@ -52,20 +50,30 @@ const debounce = (delay: number | undefined, fn: Function) => {
     };
 };
 
-const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp';
+export const LAST_ACTIVE_TIMESTAMP = 'lastActiveTimestamp';
 
 export const AutoLogoutComponent = (props: AutoLogoutProps) => {
     let logoutTimer: NodeJS.Timer;
     const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000;
-    const handleOtherTabActivity = debounce(500, () => {
-        handleOnActive();
-        reset();
-    });
 
-    window.addEventListener('storage', (e: StorageEvent) => {
-        // Other tab activity detected by a localStorage change event.
-        if (e.key === LAST_ACTIVE_TIMESTAMP) { handleOtherTabActivity(); }
-    });
+    // Runs once after render
+    React.useEffect(() => {
+        window.addEventListener('storage', handleStorageEvents);
+        // Component cleanup
+        return () => {
+            window.removeEventListener('storage', handleStorageEvents);
+        };
+    }, []);
+
+    const handleStorageEvents = (e: StorageEvent) => {
+        if (e.key === LAST_ACTIVE_TIMESTAMP) {
+            // Other tab activity detected by a localStorage change event.
+            debounce(500, () => {
+                handleOnActive();
+                reset();
+            })();
+        }
+    };
 
     const handleOnIdle = () => {
         logoutTimer = setTimeout(