<commit_before>import React from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'

import { Content, Overlay } from 'cozy-ui/transpiled/react'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import { models, withClient } from 'cozy-client'
import { ModalManager } from 'react-cozy-helpers'

import FileList from 'drive/web/modules/filelist/FileList'
import Main from 'drive/web/modules/layout/Main'
import Topbar from 'drive/web/modules/layout/Topbar'
import Breadcrumb from 'drive/web/modules/navigation/Breadcrumb'
import ErrorShare from 'components/Error/ErrorShare'
import PublicToolbar from './PublicToolbar'

import {
  openFolder,
  getOpenedFolderId,
  getFolderIdFromRoute,
  fetchMoreFiles,
  getVisibleFiles,
  getFolderUrl
} from 'drive/web/modules/navigation/duck'

import { FILES_FETCH_LIMIT } from 'drive/constants/config'
import Viewer from 'drive/web/modules/viewer/PublicViewer'
import { isMobileApp } from 'cozy-device-helper'

class DumbFolderView extends React.Component {
  state = {
    revoked: false,
    viewerOpened: false,
    currentViewedIndex: null
  }
  handleFileOpen = async file => {
    const isNote = models.file.isNote(file)
    const { client } = this.props
    if (isNote) {
      try {
        const noteUrl = await models.note.fetchURL(client, file)
        const url = new URL(noteUrl)
        if (!isMobileApp()) {
          url.searchParams.set('returnUrl', window.location.href)
        }
        window.location.href = url.toString()
      } catch (e) {
        Alerter.error('alert.offline')
      }
    } else {
      this.showInViewer(file)
    }
  }
  showInViewer = file => {
    const { files, fileCount, params, location, fetchMoreFiles } = this.props
    const currentIndex = this.props.files.findIndex(f => f.id === file.id)
    this.setState(state => ({
      ...state,
      viewerOpened: true,
      currentViewedIndex: currentIndex
    }))
    if (files.length !== fileCount && files.length - currentIndex <= 5) {
      const folderId = getFolderIdFromRoute(location, params)
      fetchMoreFiles(folderId, files.length, FILES_FETCH_LIMIT)
    }
  }

  closeViewer = () =>
    this.setState(state => ({
      ...state,
      viewerOpened: false,
      currentViewedIndex: null
    }))

  componentWillMount() {
    this.props
      .fetchFolder(getFolderIdFromRoute(this.props.location, this.props.params))
      .then(e => {
        if (e.type === 'OPEN_FOLDER_FAILURE') {
          this.setState(state => ({ ...state, revoked: true }))
        }
      })
  }

  navigateToFolder = async folderId => {
    await this.props.fetchFolder(folderId)
    this.props.router.push(getFolderUrl(folderId, this.props.location))
  }

  render() {
    if (this.state.revoked) {
      return <ErrorShare errorType={`public_unshared`} />
    }
    const { viewerOpened, currentViewedIndex } = this.state
    const { children, ...fileListProps } = this.props

    return (
      <Main isPublic>
        <Topbar>
          <Breadcrumb isPublic onFolderOpen={this.props.fetchFolder} />
          <PublicToolbar files={[this.props.displayedFolder]} isFile={false} />
        </Topbar>
        <Content>
          <FileList
            onFileOpen={this.handleFileOpen}
            onFolderOpen={this.navigateToFolder}
            withSelectionCheckbox={false}
            {...this.props}
          />
          {viewerOpened && (
            <Overlay>
              <Viewer
                files={this.props.files}
                currentIndex={currentViewedIndex}
                onChangeRequest={this.showInViewer}
                onCloseRequest={this.closeViewer}
              /><commit_msg>refactor: Pass write permission to toolbar<commit_after>import React from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'
import get from 'lodash/get'

import { Content, Overlay } from 'cozy-ui/transpiled/react'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import { models, withClient } from 'cozy-client'
import { ModalManager } from 'react-cozy-helpers'

import FileList from 'drive/web/modules/filelist/FileList'
import Main from 'drive/web/modules/layout/Main'
import Topbar from 'drive/web/modules/layout/Topbar'
import Breadcrumb from 'drive/web/modules/navigation/Breadcrumb'
import ErrorShare from 'components/Error/ErrorShare'
import PublicToolbar from './PublicToolbar'

import {
  openFolder,
  getOpenedFolderId,
  getFolderIdFromRoute,
  fetchMoreFiles,
  getVisibleFiles,
  getFolderUrl
} from 'drive/web/modules/navigation/duck'

import { FILES_FETCH_LIMIT } from 'drive/constants/config'
import Viewer from 'drive/web/modules/viewer/PublicViewer'
import { isMobileApp } from 'cozy-device-helper'

class DumbFolderView extends React.Component {
  state = {
    revoked: false,
    viewerOpened: false,
    currentViewedIndex: null,
    hasWriteAccess: false
  }
  handleFileOpen = async file => {
    const isNote = models.file.isNote(file)
    const { client } = this.props
    if (isNote) {
      try {
        const noteUrl = await models.note.fetchURL(client, file)
        const url = new URL(noteUrl)
        if (!isMobileApp()) {
          url.searchParams.set('returnUrl', window.location.href)
        }
        window.location.href = url.toString()
      } catch (e) {
        Alerter.error('alert.offline')
      }
    } else {
      this.showInViewer(file)
    }
  }
  showInViewer = file => {
    const { files, fileCount, params, location, fetchMoreFiles } = this.props
    const currentIndex = this.props.files.findIndex(f => f.id === file.id)
    this.setState(state => ({
      ...state,
      viewerOpened: true,
      currentViewedIndex: currentIndex
    }))
    if (files.length !== fileCount && files.length - currentIndex <= 5) {
      const folderId = getFolderIdFromRoute(location, params)
      fetchMoreFiles(folderId, files.length, FILES_FETCH_LIMIT)
    }
  }

  closeViewer = () =>
    this.setState(state => ({
      ...state,
      viewerOpened: false,
      currentViewedIndex: null
    }))

  componentWillMount() {
    this.loadPermissions()
    this.props
      .fetchFolder(getFolderIdFromRoute(this.props.location, this.props.params))
      .then(e => {
        if (e.type === 'OPEN_FOLDER_FAILURE') {
          this.setState(state => ({ ...state, revoked: true }))
        }
      })
  }

  loadPermissions = async () => {
    const { client } = this.props
    const response = await client
      .collection('io.cozy.permissions')
      .getOwnPermissions()
    const permission = get(response, 'data.attributes.permissions.files', {})
    this.setState({ hasWriteAccess: !models.permission.isReadOnly(permission) })
  }

  navigateToFolder = async folderId => {
    await this.props.fetchFolder(folderId)
    this.props.router.push(getFolderUrl(folderId, this.props.location))
  }

  render() {
    if (this.state.revoked) {
      return <ErrorShare errorType={`public_unshared`} />
    }
    const { viewerOpened, currentViewedIndex, hasWriteAccess } = this.state
    const { children, ...fileListProps } = this.props

    return (
      <Main isPublic>
        <Topbar>
          <Breadcrumb isPublic onFolderOpen={this.props.fetchFolder} />
          <PublicToolbar
            files={[this.props.displayedFolder]}
            isFile={false}
            hasWriteAccess={hasWriteAccess}
          />
        </Topbar>
        <Content>
          <FileList
            onFileOpen={this.handleFileOpen}
            onFolderOpen={this.navigateToFolder}
            withSelectionCheckbox={false}
            {...this.props}
          />
          {viewerOpened && (
            <Overlay>
              <Viewer
                files={this.props.files}
                currentIndex={currentViewedIndex}
                onChangeRequest={this.showInViewer}
                onCloseRequest={this.closeViewer}
              />
{'fraction_non_alphanumeric': 0.11791497975708502, 'fraction_numerical': 0.00025303643724696357, 'mean_word_length': 2.7165021156558535, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 42, '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': '364412', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2412, 'n_words': 462}