mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 01:49:13 +01:00
Remove response body and basic hotkeys
This commit is contained in:
60
src-web/hooks/useHotkey.ts
Normal file
60
src-web/hooks/useHotkey.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
export type HotkeyAction =
|
||||
| 'request.send'
|
||||
| 'request.create'
|
||||
| 'request.duplicate'
|
||||
| 'sidebar.toggle'
|
||||
| 'sidebar.focus'
|
||||
| 'url.focus';
|
||||
|
||||
const hotkeys: Record<HotkeyAction, string[]> = {
|
||||
'request.send': ['Meta+Enter', 'Meta+r'],
|
||||
'request.create': ['Meta+n'],
|
||||
'request.duplicate': ['Meta+d'],
|
||||
'sidebar.toggle': ['Meta+b'],
|
||||
'sidebar.focus': ['Meta+1'],
|
||||
'url.focus': ['Meta+l'],
|
||||
};
|
||||
|
||||
export function useHotkey(action: HotkeyAction | null, callback: (e: KeyboardEvent) => void) {
|
||||
const currentKeys = useRef<Set<string>>(new Set());
|
||||
const callbackRef = useRef(callback);
|
||||
|
||||
useEffect(() => {
|
||||
callbackRef.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
useEffect(() => {
|
||||
const down = (e: KeyboardEvent) => {
|
||||
console.log('KEY DOWN', e.key);
|
||||
currentKeys.current.add(e.key);
|
||||
for (const [hkAction, hkKeys] of Object.entries(hotkeys)) {
|
||||
for (const hkKey of hkKeys) {
|
||||
const keys = hkKey.split('+');
|
||||
if (
|
||||
keys.length === currentKeys.current.size &&
|
||||
keys.every((key) => currentKeys.current.has(key)) &&
|
||||
hkAction === action
|
||||
) {
|
||||
// Triggered hotkey!
|
||||
console.log('TRIGGER!', action);
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
callbackRef.current(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const up = (e: KeyboardEvent) => {
|
||||
currentKeys.current.delete(e.key);
|
||||
};
|
||||
window.addEventListener('keydown', down);
|
||||
window.addEventListener('keyup', up);
|
||||
return () => {
|
||||
window.removeEventListener('keydown', down);
|
||||
window.removeEventListener('keyup', up);
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [action, callback]);
|
||||
}
|
||||
@@ -13,9 +13,7 @@ export function useResponses(requestId: string | null) {
|
||||
initialData: [],
|
||||
queryKey: responsesQueryKey({ requestId: requestId ?? 'n/a' }),
|
||||
queryFn: async () => {
|
||||
return (await invoke('list_responses', {
|
||||
requestId,
|
||||
})) as HttpResponse[];
|
||||
return (await invoke('list_responses', { requestId, limit: 200 })) as HttpResponse[];
|
||||
},
|
||||
}).data ?? []
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user