<commit_before>import React from 'react'
import IconButton from '@material-ui/core/IconButton'
import Tooltip from '@material-ui/core/Tooltip'
import ExportIcon from '@material-ui/icons/MoreVert'
import { defineMessages, useIntl, FormattedMessage } from 'react-intl'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import Dialog from '@material-ui/core/Dialog'
import MuiDialogTitle from '@material-ui/core/DialogTitle'
import DialogActions from '@material-ui/core/DialogActions'
import DialogContent from '@material-ui/core/DialogContent'
import DialogContentText from '@material-ui/core/DialogContentText'
import Button from '@material-ui/core/Button'
import LinearProgress from '@material-ui/core/LinearProgress'
import { makeStyles } from '@material-ui/core/styles'
import { remote } from 'electron'

import logger from '../../../logger'
import api from '../../new-api'

const m = defineMessages({
  // Button tooltip on iD Editor toolbar
  exportButton: 'Export map data',
  // Menu item for exporting GeoJSON
  exportGeoJson: 'Export Territory Data as GeoJSON…',
  // Menu item for exporting Shapefile
  exportShapefile: 'Export Territory Data as Shapefile…',
  // OK button after successful export
  okButton: 'OK',
  // Close button after export error
  closeButton: 'Close',
  // Export dialog title - shown during export progress and after success/failure
  dialogTitle: 'Exporting data',
  // Export success message,
  dialogSuccess: 'Export complete',
  // Expor error message - if there was an error during export
  dialogError: 'Export failed due to an internal error',
  // Save dialog title
  saveTitle: 'Export Territory Data',
  // Default filename for map export
  defaultFilename: 'mapeo-map-data'
})

const DialogTitle = () => (
  <MuiDialogTitle id='dialog-title'>
    <FormattedMessage {...m.dialogTitle} />
  </MuiDialogTitle>
)

const DialogAction = ({ children, onClick }) => (
  <DialogActions>
    <Button onClick={onClick} color='primary' variant='contained' autoFocus>
      {children}
    </Button>
  </DialogActions>
)

const ExportButton = () => {
  const { formatMessage: t } = useIntl()
  const [status, setStatus] = React.useState('idle')
  const cx = useStyles()
  const [menuAnchor, setMenuAnchor] = React.useState(null)

  const handleExportClick = event => {
    setMenuAnchor(event.currentTarget)
  }

  const handleMenuItemClick = format => () => {
    setMenuAnchor(null)
    const ext = format === 'shapefile' ? 'zip' : 'geojson'
    remote.dialog
      .showSaveDialog({
        title: t(m.saveTitle),
        defaultPath: t(m.defaultFilename) + '.' + ext,
        filters: [{ name: format, extensions: [ext] }]
      })
      .then(({ canceled, filePath }) => {
        if (!filePath || canceled) return
        setStatus('pending')
        api
          .exportData(filePath, { format })
          .then(() => {
            setStatus('success')
          })
          .catch(err => {
            setStatus('reject')
            logger.error('ExportButton save dialog', err)
          })
      })
  }

  const close = event => {
    setStatus('idle')
  }

  let dialogContent
  switch (status) {
    case 'idle':
      dialogContent = null
      break
    case 'pending':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <LinearProgress indeterminate />
          </DialogContent>
        </>
      )
      break
    case 'success':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <DialogContentText>
              <FormattedMessage {...m.dialogSuccess} />
            </DialogContentText>
          </DialogContent>
          <DialogAction onClick={close}>
            <FormattedMessage {...m.okButton} />
          </DialogAction>
        </>
      )
      break
    case 'reject':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <DialogContentText>
              <FormattedMessage {...m.dialogError} />
            </DialogContentText>
          </DialogContent>
          <DialogAction onClick={close}>
            <FormattedMessage {...m.closeButton} />
          </DialogAction>
        </>
      )
      break
  }

  return (
    <>
      <Tooltip title={t(m.exportButton)}>
        <IconButton
          aria-label='export'
          onClick={handleExportClick}
          aria-controls='export-menu'
          aria-haspopup='true'
          className={cx.button}
        >
          <ExportIcon />
        </IconButton>
      </Tooltip>
      <Menu
        id='export-menu'
        anchorEl={menuAnchor}
        open={Boolean(menuAnchor)}
        onClose={handleMenuItemClick(null)}
      >
        <MenuItem onClick={handleMenuItemClick('geojson')}><commit_msg>fix: do not show download dialog when closing export menu (#489)<commit_after>import React from 'react'
import IconButton from '@material-ui/core/IconButton'
import Tooltip from '@material-ui/core/Tooltip'
import ExportIcon from '@material-ui/icons/MoreVert'
import { defineMessages, useIntl, FormattedMessage } from 'react-intl'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import Dialog from '@material-ui/core/Dialog'
import MuiDialogTitle from '@material-ui/core/DialogTitle'
import DialogActions from '@material-ui/core/DialogActions'
import DialogContent from '@material-ui/core/DialogContent'
import DialogContentText from '@material-ui/core/DialogContentText'
import Button from '@material-ui/core/Button'
import LinearProgress from '@material-ui/core/LinearProgress'
import { makeStyles } from '@material-ui/core/styles'
import { remote } from 'electron'

import logger from '../../../logger'
import api from '../../new-api'

const m = defineMessages({
  // Button tooltip on iD Editor toolbar
  exportButton: 'Export map data',
  // Menu item for exporting GeoJSON
  exportGeoJson: 'Export Territory Data as GeoJSON…',
  // Menu item for exporting Shapefile
  exportShapefile: 'Export Territory Data as Shapefile…',
  // OK button after successful export
  okButton: 'OK',
  // Close button after export error
  closeButton: 'Close',
  // Export dialog title - shown during export progress and after success/failure
  dialogTitle: 'Exporting data',
  // Export success message,
  dialogSuccess: 'Export complete',
  // Expor error message - if there was an error during export
  dialogError: 'Export failed due to an internal error',
  // Save dialog title
  saveTitle: 'Export Territory Data',
  // Default filename for map export
  defaultFilename: 'mapeo-map-data'
})

const DialogTitle = () => (
  <MuiDialogTitle id='dialog-title'>
    <FormattedMessage {...m.dialogTitle} />
  </MuiDialogTitle>
)

const DialogAction = ({ children, onClick }) => (
  <DialogActions>
    <Button onClick={onClick} color='primary' variant='contained' autoFocus>
      {children}
    </Button>
  </DialogActions>
)

const ExportButton = () => {
  const { formatMessage: t } = useIntl()
  const [status, setStatus] = React.useState('idle')
  const cx = useStyles()
  const [menuAnchor, setMenuAnchor] = React.useState(null)

  const handleExportClick = event => {
    setMenuAnchor(event.currentTarget)
  }

  const closeMenu = () => {
    setMenuAnchor(null)
  }

  const handleMenuItemClick = format => () => {
    closeMenu()
    const ext = format === 'shapefile' ? 'zip' : 'geojson'
    remote.dialog
      .showSaveDialog({
        title: t(m.saveTitle),
        defaultPath: t(m.defaultFilename) + '.' + ext,
        filters: [{ name: format, extensions: [ext] }]
      })
      .then(({ canceled, filePath }) => {
        if (!filePath || canceled) return
        setStatus('pending')
        api
          .exportData(filePath, { format })
          .then(() => {
            setStatus('success')
          })
          .catch(err => {
            setStatus('reject')
            logger.error('ExportButton save dialog', err)
          })
      })
  }

  const close = event => {
    setStatus('idle')
  }

  let dialogContent
  switch (status) {
    case 'idle':
      dialogContent = null
      break
    case 'pending':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <LinearProgress indeterminate />
          </DialogContent>
        </>
      )
      break
    case 'success':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <DialogContentText>
              <FormattedMessage {...m.dialogSuccess} />
            </DialogContentText>
          </DialogContent>
          <DialogAction onClick={close}>
            <FormattedMessage {...m.okButton} />
          </DialogAction>
        </>
      )
      break
    case 'reject':
      dialogContent = (
        <>
          <DialogTitle />
          <DialogContent>
            <DialogContentText>
              <FormattedMessage {...m.dialogError} />
            </DialogContentText>
          </DialogContent>
          <DialogAction onClick={close}>
            <FormattedMessage {...m.closeButton} />
          </DialogAction>
        </>
      )
      break
  }

  return (
    <>
      <Tooltip title={t(m.exportButton)}>
        <IconButton
          aria-label='export'
          onClick={handleExportClick}
          aria-controls='export-menu'
          aria-haspopup='true'
          className={cx.button}
        >
          <ExportIcon />
        </IconButton>
      </Tooltip>
      <Menu
        id='export-menu'
        anchorEl={menuAnchor}
        open={Boolean(menuAnchor)}
        onClose={closeMenu}
      >
        <MenuItem onClick={handleMenuItemClick('geojson')}>
{'fraction_non_alphanumeric': 0.1253605274000824, 'fraction_numerical': 0.00030902348578491963, 'mean_word_length': 2.4551601423487543, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011961', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2683, 'n_words': 670}