<commit_before>import { h, Component } from 'preact';
import Tracker from '../tracker';
import Icon from './icon';

interface Props {
  src?: string;
  sentence?: string;
  vote?: string;
  onVote?(valid: boolean): void;
  onDelete?(): void;
}

interface State {
  loaded: boolean;
  played: boolean;
  playing: boolean;
  audio: HTMLAudioElement;
}

/**
 * Widget for listening to a recording.
 */
export default class ListenBox extends Component<Props, State> {
  el: HTMLAudioElement;
  tracker: Tracker;

  constructor(props: Props) {
    super(props);

    this.tracker = new Tracker();

    // Pre-bind some handlers to avoid memory leaks later.
    this.onLoadStart = this.onLoadStart.bind(this);
    this.onCanPlayThrough = this.onCanPlayThrough.bind(this);
    this.onPlayEnded = this.onPlayEnded.bind(this);
    this.onPlay = this.onPlay.bind(this);
    this.onDelete = this.onDelete.bind(this);
    this.voteYes = this.voteYes.bind(this);
    this.voteNo = this.voteNo.bind(this);
  }

  state = {
    loaded: false,
    playing: false,
    played: false,
    audio: null
  };

  private onLoadStart() {<commit_msg>Reset ListenBox's state if props.sentence has changed (#422)<commit_after>import { h, Component } from 'preact';
import Tracker from '../tracker';
import Icon from './icon';

interface Props {
  src?: string;
  sentence?: string;
  vote?: string;
  onVote?(valid: boolean): void;
  onDelete?(): void;
}

interface State {
  loaded: boolean;
  played: boolean;
  playing: boolean;
  audio: HTMLAudioElement;
}

/**
 * Widget for listening to a recording.
 */
export default class ListenBox extends Component<Props, State> {
  el: HTMLAudioElement;
  tracker: Tracker;

  constructor(props: Props) {
    super(props);

    this.tracker = new Tracker();

    // Pre-bind some handlers to avoid memory leaks later.
    this.onLoadStart = this.onLoadStart.bind(this);
    this.onCanPlayThrough = this.onCanPlayThrough.bind(this);
    this.onPlayEnded = this.onPlayEnded.bind(this);
    this.onPlay = this.onPlay.bind(this);
    this.onDelete = this.onDelete.bind(this);
    this.voteYes = this.voteYes.bind(this);
    this.voteNo = this.voteNo.bind(this);
  }

  state = {
    loaded: false,
    playing: false,
    played: false,
    audio: null
  };

  private resetState() {
    this.setState({
      loaded: false,
      playing: false,
      played: false,
      audio: null
    });
  }

  componentWillReceiveProps(nextProps: Props) {
    if (nextProps.sentence !== this.props.sentence) {
      this.resetState();
    }
  }

  private onLoadStart() {
{'fraction_non_alphanumeric': 0.12999611951882034, 'fraction_numerical': 0.0011641443538998836, 'mean_word_length': 3.3620981387478848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3614461', 'n_tokens_mistral': 853, 'n_tokens_neox': 828, 'n_words': 208}