Skip to content

Commit

Permalink
use event listener on gridlist
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav authored and gatzjames committed Nov 14, 2023
1 parent c64c9aa commit 7914555
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
24 changes: 18 additions & 6 deletions packages/insomnia/src/ui/components/request-url-bar.tsx
Expand Up @@ -88,24 +88,24 @@ export const RequestUrlBar = forwardRef<RequestUrlBarHandle, Props>(({
}
}, [fetcher.state, setLoading]);
const { organizationId, projectId, workspaceId, requestId } = useParams() as { organizationId: string; projectId: string; workspaceId: string; requestId: string };
const connect = (connectParams: ConnectActionParams) => {
const connect = useCallback((connectParams: ConnectActionParams) => {
fetcher.submit(JSON.stringify(connectParams),
{
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspaceId}/debug/request/${requestId}/connect`,
method: 'post',
encType: 'application/json',
});
};
const send = (sendParams: SendActionParams) => {
}, [fetcher, organizationId, projectId, requestId, workspaceId]);
const send = useCallback((sendParams: SendActionParams) => {
fetcher.submit(JSON.stringify(sendParams),
{
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspaceId}/debug/request/${requestId}/send`,
method: 'post',
encType: 'application/json',
});
};
}, [fetcher, organizationId, projectId, requestId, workspaceId]);

const sendOrConnect = async (shouldPromptForPathAfterResponse?: boolean) => {
const sendOrConnect = useCallback(async (shouldPromptForPathAfterResponse?: boolean) => {
models.stats.incrementExecutedRequests();
window.main.trackSegmentEvent({
event: SegmentEvent.requestExecute,
Expand Down Expand Up @@ -170,7 +170,19 @@ export const RequestUrlBar = forwardRef<RequestUrlBarHandle, Props>(({
),
});
}
};
}, [activeEnvironment._id, activeRequest, activeWorkspace._id, connect, requestId, send, settings.preferredHttpVersion]);

useEffect(() => {
const sendOnMetaEnter = (event: KeyboardEvent) => {
if (event.metaKey && event.key === 'Enter') {
sendOrConnect();
}
};
document.getElementById('sidebar-request-gridlist')?.addEventListener('keydown', sendOnMetaEnter, { capture: true });
return () => {
document.getElementById('sidebar-request-gridlist')?.removeEventListener('keydown', sendOnMetaEnter, { capture: true });
};
}, [sendOrConnect]);

useInterval(sendOrConnect, currentInterval ? currentInterval : null);
useTimeoutWhen(sendOrConnect, currentTimeout, !!currentTimeout);
Expand Down
23 changes: 18 additions & 5 deletions packages/insomnia/src/ui/components/websockets/action-bar.tsx
@@ -1,4 +1,4 @@
import React, { FC, useLayoutEffect, useRef } from 'react';
import React, { FC, useCallback, useEffect, useLayoutEffect, useRef } from 'react';
import { useFetcher, useParams } from 'react-router-dom';
import styled from 'styled-components';

Expand Down Expand Up @@ -74,16 +74,17 @@ export const WebSocketActionBar: FC<ActionBarProps> = ({ request, environmentId,

const fetcher = useFetcher();
const { organizationId, projectId, workspaceId, requestId } = useParams() as { organizationId: string; projectId: string; workspaceId: string; requestId: string };
const connect = (connectParams: ConnectActionParams) => {

const connect = useCallback((connectParams: ConnectActionParams) => {
fetcher.submit(JSON.stringify(connectParams),
{
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspaceId}/debug/request/${requestId}/connect`,
method: 'post',
encType: 'application/json',
});
};
}, [fetcher, organizationId, projectId, requestId, workspaceId]);

const handleSubmit = async () => {
const handleSubmit = useCallback(async () => {
if (isOpen) {
window.main.webSocket.close({ requestId: request._id });
return;
Expand All @@ -109,7 +110,19 @@ export const WebSocketActionBar: FC<ActionBarProps> = ({ request, environmentId,
suppressUserAgent: rendered.suppressUserAgent,
});

};
}, [connect, environmentId, isOpen, request, workspaceId]);

useEffect(() => {
const sendOnMetaEnter = (event: KeyboardEvent) => {
if (event.metaKey && event.key === 'Enter') {
handleSubmit();
}
};
document.getElementById('sidebar-request-gridlist')?.addEventListener('keydown', sendOnMetaEnter, { capture: true });
return () => {
document.getElementById('sidebar-request-gridlist')?.removeEventListener('keydown', sendOnMetaEnter, { capture: true });
};
}, [handleSubmit]);

useDocBodyKeyboardShortcuts({
request_send: () => handleSubmit(),
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/ui/routes/debug.tsx
Expand Up @@ -978,6 +978,7 @@ export const Debug: FC = () => {

<div className='flex-1 overflow-y-auto' ref={parentRef} >
<GridList
id="sidebar-request-gridlist"
style={{ height: virtualizer.getTotalSize() }}
items={virtualizer.getVirtualItems()}
className="relative"
Expand Down

0 comments on commit 7914555

Please sign in to comment.