/**
 * Copyright 2020 Gravitational, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import React from 'react' ; import ConsoleContext from './consoleContext' ; import * as stores from './stores/types' ; import session from 'teleport/services/session' ; const TAB_MIN_AGE = 30000 ; param ctx function useOnExitConfirmation ( ctx ) { React . useEffect ( ( ) => { /**
         * handleBeforeUnload listens for browser closes and refreshes.
         * Checks if users need to be notified before closing based on type
         * of document opened and how long it has been active for.
         */ const handleBeforeunload = event => { if ( ! session . isValid ( ) ) { return ; } const shouldNotify = ctx . getDocuments ( ) . some ( hasLastingSshConnection ) ; if ( shouldNotify ) { event . preventDefault ( ) ; event . returnValue = '' ; } } ; window . addEventListener ( 'beforeunload' , handleBeforeunload ) ; return ( ) => { window . removeEventListener ( 'beforeunload' , handleBeforeunload ) ; } ; } , [ ] ) ; param doc function hasLastingSshConnection ( doc ) { if ( doc . kind !== 'terminal' || doc . status !== 'connected' ) { return false ; } const created = doc . created . getTime ( ) ; const fromNow = new Date ( ) . getTime ( ) ; return fromNow - created > TAB_MIN_AGE ; } param doc function verifyAndConfirm ( doc ) { if ( hasLastingSshConnection ( doc ) ) { const sid = ( doc as stores . DocumentSsh ) . sid ; const participants = ctx . storeParties . state [ sid ] ; if ( ! participants ) { return true ; } if ( participants . length > 1 ) { return window . confirm ( 'Are you sure you want to leave this session?' ) ; } return window . confirm ( 'Are you sure you want to terminate this session?' ) ; } return true ; } return { verifyAndConfirm , hasLastingSshConnection } ; } export default useOnExitConfirmation ;