<commit_before>/* global cozy */
import React from 'react'
import { connect } from 'react-redux'
import classnames from 'classnames'
import {
  ButtonLink,
  withBreakpoints,
  Menu,
  MenuItem,
  Icon
} from 'cozy-ui/react'
import { MoreButton } from 'components/Button'
import DownloadButton from './DownloadButton'
import { downloadFiles } from 'drive/web/modules/navigation/duck'
import toolbarstyles from 'drive/styles/toolbar'
import { getQueryParameter } from 'react-cozy-helpers'
import CozyHomeLink, { getHomeLinkHref } from 'components/Button/CozyHomeLink'

import CloudIcon from 'drive/assets/icons/icon-cloud-open.svg'
import CloudNegative from 'drive/assets/icons/icon-cloud-negative.svg'
import DownloadIcon from 'drive/assets/icons/icon-download-16.svg'

const { BarRight } = cozy.bar

const OpenInCozyButton = ({ t, size = 'normal', href = '' }) => (
  <ButtonLink
    href={href}
    size={size}
    label={t('toolbar.menu_open_cozy')}
    icon={CloudIcon}
  />
)

const DownloadFilesButton = ({ t, onDownload, size = 'normal' }) => (
  <DownloadButton
    label={t('toolbar.menu_download_folder')}
    className={toolbarstyles['fil-public-download']}
    onDownload={onDownload}
    theme="secondary"
    size={size}
  />
)
const MoreMenu = ({ t, onDownload, onOpenInCozy, onCreateCozy }) => (
  <Menu
    title={t('toolbar.item_more')}
    className={classnames(
      toolbarstyles['fil-toolbar-menu'],
      toolbarstyles['fil-toolbar-menu--public']
    )}
    buttonClassName={toolbarstyles['fil-toolbar-more-btn']}
    component={<MoreButton>{t('Toolbar.more')}</MoreButton>}
    position="right"
  >
    {onOpenInCozy && (
      <MenuItem onSelect={onOpenInCozy} icon={<Icon icon={CloudIcon} />}>
        {t('toolbar.menu_open_cozy')}
      </MenuItem>
    )}
    {onCreateCozy && (
      <MenuItem onSelect={onCreateCozy} icon={<Icon icon={CloudNegative} />}>
        {t('Share.create-cozy')}
      </MenuItem>
    )}
    <MenuItem onSelect={onDownload} icon={<Icon icon={DownloadIcon} />}>
      {t('toolbar.menu_download_folder')}
    </MenuItem>
  </Menu>
)

class PublicToolbar extends React.Component {
  state = {
    discoveryLink: null
  }

  componentDidMount() {
    if (window.location.pathname === '/preview') this.loadSharingDiscoveryLink()
  }

  async loadSharingDiscoveryLink() {
    try {
      const { client } = this.context
      const response = await client
        .collection('io.cozy.permissions')
        .getOwnPermissions()
      const sourceId = response.data.attributes.source_id
      const sharingId = sourceId.split('/')[1]

      const { sharecode } = getQueryParameter()

      const link = client
        .collection('io.cozy.sharings')
        .getDiscoveryLink(sharingId, sharecode)
      this.setState({ discoveryLink: link })
    } catch (err) {
      console.warn('Failed to load sharing discovery link', err)
    }
  }

  redirectTo(url) {
    window.location.href = url
  }

  render() {
    const {
      files,
      onDownload,
      breakpoints: { isMobile },
      renderInBar = false
    } = this.props
    const { t } = this.context
    const { discoveryLink } = this.state

    if (isMobile) {
      return (
        <BarRight>
          <MoreMenu
            t={t}
            onDownload={() => onDownload(files)}
            onOpenInCozy={
              discoveryLink ? () => this.redirectTo(discoveryLink) : false
            }
            onCreateCozy={
              discoveryLink
                ? false
                : () => this.redirectTo(getHomeLinkHref('sharing-drive'))
            }
          />
        </BarRight>
      )
    } else if (renderInBar) {
      return (
        <BarRight>
          <div className={toolbarstyles['toolbar-inside-bar']}>
            {discoveryLink ? (
              <OpenInCozyButton href={discoveryLink} t={t} size="small" />
            ) : (
              <CozyHomeLink from="sharing-drive" />
            )}
            <DownloadFilesButton
              t={t}
              onDownload={() => onDownload(files)}
              size="small"
            />
          </div>
        </BarRight>
      )
    } else {
      return (
        <div className={toolbarstyles['fil-toolbar-files']} role="toolbar">
          {discoveryLink ? (
            <OpenInCozyButton href={discoveryLink} t={t} />
          ) : (
            <CozyHomeLink from="sharing-drive" />
          )}
          <DownloadFilesButton t={t} onDownload={() => onDownload(files)} />
          <BarRight>
            <div />
          </BarRight>
        </div>
      )
    }
  }
}

const mapDispatchToProps = dispatch => ({
  onDownload: files => dispatch(downloadFiles(files))
})

export default connect(null, mapDispatchToProps)(
  withBreakpoints()(PublicToolbar)
)<commit_msg>refactor: Split render into components<commit_after>/* global cozy */
import React from 'react'
import { connect } from 'react-redux'
import classnames from 'classnames'
import {
  ButtonLink,
  withBreakpoints,
  Menu,
  MenuItem,
  Icon
} from 'cozy-ui/react'
import { MoreButton } from 'components/Button'
import DownloadButton from './DownloadButton'
import { downloadFiles } from 'drive/web/modules/navigation/duck'
import toolbarstyles from 'drive/styles/toolbar'
import { getQueryParameter } from 'react-cozy-helpers'
import CozyHomeLink, { getHomeLinkHref } from 'components/Button/CozyHomeLink'

import CloudIcon from 'drive/assets/icons/icon-cloud-open.svg'
import CloudNegative from 'drive/assets/icons/icon-cloud-negative.svg'
import DownloadIcon from 'drive/assets/icons/icon-download-16.svg'

const { BarRight } = cozy.bar

const OpenInCozyButton = ({ t, size = 'normal', href = '' }) => (
  <ButtonLink
    href={href}
    size={size}
    label={t('toolbar.menu_open_cozy')}
    icon={CloudIcon}
  />
)

const DownloadFilesButton = ({ t, onDownload, size = 'normal' }) => (
  <DownloadButton
    label={t('toolbar.menu_download_folder')}
    className={toolbarstyles['fil-public-download']}
    onDownload={onDownload}
    theme="secondary"
    size={size}
  />
)
const MoreMenu = ({ t, onDownload, onOpenInCozy, onCreateCozy }) => (
  <Menu
    title={t('toolbar.item_more')}
    className={classnames(
      toolbarstyles['fil-toolbar-menu'],
      toolbarstyles['fil-toolbar-menu--public']
    )}
    buttonClassName={toolbarstyles['fil-toolbar-more-btn']}
    component={<MoreButton>{t('Toolbar.more')}</MoreButton>}
    position="right"
  >
    {onOpenInCozy && (
      <MenuItem onSelect={onOpenInCozy} icon={<Icon icon={CloudIcon} />}>
        {t('toolbar.menu_open_cozy')}
      </MenuItem>
    )}
    {onCreateCozy && (
      <MenuItem onSelect={onCreateCozy} icon={<Icon icon={CloudNegative} />}>
        {t('Share.create-cozy')}
      </MenuItem>
    )}
    <MenuItem onSelect={onDownload} icon={<Icon icon={DownloadIcon} />}>
      {t('toolbar.menu_download_folder')}
    </MenuItem>
  </Menu>
)

const MobileToolbar = ({ t, onDownload, discoveryLink, redirectTo }) => (
  <BarRight>
    <MoreMenu
      t={t}
      onDownload={() => onDownload(files)}
      onOpenInCozy={discoveryLink ? () => redirectTo(discoveryLink) : false}
      onCreateCozy={
        discoveryLink
          ? false
          : () => redirectTo(getHomeLinkHref('sharing-drive'))
      }
    />
  </BarRight>
)

const CozybarToolbar = ({ t, onDownload, discoveryLink }) => (
  <BarRight>
    <div className={toolbarstyles['toolbar-inside-bar']}>
      {discoveryLink ? (
        <OpenInCozyButton href={discoveryLink} t={t} size="small" />
      ) : (
        <CozyHomeLink from="sharing-drive" />
      )}
      <DownloadFilesButton
        t={t}
        onDownload={() => onDownload(files)}
        size="small"
      />
    </div>
  </BarRight>
)

const DesktopToolbar = ({ t, onDownload, discoveryLink }) => (
  <div className={toolbarstyles['fil-toolbar-files']} role="toolbar">
    {discoveryLink ? (
      <OpenInCozyButton href={discoveryLink} t={t} />
    ) : (
      <CozyHomeLink from="sharing-drive" />
    )}
    <DownloadFilesButton t={t} onDownload={() => onDownload(files)} />
    <BarRight>
      <div />
    </BarRight>
  </div>
)

class PublicToolbar extends React.Component {
  state = {
    discoveryLink: null
  }

  componentDidMount() {
    if (window.location.pathname === '/preview') this.loadSharingDiscoveryLink()
  }

  async loadSharingDiscoveryLink() {
    try {
      const { client } = this.context
      const response = await client
        .collection('io.cozy.permissions')
        .getOwnPermissions()
      const sourceId = response.data.attributes.source_id
      const sharingId = sourceId.split('/')[1]

      const { sharecode } = getQueryParameter()

      const link = client
        .collection('io.cozy.sharings')
        .getDiscoveryLink(sharingId, sharecode)
      this.setState({ discoveryLink: link })
    } catch (err) {
      console.warn('Failed to load sharing discovery link', err)
    }
  }

  redirectTo(url) {
    window.location.href = url
  }

  render() {
    const {
      files,
      onDownload,
      breakpoints: { isMobile },
      renderInBar = false
    } = this.props
    const { t } = this.context
    const { discoveryLink } = this.state

    if (isMobile) {
      return (
        <MobilePublicToolbar
          t={t}
          onDownload={onDownload}
          discoveryLink={discoveryLink}
          redirectTo={this.redirectTo}
        />
      )
    } else if (renderInBar) {
      return (
        <CozybarToolbar
          t={t}
          onDownload={onDownload}
          discoveryLink={discoveryLink}
        />
      )
    } else {
      return (
        <DesktopToolbar
          t={t}
          onDownload={onDownload}
          discoveryLink={discoveryLink}
        />
      )
    }
  }
}

const mapDispatchToProps = dispatch => ({
  onDownload: files => dispatch(downloadFiles(files))
})

export default connect(null, mapDispatchToProps)(
  withBreakpoints()(PublicToolbar)
)
{'fraction_non_alphanumeric': 0.14081529944640162, 'fraction_numerical': 0.0006039255158530448, 'mean_word_length': 2.707462686567164, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 63, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2004210', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2998, 'n_words': 548}