<commit_before>import React from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { Empty } from 'cozy-ui/react'

import Container from 'drive/web/modules/drive/Container'
import {
  FETCH_SHARINGS,
  FETCH_SHARINGS_SUCCESS,
  FETCH_SHARINGS_FAILURE
} from 'drive/web/modules/navigation/duck/actions'
import SharedDocuments from 'sharing/components/SharedDocuments'

import { makeCancelable } from 'lib/promise'

import { openFolder, getFolderUrl } from 'drive/web/modules/navigation/duck'

export class SharingFetcher extends React.Component {
  state = {
    error: null
  }

  static contextTypes = {
    client: PropTypes.object.isRequired,
    t: PropTypes.func.isRequired
  }
  constructor(props) {
    super(props)
    this.fetchSharedFiles = null
    this.fetSharedParents = null
  }
  async fetchSharedDocuments() {
    const { sharedDocuments } = this.props
    const { client } = this.context

    try {
      this.props.startFetch()
      this.setState({ error: null })

      this.fetchSharedFiles = makeCancelable(
        client.collection('io.cozy.files').all({ keys: sharedDocuments })
      )

      const resp = await this.fetchSharedFiles.promise
      const parentIds = resp.data.map(f => f.dir_id)
      this.fetSharedParents = makeCancelable(
        client.collection('io.cozy.files').all({
          keys: parentIds
        })
      )
      const parentsResp = await this.fetSharedParents.promise
      const parents = parentsResp.data
      const files = resp.data.sort((a, b) => {
        if (a.type === 'directory' && b.type !== 'directory') return -1
        else if (a.type !== 'directory' && b.type === 'directory') return 1
        else return a.name.localeCompare(b.name)
      })

      const filesWithPath = files.map(file => ({
        ...file,
        path: parents.find(d => d.id === file.dir_id).path
      }))
      this.props.fetchSuccess(filesWithPath)
    } catch (e) {
      /* 
      If the error is a cancelable promise, don't use setState sinnce the 
      component is not mounted anymore
      */
      if (e.isCanceled !== true) {
        this.setState({ error: e })
        this.props.fetchFailure(e)
      }
    }
  }

  componentDidMount() {
    if (!this.props.params.folderId) {
      this.fetchSharedDocuments()
    } else {
      this.props.onFolderOpen(this.props.params.folderId, false)
    }
  }
  componentWillUnmount() {
    //In order to not dispatch fetch sucess, we cancel the promises
    if (this.fetchSharedFiles) this.fetchSharedFiles.cancel()
    if (this.fetSharedParents) this.fetSharedParents.cancel()
  }


  async componentDidUpdate(prevProps) {
    const { sharedDocuments } = this.props

    const hasNewSharings =
      sharedDocuments.length !== prevProps.sharedDocuments.length ||
      sharedDocuments.find(id => !prevProps.sharedDocuments.includes(id)) !==
        undefined
    /**
     * Il faut faire un fetchSharedDocument pour afficher les sharings une première fois. C'est fait dans le didMount() 
     * mais si on se balade jusqu'à la racine du Sharings via le breadcrumb (ie on a commencé dans un sous-dossier) alors 
     * on ne dispatch jamais le fetch_sharings et donc on réucpère jamais les sharings de la racine 
     */
    if (hasNewSharings) {
      this.fetchSharedDocuments()<commit_msg>comments<commit_after>import React from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { Empty } from 'cozy-ui/react'

import Container from 'drive/web/modules/drive/Container'
import {
  FETCH_SHARINGS,
  FETCH_SHARINGS_SUCCESS,
  FETCH_SHARINGS_FAILURE
} from 'drive/web/modules/navigation/duck/actions'
import SharedDocuments from 'sharing/components/SharedDocuments'

import { makeCancelable } from 'lib/promise'

import { openFolder, getFolderUrl } from 'drive/web/modules/navigation/duck'

export class SharingFetcher extends React.Component {
  state = {
    error: null
  }

  static contextTypes = {
    client: PropTypes.object.isRequired,
    t: PropTypes.func.isRequired
  }
  constructor(props) {
    super(props)
    this.fetchSharedFiles = null
    this.fetSharedParents = null
  }
  async fetchSharedDocuments() {
    const { sharedDocuments } = this.props
    const { client } = this.context

    try {
      this.props.startFetch()
      this.setState({ error: null })

      this.fetchSharedFiles = makeCancelable(
        client.collection('io.cozy.files').all({ keys: sharedDocuments })
      )

      const resp = await this.fetchSharedFiles.promise
      const parentIds = resp.data.map(f => f.dir_id)
      this.fetSharedParents = makeCancelable(
        client.collection('io.cozy.files').all({
          keys: parentIds
        })
      )
      const parentsResp = await this.fetSharedParents.promise
      const parents = parentsResp.data
      const files = resp.data.sort((a, b) => {
        if (a.type === 'directory' && b.type !== 'directory') return -1
        else if (a.type !== 'directory' && b.type === 'directory') return 1
        else return a.name.localeCompare(b.name)
      })

      const filesWithPath = files.map(file => ({
        ...file,
        path: parents.find(d => d.id === file.dir_id).path
      }))
      this.props.fetchSuccess(filesWithPath)
    } catch (e) {
      /* 
      If the error is a cancelable promise, don't use setState sinnce the 
      component is not mounted anymore
      */
      if (e.isCanceled !== true) {
        this.setState({ error: e })
        this.props.fetchFailure(e)
      }
    }
  }

  componentDidMount() {
    if (!this.props.params && !this.props.params.folderId) {
      this.fetchSharedDocuments()
    } else {
      this.props.onFolderOpen(this.props.params.folderId, false)
    }
  }
  componentWillUnmount() {
    //In order to not dispatch fetch sucess, we cancel the promises
    if (this.fetchSharedFiles) this.fetchSharedFiles.cancel()
    if (this.fetSharedParents) this.fetSharedParents.cancel()
  }


  async componentDidUpdate(prevProps) {
    const { sharedDocuments } = this.props

    const hasNewSharings =
      sharedDocuments.length !== prevProps.sharedDocuments.length ||
      sharedDocuments.find(id => !prevProps.sharedDocuments.includes(id)) !==
        undefined
    /**
     * Il faut faire un fetchSharedDocument pour afficher les sharings une première fois. C'est fait dans le didMount() 
     * mais si on se balade jusqu'à la racine du Sharings via le breadcrumb (ie on a commencé dans un sous-dossier) alors 
     * on ne dispatch jamais le fetch_sharings et donc on réucpère jamais les sharings de la racine 
     */
    if (hasNewSharings) {
      this.fetchSharedDocuments()
{'fraction_non_alphanumeric': 0.10834707028323093, 'fraction_numerical': 0.0005994305409860632, 'mean_word_length': 2.914369501466276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '6339120', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1989, 'n_words': 496}