insomnia/packages/insomnia-app/app/ui/components/request-url-bar.tsx

390 lines
11 KiB
TypeScript
Raw Normal View History

import { autoBindMethodsForReact } from 'class-autobind-decorator';
import { OpenDialogOptions } from 'electron';
import { HotKeyRegistry } from 'insomnia-common';
2021-07-22 23:04:56 +00:00
import React, { PureComponent, ReactNode } from 'react';
import { AUTOBIND_CFG, DEBOUNCE_MILLIS, isMac } from '../../common/constants';
import { hotKeyRefs } from '../../common/hotkeys';
import { executeHotKey } from '../../common/hotkeys-listener';
import type { Request } from '../../models/request';
import { Dropdown } from './base/dropdown/dropdown';
import { DropdownButton } from './base/dropdown/dropdown-button';
import { DropdownDivider } from './base/dropdown/dropdown-divider';
import { DropdownHint } from './base/dropdown/dropdown-hint';
import { DropdownItem } from './base/dropdown/dropdown-item';
import { PromptButton } from './base/prompt-button';
import { OneLineEditor } from './codemirror/one-line-editor';
import { MethodDropdown } from './dropdowns/method-dropdown';
import { KeydownBinder } from './keydown-binder';
2021-07-22 23:04:56 +00:00
import { showPrompt } from './modals/index';
interface Props {
handleAutocompleteUrls: () => Promise<string[]>;
handleGenerateCode: Function;
handleImport: Function;
handleSend: () => void;
handleSendAndDownload: (filepath?: string) => Promise<void>;
handleUpdateDownloadPath: Function;
nunjucksPowerUserMode: boolean;
onMethodChange: (r: Request, method: string) => Promise<Request>;
onUrlChange: (r: Request, url: string) => Promise<Request>;
request: Request;
uniquenessKey: string;
hotKeyRegistry: HotKeyRegistry;
downloadPath: string | null;
}
interface State {
currentInterval: number | null;
currentTimeout: number | null;
}
@autoBindMethodsForReact(AUTOBIND_CFG)
export class RequestUrlBar extends PureComponent<Props, State> {
_urlChangeDebounceTimeout: NodeJS.Timeout | null = null;
_sendTimeout: NodeJS.Timeout | null = null;
_sendInterval: NodeJS.Timeout | null = null;
_dropdown: Dropdown | null = null;
_methodDropdown: MethodDropdown | null = null;
_input: OneLineEditor | null = null;
state: State = {
currentInterval: null,
currentTimeout: null,
};
_lastPastedText?: string;
_setDropdownRef(n: Dropdown) {
this._dropdown = n;
}
_setMethodDropdownRef(n: MethodDropdown) {
this._methodDropdown = n;
}
_setInputRef(n: OneLineEditor) {
this._input = n;
}
_handleMetaClickSend(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault();
2021-07-30 03:32:08 +00:00
this._dropdown?.show();
}
_handleFormSubmit(e: React.SyntheticEvent<HTMLFormElement>) {
2018-12-16 01:37:40 +00:00
e.preventDefault();
e.stopPropagation();
this._handleSend();
}
_handleMethodChange(method: string) {
this.props.onMethodChange(this.props.request, method);
}
_handleUrlChange(url: string) {
if (this._urlChangeDebounceTimeout !== null) {
clearTimeout(this._urlChangeDebounceTimeout);
}
this._urlChangeDebounceTimeout = setTimeout(async () => {
const pastedText = this._lastPastedText;
// If no pasted text in the queue, just fire the regular change handler
if (!pastedText) {
this.props.onUrlChange(this.props.request, url);
return;
}
// Reset pasted text cache
this._lastPastedText = undefined;
// Attempt to import the pasted text
const importedRequest = await this.props.handleImport(pastedText);
// Update depending on whether something was imported
if (!importedRequest) {
this.props.onUrlChange(this.props.request, url);
}
}, DEBOUNCE_MILLIS);
}
_handleUrlPaste(e: ClipboardEvent) {
// NOTE: We're not actually doing the import here to avoid races with onChange
this._lastPastedText = e.clipboardData?.getData('text/plain');
}
2018-06-25 17:42:50 +00:00
_handleGenerateCode() {
2016-11-28 07:12:17 +00:00
this.props.handleGenerateCode();
}
2016-11-28 07:12:17 +00:00
async _handleSetDownloadLocation() {
const { request } = this.props;
const options: OpenDialogOptions = {
title: 'Select Download Location',
buttonLabel: 'Select',
properties: ['openDirectory'],
};
const { canceled, filePaths } = await window.dialog.showOpenDialog(options);
if (canceled) {
return;
}
this.props.handleUpdateDownloadPath(request._id, filePaths[0]);
}
2018-06-25 17:42:50 +00:00
_handleClearDownloadLocation() {
const { request } = this.props;
this.props.handleUpdateDownloadPath(request._id, null);
}
async _handleKeyDown(event: KeyboardEvent) {
2016-11-28 07:12:17 +00:00
if (!this._input) {
return;
}
executeHotKey(event, hotKeyRefs.REQUEST_FOCUS_URL, () => {
2021-07-30 03:32:08 +00:00
this._input?.focus();
this._input?.selectAll();
});
executeHotKey(event, hotKeyRefs.REQUEST_TOGGLE_HTTP_METHOD_MENU, () => {
2021-07-30 03:32:08 +00:00
this._methodDropdown?.toggle();
});
executeHotKey(event, hotKeyRefs.REQUEST_SHOW_OPTIONS, () => {
2021-07-30 03:32:08 +00:00
this._dropdown?.toggle(true);
});
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
_handleSend() {
2016-11-28 07:12:17 +00:00
// Don't stop interval because duh, it needs to keep going!
// XXX this._handleStopInterval(); XXX
2016-11-28 07:12:17 +00:00
this._handleStopTimeout();
const { downloadPath } = this.props;
if (downloadPath) {
this.props.handleSendAndDownload(downloadPath);
} else {
this.props.handleSend();
}
}
2016-11-28 07:12:17 +00:00
_handleClickSendAndDownload() {
this.props.handleSendAndDownload();
}
2018-06-25 17:42:50 +00:00
_handleSendAfterDelay() {
showPrompt({
2016-11-28 07:12:17 +00:00
inputType: 'decimal',
title: 'Send After Delay',
2016-11-28 07:12:17 +00:00
label: 'Delay in seconds',
// @ts-expect-error TSCONVERSION string vs number issue
2016-11-28 07:12:17 +00:00
defaultValue: 3,
submitName: 'Start',
onComplete: seconds => {
this._handleStopTimeout();
// @ts-expect-error TSCONVERSION string vs number issue
this._sendTimeout = setTimeout(this._handleSend, seconds * 1000);
this.setState({
// @ts-expect-error TSCONVERSION string vs number issue
currentTimeout: seconds,
});
},
});
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
_handleSendOnInterval() {
showPrompt({
2016-11-28 07:12:17 +00:00
inputType: 'decimal',
title: 'Send on Interval',
2016-11-28 07:12:17 +00:00
label: 'Interval in seconds',
// @ts-expect-error TSCONVERSION string vs number issue
2016-11-28 07:12:17 +00:00
defaultValue: 3,
submitName: 'Start',
onComplete: seconds => {
this._handleStopInterval();
// @ts-expect-error TSCONVERSION string vs number issue
this._sendInterval = setInterval(this._handleSend, seconds * 1000);
this.setState({
// @ts-expect-error TSCONVERSION string vs number issue
currentInterval: seconds,
});
},
});
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
_handleStopInterval() {
if (this._sendInterval) {
clearInterval(this._sendInterval);
}
2016-11-28 07:12:17 +00:00
if (this.state.currentInterval) {
this.setState({
currentInterval: null,
});
2016-11-28 07:12:17 +00:00
}
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
_handleStopTimeout() {
if (this._sendTimeout !== null) {
clearTimeout(this._sendTimeout);
}
2016-11-28 07:12:17 +00:00
if (this.state.currentTimeout) {
this.setState({
currentTimeout: null,
});
2016-11-28 07:12:17 +00:00
}
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
_handleResetTimeouts() {
this._handleStopTimeout();
this._handleStopInterval();
}
_handleClickSend(e: React.MouseEvent<HTMLButtonElement>) {
const metaPressed = isMac() ? e.metaKey : e.ctrlKey;
// If we're pressing a meta key, let the dropdown open
if (metaPressed) {
return;
}
// If we're not pressing a meta key, cancel dropdown and send the request
e.stopPropagation(); // Don't trigger the dropdown
2018-12-16 01:37:40 +00:00
this._handleSend();
}
2020-01-06 20:51:52 +00:00
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (nextProps.request._id !== this.props.request._id) {
this._handleResetTimeouts();
}
}
2018-06-25 17:42:50 +00:00
renderSendButton() {
const { hotKeyRegistry, downloadPath } = this.props;
const { currentInterval, currentTimeout } = this.state;
let cancelButton: ReactNode = null;
2016-11-28 07:12:17 +00:00
if (currentInterval) {
cancelButton = (
2018-06-25 17:42:50 +00:00
<button
type="button"
key="cancel-interval"
className="urlbar__send-btn danger"
2021-08-07 08:03:56 +00:00
onClick={this._handleStopInterval}
>
2016-11-28 07:12:17 +00:00
Stop
</button>
);
2016-11-28 07:12:17 +00:00
} else if (currentTimeout) {
cancelButton = (
2018-06-25 17:42:50 +00:00
<button
type="button"
key="cancel-timeout"
className="urlbar__send-btn danger"
2021-08-07 08:03:56 +00:00
onClick={this._handleStopTimeout}
>
2016-11-28 07:12:17 +00:00
Cancel
</button>
);
2016-11-28 07:12:17 +00:00
}
let sendButton;
2016-11-28 07:12:17 +00:00
if (!cancelButton) {
sendButton = (
2018-10-17 16:42:33 +00:00
<Dropdown key="dropdown" className="tall" right ref={this._setDropdownRef}>
2018-06-25 17:42:50 +00:00
<DropdownButton
className="urlbar__send-btn"
onContextMenu={this._handleMetaClickSend}
2021-08-07 08:03:56 +00:00
onClick={this._handleClickSend}
>
{downloadPath ? 'Download' : 'Send'}
2016-11-28 07:12:17 +00:00
</DropdownButton>
<DropdownDivider>Basic</DropdownDivider>
<DropdownItem onClick={this._handleClickSend}>
<i className="fa fa-arrow-circle-o-right" /> Send Now
<DropdownHint keyBindings={hotKeyRegistry[hotKeyRefs.REQUEST_SEND.id]} />
2016-11-28 07:12:17 +00:00
</DropdownItem>
<DropdownItem onClick={this._handleGenerateCode}>
<i className="fa fa-code" /> Generate Client Code
2016-11-28 07:12:17 +00:00
</DropdownItem>
<DropdownDivider>Advanced</DropdownDivider>
2016-11-28 07:12:17 +00:00
<DropdownItem onClick={this._handleSendAfterDelay}>
<i className="fa fa-clock-o" /> Send After Delay
2016-11-28 07:12:17 +00:00
</DropdownItem>
<DropdownItem onClick={this._handleSendOnInterval}>
<i className="fa fa-repeat" /> Repeat on Interval
2016-11-28 07:12:17 +00:00
</DropdownItem>
{downloadPath ? (
2018-06-25 17:42:50 +00:00
<DropdownItem
stayOpenAfterClick
addIcon
buttonClass={PromptButton}
2021-08-07 08:03:56 +00:00
onClick={this._handleClearDownloadLocation}
>
<i className="fa fa-stop-circle" /> Stop Auto-Download
</DropdownItem>
) : (
<DropdownItem onClick={this._handleSetDownloadLocation}>
<i className="fa fa-download" /> Download After Send
</DropdownItem>
)}
<DropdownItem onClick={this._handleClickSendAndDownload}>
<i className="fa fa-download" /> Send And Download
</DropdownItem>
2016-11-28 07:12:17 +00:00
</Dropdown>
);
2016-11-28 07:12:17 +00:00
}
2018-06-25 17:42:50 +00:00
return [cancelButton, sendButton];
}
// note: not an unused function, used by parent, RequestPane
focusInput() {
this._input?.focus(true);
}
2018-06-25 17:42:50 +00:00
render() {
const {
request,
handleAutocompleteUrls,
uniquenessKey,
} = this.props;
const { url, method } = request;
return (
<KeydownBinder onKeydown={this._handleKeyDown}>
<div className="urlbar">
2018-06-25 17:42:50 +00:00
<MethodDropdown
ref={this._setMethodDropdownRef}
onChange={this._handleMethodChange}
2021-08-07 08:03:56 +00:00
method={method}
>
{method} <i className="fa fa-caret-down" />
</MethodDropdown>
<form onSubmit={this._handleFormSubmit}>
<OneLineEditor
key={uniquenessKey}
ref={this._setInputRef}
onPaste={this._handleUrlPaste}
forceEditor
type="text"
getAutocompleteConstants={handleAutocompleteUrls}
placeholder="https://api.myproduct.com/v1/users"
defaultValue={url}
2018-06-25 17:42:50 +00:00
onChange={this._handleUrlChange}
/>
{this.renderSendButton()}
</form>
</div>
</KeydownBinder>
);
}
}