/**
 * 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 { getPlatform } from 'design/theme/utils' ; param event export function getMappedAction ( event ) { const index = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ] . indexOf ( event . key ) ; const { isMac } = getPlatform ( ) ; const isModifierKey = ( isMac && event . ctrlKey ) || event . altKey ; let tabSwitch = undefined ; if ( isModifierKey && index !== - 1 ) { tabSwitch = { index } ; } return { tabSwitch } ; } param ctx const useKeyboardNav = ( ctx ) => { React . useEffect ( ( ) => { const handleKeydown = event => { const { tabSwitch } = getMappedAction ( event ) ; if ( ! tabSwitch ) { return ; } event . preventDefault ( ) ; const doc = ctx . getDocuments ( ) [ tabSwitch . index + 1 ] ; if ( doc ) { ctx . gotoTab ( doc ) ; } } ; window . addEventListener ( 'keydown' , handleKeydown ) ; return ( ) => window . removeEventListener ( 'keydown' , handleKeydown ) ; } , [ ] ) ; } ; export default useKeyboardNav ;