2017-09-17 14:04:56 +00:00
|
|
|
import classnames from 'classnames';
|
2021-10-14 14:59:45 +00:00
|
|
|
import { KeyBindings, KeyCombination } from 'insomnia-common';
|
2021-09-15 20:16:04 +00:00
|
|
|
import React, { FC, memo } from 'react';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
2019-02-08 18:20:24 +00:00
|
|
|
import { isMac } from '../../common/constants';
|
|
|
|
import { constructKeyCombinationDisplay, getPlatformKeyCombinations } from '../../common/hotkeys';
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
|
|
|
/** One of these two must be given. If both is given, keyCombination will be used. */
|
|
|
|
keyCombination?: KeyCombination;
|
|
|
|
keyBindings?: KeyBindings;
|
|
|
|
className?: string;
|
|
|
|
/** Show fallback message if keyCombination is not given, but keyBindings has no key combinations. */
|
|
|
|
useFallbackMessage?: boolean;
|
|
|
|
}
|
2017-09-17 14:04:56 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
export const Hotkey: FC<Props> = memo(({ keyCombination, keyBindings, className, useFallbackMessage }) => {
|
|
|
|
if (keyCombination == null && keyBindings == null) {
|
|
|
|
console.error('Hotkey needs one of keyCombination or keyBindings!');
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-08 18:20:24 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
let keyComb: KeyCombination | null = null;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
if (keyCombination != null) {
|
|
|
|
keyComb = keyCombination;
|
|
|
|
} else if (keyBindings != null) {
|
|
|
|
const keyCombs = getPlatformKeyCombinations(keyBindings);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
// Only take the first key combination if there is a mapping.
|
|
|
|
if (keyCombs.length > 0) {
|
|
|
|
keyComb = keyCombs[0];
|
2019-02-08 18:20:24 +00:00
|
|
|
}
|
2021-09-15 20:16:04 +00:00
|
|
|
}
|
2019-02-08 18:20:24 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
let display = '';
|
2019-02-08 18:20:24 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
if (keyComb != null) {
|
|
|
|
display = constructKeyCombinationDisplay(keyComb, false);
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
const isFallback = display.length === 0 && useFallbackMessage;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
if (isFallback) {
|
|
|
|
display = 'Not defined';
|
2017-03-28 22:45:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 20:16:04 +00:00
|
|
|
const classes = {
|
|
|
|
'font-normal': isMac(),
|
|
|
|
italic: isFallback,
|
|
|
|
};
|
|
|
|
|
2021-12-01 19:25:13 +00:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
className={classnames(className, classes)}
|
|
|
|
style={{ verticalAlign: 'middle' }}
|
|
|
|
>
|
|
|
|
{display}
|
|
|
|
</span>
|
|
|
|
);
|
2021-09-15 20:16:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Hotkey.displayName = 'Hotkey';
|