<commit_before>/* eslint-disable no-multi-spaces */

import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

import { Table, Popup } from 'semantic-ui-react'

import { NoBorderTable, ButtonLink } from '../../StyledComponents'

const NameCell = styled(Table.Cell)`
 height: 20px;
 padding: 3px;
`

const LinkCell = styled(Table.Cell)`
  padding: 3px 3px 3px 20px;
  width: 80px;
 verticalAlign: middle;
`

const EXT_CONFIG = {
  tsv: {
    dataType: 'tab-separated-values',
    delimiter: '\t',
  },
  xls: {
    imageName: 'excel',
    dataType: 'csv',
    delimiter: ',',
    dataExt: 'csv',
  },
}

export const FileLink = ({ url, data, ext, linkContent }) => {
  const extConfig = EXT_CONFIG[ext]
  if (!linkContent) {
    linkContent =
      <span><img alt={ext} src={`/static/images/table_${extConfig.imageName || ext}.png`} /> &nbsp; .{ext}</span>
  }

  if (data) {
    let content = data.rawData.map(row => data.processRow(row).map(
      item => `"${(item === null || item === undefined) ? '' : item}"`.replace(/\n/g, ' '),
    ).join(extConfig.delimiter)).join('\n')
    if (data.headers) {
      content = `${data.headers.join(extConfig.delimiter)}\n${content}`
    }
    const href = URL.createObjectURL(new Blob([content], {  type: 'application/octet-stream' }))

    return <a href={href} download={`${data.filename}.${extConfig.dataExt || ext}`}>{linkContent}</a>
  }

  if (!url.includes('?')) {
    url += '?'
  }
  if (!url.endsWith('?')) {
    url += '&'
  }
  return <a href={`${url}file_format=${ext}`}>{linkContent}</a>
}

FileLink.propTypes = {
  ext: PropTypes.string.isRequired,
  url: PropTypes.string,
  data: PropTypes.object,
  linkContent: PropTypes.node,
}

const ExportTableButton = ({ downloads, buttonText, ...buttonProps }) =>
  <Popup<commit_msg>fix discovery sheet downalod formatting<commit_after>/* eslint-disable no-multi-spaces */

import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

import { Table, Popup } from 'semantic-ui-react'

import { NoBorderTable, ButtonLink } from '../../StyledComponents'

const NameCell = styled(Table.Cell)`
 height: 20px;
 padding: 3px;
`

const LinkCell = styled(Table.Cell)`
  padding: 3px 3px 3px 20px;
  width: 80px;
 verticalAlign: middle;
`

const EXT_CONFIG = {
  tsv: {
    dataType: 'tab-separated-values',
    delimiter: '\t',
  },
  xls: {
    imageName: 'excel',
    dataType: 'csv',
    delimiter: ',',
    dataExt: 'csv',
  },
}

const escapeExportItem = item => (item.replace ? item.replace(/"/g, '\'\'') : item)

export const FileLink = ({ url, data, ext, linkContent }) => {
  const extConfig = EXT_CONFIG[ext]
  if (!linkContent) {
    linkContent =
      <span><img alt={ext} src={`/static/images/table_${extConfig.imageName || ext}.png`} /> &nbsp; .{ext}</span>
  }

  if (data) {
    let content = data.rawData.map(row => data.processRow(row).map(
      item => `"${(item === null || item === undefined) ? '' : escapeExportItem(item)}"`,
    ).join(extConfig.delimiter)).join('\n')
    if (data.headers) {
      content = `${data.headers.join(extConfig.delimiter)}\n${content}`
    }
    const href = URL.createObjectURL(new Blob([content], {  type: 'application/octet-stream' }))

    return <a href={href} download={`${data.filename}.${extConfig.dataExt || ext}`}>{linkContent}</a>
  }

  if (!url.includes('?')) {
    url += '?'
  }
  if (!url.endsWith('?')) {
    url += '&'
  }
  return <a href={`${url}file_format=${ext}`}>{linkContent}</a>
}

FileLink.propTypes = {
  ext: PropTypes.string.isRequired,
  url: PropTypes.string,
  data: PropTypes.object,
  linkContent: PropTypes.node,
}

const ExportTableButton = ({ downloads, buttonText, ...buttonProps }) =>
  <Popup
{'fraction_non_alphanumeric': 0.19080520861015146, 'fraction_numerical': 0.005314908317831517, 'mean_word_length': 3.6874221668742218, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6986858', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1322, 'n_words': 269}