2022-04-28 16:51:02 +00:00
|
|
|
import React, {
|
|
|
|
|
useEffect,
|
|
|
|
|
useRef,
|
|
|
|
|
RefObject,
|
|
|
|
|
useCallback,
|
|
|
|
|
useState,
|
|
|
|
|
} from "react";
|
2022-05-19 13:13:51 +00:00
|
|
|
import { connect, useDispatch, useSelector } from "react-redux";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { withRouter, RouteComponentProps } from "react-router";
|
|
|
|
|
import styled from "styled-components";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { JSEditorRouteParams } from "constants/routes";
|
|
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
DEBUGGER_ERRORS,
|
|
|
|
|
DEBUGGER_LOGS,
|
|
|
|
|
EXECUTING_FUNCTION,
|
2021-09-21 06:02:45 +00:00
|
|
|
PARSING_ERROR,
|
2021-11-08 06:49:22 +00:00
|
|
|
EMPTY_RESPONSE_FIRST_HALF,
|
2022-04-28 16:51:02 +00:00
|
|
|
EMPTY_JS_RESPONSE_LAST_HALF,
|
|
|
|
|
NO_JS_FUNCTION_RETURN_VALUE,
|
2022-05-11 10:20:33 +00:00
|
|
|
JS_ACTION_EXECUTION_ERROR,
|
2022-05-19 13:13:51 +00:00
|
|
|
UPDATING_JS_COLLECTION,
|
2022-02-11 18:08:46 +00:00
|
|
|
} from "@appsmith/constants/messages";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { EditorTheme } from "./CodeEditor/EditorConfig";
|
|
|
|
|
import DebuggerLogs from "./Debugger/DebuggerLogs";
|
|
|
|
|
import ErrorLogs from "./Debugger/Errors";
|
|
|
|
|
import Resizer, { ResizerCSS } from "./Debugger/Resizer";
|
|
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
|
|
|
import { JSCollection, JSAction } from "entities/JSCollection";
|
|
|
|
|
import ReadOnlyEditor from "components/editorComponents/ReadOnlyEditor";
|
2022-10-13 12:38:32 +00:00
|
|
|
import { Button, Callout, Icon, Size, Text, TextType } from "design-system";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { Classes } from "components/ads/common";
|
|
|
|
|
import LoadingOverlayScreen from "components/editorComponents/LoadingOverlayScreen";
|
|
|
|
|
import { JSCollectionData } from "reducers/entityReducers/jsActionsReducer";
|
2021-09-21 06:02:45 +00:00
|
|
|
import { Variant } from "components/ads/common";
|
|
|
|
|
import { EvaluationError } from "utils/DynamicBindingUtils";
|
2021-09-28 07:31:46 +00:00
|
|
|
import { DebugButton } from "./Debugger/DebugCTA";
|
2021-10-07 06:53:58 +00:00
|
|
|
import { DEBUGGER_TAB_KEYS } from "./Debugger/helpers";
|
|
|
|
|
import EntityBottomTabs from "./EntityBottomTabs";
|
2022-10-13 20:13:44 +00:00
|
|
|
import { TAB_MIN_HEIGHT } from "design-system";
|
2022-04-28 16:51:02 +00:00
|
|
|
import { CodeEditorWithGutterStyles } from "pages/Editor/JSEditor/constants";
|
2022-05-19 13:13:51 +00:00
|
|
|
import { getIsSavingEntity } from "selectors/editorSelectors";
|
|
|
|
|
import { getJSResponseViewState } from "./utils";
|
2022-10-17 15:16:38 +00:00
|
|
|
import {
|
|
|
|
|
getJSPaneResponsePaneHeight,
|
|
|
|
|
getJSPaneResponseSelectedTab,
|
|
|
|
|
} from "selectors/jsPaneSelectors";
|
|
|
|
|
import {
|
|
|
|
|
setJsPaneResponsePaneHeight,
|
|
|
|
|
setJsPaneResponseSelectedTab,
|
|
|
|
|
} from "actions/jsPaneActions";
|
2022-10-20 12:08:48 +00:00
|
|
|
import { ActionExecutionResizerHeight } from "pages/Editor/APIEditor/constants";
|
2021-09-08 17:32:22 +00:00
|
|
|
|
|
|
|
|
const ResponseContainer = styled.div`
|
|
|
|
|
${ResizerCSS}
|
|
|
|
|
width: 100%;
|
|
|
|
|
// Minimum height of bottom tabs as it can be resized
|
2022-04-28 16:51:02 +00:00
|
|
|
min-height: ${TAB_MIN_HEIGHT};
|
2021-09-08 17:32:22 +00:00
|
|
|
background-color: ${(props) => props.theme.colors.apiPane.responseBody.bg};
|
2022-10-20 12:08:48 +00:00
|
|
|
height: ${ActionExecutionResizerHeight}px;
|
2021-09-08 17:32:22 +00:00
|
|
|
|
|
|
|
|
.react-tabs__tab-panel {
|
2022-04-28 16:51:02 +00:00
|
|
|
${CodeEditorWithGutterStyles}
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
height: calc(100% - ${TAB_MIN_HEIGHT});
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ResponseTabWrapper = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
height: 100%;
|
|
|
|
|
width: 100%;
|
2021-09-21 06:02:45 +00:00
|
|
|
&.disable * {
|
|
|
|
|
opacity: 0.8;
|
|
|
|
|
pointer-events: none;
|
|
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
.response-run {
|
|
|
|
|
margin: 0 10px;
|
|
|
|
|
}
|
2021-09-08 17:32:22 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const TabbedViewWrapper = styled.div`
|
2021-10-04 11:08:02 +00:00
|
|
|
height: 100%;
|
2021-09-08 17:32:22 +00:00
|
|
|
|
|
|
|
|
&&& {
|
|
|
|
|
ul.react-tabs__tab-list {
|
2022-05-19 12:21:51 +00:00
|
|
|
padding: 0px ${(props) => props.theme.spaces[11]}px;
|
2022-04-28 16:51:02 +00:00
|
|
|
height: ${TAB_MIN_HEIGHT};
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ResponseViewer = styled.div`
|
2022-04-28 16:51:02 +00:00
|
|
|
width: 100%;
|
2021-09-08 17:32:22 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const NoResponseContainer = styled.div`
|
|
|
|
|
height: 100%;
|
2022-04-28 16:51:02 +00:00
|
|
|
width: max-content;
|
2021-09-08 17:32:22 +00:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
flex-direction: column;
|
2022-04-28 16:51:02 +00:00
|
|
|
margin: 0 auto;
|
2021-09-08 17:32:22 +00:00
|
|
|
&.empty {
|
|
|
|
|
background-color: #fafafa;
|
|
|
|
|
}
|
|
|
|
|
.${Classes.ICON} {
|
|
|
|
|
margin-right: 0px;
|
|
|
|
|
svg {
|
2022-04-28 16:51:02 +00:00
|
|
|
width: auto;
|
2021-09-08 17:32:22 +00:00
|
|
|
height: 150px;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
margin-top: ${(props) => props.theme.spaces[9]}px;
|
|
|
|
|
color: #090707;
|
|
|
|
|
}
|
|
|
|
|
`;
|
2021-09-21 06:02:45 +00:00
|
|
|
const HelpSection = styled.div`
|
|
|
|
|
padding-bottom: 5px;
|
|
|
|
|
padding-top: 10px;
|
|
|
|
|
`;
|
2021-09-08 17:32:22 +00:00
|
|
|
|
2021-09-28 07:31:46 +00:00
|
|
|
const FailedMessage = styled.div`
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-left: 5px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const StyledCallout = styled(Callout)`
|
|
|
|
|
.${Classes.TEXT} {
|
|
|
|
|
line-height: normal;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2022-04-28 16:51:02 +00:00
|
|
|
const NoReturnValueWrapper = styled.div`
|
|
|
|
|
padding-left: ${(props) => props.theme.spaces[12]}px;
|
|
|
|
|
padding-top: ${(props) => props.theme.spaces[6]}px;
|
|
|
|
|
`;
|
|
|
|
|
const InlineButton = styled(Button)`
|
|
|
|
|
display: inline-flex;
|
|
|
|
|
margin: 0 4px;
|
|
|
|
|
`;
|
|
|
|
|
|
2022-05-19 13:13:51 +00:00
|
|
|
export enum JSResponseState {
|
2022-04-28 16:51:02 +00:00
|
|
|
IsExecuting = "IsExecuting",
|
|
|
|
|
IsDirty = "IsDirty",
|
2022-05-19 13:13:51 +00:00
|
|
|
IsUpdating = "IsUpdating",
|
2022-04-28 16:51:02 +00:00
|
|
|
NoResponse = "NoResponse",
|
|
|
|
|
ShowResponse = "ShowResponse",
|
|
|
|
|
NoReturnValue = "NoReturnValue",
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 17:32:22 +00:00
|
|
|
interface ReduxStateProps {
|
|
|
|
|
responses: Record<string, any>;
|
|
|
|
|
isExecuting: Record<string, boolean>;
|
2022-05-19 13:13:51 +00:00
|
|
|
isDirty: Record<string, boolean>;
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Props = ReduxStateProps &
|
|
|
|
|
RouteComponentProps<JSEditorRouteParams> & {
|
2022-04-28 16:51:02 +00:00
|
|
|
currentFunction: JSAction | null;
|
2021-09-08 17:32:22 +00:00
|
|
|
theme?: EditorTheme;
|
|
|
|
|
jsObject: JSCollection;
|
2021-09-21 06:02:45 +00:00
|
|
|
errors: Array<EvaluationError>;
|
2022-04-28 16:51:02 +00:00
|
|
|
disabled: boolean;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
onButtonClick: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
2021-09-08 17:32:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function JSResponseView(props: Props) {
|
2022-04-28 16:51:02 +00:00
|
|
|
const {
|
|
|
|
|
currentFunction,
|
|
|
|
|
disabled,
|
|
|
|
|
errors,
|
2022-05-19 13:13:51 +00:00
|
|
|
isDirty,
|
2022-04-28 16:51:02 +00:00
|
|
|
isExecuting,
|
|
|
|
|
isLoading,
|
|
|
|
|
jsObject,
|
|
|
|
|
onButtonClick,
|
|
|
|
|
responses,
|
|
|
|
|
} = props;
|
|
|
|
|
const [responseStatus, setResponseStatus] = useState<JSResponseState>(
|
|
|
|
|
JSResponseState.NoResponse,
|
|
|
|
|
);
|
2021-09-08 17:32:22 +00:00
|
|
|
const panelRef: RefObject<HTMLDivElement> = useRef(null);
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const response =
|
2022-04-28 16:51:02 +00:00
|
|
|
currentFunction && currentFunction.id && currentFunction.id in responses
|
|
|
|
|
? responses[currentFunction.id]
|
2021-09-08 17:32:22 +00:00
|
|
|
: "";
|
2022-05-19 10:57:49 +00:00
|
|
|
// parse error found while trying to execute function
|
|
|
|
|
const hasExecutionParseErrors = responseStatus === JSResponseState.IsDirty;
|
|
|
|
|
// error found while trying to parse JS Object
|
|
|
|
|
const hasJSObjectParseError = errors.length > 0;
|
2021-09-28 07:31:46 +00:00
|
|
|
|
2022-05-19 13:13:51 +00:00
|
|
|
const isSaving = useSelector(getIsSavingEntity);
|
2021-09-28 07:31:46 +00:00
|
|
|
const onDebugClick = useCallback(() => {
|
|
|
|
|
AnalyticsUtil.logEvent("OPEN_DEBUGGER", {
|
|
|
|
|
source: "JS_OBJECT",
|
|
|
|
|
});
|
2022-10-17 15:16:38 +00:00
|
|
|
dispatch(setJsPaneResponseSelectedTab(DEBUGGER_TAB_KEYS.ERROR_TAB));
|
2021-09-28 07:31:46 +00:00
|
|
|
}, []);
|
2022-04-28 16:51:02 +00:00
|
|
|
useEffect(() => {
|
2022-05-19 13:13:51 +00:00
|
|
|
setResponseStatus(
|
|
|
|
|
getJSResponseViewState(
|
|
|
|
|
currentFunction,
|
|
|
|
|
isDirty,
|
|
|
|
|
isExecuting,
|
|
|
|
|
isSaving,
|
|
|
|
|
responses,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}, [responses, isExecuting, currentFunction, isSaving, isDirty]);
|
2021-09-08 17:32:22 +00:00
|
|
|
const tabs = [
|
|
|
|
|
{
|
2022-06-30 07:21:20 +00:00
|
|
|
key: "response",
|
2021-09-08 17:32:22 +00:00
|
|
|
title: "Response",
|
|
|
|
|
panelComponent: (
|
2021-09-21 06:02:45 +00:00
|
|
|
<>
|
2022-05-19 10:57:49 +00:00
|
|
|
{(hasExecutionParseErrors || hasJSObjectParseError) && (
|
|
|
|
|
<HelpSection
|
|
|
|
|
className={`${
|
|
|
|
|
hasJSObjectParseError
|
|
|
|
|
? "t--js-response-parse-error-call-out"
|
|
|
|
|
: "t--function-execution-parse-error-call-out"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2021-09-28 07:31:46 +00:00
|
|
|
<StyledCallout
|
2021-09-21 06:02:45 +00:00
|
|
|
fill
|
2021-09-28 07:31:46 +00:00
|
|
|
label={
|
|
|
|
|
<FailedMessage>
|
2022-06-30 07:21:20 +00:00
|
|
|
<DebugButton
|
|
|
|
|
className="js-editor-debug-cta"
|
|
|
|
|
onClick={onDebugClick}
|
|
|
|
|
/>
|
2021-09-28 07:31:46 +00:00
|
|
|
</FailedMessage>
|
|
|
|
|
}
|
2022-05-11 10:20:33 +00:00
|
|
|
text={
|
2022-05-19 10:57:49 +00:00
|
|
|
hasJSObjectParseError
|
|
|
|
|
? createMessage(PARSING_ERROR)
|
|
|
|
|
: createMessage(
|
2022-05-11 10:20:33 +00:00
|
|
|
JS_ACTION_EXECUTION_ERROR,
|
|
|
|
|
`${jsObject.name}.${currentFunction?.name}`,
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-09-21 06:02:45 +00:00
|
|
|
variant={Variant.danger}
|
|
|
|
|
/>
|
2022-04-28 16:51:02 +00:00
|
|
|
</HelpSection>
|
|
|
|
|
)}
|
|
|
|
|
<ResponseTabWrapper className={errors.length ? "disable" : ""}>
|
|
|
|
|
<ResponseViewer>
|
2021-09-21 06:02:45 +00:00
|
|
|
<>
|
2022-04-28 16:51:02 +00:00
|
|
|
{responseStatus === JSResponseState.NoResponse && (
|
|
|
|
|
<NoResponseContainer>
|
|
|
|
|
<Icon name="no-response" />
|
|
|
|
|
<Text type={TextType.P1}>
|
|
|
|
|
{createMessage(EMPTY_RESPONSE_FIRST_HALF)}
|
|
|
|
|
<InlineButton
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
onClick={onButtonClick}
|
|
|
|
|
size={Size.medium}
|
|
|
|
|
tag="button"
|
|
|
|
|
text="Run"
|
|
|
|
|
type="button"
|
|
|
|
|
/>
|
|
|
|
|
{createMessage(EMPTY_JS_RESPONSE_LAST_HALF)}
|
|
|
|
|
</Text>
|
|
|
|
|
</NoResponseContainer>
|
|
|
|
|
)}
|
|
|
|
|
{responseStatus === JSResponseState.IsExecuting && (
|
|
|
|
|
<LoadingOverlayScreen theme={props.theme}>
|
|
|
|
|
{createMessage(EXECUTING_FUNCTION)}
|
|
|
|
|
</LoadingOverlayScreen>
|
|
|
|
|
)}
|
|
|
|
|
{responseStatus === JSResponseState.NoReturnValue && (
|
|
|
|
|
<NoReturnValueWrapper>
|
|
|
|
|
<Text type={TextType.P1}>
|
|
|
|
|
{createMessage(
|
|
|
|
|
NO_JS_FUNCTION_RETURN_VALUE,
|
|
|
|
|
currentFunction?.name,
|
|
|
|
|
)}
|
|
|
|
|
</Text>
|
|
|
|
|
</NoReturnValueWrapper>
|
|
|
|
|
)}
|
|
|
|
|
{responseStatus === JSResponseState.ShowResponse && (
|
|
|
|
|
<ReadOnlyEditor
|
|
|
|
|
folding
|
|
|
|
|
height={"100%"}
|
|
|
|
|
input={{
|
|
|
|
|
value: response,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2022-05-19 13:13:51 +00:00
|
|
|
{responseStatus === JSResponseState.IsUpdating && (
|
|
|
|
|
<LoadingOverlayScreen theme={props.theme}>
|
|
|
|
|
{createMessage(UPDATING_JS_COLLECTION)}
|
|
|
|
|
</LoadingOverlayScreen>
|
|
|
|
|
)}
|
2021-09-21 06:02:45 +00:00
|
|
|
</>
|
2022-04-28 16:51:02 +00:00
|
|
|
</ResponseViewer>
|
2021-09-21 06:02:45 +00:00
|
|
|
</ResponseTabWrapper>
|
|
|
|
|
</>
|
2021-09-08 17:32:22 +00:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-10-07 06:53:58 +00:00
|
|
|
key: DEBUGGER_TAB_KEYS.ERROR_TAB,
|
2021-09-08 17:32:22 +00:00
|
|
|
title: createMessage(DEBUGGER_ERRORS),
|
|
|
|
|
panelComponent: <ErrorLogs />,
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-10-07 06:53:58 +00:00
|
|
|
key: DEBUGGER_TAB_KEYS.LOGS_TAB,
|
2021-09-08 17:32:22 +00:00
|
|
|
title: createMessage(DEBUGGER_LOGS),
|
|
|
|
|
panelComponent: <DebuggerLogs searchQuery={jsObject?.name} />,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2022-10-17 15:16:38 +00:00
|
|
|
const selectedResponseTab = useSelector(getJSPaneResponseSelectedTab);
|
|
|
|
|
const responseTabHeight = useSelector(getJSPaneResponsePaneHeight);
|
|
|
|
|
const setSelectedResponseTab = useCallback((selectedTab: string) => {
|
|
|
|
|
dispatch(setJsPaneResponseSelectedTab(selectedTab));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const setResponseHeight = useCallback((height: number) => {
|
|
|
|
|
dispatch(setJsPaneResponsePaneHeight(height));
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-09-08 17:32:22 +00:00
|
|
|
return (
|
2022-10-20 12:08:48 +00:00
|
|
|
<ResponseContainer
|
|
|
|
|
className="t--js-editor-bottom-pane-container"
|
|
|
|
|
ref={panelRef}
|
|
|
|
|
>
|
2022-10-17 15:16:38 +00:00
|
|
|
<Resizer
|
|
|
|
|
initialHeight={responseTabHeight}
|
|
|
|
|
onResizeComplete={setResponseHeight}
|
|
|
|
|
panelRef={panelRef}
|
|
|
|
|
/>
|
2021-09-08 17:32:22 +00:00
|
|
|
<TabbedViewWrapper>
|
2022-04-28 16:51:02 +00:00
|
|
|
<EntityBottomTabs
|
|
|
|
|
containerRef={panelRef}
|
2022-10-20 12:08:48 +00:00
|
|
|
expandedHeight={`${ActionExecutionResizerHeight}px`}
|
2022-10-17 15:16:38 +00:00
|
|
|
onSelect={setSelectedResponseTab}
|
|
|
|
|
selectedTabKey={selectedResponseTab}
|
2022-04-28 16:51:02 +00:00
|
|
|
tabs={tabs}
|
|
|
|
|
/>
|
2021-09-08 17:32:22 +00:00
|
|
|
</TabbedViewWrapper>
|
|
|
|
|
</ResponseContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (
|
|
|
|
|
state: AppState,
|
|
|
|
|
props: { jsObject: JSCollection },
|
|
|
|
|
) => {
|
|
|
|
|
const jsActions = state.entities.jsActions;
|
|
|
|
|
const { jsObject } = props;
|
|
|
|
|
const seletedJsObject =
|
|
|
|
|
jsObject &&
|
|
|
|
|
jsActions.find(
|
|
|
|
|
(action: JSCollectionData) => action.config.id === jsObject.id,
|
|
|
|
|
);
|
|
|
|
|
const responses = (seletedJsObject && seletedJsObject.data) || {};
|
2022-05-19 13:13:51 +00:00
|
|
|
const isDirty = (seletedJsObject && seletedJsObject.isDirty) || {};
|
2021-09-08 17:32:22 +00:00
|
|
|
const isExecuting = (seletedJsObject && seletedJsObject.isExecuting) || {};
|
|
|
|
|
return {
|
2022-05-11 10:20:33 +00:00
|
|
|
responses,
|
|
|
|
|
isExecuting,
|
2022-05-19 13:13:51 +00:00
|
|
|
isDirty,
|
2021-09-08 17:32:22 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withRouter(JSResponseView));
|