PromucFlow_constructor/app/client/src/components/editorComponents/ApiResponseView.tsx

301 lines
7.9 KiB
TypeScript
Raw Normal View History

2021-04-23 13:50:55 +00:00
import React, { useState, useRef, RefObject, useCallback } from "react";
import { connect } from "react-redux";
import { withRouter, RouteComponentProps } from "react-router";
2019-11-25 05:07:27 +00:00
import { BaseText } from "components/designSystems/blueprint/TextComponent";
import styled from "styled-components";
2019-11-25 05:07:27 +00:00
import { AppState } from "reducers";
import { ActionResponse } from "api/ActionAPI";
import { formatBytes } from "utils/helpers";
2019-11-22 17:04:40 +00:00
import { APIEditorRouteParams } from "constants/routes";
2019-12-11 15:14:38 +00:00
import LoadingOverlayScreen from "components/editorComponents/LoadingOverlayScreen";
import ReadOnlyEditor from "components/editorComponents/ReadOnlyEditor";
2020-01-30 13:23:04 +00:00
import { getActionResponses } from "selectors/entitiesSelector";
import { Colors } from "constants/Colors";
2020-06-04 13:49:22 +00:00
import _ from "lodash";
2020-06-23 03:58:42 +00:00
import { useLocalStorage } from "utils/hooks/localstorage";
2021-04-23 13:50:55 +00:00
import { CHECK_REQUEST_BODY, createMessage } from "constants/messages";
import { TabComponent } from "components/ads/Tabs";
2021-04-23 13:50:55 +00:00
import Text, { TextType } from "components/ads/Text";
import Icon from "components/ads/Icon";
import { Classes, Variant } from "components/ads/common";
import { EditorTheme } from "./CodeEditor/EditorConfig";
import Callout from "components/ads/Callout";
2021-04-23 13:50:55 +00:00
import DebuggerLogs from "./Debugger/DebuggerLogs";
import ErrorLogs from "./Debugger/Errors";
import Resizer, { ResizerCSS } from "./Debugger/Resizer";
import AnalyticsUtil from "utils/AnalyticsUtil";
import { DebugButton } from "./Debugger/DebugCTA";
const ResponseContainer = styled.div`
2021-04-23 13:50:55 +00:00
${ResizerCSS}
// Initial height of bottom tabs
height: 60%;
// Minimum height of bottom tabs as it can be resized
min-height: 36px;
background-color: ${(props) => props.theme.colors.apiPane.responseBody.bg};
.react-tabs__tab-panel {
overflow: hidden;
}
`;
const ResponseMetaInfo = styled.div`
display: flex;
${BaseText} {
color: #768896;
margin-left: ${(props) => props.theme.spaces[9]}px;
}
`;
const ResponseMetaWrapper = styled.div`
align-items: center;
display: flex;
position: absolute;
right: ${(props) => props.theme.spaces[12]}px;
top: ${(props) => props.theme.spaces[4]}px;
`;
const ResponseTabWrapper = styled.div`
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
`;
const TabbedViewWrapper = styled.div<{ isCentered: boolean }>`
height: calc(100% - 30px);
&&& {
ul.react-tabs__tab-list {
padding: 0px ${(props) => props.theme.spaces[12]}px;
}
}
${(props) =>
props.isCentered
? `
&&& {
.react-tabs__tab-panel {
display: flex;
align-items: center;
justify-content: center;
}
}
`
: null}
`;
const SectionDivider = styled.div`
height: 2px;
2020-06-23 03:58:42 +00:00
width: 100%;
background: ${(props) => props.theme.colors.apiPane.dividerBg};
`;
const Flex = styled.div`
display: flex;
align-items: center;
margin-left: 20px;
span:first-child {
margin-right: ${(props) => props.theme.spaces[1] + 1}px;
2020-06-23 03:58:42 +00:00
}
`;
const NoResponseContainer = styled.div`
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
.${Classes.ICON} {
margin-right: 0px;
svg {
width: 150px;
height: 150px;
}
}
.${Classes.TEXT} {
margin-top: ${(props) => props.theme.spaces[9]}px;
}
2020-06-12 03:54:12 +00:00
`;
const FailedMessage = styled.div`
display: flex;
align-items: center;
2021-04-23 13:50:55 +00:00
margin-left: 5px;
2020-06-23 03:58:42 +00:00
`;
interface ReduxStateProps {
responses: Record<string, ActionResponse | undefined>;
isRunning: Record<string, boolean>;
}
type Props = ReduxStateProps &
2021-04-23 13:50:55 +00:00
RouteComponentProps<APIEditorRouteParams> & {
theme?: EditorTheme;
apiName: string;
};
export const EMPTY_RESPONSE: ActionResponse = {
statusCode: "",
duration: "",
body: {},
headers: {},
request: {
headers: {},
body: {},
httpMethod: "",
url: "",
},
size: "",
};
const StatusCodeText = styled(BaseText)<{ code: string }>`
color: ${(props) =>
props.code.startsWith("2") ? props.theme.colors.primaryOld : Colors.RED};
`;
function ApiResponseView(props: Props) {
2019-11-22 17:04:40 +00:00
const {
match: {
params: { apiId },
},
responses,
} = props;
let response: ActionResponse = EMPTY_RESPONSE;
2019-12-11 15:14:38 +00:00
let isRunning = false;
let hasFailed = false;
2019-11-22 17:04:40 +00:00
if (apiId && apiId in responses) {
2020-01-30 13:23:04 +00:00
response = responses[apiId] || EMPTY_RESPONSE;
isRunning = props.isRunning[apiId];
hasFailed = response.statusCode ? response.statusCode[0] !== "2" : false;
2019-11-22 17:04:40 +00:00
}
2021-04-23 13:50:55 +00:00
const panelRef: RefObject<HTMLDivElement> = useRef(null);
2020-06-23 03:58:42 +00:00
const [requestDebugVisible, setRequestDebugVisible] = useLocalStorage(
"requestDebugVisible",
"true",
);
2021-04-23 13:50:55 +00:00
const onDebugClick = useCallback(() => {
AnalyticsUtil.logEvent("OPEN_DEBUGGER", {
source: "API",
});
setSelectedIndex(1);
}, []);
const [selectedIndex, setSelectedIndex] = useState(0);
const tabs = [
{
key: "body",
title: "Response Body",
panelComponent: (
<ResponseTabWrapper>
{hasFailed && !isRunning && requestDebugVisible && (
<Callout
closeButton
fill
label={
<FailedMessage>
<DebugButton onClick={onDebugClick} />
</FailedMessage>
}
onClose={() => setRequestDebugVisible(false)}
text={createMessage(CHECK_REQUEST_BODY)}
variant={Variant.danger}
/>
)}
{_.isEmpty(response.statusCode) ? (
<NoResponseContainer>
<Icon name="no-response" />
<Text type={TextType.P1}>Hit Run to get a Response</Text>
</NoResponseContainer>
) : (
<ReadOnlyEditor
folding
height={"100%"}
input={{
value: response.body
? JSON.stringify(response.body, null, 2)
: "",
}}
/>
2020-06-23 03:58:42 +00:00
)}
</ResponseTabWrapper>
),
},
{
2021-04-23 13:50:55 +00:00
key: "error-logs",
title: "Errors",
panelComponent: <ErrorLogs />,
},
{
key: "logs",
title: "Logs",
panelComponent: <DebuggerLogs searchQuery={props.apiName} />,
},
];
return (
2021-04-23 13:50:55 +00:00
<ResponseContainer ref={panelRef}>
<Resizer panelRef={panelRef} />
<SectionDivider />
2019-12-11 15:14:38 +00:00
{isRunning && (
<LoadingOverlayScreen theme={props.theme}>
Sending Request
</LoadingOverlayScreen>
2019-12-11 15:14:38 +00:00
)}
<TabbedViewWrapper
isCentered={_.isEmpty(response.body) && selectedIndex === 0}
>
{response.statusCode && (
<ResponseMetaWrapper>
{response.statusCode && (
<Flex>
<Text type={TextType.P3}>Status: </Text>
<StatusCodeText
accent="secondary"
code={response.statusCode.toString()}
>
{response.statusCode}
</StatusCodeText>
</Flex>
)}
<ResponseMetaInfo>
{response.duration && (
<Flex>
<Text type={TextType.P3}>Time: </Text>
<Text type={TextType.H5}>{response.duration} ms</Text>
</Flex>
)}
{response.size && (
<Flex>
<Text type={TextType.P3}>Size: </Text>
<Text type={TextType.H5}>
{formatBytes(parseInt(response.size))}
</Text>
</Flex>
)}
</ResponseMetaInfo>
</ResponseMetaWrapper>
)}
<TabComponent
onSelect={setSelectedIndex}
selectedIndex={selectedIndex}
tabs={tabs}
2020-06-12 03:54:12 +00:00
/>
</TabbedViewWrapper>
</ResponseContainer>
);
}
const mapStateToProps = (state: AppState): ReduxStateProps => {
return {
responses: getActionResponses(state),
isRunning: state.ui.apiPane.isRunning,
};
};
export default connect(mapStateToProps)(withRouter(ApiResponseView));