mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 01:28:35 +02:00
Env dialog hotkey
This commit is contained in:
@@ -16,6 +16,7 @@ interface State {
|
|||||||
|
|
||||||
interface Actions {
|
interface Actions {
|
||||||
show: (d: DialogEntryOptionalId) => void;
|
show: (d: DialogEntryOptionalId) => void;
|
||||||
|
toggle: (d: DialogEntry) => void;
|
||||||
hide: (id: string) => void;
|
hide: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,15 +27,20 @@ export const DialogProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
const [dialogs, setDialogs] = useState<State['dialogs']>([]);
|
const [dialogs, setDialogs] = useState<State['dialogs']>([]);
|
||||||
const actions = useMemo<Actions>(
|
const actions = useMemo<Actions>(
|
||||||
() => ({
|
() => ({
|
||||||
show: ({ id: oid, ...props }: DialogEntryOptionalId) => {
|
show({ id: oid, ...props }: DialogEntryOptionalId) {
|
||||||
const id = oid ?? Math.random().toString(36).slice(2);
|
const id = oid ?? Math.random().toString(36).slice(2);
|
||||||
setDialogs((a) => [...a.filter((d) => d.id !== id), { id, ...props }]);
|
setDialogs((a) => [...a.filter((d) => d.id !== id), { id, ...props }]);
|
||||||
},
|
},
|
||||||
|
toggle({ id: oid, ...props }: DialogEntryOptionalId) {
|
||||||
|
const id = oid ?? Math.random().toString(36).slice(2);
|
||||||
|
if (dialogs.some((d) => d.id === id)) this.hide(id);
|
||||||
|
else this.show({ id, ...props });
|
||||||
|
},
|
||||||
hide: (id: string) => {
|
hide: (id: string) => {
|
||||||
setDialogs((a) => a.filter((d) => d.id !== id));
|
setDialogs((a) => a.filter((d) => d.id !== id));
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
[],
|
[dialogs],
|
||||||
);
|
);
|
||||||
|
|
||||||
const state: State = {
|
const state: State = {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { memo, useCallback, useMemo } from 'react';
|
|||||||
import { useActiveEnvironment } from '../hooks/useActiveEnvironment';
|
import { useActiveEnvironment } from '../hooks/useActiveEnvironment';
|
||||||
import { useAppRoutes } from '../hooks/useAppRoutes';
|
import { useAppRoutes } from '../hooks/useAppRoutes';
|
||||||
import { useEnvironments } from '../hooks/useEnvironments';
|
import { useEnvironments } from '../hooks/useEnvironments';
|
||||||
|
import { useHotkey } from '../hooks/useHotkey';
|
||||||
import type { ButtonProps } from './core/Button';
|
import type { ButtonProps } from './core/Button';
|
||||||
import { Button } from './core/Button';
|
import { Button } from './core/Button';
|
||||||
import type { DropdownItem } from './core/Dropdown';
|
import type { DropdownItem } from './core/Dropdown';
|
||||||
@@ -25,12 +26,15 @@ export const EnvironmentActionsDropdown = memo(function EnvironmentActionsDropdo
|
|||||||
const routes = useAppRoutes();
|
const routes = useAppRoutes();
|
||||||
|
|
||||||
const showEnvironmentDialog = useCallback(() => {
|
const showEnvironmentDialog = useCallback(() => {
|
||||||
dialog.show({
|
dialog.toggle({
|
||||||
|
id: 'environment-editor',
|
||||||
title: 'Manage Environments',
|
title: 'Manage Environments',
|
||||||
render: () => <EnvironmentEditDialog initialEnvironment={activeEnvironment} />,
|
render: () => <EnvironmentEditDialog initialEnvironment={activeEnvironment} />,
|
||||||
});
|
});
|
||||||
}, [dialog, activeEnvironment]);
|
}, [dialog, activeEnvironment]);
|
||||||
|
|
||||||
|
useHotkey('environmentEditor.show', showEnvironmentDialog);
|
||||||
|
|
||||||
const items: DropdownItem[] = useMemo(
|
const items: DropdownItem[] = useMemo(
|
||||||
() => [
|
() => [
|
||||||
...environments.map(
|
...environments.map(
|
||||||
|
|||||||
@@ -40,7 +40,13 @@ export const UrlBar = memo(function UrlBar({ id: requestId, url, method, classNa
|
|||||||
[sendRequest],
|
[sendRequest],
|
||||||
);
|
);
|
||||||
|
|
||||||
useHotkey('url.focus', () => inputRef.current?.focus());
|
useHotkey('urlBar.focus', () => {
|
||||||
|
const head = inputRef.current?.state.doc.length ?? 0;
|
||||||
|
inputRef.current?.dispatch({
|
||||||
|
selection: { anchor: 0, head },
|
||||||
|
});
|
||||||
|
inputRef.current?.focus();
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={handleSubmit} className={classNames('url-bar', className)}>
|
<form onSubmit={handleSubmit} className={classNames('url-bar', className)}>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { convertFileSrc } from '@tauri-apps/api/tauri';
|
import { convertFileSrc } from '@tauri-apps/api/tauri';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import { useState } from 'react';
|
||||||
import type { HttpResponse } from '../../lib/models';
|
import type { HttpResponse } from '../../lib/models';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -8,11 +9,31 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ImageViewer({ response, className }: Props) {
|
export function ImageViewer({ response, className }: Props) {
|
||||||
|
const bytes = response.contentLength ?? 0;
|
||||||
|
const [show, setShow] = useState(bytes < 3 * 1000 * 1000);
|
||||||
|
|
||||||
if (response.bodyPath === null) {
|
if (response.bodyPath === null) {
|
||||||
return <div>Empty response body</div>;
|
return <div>Empty response body</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const src = convertFileSrc(response.bodyPath);
|
const src = convertFileSrc(response.bodyPath);
|
||||||
|
if (!show) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="text-sm italic text-gray-500">
|
||||||
|
Response body is too large to preview.{' '}
|
||||||
|
<button
|
||||||
|
className="cursor-pointer underline hover:text-gray-800"
|
||||||
|
color="gray"
|
||||||
|
onClick={() => setShow(true)}
|
||||||
|
>
|
||||||
|
Show anyway
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<img
|
<img
|
||||||
src={src}
|
src={src}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ export type HotkeyAction =
|
|||||||
| 'request.duplicate'
|
| 'request.duplicate'
|
||||||
| 'sidebar.toggle'
|
| 'sidebar.toggle'
|
||||||
| 'sidebar.focus'
|
| 'sidebar.focus'
|
||||||
| 'url.focus';
|
| 'urlBar.focus'
|
||||||
|
| 'environmentEditor.show';
|
||||||
|
|
||||||
const hotkeys: Record<HotkeyAction, string[]> = {
|
const hotkeys: Record<HotkeyAction, string[]> = {
|
||||||
'request.send': ['Meta+Enter', 'Meta+r'],
|
'request.send': ['Meta+Enter', 'Meta+r'],
|
||||||
@@ -14,7 +15,8 @@ const hotkeys: Record<HotkeyAction, string[]> = {
|
|||||||
'request.duplicate': ['Meta+d'],
|
'request.duplicate': ['Meta+d'],
|
||||||
'sidebar.toggle': ['Meta+b'],
|
'sidebar.toggle': ['Meta+b'],
|
||||||
'sidebar.focus': ['Meta+1'],
|
'sidebar.focus': ['Meta+1'],
|
||||||
'url.focus': ['Meta+l'],
|
'urlBar.focus': ['Meta+l'],
|
||||||
|
'environmentEditor.show': ['Meta+e'],
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useHotkey(action: HotkeyAction | null, callback: (e: KeyboardEvent) => void) {
|
export function useHotkey(action: HotkeyAction | null, callback: (e: KeyboardEvent) => void) {
|
||||||
@@ -27,7 +29,6 @@ export function useHotkey(action: HotkeyAction | null, callback: (e: KeyboardEve
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const down = (e: KeyboardEvent) => {
|
const down = (e: KeyboardEvent) => {
|
||||||
console.log('KEY DOWN', e.key);
|
|
||||||
currentKeys.current.add(e.key);
|
currentKeys.current.add(e.key);
|
||||||
for (const [hkAction, hkKeys] of Object.entries(hotkeys)) {
|
for (const [hkAction, hkKeys] of Object.entries(hotkeys)) {
|
||||||
for (const hkKey of hkKeys) {
|
for (const hkKey of hkKeys) {
|
||||||
|
|||||||
Reference in New Issue
Block a user