All files / src/form/action ActionResultDisplay.tsx

95.34% Statements 41/43
72.85% Branches 51/70
83.33% Functions 5/6
95.23% Lines 40/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                                                            66x     4x   3x 3x           11x 11x         11x   11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   11x     11x     11x                                                                                                                                                                                                                           11x     3x     2x 2x 2x 10x     10x 10x 10x   10x 2x     3x              
/* eslint-disable @typescript-eslint/ban-ts-comment */
import React, { ReactElement } from 'react';
import {
  ActionProps, ExecResultProps, ActionExecResult, ActionMode, MiddleStepExecContextProps, ExtendUploadFile
} from "@props/RecordProps";
import { Space, Timeline } from "antd";
import { random } from 'lodash';
import {
  FileDoneOutlined, CloseCircleOutlined, CheckCircleOutlined,
  ClockCircleOutlined, IssuesCloseOutlined, LinkOutlined,
  ExceptionOutlined, PaperClipOutlined, DownloadOutlined
} from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import { ExpandableContentComponent } from '../../components';
import { ActionExecuteResultPanelMaxWidth, CustomIcon } from '@config/base';
import { formatTime } from '@utils/DateUtils';
import { getNumberOfLineBreaks } from '@utils/ObjectUtils';
import { displaySingleResult, wrapAsHtml } from '@utils/ComponentUtils';
import { StacktraceCell } from '../../form/cells';
import { RedirectComponent } from '../../components/redirect';
import { onDownload } from '../../components/fileOperator';
import PreviewAction from './PreviewAction';
 
export interface ActionResultDisplayProps {
  action: ActionProps;
  result: ExecResultProps | ActionExecResult;
  initDisplay: boolean;
  toggleDisplayCallback: (visible: boolean) => void;
  titleRightExtraRenderFunc?: (objectIds?: Array<number>) => ReactElement;
  mode: ActionMode;
  zIndex: number
  fetchDataCallback?: () => void;
  isMiddleStepResult?: boolean;
  round?: number;
  middleStepExecContext?: MiddleStepExecContextProps;
  fineTuningResult?: string;
}
 
const ActionResultDisplay = (props: ActionResultDisplayProps): ReactElement => {
  const {
    result, action, fetchDataCallback, zIndex, isMiddleStepResult, round, fineTuningResult
  } = props;
 
  const { t, i18n } = useTranslation();
  const renderSingleResult = (
    props: ActionResultDisplayProps, result: ExecResultProps,
    forMultiple: boolean
  ): ReactElement => {
    const {
      action, initDisplay, toggleDisplayCallback, titleRightExtraRenderFunc,
    } = props;
    const { name } = action;
    const {
      status, execLog, execResult, submitUser, submitTime,
      finishTime, startTime, redirect, stackTrace, objectId,
      download
    } = result;
 
    const fail = (status === 'FAILED');
    const successWithWarning = (status === 'SUCCESS_WITH_WARNING');
    const color = (fail) ? "red" : (successWithWarning) ? "#FFA500" : "green";
    const style = successWithWarning ? { color: "#FFA500" } : undefined;
    const statusIcon = (fail) ? <CloseCircleOutlined /> : (successWithWarning) ?
      <IssuesCloseOutlined style={style} /> : <CheckCircleOutlined />;
    const stackTraceNotEmpty = (stackTrace != null && stackTrace !== '');
    const execResultNotEmpty = (execResult != null && execResult !== '');
    const execLogNotEmpty = (execLog != null && execLog !== '');
    const startTimeNotEmpty = (startTime != null && startTime !== '');
    const finishTimeNotEmpty = (finishTime != null && finishTime !== '');
    const actionTitleTrans = t(`dynamicAction:${action.label ?? action.name}`);
    const roundTitle = (round != null) ? t("Round") + " " + round : '';
    const title = `${actionTitleTrans} ${objectId ?? ""} ${roundTitle}`;
    const redirectNotEmpty = (redirect != null && redirect !== '');
    const downloadNotEmpty = (download != null);
    const key = (objectId ?? name).toLocaleString();
    const execLogDisplay = (i18n.exists(execLog)) ? t(execLog) : execLog;
    const idParam = (Array.isArray(objectId)) ?
      objectId : [objectId as number];
    const extraRight = (titleRightExtraRenderFunc && objectId) ?
      titleRightExtraRenderFunc(idParam) : undefined;
    const modal = (
      <ExpandableContentComponent
        titleRightExtra={extraRight}
        toggleDisplayCallback={(visible) => {
          toggleDisplayCallback(visible);
        }}
        initDisplay={initDisplay ? random(0, 1) : false}
        initNumOfLineBreaks={getNumberOfLineBreaks(result, fineTuningResult, isMiddleStepResult)}
        className={`action-execute-result ${status?.toLowerCase()}`}
        key={key}
        visible={result != null}
        icon={statusIcon}
        content={(
          <div className="timeline-container">
            <Timeline mode="left">
              {!isMiddleStepResult && <Timeline.Item
                color="blue"
                label={formatTime(submitTime)}
                dot={<ClockCircleOutlined />}
              > {t('Submitted by', { submitUser })} </Timeline.Item>
              }
              {(startTimeNotEmpty && !isMiddleStepResult) &&
                <Timeline.Item
                  label={startTime != null ? formatTime(startTime) : "未开始"}
                  color="blue"
                  dot={<ClockCircleOutlined />}
                > {t('Started')} </Timeline.Item>
              }
              {(finishTimeNotEmpty && !isMiddleStepResult) &&
                <Timeline.Item
                  color={color}
                  label={formatTime(finishTime)}
                  dot={statusIcon}
                > {t('Finished', { status })} </Timeline.Item>
              }
              {execLogNotEmpty &&
                <Timeline.Item
                  color="blue"
                  label={t("Log")}
                  dot={<CustomIcon type="icon-icon_log" />}
                > {wrapAsHtml(execLogDisplay)} </Timeline.Item>}
              {execResultNotEmpty &&
                <Timeline.Item
                  color="blue"
                  label={t("Result")}
                  dot={<FileDoneOutlined />}
                > {wrapAsHtml(execResult)} </Timeline.Item>}
              {(fineTuningResult != null) &&
                <Timeline.Item
                  color="blue"
                  label={t("Fine Tuning Result")}
                  dot={<FileDoneOutlined />}
                > {wrapAsHtml(fineTuningResult)} </Timeline.Item>}
              {redirectNotEmpty && !isMiddleStepResult &&
                <Timeline.Item
                  color="blue"
                  label={t("Redirect")}
                  dot={<LinkOutlined />}
                >
                  <RedirectComponent
                    forMultiple={forMultiple}
                    fetchDataCallback={fetchDataCallback}
                    redirect={redirect}
                    zIndex={zIndex + 1}
                  />
                </Timeline.Item>}
              {downloadNotEmpty && !isMiddleStepResult &&
                <Timeline.Item
                  color="blue"
                  label={t("Result file download")}
                  dot={<DownloadOutlined />}
                >
                  <Space
                    style={{ cursor: "pointer" }}
                    onClick={() => {
                      onDownload({
                        id: download.id,
                        name: download.name,
                        data: download
                      } as ExtendUploadFile);
                    }}
                  >
                    <PaperClipOutlined />
                    {t("Download")}
                  </Space>
                  {<PreviewAction
                     file={download}
                     zIndex={zIndex + 1}
                     displayTextAndIcon={true}
                  />}
                </Timeline.Item>}
              {stackTraceNotEmpty &&
                <Timeline.Item
                  color={"blue"}
                  label={t("Exception Stacktrace")}
                  dot={<ExceptionOutlined />}
                >
                  <StacktraceCell
                    zIndex={zIndex + 1}
                    value={stackTrace ?? ""}
                    page="ACTION_RESULT"
                  />
                </Timeline.Item>}
            </Timeline>
          </div >
        )}
        showDownload={false}
        title={< span title={title} className="title-label" > {title} </span >}
        customStyle={{
          width: "100%",
          maxWidth: ActionExecuteResultPanelMaxWidth,
        }} />
    );
 
    return (<div key={key}> {modal} </div>);
  };
 
  const renderMultipleResult = (
    props: ActionResultDisplayProps, result: ActionExecResult
  ): ReactElement => {
    const elements: Array<ReactElement> = [];
    let idx = 0;
    for (const objId in result) {
      Iif (!Object.prototype.hasOwnProperty.call(result, objId)) {
        continue;
      }
      const resultForObj = result[objId];
      elements.push(renderSingleResult(props, resultForObj, idx !== 0));
      idx += 1;
    }
    const renderElement = (<span>{elements.map(e => e)}</span>);
    return renderElement;
  };
 
  return displaySingleResult(action.mode) ?
    //@ts-ignore
    renderSingleResult(props, result) : renderMultipleResult(props, result);
 
};
 
export default ActionResultDisplay;