<commit_before>import './SectionTable.less';
import React from 'react';
import PropTypes from 'prop-types';
import { TABLE_CELL_TYPE, DEFAULT_MAX_LENGTH } from '../../constants/Constants';
import isArray from 'lodash/isArray';
import map from 'lodash/map';
import isString from 'lodash/isString';
import truncate from 'lodash/truncate';
import isObjectLike from 'lodash/isObjectLike';


const SectionTable = ({ columns, readableHeaders, data, classes, style, title, titleStyle, emptyString }) => {
  let tableData = data || [];

  if (isString(data)) {
    try {
      tableData = JSON.parse(data);
    } catch (ignored) {
      return <div>Error parsing table</div>;
    }
  }

  if (!isArray(tableData) && isObjectLike(tableData)) {
    tableData = tableData.data || tableData.iocs || tableData.messages;
  }

  let readyColumns = columns;
  if (!isArray(columns) && isArray(tableData)) {
    const headerKeys = {};
    tableData.forEach((val, i) => {
      for (const key in tableData[i]) { // eslint-disable-line no-restricted-syntax
        if (headerKeys[key] !== key) {
          headerKeys[key] = key;
        }
      }
    });

    readyColumns = Object.keys(headerKeys);
  }


  let tableBody;
  if (isArray(readyColumns)) {
    tableBody = tableData.length > 0 ? (
      <table className={'ui compact table unstackable section-table ' + classes} style={{ tableLayout: 'fixed' }}>
        <thead>
          <tr>
            {readyColumns.map((col) => {
              const key = col.key || col;
              return <th key={key}>{!col.hidden && ((readableHeaders && readableHeaders[key]) || key)}</th>;
            })}
          </tr>
        </thead>
        <tbody>
          {tableData.map((row, i) =>
            <tr key={i}>
              {readyColumns.map((col, j) =>
                (() => {
                  const key = col.key || col;
                  const cell = row[key] || (readableHeaders && row[readableHeaders[key]]);

                  let cellToRender = '';
                  if (cell) {
                    switch (cell.type) {
                      case TABLE_CELL_TYPE.image:
                        cellToRender = (
                          <img
                            src={cell.data}
                            alt={cell.alt}
                            className={'ui image ' + cell.classes}
                            style={cell.style}
                          />
                        );
                        break;
                      default:
                        cellToRender = truncate(cell, { length: DEFAULT_MAX_LENGTH });
                    }
                  }
                  return <td key={j} style={{ wordBreak: 'break-word' }}>{cellToRender}</td>;
                })()
              )}
            </tr>)
          }
        </tbody>
      </table>
    ) : <div className="no-data">{emptyString}</div>;
  } else {
    tableBody = (
      <table className={'ui compact table unstackable ' + classes}>
        <tbody><commit_msg>fix line breaks does not show in table rows<commit_after>import './SectionTable.less';
import React from 'react';
import PropTypes from 'prop-types';
import { TABLE_CELL_TYPE, DEFAULT_MAX_LENGTH } from '../../constants/Constants';
import isArray from 'lodash/isArray';
import map from 'lodash/map';
import isString from 'lodash/isString';
import truncate from 'lodash/truncate';
import isObjectLike from 'lodash/isObjectLike';


const SectionTable = ({ columns, readableHeaders, data, classes, style, title, titleStyle, emptyString }) => {
  let tableData = data || [];

  if (isString(data)) {
    try {
      tableData = JSON.parse(data);
    } catch (ignored) {
      return <div>Error parsing table</div>;
    }
  }

  if (!isArray(tableData) && isObjectLike(tableData)) {
    tableData = tableData.data || tableData.iocs || tableData.messages;
  }

  let readyColumns = columns;
  if (!isArray(columns) && isArray(tableData)) {
    const headerKeys = {};
    tableData.forEach((val, i) => {
      for (const key in tableData[i]) { // eslint-disable-line no-restricted-syntax
        if (headerKeys[key] !== key) {
          headerKeys[key] = key;
        }
      }
    });

    readyColumns = Object.keys(headerKeys);
  }


  let tableBody;
  if (isArray(readyColumns)) {
    tableBody = tableData.length > 0 ? (
      <table className={'ui compact table unstackable section-table ' + classes} style={{ tableLayout: 'fixed' }}>
        <thead>
          <tr>
            {readyColumns.map((col) => {
              const key = col.key || col;
              return <th key={key}>{!col.hidden && ((readableHeaders && readableHeaders[key]) || key)}</th>;
            })}
          </tr>
        </thead>
        <tbody>
          {tableData.map((row, i) =>
            <tr key={i}>
              {readyColumns.map((col, j) =>
                (() => {
                  const key = col.key || col;
                  const cell = row[key] || (readableHeaders && row[readableHeaders[key]]);

                  let cellToRender = '';
                  if (cell) {
                    switch (cell.type) {
                      case TABLE_CELL_TYPE.image:
                        cellToRender = (
                          <img
                            src={cell.data}
                            alt={cell.alt}
                            className={'ui image ' + cell.classes}
                            style={cell.style}
                          />
                        );
                        break;
                      default:
                        cellToRender = truncate(cell, { length: DEFAULT_MAX_LENGTH });
                    }
                  }
                  return <td key={j} style={{ wordBreak: 'break-word', whiteSpace: 'pre' }}>{cellToRender}</td>;
                })()
              )}
            </tr>)
          }
        </tbody>
      </table>
    ) : <div className="no-data">{emptyString}</div>;
  } else {
    tableBody = (
      <table className={'ui compact table unstackable ' + classes}>
        <tbody>
{'fraction_non_alphanumeric': 0.13740710156895128, 'fraction_numerical': 0.00033030553261767135, 'mean_word_length': 1.650328227571116, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258593', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1639, 'n_words': 371}