All files / src/components ExpandableContent.tsx

96.96% Statements 32/33
84.21% Branches 32/38
87.5% Functions 7/8
96.96% Lines 32/33

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                  67x                                     48x 48x   48x   48x 48x 48x 48x 48x 48x 48x 23x 23x     48x   48x 2x 2x 2x     2x     48x             13x 13x 13x 13x 13x                                       6x 2x   4x                   2x 2x   2x               2x                                                                
import React, { useEffect, useState, ReactElement, CSSProperties } from 'react';
import { Card, Space } from 'antd';
import {
  CloudDownloadOutlined, MinusOutlined, PlusOutlined,
  CaretDownOutlined, CaretRightOutlined,
} from '@ant-design/icons';
import { CSVDownload } from 'react-csv';
import { useTranslation } from 'react-i18next';
 
const ExpandableContentComponent = (props: {
  content: string | ReactElement;
  title: string | ReactElement;
  showDownload: boolean;
  breakLine?: boolean
  customStyle?: CSSProperties;
  icon?: ReactElement;
  fixHeight?: number;
  initDisplay?: number | boolean;
  visible?: boolean;
  initNumOfLineBreaks?: number;
  className?: string;
  toggleDisplayCallback?: (display: boolean) => void;
  titleRightExtra?: ReactElement;
}): ReactElement => {
  const {
    content, title, showDownload, customStyle, icon, breakLine, fixHeight,
    initDisplay, visible, initNumOfLineBreaks, className, toggleDisplayCallback,
    titleRightExtra,
  } = props;
  const { t } = useTranslation();
 
  const numOfLineBreaks = (initNumOfLineBreaks != null && !isNaN(initNumOfLineBreaks)) ?
    initNumOfLineBreaks : (typeof content === "string") ? content.split("\n").length : 1;
  const maxHeight = 500;
  const calculatedHeight = 90 + (numOfLineBreaks * 25);
  const expandHeight = (calculatedHeight > maxHeight) ? maxHeight : calculatedHeight;
  const [height, setHeight] = useState<number>(expandHeight);
  const [display, setDisplay] = useState<boolean>(true);
  const [triggerDownload, setTriggerDownload] = useState<boolean>(false);
  useEffect(() => {
    setHeight(fixHeight ? fixHeight : expandHeight);
    setDisplay(!(initDisplay === -1 || initDisplay === false));
  }, [expandHeight, initDisplay, fixHeight]);
 
  const whiteSpace = (breakLine) ? "break-spaces" : "pre";
 
  const prepareDownloadData = (logContent: string | unknown):Array<{[propName: string]: string}> => {
    const result = [] as Array<{[propName: string]: string}>;
    result.push({ "": logContent as string} as {[propName: string]: string});
    setTimeout(() => {
      setTriggerDownload(false);
    } , 100);
    return result;
  };
 
  return visible === false ? (<div />) : (
    <Card size="small"
      style={(customStyle != null) ? customStyle : {}}
      className={`expandable-content ${className}`}
      title={(<div
        role="link"
        onClick={(e: React.MouseEvent<unknown>) => {
          e.preventDefault();
          const v = !display;
          setDisplay(v);
          Eif (toggleDisplayCallback != null) {
            toggleDisplayCallback(v);
          }
        }}
        title={t("Toggle display of this panel")}
      >
        <Space
          direction="horizontal"
          size="small">
          {display ? <CaretDownOutlined /> : <CaretRightOutlined />}
          <span>
            <span className="title">{title}</span> {icon ? icon : undefined}
          </span>
        </Space>
      </div>)}
      extra={
        <Space size="middle" direction="horizontal">
          <span
            title={t("Make the panel bigger")}
            onClick={
              () => {
                if (display === false) {
                  setDisplay(true);
                } else {
                  setHeight(height >= maxHeight ? maxHeight : height + 100);
                }
              }
            }>
            <PlusOutlined className="small-clickable-icon" />
          </span>
          <span
            title={t("Make the panel smaller")}
            onClick={
              () => {
                Eif (height <= expandHeight) {
                  setDisplay(false);
                }
                setHeight(height <= expandHeight ? expandHeight : height - 100);
              }
            }>
            <MinusOutlined className="small-clickable-icon" />
          </span>
          {showDownload &&
            <span
              title={t("Download the content as txt file")}
              onClick={() => setTriggerDownload(true)}
            >
              {triggerDownload &&
               <CSVDownload
                 data={prepareDownloadData(content)}
                 headers={undefined}
                 filename={`${new Date().toISOString().slice(0, 10)}.csv`}
                 enclosingCharacter={""}
                 key={Math.random()}
               />
              }
              <CloudDownloadOutlined className="small-clickable-icon" />
            </span>
          }
          {titleRightExtra}
        </Space>
      }
    >
      {
        display &&
        <pre
          className="import-info-pre"
          style={{ height, whiteSpace }}
        >
          {content}
        </pre>
      }
    </Card>
  );
};
 
export default ExpandableContentComponent;