Remove Escape from hotkeys

This commit is contained in:
Gregory Schier
2024-02-27 18:58:41 -08:00
parent a40437a7fb
commit 597664d6f9
7 changed files with 52 additions and 26 deletions

View File

@@ -828,11 +828,14 @@ async fn cmd_send_http_request(
.expect("Failed to get request"); .expect("Failed to get request");
let environment = match environment_id { let environment = match environment_id {
Some(id) => Some( Some(id) =>
get_environment(&window, id) match get_environment(&window, id).await {
.await Ok(env) => Some(env),
.expect("Failed to get environment"), Err(e) => {
), warn!("Failed to find environment by id {id} {}", e);
None
}
},
None => None, None => None,
}; };

View File

@@ -1,7 +1,7 @@
import classNames from 'classnames'; import classNames from 'classnames';
import { useMemo, useRef } from 'react'; import { useMemo, useRef } from 'react';
import { useKey, useKeyPressEvent } from 'react-use'; import { useKey, useKeyPressEvent } from 'react-use';
import { useActiveEnvironmentId } from '../hooks/useActiveEnvironmentId'; import { useActiveEnvironment } from '../hooks/useActiveEnvironment';
import { useActiveRequest } from '../hooks/useActiveRequest'; import { useActiveRequest } from '../hooks/useActiveRequest';
import { useActiveWorkspaceId } from '../hooks/useActiveWorkspaceId'; import { useActiveWorkspaceId } from '../hooks/useActiveWorkspaceId';
import { useAppRoutes } from '../hooks/useAppRoutes'; import { useAppRoutes } from '../hooks/useAppRoutes';
@@ -20,7 +20,7 @@ export function RecentRequestsDropdown({ className }: Pick<ButtonProps, 'classNa
const dropdownRef = useRef<DropdownRef>(null); const dropdownRef = useRef<DropdownRef>(null);
const activeRequest = useActiveRequest(); const activeRequest = useActiveRequest();
const activeWorkspaceId = useActiveWorkspaceId(); const activeWorkspaceId = useActiveWorkspaceId();
const activeEnvironmentId = useActiveEnvironmentId(); const activeEnvironment = useActiveEnvironment();
const httpRequests = useHttpRequests(); const httpRequests = useHttpRequests();
const grpcRequests = useGrpcRequests(); const grpcRequests = useGrpcRequests();
const routes = useAppRoutes(); const routes = useAppRoutes();
@@ -68,7 +68,7 @@ export function RecentRequestsDropdown({ className }: Pick<ButtonProps, 'classNa
onSelect: () => { onSelect: () => {
routes.navigate('request', { routes.navigate('request', {
requestId: request.id, requestId: request.id,
environmentId: activeEnvironmentId ?? undefined, environmentId: activeEnvironment?.id,
workspaceId: activeWorkspaceId, workspaceId: activeWorkspaceId,
}); });
}, },
@@ -87,7 +87,7 @@ export function RecentRequestsDropdown({ className }: Pick<ButtonProps, 'classNa
} }
return recentRequestItems.slice(0, 20); return recentRequestItems.slice(0, 20);
}, [activeWorkspaceId, activeEnvironmentId, recentRequestIds, requests, routes]); }, [activeWorkspaceId, activeEnvironment?.id, recentRequestIds, requests, routes]);
return ( return (
<Dropdown ref={dropdownRef} items={items}> <Dropdown ref={dropdownRef} items={items}>

View File

@@ -2,7 +2,7 @@ import classNames from 'classnames';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import type { ReactNode } from 'react'; import type { ReactNode } from 'react';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useHotKey } from '../../hooks/useHotKey'; import { useKey } from 'react-use';
import { Overlay } from '../Overlay'; import { Overlay } from '../Overlay';
import { Heading } from './Heading'; import { Heading } from './Heading';
import { IconButton } from './IconButton'; import { IconButton } from './IconButton';
@@ -36,7 +36,15 @@ export function Dialog({
[description], [description],
); );
useHotKey('popup.close', onClose, { enable: open }); useKey(
'Escape',
() => {
if (!open) return;
onClose();
},
{},
[open],
);
return ( return (
<Overlay open={open} onClose={onClose} portalName="dialog"> <Overlay open={open} onClose={onClose} portalName="dialog">

View File

@@ -244,13 +244,15 @@ const Menu = forwardRef<Omit<DropdownRef, 'open' | 'isOpen' | 'toggle'>, MenuPro
} }
}; };
useHotKey( useKey(
'popup.close', 'Escape',
() => { () => {
if (!isOpen) return;
if (filter !== '') setFilter(''); if (filter !== '') setFilter('');
else handleClose(); else handleClose();
}, },
{ enable: isOpen }, {},
[isOpen, filter, setFilter, handleClose],
); );
const handlePrev = useCallback(() => { const handlePrev = useCallback(() => {
@@ -288,11 +290,13 @@ const Menu = forwardRef<Omit<DropdownRef, 'open' | 'isOpen' | 'toggle'>, MenuPro
}, [items]); }, [items]);
useKey('ArrowUp', (e) => { useKey('ArrowUp', (e) => {
if (!isOpen) return;
e.preventDefault(); e.preventDefault();
handlePrev(); handlePrev();
}); });
useKey('ArrowDown', (e) => { useKey('ArrowDown', (e) => {
if (!isOpen) return;
e.preventDefault(); e.preventDefault();
handleNext(); handleNext();
}); });

View File

@@ -4,10 +4,9 @@ import { capitalize } from '../lib/capitalize';
import { debounce } from '../lib/debounce'; import { debounce } from '../lib/debounce';
import { useOsInfo } from './useOsInfo'; import { useOsInfo } from './useOsInfo';
const HOLD_KEYS = ['Shift', 'CmdCtrl', 'Alt', 'Meta']; const HOLD_KEYS = ['Shift', 'Control', 'Command', 'Alt', 'Meta'];
export type HotkeyAction = export type HotkeyAction =
| 'popup.close'
| 'environmentEditor.toggle' | 'environmentEditor.toggle'
| 'hotkeys.showHelp' | 'hotkeys.showHelp'
| 'grpc_request.send' | 'grpc_request.send'
@@ -22,7 +21,6 @@ export type HotkeyAction =
| 'urlBar.focus'; | 'urlBar.focus';
const hotkeys: Record<HotkeyAction, string[]> = { const hotkeys: Record<HotkeyAction, string[]> = {
'popup.close': ['Escape'],
'environmentEditor.toggle': ['CmdCtrl+Shift+e'], 'environmentEditor.toggle': ['CmdCtrl+Shift+e'],
'grpc_request.send': ['CmdCtrl+Enter', 'CmdCtrl+r'], 'grpc_request.send': ['CmdCtrl+Enter', 'CmdCtrl+r'],
'hotkeys.showHelp': ['CmdCtrl+Shift+/'], 'hotkeys.showHelp': ['CmdCtrl+Shift+/'],
@@ -38,7 +36,6 @@ const hotkeys: Record<HotkeyAction, string[]> = {
}; };
const hotkeyLabels: Record<HotkeyAction, string> = { const hotkeyLabels: Record<HotkeyAction, string> = {
'popup.close': 'Close Dropdown',
'environmentEditor.toggle': 'Edit Environments', 'environmentEditor.toggle': 'Edit Environments',
'grpc_request.send': 'Send Message', 'grpc_request.send': 'Send Message',
'hotkeys.showHelp': 'Show Keyboard Shortcuts', 'hotkeys.showHelp': 'Show Keyboard Shortcuts',
@@ -83,15 +80,30 @@ export function useHotKey(
} }
const key = normalizeKey(e.key, os); const key = normalizeKey(e.key, os);
// Don't add hold keys
if (HOLD_KEYS.includes(key)) {
return;
}
currentKeys.current.add(key); currentKeys.current.add(key);
const currentKeysWithModifiers = new Set(currentKeys.current);
if (e.altKey) currentKeysWithModifiers.add(normalizeKey('Alt', os));
if (e.ctrlKey) currentKeysWithModifiers.add(normalizeKey('Control', os));
if (e.metaKey) currentKeysWithModifiers.add(normalizeKey('Meta', os));
if (e.shiftKey) currentKeysWithModifiers.add(normalizeKey('Shift', os));
for (const [hkAction, hkKeys] of Object.entries(hotkeys) as [HotkeyAction, string[]][]) { for (const [hkAction, hkKeys] of Object.entries(hotkeys) as [HotkeyAction, string[]][]) {
for (const hkKey of hkKeys) { for (const hkKey of hkKeys) {
if (hkAction !== action) {
continue;
}
const keys = hkKey.split('+'); const keys = hkKey.split('+');
if ( if (
keys.length === currentKeys.current.size && keys.length === currentKeysWithModifiers.size &&
keys.every((key) => currentKeys.current.has(key)) && keys.every((key) => currentKeysWithModifiers.has(key))
hkAction === action
) { ) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@@ -112,7 +124,7 @@ export function useHotKey(
// Clear all keys if no longer holding modifier // Clear all keys if no longer holding modifier
// HACK: This is to get around the case of DOWN SHIFT -> DOWN : -> UP SHIFT -> UP ; // HACK: This is to get around the case of DOWN SHIFT -> DOWN : -> UP SHIFT -> UP ;
// As you see, the ":" is not removed because it turned into ";" when shift was released // As you see, the ":" is not removed because it turned into ";" when shift was released
const isHoldingModifier = HOLD_KEYS.some((k) => currentKeys.current.has(k)); const isHoldingModifier = e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;
if (!isHoldingModifier) { if (!isHoldingModifier) {
currentKeys.current.clear(); currentKeys.current.clear();
} }

View File

@@ -85,6 +85,5 @@ export function useIntrospectGraphQL(baseRequest: HttpRequest) {
[introspection], [introspection],
); );
console.log('SCHEMA', introspection);
return { schema, isLoading, error, refetch }; return { schema, isLoading, error, refetch };
} }

View File

@@ -6,11 +6,11 @@ import { trackEvent } from '../lib/analytics';
import type { HttpResponse } from '../lib/models'; import type { HttpResponse } from '../lib/models';
import { getHttpRequest } from '../lib/store'; import { getHttpRequest } from '../lib/store';
import { useActiveCookieJar } from './useActiveCookieJar'; import { useActiveCookieJar } from './useActiveCookieJar';
import { useActiveEnvironmentId } from './useActiveEnvironmentId'; import { useActiveEnvironment } from './useActiveEnvironment';
import { useAlert } from './useAlert'; import { useAlert } from './useAlert';
export function useSendAnyRequest(options: { download?: boolean } = {}) { export function useSendAnyRequest(options: { download?: boolean } = {}) {
const environmentId = useActiveEnvironmentId(); const environment = useActiveEnvironment();
const alert = useAlert(); const alert = useAlert();
const { activeCookieJar } = useActiveCookieJar(); const { activeCookieJar } = useActiveCookieJar();
return useMutation<HttpResponse | null, string, string | null>({ return useMutation<HttpResponse | null, string, string | null>({
@@ -33,7 +33,7 @@ export function useSendAnyRequest(options: { download?: boolean } = {}) {
return invoke('cmd_send_http_request', { return invoke('cmd_send_http_request', {
requestId: id, requestId: id,
environmentId, environmentId: environment?.id,
downloadDir: downloadDir, downloadDir: downloadDir,
cookieJarId: activeCookieJar?.id, cookieJarId: activeCookieJar?.id,
}); });