Skip to content

Commit a880a1d

Browse files
committed
feat: add support for extensible shortcut for LogResizeButton
1 parent 288808b commit a880a1d

File tree

5 files changed

+57
-31
lines changed

5 files changed

+57
-31
lines changed

src/Common/Constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,5 @@ export const VULNERABILITIES_SORT_PRIORITY = {
548548
low: 4,
549549
unknown: 5,
550550
}
551+
552+
export const IS_PLATFORM_MAC_OS = window.navigator.userAgent.toUpperCase().includes('MAC')

src/Common/Tooltip/ShortcutKeyComboTooltipContent.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { KEYBOARD_KEYS_MAP, TooltipProps } from './types'
33
const ShortcutKeyComboTooltipContent = ({ text, combo }: TooltipProps['shortcutKeyCombo']) => (
44
<div className="flexbox dc__gap-8 px-8 py-4 flex-wrap">
55
<span className="lh-18 fs-12 fw-4 cn-0">{text}</span>
6-
<div className="flexbox dc__gap-4 dc__align-items-center flex-wrap">
7-
{combo.map((key) => (
8-
<span key={key} className="shortcut-keys__chip dc__capitalize lh-16 fs-11 fw-5 flex">
9-
{KEYBOARD_KEYS_MAP[key]}
10-
</span>
11-
))}
12-
</div>
6+
{!!combo?.length && (
7+
<div className="flexbox dc__gap-4 dc__align-items-center flex-wrap">
8+
{combo.map((key) => (
9+
<span key={key} className="shortcut-keys__chip dc__capitalize lh-16 fs-11 fw-5 flex">
10+
{KEYBOARD_KEYS_MAP[key]}
11+
</span>
12+
))}
13+
</div>
14+
)}
1315
</div>
1416
)
1517

src/Common/Tooltip/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { IS_PLATFORM_MAC_OS } from '@Common/Constants'
12
import { TippyProps } from '@tippyjs/react'
23

3-
const isMacOS = navigator.userAgent.toUpperCase().includes('MAC')
4-
54
export const KEYBOARD_KEYS_MAP = {
6-
Control: isMacOS ? '⌘' : 'Ctrl',
5+
Control: IS_PLATFORM_MAC_OS ? '⌘' : 'Ctrl',
76
Shift: '⇧',
87
F: 'F',
98
E: 'E',

src/Shared/Components/CICDHistory/History.components.tsx

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ import Tippy from '@tippyjs/react'
1818
import { useEffect, useRef } from 'react'
1919
import { useLocation } from 'react-router-dom'
2020
import { withShortcut, IWithShortcut } from 'react-keybind'
21-
import { ClipboardButton, GenericEmptyState, Tooltip, extractImage, useSuperAdmin } from '../../../Common'
21+
import {
22+
ClipboardButton,
23+
GenericEmptyState,
24+
Tooltip,
25+
extractImage,
26+
IS_PLATFORM_MAC_OS,
27+
useSuperAdmin,
28+
} from '../../../Common'
2229
import { EMPTY_STATE_STATUS } from '../../constants'
2330
import { ReactComponent as DropDownIcon } from '../../../Assets/Icon/ic-chevron-down.svg'
2431
import { GitChangesType, LogResizeButtonType, ScrollerType } from './types'
@@ -29,47 +36,63 @@ import { ReactComponent as ZoomOut } from '../../../Assets/Icon/ic-exit-fullscre
2936
import './cicdHistory.scss'
3037

3138
export const LogResizeButton = withShortcut(
32-
({ fullScreenView, setFullScreenView, shortcut }: LogResizeButtonType & IWithShortcut): JSX.Element => {
39+
({
40+
shortcutCombo = ['F'],
41+
onlyOnLogs = true,
42+
fullScreenView,
43+
setFullScreenView,
44+
shortcut,
45+
}: LogResizeButtonType & IWithShortcut): JSX.Element => {
3346
const { pathname } = useLocation()
34-
const zoomButtonRef = useRef<HTMLDivElement>(null)
47+
const zoomButtonRef = useRef<HTMLButtonElement>(null)
3548

36-
const toggleFullScreen = (): void => {
49+
const toggleFullScreen = () => {
3750
// NOTE: need to use ref due to the problem of stale function reference after registering the callback
3851
setFullScreenView(!(zoomButtonRef.current.dataset.isFullscreenView === 'true'))
3952
}
4053

54+
const showButton = pathname.includes('/logs') || !onlyOnLogs
55+
const doesShortcutContainCmdKey = shortcutCombo.some((key) => key === 'Control') && IS_PLATFORM_MAC_OS
56+
4157
useEffect(() => {
42-
shortcut.registerShortcut(toggleFullScreen, ['f'], 'ToggleFullscreen', 'Enter/Exit fullscreen')
43-
shortcut.registerShortcut(
44-
() => setFullScreenView(false),
45-
['Escape'],
46-
'ToggleFullscreen',
47-
'Enter/Exit fullscreen',
48-
)
58+
const combo = shortcutCombo
59+
.map((key) => {
60+
if (key === 'Control') {
61+
return IS_PLATFORM_MAC_OS ? 'cmd' : 'ctrl'
62+
}
63+
return key.toLowerCase()
64+
})
65+
.join('+')
66+
67+
// FIXME: disabling shortcut for macos since pressing cmd breaks shortcuts through react-keybind
68+
if (showButton && shortcutCombo.length && !doesShortcutContainCmdKey) {
69+
shortcut.registerShortcut(toggleFullScreen, [combo], 'ToggleFullscreen', 'Enter/Exit fullscreen')
70+
}
4971

5072
return () => {
51-
shortcut.unregisterShortcut(['f'])
52-
shortcut.unregisterShortcut(['Escape'])
73+
shortcut.unregisterShortcut([combo])
5374
}
54-
}, [pathname.includes('/logs')])
75+
}, [showButton])
5576

5677
return (
57-
pathname.includes('/logs') && (
78+
showButton && (
5879
<Tooltip
5980
placement="left"
6081
shortcutKeyCombo={{
6182
text: fullScreenView ? 'Exit fullscreen' : 'Enter fullscreen',
62-
combo: ['F'] as const,
83+
combo: doesShortcutContainCmdKey ? null : shortcutCombo,
6384
}}
6485
>
65-
<div
86+
<button
87+
type="button"
88+
aria-label="Enter/Exit fullscreen"
6689
ref={zoomButtonRef}
6790
data-is-fullscreen-view={fullScreenView}
68-
className={`zoom ${fullScreenView ? 'zoom--out' : 'zoom--in'} pointer dc__zi-4 flex`}
91+
className={`zoom ${fullScreenView ? 'zoom--out' : 'zoom--in'} pointer dc__zi-4 flex dc__transparent`}
6992
onClick={toggleFullScreen}
7093
>
7194
{fullScreenView ? <ZoomOut className="icon-dim-16" /> : <ZoomIn className="icon-dim-16" />}
72-
</div>
95+
</button>
7396
</Tooltip>
7497
)
7598
)

src/Shared/Components/CICDHistory/types.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
PaginationProps,
2828
useScrollable,
2929
SortingOrder,
30+
SupportedKeyboardKeysType,
3031
} from '../../../Common'
3132
import { DeploymentStageType } from '../../constants'
3233
import { AggregationKeys, GitTriggers, Node, NodeType, ResourceKindType, ResourceVersionType } from '../../types'
@@ -46,9 +47,8 @@ export enum FetchIdDataStatus {
4647
}
4748

4849
export interface LogResizeButtonType {
49-
shortcutCombo?: string[]
50+
shortcutCombo?: SupportedKeyboardKeysType[]
5051
onlyOnLogs?: boolean
51-
disableKeybindings?: boolean
5252
fullScreenView: boolean
5353
setFullScreenView: React.Dispatch<React.SetStateAction<boolean>>
5454
}

0 commit comments

Comments
 (0)