<commit_before>import React from 'react';

import './header.scss';

const Header = React.createClass({

  getInitialState: function() {
    return {
      mods: {
        subject: [],
        originInfo: {}
      }
    };
  },

  render: function() {
    var uuid = this.props.item.uuid;

    var subject = this.state.mods.subject;
    if (!Array.isArray(subject)) {
      subject = [subject];
    }

    var originInfo = this.state.mods.originInfo;
    if (!Array.isArray(originInfo)) {
      originInfo = [originInfo];
    }

    var date = originInfo.filter(o => o.dateCreated || o.dateIssued || o.dateOther)
      .map(o => o.dateCreated || o.dateIssued || o.dateOther)
      .filter(o => o.keyDate)
      .map(o => o['$'])
      .sort(function(a, b) {
        return b.length - a.length;
      });

    var geographic = subject.filter(s => s && s.geographic && s.geographic['$'])
      .map(s => s.geographic['$'])
      .sort(function(a, b) {
        return b.length - a.length;
      });

    var collectionHeader;
    if (this.props.collections[this.props.item.collection]) {
      collectionHeader = (
        <h2>
          <span className='header-text'>
            From <a href={`http://digitalcollections.nypl.org/items/${this.props.item.collection}`} target='_black'>{this.props.collections[this.props.item.collection].title.trim()}</a>
          </span>
        </h2>
      );
    }

    var geoDateHeader;
    if (geographic[0] || date[0]) {
      var spans = [];

      if (geographic[0]) {
        spans.push({
          key: 'Location',
          value: geographic[0]
        });
      }

      if (date[0]) {
        spans.push({
          key: 'Date',
          value: date[0]
        });
      }

      geoDateHeader = (
        <div>
          <span className='header-text'>
            {spans.map(function(span, i) {
              return <span key={i}>{i ? span.key.toLowerCase() : span.key}: {span.value}{i < (spans.length - 1) ? ', ' : ''}</span>;
            })}
          </span>
        </div>
      );
    }

    return (
      <header>
        <h1>
          <span className='header-text'>{this.props.item.title}</span>
        </h1>
        {collectionHeader}
        {geoDateHeader}
        <span className='header-text light'>
          <a href={`http://digitalcollections.nypl.org/items/${uuid}`} target='_black'>Open item in Digital Collections</a>
        </span>
      </header>
    );
  },

  componentDidMount: function() {
    this.fetchMods(this.props.item.uuid);
  },

  componentDidUpdate: function(prevProps) {
    var prevUuid = prevProps.item.uuid;
    var uuid = this.props.item.uuid;

    if (prevUuid !== uuid) {
      this.fetchMods(uuid);
    }
  },

  fetchMods: function(uuid) {
    fetch(`${this.props.api.url}items/${uuid}/mods`)
    .then(function(response) {
      return response.json();
    }).then(json => {
      this.setState({
        mods: json
      });
    }).catch(function(err) {
      console.error(`Error fetching MODS for ${uuid}`, err);
    });
  }

});

export default Header;<commit_msg>don't set state after unmounting - closes #18<commit_after>import React from 'react';

import './header.scss';

const Header = React.createClass({

  getInitialState: function() {
    return {
      mods: {
        subject: [],
        originInfo: {}
      }
    };
  },

  render: function() {
    var uuid = this.props.item.uuid;

    var subject = this.state.mods.subject;
    if (!Array.isArray(subject)) {
      subject = [subject];
    }

    var originInfo = this.state.mods.originInfo;
    if (!Array.isArray(originInfo)) {
      originInfo = [originInfo];
    }

    var date = originInfo.filter(o => o.dateCreated || o.dateIssued || o.dateOther)
      .map(o => o.dateCreated || o.dateIssued || o.dateOther)
      .filter(o => o.keyDate)
      .map(o => o['$'])
      .sort(function(a, b) {
        return b.length - a.length;
      });

    var geographic = subject.filter(s => s && s.geographic && s.geographic['$'])
      .map(s => s.geographic['$'])
      .sort(function(a, b) {
        return b.length - a.length;
      });

    var collectionHeader;
    if (this.props.collections[this.props.item.collection]) {
      collectionHeader = (
        <h2>
          <span className='header-text'>
            From <a href={`http://digitalcollections.nypl.org/items/${this.props.item.collection}`} target='_black'>{this.props.collections[this.props.item.collection].title.trim()}</a>
          </span>
        </h2>
      );
    }

    var geoDateHeader;
    if (geographic[0] || date[0]) {
      var spans = [];

      if (geographic[0]) {
        spans.push({
          key: 'Location',
          value: geographic[0]
        });
      }

      if (date[0]) {
        spans.push({
          key: 'Date',
          value: date[0]
        });
      }

      geoDateHeader = (
        <div>
          <span className='header-text'>
            {spans.map(function(span, i) {
              return <span key={i}>{i ? span.key.toLowerCase() : span.key}: {span.value}{i < (spans.length - 1) ? ', ' : ''}</span>;
            })}
          </span>
        </div>
      );
    }

    return (
      <header>
        <h1>
          <span className='header-text'>{this.props.item.title}</span>
        </h1>
        {collectionHeader}
        {geoDateHeader}
        <span className='header-text light'>
          <a href={`http://digitalcollections.nypl.org/items/${uuid}`} target='_black'>Open item in Digital Collections</a>
        </span>
      </header>
    );
  },

  componentDidMount: function() {
    this.fetchMods(this.props.item.uuid);
  },

  componentWillUnmount: function() {
    this.unmounted = true;
  },

  componentDidUpdate: function(prevProps) {
    var prevUuid = prevProps.item.uuid;
    var uuid = this.props.item.uuid;

    if (prevUuid !== uuid) {
      this.fetchMods(uuid);
    }
  },

  fetchMods: function(uuid) {
    fetch(`${this.props.api.url}items/${uuid}/mods`)
      .then(function(response) {
        return response.json();
      }).then(json => {
        if (!this.unmounted) {
          this.setState({
            mods: json
          });
        }
      }).catch(function(err) {
        console.error(`Error fetching MODS for ${uuid}`, err);
      });
  }

});

export default Header;
{'fraction_non_alphanumeric': 0.17135590526148467, 'fraction_numerical': 0.003814973772055317, 'mean_word_length': 2.035214664737096, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 61, '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': '3676677', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2045, 'n_words': 357}