<commit_before>import PropTypes from 'prop-types';
import React from 'react';
import { IndexLink, Link } from 'react-router';
import Translate from 'react-translate-component';
import apiClient from 'panoptes-client/lib/api-client';
import classNames from 'classnames';
import counterpart from 'counterpart';
import { Helmet } from 'react-helmet';
import Loading from '../components/loading-indicator';

counterpart.registerTranslations('en', {
  collectionPage: {
    settings: 'Collection Settings',
    collaborators: 'Collaborators',
    collectionsLink: '%(user)s\'s Collections',
    favoritesLink: '%(user)s\'s Favorites'
  },
  collectionsPageWrapper: {
    error: 'There was an error retrieving this collection.'
  },
  loading: '(Loading)'
});

class CollectionPage extends React.Component {
  static propTypes = {
    user: PropTypes.object,
    collection: PropTypes.object.isRequired,
    project: PropTypes.object,
    children: PropTypes.node,
    roles: PropTypes.array
  };

  static contextTypes = {
    geordi: PropTypes.object
  };

  static defaultProps = {
    collection: null
  };

  state = {
    canCollaborate: false
  };

  componentDidMount() {
    this.canCollaborate();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (!!nextContext.geordi && !!nextContext.geordi.makeHandler) {
      this.logClick = nextContext.geordi.makeHandler('about-menu');
    }
  }

  canCollaborate = () => {
    let canCollaborate;
    if (!this.props.user) {
      canCollaborate = false;
    } else {
      canCollaborate = this.props.roles.some((role) => {
        const idMatch = (role.links.owner.id === this.props.user.id);
        const isCollaborator = role.roles.includes('collaborator');

        return isCollaborator && idMatch;
      });
    }
    const isOwner = this.props.user.id === this.props.owner.id;

    this.setState({ canCollaborate: canCollaborate || isOwner });
  };

  render() {
    const title = `${this.props.collection.display_name} (${this.props.collection.links.subjects ? this.props.collection.links.subjects.length : 0})`;
    const baseType = this.props.collection.favorite ? 'favorites' : 'collections';
    let baseLink = '';
    if (!!this.props.project) {
      baseLink = `/projects/${this.props.project.slug}`;
    }
    const baseCollectionLink = `${baseLink}/collections/${this.props.collection.slug}`;
    const baseCollectionsLink = `${baseLink}/${baseType}/${this.props.owner.login}`;
    const profileLink = `${baseLink}/users/${this.props.owner.login}`;
    const collectionsLinkMessageKey = `collectionPage.${baseType}Link`;

    let userRole = [];
    if (!!this.props.user) {
      userRole = this.props.roles.filter((collectionRole) => {
        return collectionRole.links.owner.id === this.props.user.id;
      });
    }<commit_msg>Fix type error on collections page (#4685)<commit_after>import PropTypes from 'prop-types';
import React from 'react';
import { IndexLink, Link } from 'react-router';
import Translate from 'react-translate-component';
import apiClient from 'panoptes-client/lib/api-client';
import classNames from 'classnames';
import counterpart from 'counterpart';
import { Helmet } from 'react-helmet';
import Loading from '../components/loading-indicator';

counterpart.registerTranslations('en', {
  collectionPage: {
    settings: 'Collection Settings',
    collaborators: 'Collaborators',
    collectionsLink: '%(user)s\'s Collections',
    favoritesLink: '%(user)s\'s Favorites'
  },
  collectionsPageWrapper: {
    error: 'There was an error retrieving this collection.'
  },
  loading: '(Loading)'
});

class CollectionPage extends React.Component {
  static propTypes = {
    user: PropTypes.object,
    collection: PropTypes.object.isRequired,
    project: PropTypes.object,
    children: PropTypes.node,
    roles: PropTypes.array
  };

  static contextTypes = {
    geordi: PropTypes.object
  };

  static defaultProps = {
    collection: null
  };

  state = {
    canCollaborate: false
  };

  componentDidMount() {
    this.canCollaborate();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (!!nextContext.geordi && !!nextContext.geordi.makeHandler) {
      this.logClick = nextContext.geordi.makeHandler('about-menu');
    }
  }

  canCollaborate() {
    const { owner, roles, user } = this.props;
    let canCollaborate = false;
    let isOwner = false;
    if (user && user.id) {
      canCollaborate = roles.some((role) => {
        const idMatch = (role.links.owner.id === user.id);
        const isCollaborator = role.roles.includes('collaborator');
        return isCollaborator && idMatch;
      });
      isOwner = user.id === owner.id;
    }

    this.setState({ canCollaborate: canCollaborate || isOwner });
  }

  render() {
    const title = `${this.props.collection.display_name} (${this.props.collection.links.subjects ? this.props.collection.links.subjects.length : 0})`;
    const baseType = this.props.collection.favorite ? 'favorites' : 'collections';
    let baseLink = '';
    if (!!this.props.project) {
      baseLink = `/projects/${this.props.project.slug}`;
    }
    const baseCollectionLink = `${baseLink}/collections/${this.props.collection.slug}`;
    const baseCollectionsLink = `${baseLink}/${baseType}/${this.props.owner.login}`;
    const profileLink = `${baseLink}/users/${this.props.owner.login}`;
    const collectionsLinkMessageKey = `collectionPage.${baseType}Link`;

    let userRole = [];
    if (!!this.props.user) {
      userRole = this.props.roles.filter((collectionRole) => {
        return collectionRole.links.owner.id === this.props.user.id;
      });
    }
{'fraction_non_alphanumeric': 0.14321206743566992, 'fraction_numerical': 0.001064773735581189, 'mean_word_length': 3.6348684210526314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '5595891', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1736, 'n_words': 340}