1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect } from "react-redux";
6 import { useIdleTimer } from "react-idle-timer";
7 import { Dispatch } from "redux";
9 import { RootState } from "~/store/store";
10 import { SnackbarKind, snackbarActions } from "~/store/snackbar/snackbar-actions";
11 import { logout } from "~/store/auth/auth-action";
12 import parse from "parse-duration";
13 import * as React from "react";
14 import { min } from "lodash";
16 interface AutoLogoutDataProps {
17 sessionIdleTimeout: number;
18 lastWarningDuration: number;
21 interface AutoLogoutActionProps {
23 doWarn: (message: string, duration: number) => void;
24 doCloseWarn: () => void;
27 const mapStateToProps = (state: RootState, ownProps: any): AutoLogoutDataProps => {
29 sessionIdleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
30 lastWarningDuration: ownProps.lastWarningDuration || 60,
34 const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({
35 doLogout: () => dispatch<any>(logout(true)),
36 doWarn: (message: string, duration: number) =>
37 dispatch(snackbarActions.OPEN_SNACKBAR({
38 message, hideDuration: duration, kind: SnackbarKind.WARNING })),
39 doCloseWarn: () => dispatch(snackbarActions.CLOSE_SNACKBAR()),
42 export type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
44 export const AutoLogoutComponent = (props: AutoLogoutProps) => {
45 let logoutTimer: NodeJS.Timer;
46 const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000 ;
48 const handleOnIdle = () => {
49 logoutTimer = setTimeout(
50 () => props.doLogout(), lastWarningDuration);
52 "Your session is about to be closed due to inactivity",
56 const handleOnActive = () => {
57 clearTimeout(logoutTimer);
62 timeout: (props.lastWarningDuration < props.sessionIdleTimeout)
63 ? 1000 * (props.sessionIdleTimeout - props.lastWarningDuration)
66 onActive: handleOnActive,
73 export const AutoLogout = connect(mapStateToProps, mapDispatchToProps)(AutoLogoutComponent);