<commit_before>import {Button, ButtonGroup, Divider, Heading, Stack} from '@chakra-ui/core'
import {useDispatch} from 'react-redux'

import ConfirmButton from 'lib/components/confirm-button'
import Editable from 'lib/components/editable'
import message from 'lib/message'

import {
  deleteOpportunityDataset,
  deleteSourceSet,
  downloadOpportunityDataset,
  editOpportunityDataset
} from '../actions'

function LabelHeading({children, ...p}) {
  return (
    <Heading
      color='gray.500'
      fontWeight='normal'
      size='sm'
      style={{fontVariant: 'small-caps'}}
      {...p}
    >
      {children}
    </Heading>
  )
}

const nameIsValid = (n?: string) => typeof n === 'string' && n.length > 0

export default function EditOpportunityDatatset(p) {
  const dispatch = useDispatch<any>()

  function _editName(newName: string) {
    return dispatch(
      editOpportunityDataset({
        ...p.opportunityDataset,
        name: newName
      })
    )
  }

  function _deleteDataset() {
    return dispatch(deleteOpportunityDataset(p.opportunityDataset))
  }

  function _deleteSourceSet() {
    return dispatch(deleteSourceSet(p.opportunityDataset.sourceId))
  }

  async function _downloadTiff() {
    const value = await dispatch(
      downloadOpportunityDataset(p.opportunityDataset, 'tiff')
    )
    window.open(value.url)
  }

  async function _downloadGrid() {
    const value = await dispatch(
      downloadOpportunityDataset(p.opportunityDataset, 'grid')
    )
    window.open(value.url)
  }

  const {totalOpportunities} = p.opportunityDataset

  return (
    <Stack spacing={5}>
      <Stack spacing={0}>
        <LabelHeading>name</LabelHeading>
        <Heading size='md'>
          <Editable
            onChange={_editName}
            isValid={nameIsValid}
            placeholder='Opportunity dataset name'
            value={p.opportunityDataset.name}
          />
        </Heading>
      </Stack>

      <Stack spacing={1}>
        <LabelHeading>total opportunities</LabelHeading>
        <Heading id='totalOpportunities' size='md'>
          {typeof totalOpportunities === 'number'
            ? totalOpportunities.toLocaleString()
            : 'unknown'}
        </Heading>
      </Stack>

      <Stack spacing={1}>
        <LabelHeading>download as</LabelHeading>
        <ButtonGroup>
          <Button
            leftIcon='download'<commit_msg>Check timestamp, show unknown if 0 and before that date<commit_after>import {Button, ButtonGroup, Divider, Heading, Stack} from '@chakra-ui/core'
import {useDispatch} from 'react-redux'

import ConfirmButton from 'lib/components/confirm-button'
import Editable from 'lib/components/editable'
import message from 'lib/message'

import {
  deleteOpportunityDataset,
  deleteSourceSet,
  downloadOpportunityDataset,
  editOpportunityDataset
} from '../actions'

function LabelHeading({children, ...p}) {
  return (
    <Heading
      color='gray.500'
      fontWeight='normal'
      size='sm'
      style={{fontVariant: 'small-caps'}}
      {...p}
    >
      {children}
    </Heading>
  )
}

// Datasets before 2019 may not have had their total opportunties calculated
const cutoffTimestamp = new Date('2019').valueOf()

const nameIsValid = (n?: string) => typeof n === 'string' && n.length > 0

export default function EditOpportunityDatatset(p) {
  const dispatch = useDispatch<any>()

  function _editName(newName: string) {
    return dispatch(
      editOpportunityDataset({
        ...p.opportunityDataset,
        name: newName
      })
    )
  }

  function _deleteDataset() {
    return dispatch(deleteOpportunityDataset(p.opportunityDataset))
  }

  function _deleteSourceSet() {
    return dispatch(deleteSourceSet(p.opportunityDataset.sourceId))
  }

  async function _downloadTiff() {
    const value = await dispatch(
      downloadOpportunityDataset(p.opportunityDataset, 'tiff')
    )
    window.open(value.url)
  }

  async function _downloadGrid() {
    const value = await dispatch(
      downloadOpportunityDataset(p.opportunityDataset, 'grid')
    )
    window.open(value.url)
  }

  const {createdAt, totalOpportunities} = p.opportunityDataset
  let totalOpportunitiesDisplay = 'unknown'
  if (totalOpportunities || createdAt >= cutoffTimestamp) {
    totalOpportunitiesDisplay = totalOpportunities.toLocaleString()
  }

  return (
    <Stack spacing={5}>
      <Stack spacing={0}>
        <LabelHeading>name</LabelHeading>
        <Heading size='md'>
          <Editable
            onChange={_editName}
            isValid={nameIsValid}
            placeholder='Opportunity dataset name'
            value={p.opportunityDataset.name}
          />
        </Heading>
      </Stack>

      <Stack spacing={1}>
        <LabelHeading>total opportunities</LabelHeading>
        <Heading id='totalOpportunities' size='md'>
          {totalOpportunitiesDisplay}
        </Heading>
      </Stack>

      <Stack spacing={1}>
        <LabelHeading>download as</LabelHeading>
        <ButtonGroup>
          <Button
            leftIcon='download'
{'fraction_non_alphanumeric': 0.11397423191278494, 'fraction_numerical': 0.004955401387512388, 'mean_word_length': 2.973228346456693, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1211704', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1481, 'n_words': 305}