insomnia/packages/insomnia-app/app/ui/components/wrapper-debug.tsx

352 lines
13 KiB
TypeScript
Raw Normal View History

import { autoBindMethodsForReact } from 'class-autobind-decorator';
2021-07-22 23:04:56 +00:00
import React, { Fragment, PureComponent, ReactNode } from 'react';
import { connect } from 'react-redux';
2021-07-22 23:04:56 +00:00
import { AUTOBIND_CFG, SortOrder } from '../../common/constants';
2021-07-22 23:04:56 +00:00
import { isGrpcRequest } from '../../models/grpc-request';
import { isRemoteProject } from '../../models/project';
2021-07-22 23:04:56 +00:00
import { Request, RequestAuthentication, RequestBody, RequestHeader, RequestParameter } from '../../models/request';
import { Settings } from '../../models/settings';
import { isCollection, isDesign } from '../../models/workspace';
import { RootState } from '../redux/modules';
import { selectActiveEnvironment, selectActiveRequest, selectActiveRequestResponses, selectActiveResponse, selectActiveUnitTestResult, selectActiveWorkspace, selectEnvironments, selectLoadStartTime, selectRequestVersions, selectResponseDownloadPath, selectResponseFilter, selectResponseFilterHistory, selectResponsePreviewMode, selectSettings } from '../redux/selectors';
import { selectSidebarChildren, selectSidebarFilter } from '../redux/sidebar-selectors';
import { EnvironmentsDropdown } from './dropdowns/environments-dropdown';
import { SyncDropdown } from './dropdowns/sync-dropdown';
import { ErrorBoundary } from './error-boundary';
2021-12-08 03:11:04 +00:00
import { showCookiesModal } from './modals/cookies-modal';
import { PageLayout } from './page-layout';
import { GrpcRequestPane } from './panes/grpc-request-pane';
import { GrpcResponsePane } from './panes/grpc-response-pane';
import { RequestPane } from './panes/request-pane';
import { ResponsePane } from './panes/response-pane';
import { SidebarChildren } from './sidebar/sidebar-children';
import { SidebarFilter } from './sidebar/sidebar-filter';
import { WorkspacePageHeader } from './workspace-page-header';
import type { HandleActivityChange, WrapperProps } from './wrapper';
2020-04-26 20:33:39 +00:00
interface Props extends ReturnType<typeof mapStateToProps> {
forceRefreshKey: number;
gitSyncDropdown: ReactNode;
handleActivityChange: HandleActivityChange;
handleChangeEnvironment: Function;
handleDeleteResponse: Function;
handleDeleteResponses: Function;
handleForceUpdateRequest: (r: Request, patch: Partial<Request>) => Promise<Request>;
handleForceUpdateRequestHeaders: (r: Request, headers: RequestHeader[]) => Promise<Request>;
handleImport: Function;
handleRequestCreate: () => void;
handleRequestGroupCreate: () => void;
handleSendAndDownloadRequestWithActiveEnvironment: (filepath?: string) => Promise<void>;
handleSendRequestWithActiveEnvironment: () => void;
handleSetActiveResponse: Function;
handleSetPreviewMode: Function;
handleSetResponseFilter: (filter: string) => void;
handleShowRequestSettingsModal: Function;
handleSidebarSort: (sortOrder: SortOrder) => void;
handleUpdateRequestAuthentication: (r: Request, auth: RequestAuthentication) => Promise<Request>;
handleUpdateRequestBody: (r: Request, body: RequestBody) => Promise<Request>;
handleUpdateRequestHeaders: (r: Request, headers: RequestHeader[]) => Promise<Request>;
handleUpdateRequestMethod: (r: Request, method: string) => Promise<Request>;
handleUpdateRequestParameters: (r: Request, params: RequestParameter[]) => Promise<Request>;
handleUpdateRequestUrl: (r: Request, url: string) => Promise<Request>;
handleUpdateSettingsUseBulkHeaderEditor: Function;
handleUpdateSettingsUseBulkParametersEditor: (useBulkParametersEditor: boolean) => Promise<Settings>;
wrapperProps: WrapperProps;
}
2020-04-26 20:33:39 +00:00
@autoBindMethodsForReact(AUTOBIND_CFG)
class UnconnectedWrapperDebug extends PureComponent<Props> {
2020-04-26 20:33:39 +00:00
_renderPageHeader() {
const { gitSyncDropdown, handleActivityChange, wrapperProps: {
vcs,
activeWorkspace,
activeWorkspaceMeta,
activeProject,
syncItems,
isLoggedIn,
} } = this.props;
if (!activeWorkspace) {
return null;
}
const collection = isCollection(activeWorkspace);
let insomniaSync: ReactNode = null;
if (isLoggedIn && collection && isRemoteProject(activeProject) && vcs) {
insomniaSync = <SyncDropdown
workspace={activeWorkspace}
workspaceMeta={activeWorkspaceMeta}
project={activeProject}
vcs={vcs}
2021-08-07 08:03:56 +00:00
syncItems={syncItems}
/>;
}
const gitSync = isDesign(activeWorkspace) && gitSyncDropdown;
const sync = insomniaSync || gitSync;
2020-04-26 20:33:39 +00:00
return (
<WorkspacePageHeader
handleActivityChange={handleActivityChange}
2021-07-26 10:05:45 +00:00
gridRight={sync}
2020-04-26 20:33:39 +00:00
/>
);
}
_renderPageSidebar() {
2020-04-26 20:33:39 +00:00
const {
activeEnvironment,
activeWorkspace,
environments,
2020-04-26 20:33:39 +00:00
handleChangeEnvironment,
handleRequestCreate,
handleRequestGroupCreate,
handleSidebarSort,
settings,
sidebarChildren,
sidebarFilter,
} = this.props;
const {
handleActivateRequest,
handleCopyAsCurl,
handleCreateRequest,
handleCreateRequestGroup,
handleDuplicateRequest,
handleDuplicateRequestGroup,
handleGenerateCode,
handleSetRequestGroupCollapsed,
handleSetRequestPinned,
handleSetSidebarFilter,
} = this.props.wrapperProps;
if (!activeWorkspace) {
return null;
}
return (
<Fragment>
<div className="sidebar__menu">
<EnvironmentsDropdown
handleChangeEnvironment={handleChangeEnvironment}
activeEnvironment={activeEnvironment}
environments={environments}
workspace={activeWorkspace}
environmentHighlightColorStyle={settings.environmentHighlightColorStyle}
hotKeyRegistry={settings.hotKeyRegistry}
/>
2021-12-08 03:11:04 +00:00
<button className="btn btn--super-compact" onClick={showCookiesModal}>
<div className="sidebar__menu__thing">
<span>Cookies</span>
</div>
</button>
</div>
<SidebarFilter
key={`${activeWorkspace._id}::filter`}
onChange={handleSetSidebarFilter}
requestCreate={handleRequestCreate}
requestGroupCreate={handleRequestGroupCreate}
sidebarSort={handleSidebarSort}
filter={sidebarFilter || ''}
hotKeyRegistry={settings.hotKeyRegistry}
/>
<SidebarChildren
childObjects={sidebarChildren}
handleActivateRequest={handleActivateRequest}
handleCreateRequest={handleCreateRequest}
handleCreateRequestGroup={handleCreateRequestGroup}
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
handleSetRequestPinned={handleSetRequestPinned}
handleDuplicateRequest={handleDuplicateRequest}
handleDuplicateRequestGroup={handleDuplicateRequestGroup}
handleGenerateCode={handleGenerateCode}
handleCopyAsCurl={handleCopyAsCurl}
filter={sidebarFilter || ''}
hotKeyRegistry={settings.hotKeyRegistry}
/>
</Fragment>
);
}
_renderRequestPane() {
const {
activeEnvironment,
activeRequest,
activeWorkspace,
forceRefreshKey,
2020-04-26 20:33:39 +00:00
handleForceUpdateRequest,
handleForceUpdateRequestHeaders,
handleImport,
handleSendAndDownloadRequestWithActiveEnvironment,
handleSendRequestWithActiveEnvironment,
handleUpdateRequestAuthentication,
handleUpdateRequestBody,
handleUpdateRequestHeaders,
handleUpdateRequestMethod,
handleUpdateRequestParameters,
handleUpdateRequestUrl,
handleUpdateSettingsUseBulkHeaderEditor,
2020-08-03 10:13:08 +00:00
handleUpdateSettingsUseBulkParametersEditor,
responseDownloadPath,
settings,
2020-04-26 20:33:39 +00:00
} = this.props;
const {
handleCreateRequestForWorkspace,
handleGenerateCodeForActiveRequest,
handleUpdateDownloadPath,
handleUpdateRequestMimeType,
headerEditorKey,
} = this.props.wrapperProps;
if (!activeWorkspace) {
return null;
}
// activeRequest being truthy only needs to be checked for isGrpcRequest (for now)
// The RequestPane and ResponsePane components already handle the case where activeRequest is null
if (activeRequest && isGrpcRequest(activeRequest)) {
return (
<ErrorBoundary showAlert>
<GrpcRequestPane
activeRequest={activeRequest}
environmentId={activeEnvironment ? activeEnvironment._id : ''}
workspaceId={activeWorkspace._id}
forceRefreshKey={forceRefreshKey}
settings={settings}
/>
</ErrorBoundary>
);
}
2020-04-26 20:33:39 +00:00
return (
<ErrorBoundary showAlert>
<RequestPane
downloadPath={responseDownloadPath}
environmentId={activeEnvironment ? activeEnvironment._id : ''}
forceRefreshCounter={forceRefreshKey}
forceUpdateRequest={handleForceUpdateRequest}
forceUpdateRequestHeaders={handleForceUpdateRequestHeaders}
handleCreateRequest={handleCreateRequestForWorkspace}
handleGenerateCode={handleGenerateCodeForActiveRequest}
handleImport={handleImport}
handleSend={handleSendRequestWithActiveEnvironment}
handleSendAndDownload={handleSendAndDownloadRequestWithActiveEnvironment}
handleUpdateDownloadPath={handleUpdateDownloadPath}
headerEditorKey={headerEditorKey}
request={activeRequest}
settings={settings}
updateRequestAuthentication={handleUpdateRequestAuthentication}
updateRequestBody={handleUpdateRequestBody}
updateRequestHeaders={handleUpdateRequestHeaders}
updateRequestMethod={handleUpdateRequestMethod}
updateRequestMimeType={handleUpdateRequestMimeType}
updateRequestParameters={handleUpdateRequestParameters}
updateRequestUrl={handleUpdateRequestUrl}
updateSettingsUseBulkHeaderEditor={handleUpdateSettingsUseBulkHeaderEditor}
updateSettingsUseBulkParametersEditor={handleUpdateSettingsUseBulkParametersEditor}
workspace={activeWorkspace}
/>
</ErrorBoundary>
);
}
2020-04-26 20:33:39 +00:00
_renderResponsePane() {
const {
forceRefreshKey,
handleDeleteResponse,
handleDeleteResponses,
handleSetActiveResponse,
handleSetPreviewMode,
handleSetResponseFilter,
handleShowRequestSettingsModal,
activeEnvironment,
activeRequest,
activeRequestResponses,
activeResponse,
activeUnitTestResult,
loadStartTime,
requestVersions,
responseFilter,
responseFilterHistory,
responsePreviewMode,
settings,
} = this.props;
// activeRequest being truthy only needs to be checked for isGrpcRequest (for now)
// The RequestPane and ResponsePane components already handle the case where activeRequest is null
if (activeRequest && isGrpcRequest(activeRequest)) {
return (
<ErrorBoundary showAlert>
<GrpcResponsePane
activeRequest={activeRequest}
forceRefreshKey={forceRefreshKey}
/>
</ErrorBoundary>
);
}
return (
<ErrorBoundary showAlert>
<ResponsePane
disableHtmlPreviewJs={settings.disableHtmlPreviewJs}
disableResponsePreviewLinks={settings.disableResponsePreviewLinks}
editorFontSize={settings.editorFontSize}
environment={activeEnvironment}
filter={responseFilter}
filterHistory={responseFilterHistory}
handleDeleteResponse={handleDeleteResponse}
handleDeleteResponses={handleDeleteResponses}
handleSetActiveResponse={handleSetActiveResponse}
handleSetFilter={handleSetResponseFilter}
handleSetPreviewMode={handleSetPreviewMode}
handleShowRequestSettings={handleShowRequestSettingsModal}
hotKeyRegistry={settings.hotKeyRegistry}
loadStartTime={loadStartTime}
previewMode={responsePreviewMode}
request={activeRequest}
requestVersions={requestVersions}
response={activeResponse}
responses={activeRequestResponses}
unitTestResult={activeUnitTestResult}
/>
</ErrorBoundary>
);
}
2020-04-26 20:33:39 +00:00
render() {
return (
<PageLayout
wrapperProps={this.props.wrapperProps}
renderPageHeader={this._renderPageHeader}
renderPageSidebar={this._renderPageSidebar}
renderPaneOne={this._renderRequestPane}
renderPaneTwo={this._renderResponsePane}
2020-04-26 20:33:39 +00:00
/>
);
}
}
const mapStateToProps = (state: RootState) => ({
activeEnvironment: selectActiveEnvironment(state),
activeRequest: selectActiveRequest(state),
activeRequestResponses: selectActiveRequestResponses(state),
activeResponse: selectActiveResponse(state),
activeUnitTestResult: selectActiveUnitTestResult(state),
activeWorkspace: selectActiveWorkspace(state),
environments: selectEnvironments(state),
loadStartTime: selectLoadStartTime(state),
requestVersions: selectRequestVersions(state),
responseDownloadPath: selectResponseDownloadPath(state),
responseFilter: selectResponseFilter(state),
responseFilterHistory: selectResponseFilterHistory(state),
responsePreviewMode: selectResponsePreviewMode(state),
settings: selectSettings(state),
sidebarChildren: selectSidebarChildren(state),
sidebarFilter: selectSidebarFilter(state),
});
export const WrapperDebug = connect(mapStateToProps)(UnconnectedWrapperDebug);