mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-22 01:19:39 +01:00
Clean up
This commit is contained in:
@@ -4,7 +4,6 @@ import { Outlet } from 'react-router-dom';
|
|||||||
import { useOsInfo } from '../hooks/useOsInfo';
|
import { useOsInfo } from '../hooks/useOsInfo';
|
||||||
import { DialogProvider, Dialogs } from './DialogContext';
|
import { DialogProvider, Dialogs } from './DialogContext';
|
||||||
import { GlobalHooks } from './GlobalHooks';
|
import { GlobalHooks } from './GlobalHooks';
|
||||||
import { RequestEditorProvider } from './RequestEditorContext';
|
|
||||||
import { ToastProvider, Toasts } from './ToastContext';
|
import { ToastProvider, Toasts } from './ToastContext';
|
||||||
|
|
||||||
export function DefaultLayout() {
|
export function DefaultLayout() {
|
||||||
@@ -12,7 +11,6 @@ export function DefaultLayout() {
|
|||||||
return (
|
return (
|
||||||
<DialogProvider>
|
<DialogProvider>
|
||||||
<ToastProvider>
|
<ToastProvider>
|
||||||
<RequestEditorProvider>
|
|
||||||
<>
|
<>
|
||||||
{/* Must be inside all the providers, so they have access to them */}
|
{/* Must be inside all the providers, so they have access to them */}
|
||||||
<Toasts />
|
<Toasts />
|
||||||
@@ -30,7 +28,6 @@ export function DefaultLayout() {
|
|||||||
<Outlet />
|
<Outlet />
|
||||||
</motion.div>
|
</motion.div>
|
||||||
<GlobalHooks />
|
<GlobalHooks />
|
||||||
</RequestEditorProvider>
|
|
||||||
</ToastProvider>
|
</ToastProvider>
|
||||||
</DialogProvider>
|
</DialogProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
import EventEmitter from 'eventemitter3';
|
|
||||||
import type { DependencyList } from 'react';
|
|
||||||
import React, { createContext, useCallback, useContext, useEffect } from 'react';
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
focusParamValue: (name: string) => void;
|
|
||||||
focusParamsTab: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const RequestEditorContext = createContext<State>({} as State);
|
|
||||||
|
|
||||||
const emitter = new EventEmitter();
|
|
||||||
|
|
||||||
export const RequestEditorProvider = ({ children }: { children: React.ReactNode }) => {
|
|
||||||
const focusParamsTab = useCallback(() => {
|
|
||||||
emitter.emit('focus_http_request_params_tab');
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const focusParamValue = useCallback(
|
|
||||||
(name: string) => {
|
|
||||||
focusParamsTab();
|
|
||||||
setTimeout(() => {
|
|
||||||
emitter.emit('focus_http_request_param_value', name);
|
|
||||||
}, 50);
|
|
||||||
},
|
|
||||||
[focusParamsTab],
|
|
||||||
);
|
|
||||||
|
|
||||||
const state: State = {
|
|
||||||
focusParamValue,
|
|
||||||
focusParamsTab,
|
|
||||||
};
|
|
||||||
|
|
||||||
return <RequestEditorContext.Provider value={state}>{children}</RequestEditorContext.Provider>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function useOnFocusParamValue(cb: (name: string) => void, deps: DependencyList) {
|
|
||||||
useEffect(() => {
|
|
||||||
emitter.on('focus_http_request_param_value', cb);
|
|
||||||
return () => {
|
|
||||||
emitter.off('focus_http_request_param_value', cb);
|
|
||||||
};
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, deps);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useOnFocusParamsTab(cb: () => void) {
|
|
||||||
useEffect(() => {
|
|
||||||
emitter.on('focus_http_request_params_tab', cb);
|
|
||||||
return () => {
|
|
||||||
emitter.off('focus_http_request_params_tab', cb);
|
|
||||||
};
|
|
||||||
// Only add callback once, to prevent the need for the caller to useCallback
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useRequestEditor = () => useContext(RequestEditorContext);
|
|
||||||
@@ -298,7 +298,7 @@ export const RequestPane = memo(function RequestPane({
|
|||||||
const { updateKey } = useRequestUpdateKey(activeRequestId ?? null);
|
const { updateKey } = useRequestUpdateKey(activeRequestId ?? null);
|
||||||
const importCurl = useImportCurl();
|
const importCurl = useImportCurl();
|
||||||
|
|
||||||
useRequestEditorEvent('focus_http_request_params_tab', () => {
|
useRequestEditorEvent('request_pane.focus_tab', () => {
|
||||||
setActiveTab(TAB_PARAMS);
|
setActiveTab(TAB_PARAMS);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export function UrlParametersEditor({ pairs, forceUpdateKey, onChange }: Props)
|
|||||||
const pairEditor = useRef<PairEditorRef>(null);
|
const pairEditor = useRef<PairEditorRef>(null);
|
||||||
|
|
||||||
useRequestEditorEvent(
|
useRequestEditorEvent(
|
||||||
'focus_http_request_param_value',
|
'request_params.focus_value',
|
||||||
(name) => {
|
(name) => {
|
||||||
const pairIndex = pairs.findIndex((p) => p.name === name);
|
const pairIndex = pairs.findIndex((p) => p.name === name);
|
||||||
if (pairIndex >= 0) {
|
if (pairIndex >= 0) {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import type { DependencyList } from 'react';
|
|||||||
import { useCallback, useEffect } from 'react';
|
import { useCallback, useEffect } from 'react';
|
||||||
|
|
||||||
type EventDataMap = {
|
type EventDataMap = {
|
||||||
focus_http_request_param_value: string;
|
'request_params.focus_value': string;
|
||||||
focus_http_request_params_tab: undefined;
|
'request_pane.focus_tab': undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useRequestEditorEvent<
|
export function useRequestEditorEvent<
|
||||||
@@ -22,13 +22,13 @@ export function useRequestEditorEvent<
|
|||||||
|
|
||||||
export function useRequestEditor() {
|
export function useRequestEditor() {
|
||||||
const focusParamsTab = useCallback(() => {
|
const focusParamsTab = useCallback(() => {
|
||||||
emitter.emit('focus_http_request_params_tab', undefined);
|
emitter.emit('request_pane.focus_tab', undefined);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const focusParamValue = useCallback(
|
const focusParamValue = useCallback(
|
||||||
(name: string) => {
|
(name: string) => {
|
||||||
focusParamsTab();
|
focusParamsTab();
|
||||||
setTimeout(() => emitter.emit('focus_http_request_param_value', name), 50);
|
setTimeout(() => emitter.emit('request_params.focus_value', name), 50);
|
||||||
},
|
},
|
||||||
[focusParamsTab],
|
[focusParamsTab],
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user