<commit_before>import React from 'react';
import {formatDistanceToNowStrict} from 'date-fns';
import PropTypes from 'prop-types';

class TimeSince extends React.Component {
  static propTypes = {
    date: PropTypes.any.isRequired,
  };

  componentDidMount() {
    const delay = 2600;

    this.ticker = setInterval(this.ensureValidity, delay);
  }

  componentWillUnmount() {
    if (this.ticker) {
      clearInterval(this.ticker);
      this.ticker = null;
    }
  }

  ensureValidity = () => {
    // TODO(dcramer): this should ensure we actually *need* to update the value
    this.forceUpdate();
  };

  render() {
    const date = new Date(this.props.date);

    return (
      <time dateTime={date.toISOString()}>
        {formatDistanceToNowStrict(date, {addSuffix: true})}
      </time>
    );
  }
}

export default TimeSince;<commit_msg>ref(js): Convert TimeSince to a FC (#337)<commit_after>import React, {useCallback} from 'react';
import {formatDistanceToNowStrict} from 'date-fns';
import PropTypes from 'prop-types';

const DELAY = 2600;

function TimeSince({date}) {
  const dateObject = React.useMemo(() => new Date(date), [date]);

  const getRelativeDate = useCallback(
    () => formatDistanceToNowStrict(dateObject, {addSuffix: true}),
    [dateObject]
  );

  const [relativeDate, setRelativeDate] = React.useState(getRelativeDate());
  const tickerRef = React.useRef(undefined);

  React.useEffect(() => {
    tickerRef.current = setInterval(() => setRelativeDate(getRelativeDate), DELAY);

    return () => {
      clearInterval(tickerRef.current);
      tickerRef.current = undefined;
    };
  }, [getRelativeDate]);

  return <time dateTime={dateObject.toISOString()}>{relativeDate}</time>;
}

TimeSince.propTypes = {
  date: PropTypes.any.isRequired,
};

export default TimeSince;
{'fraction_non_alphanumeric': 0.13819789939192925, 'fraction_numerical': 0.006080707573244887, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6623280', 'n_tokens_mistral': 583, 'n_tokens_neox': 562, 'n_words': 123}