<commit_before>// @flow
import React, { Component } from 'react';
import { type Store } from 'redux';
import {
  localizeReducer,
  getActiveLanguage,
  getOptions,
  getTranslationsForActiveLanguage,
  type LocalizeState,
  type Action
} from './localize';
import {
  LocalizeContext,
  type LocalizeContextProps,
  getContextPropsFromState
} from './LocalizeContext';
import { storeDidChange } from './utils';

type LocalizeProviderState = {
  localize: LocalizeState
};

export type LocalizeProviderProps = {
  store?: Store<any, any>,
  children: any
};

export class LocalizeProvider extends Component<
  LocalizeProviderProps,
  LocalizeProviderState
> {
  unsubscribeFromStore: Function;
  getContextPropsSelector: any;
  contextProps: LocalizeContextProps;

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

    const dispatch = this.props.store
      ? this.props.store.dispatch
      : this.dispatch.bind(this);

    this.getContextPropsSelector = getContextPropsFromState(dispatch);

    this.state = {
      localize: localizeReducer(undefined, ({}: any))
    };
  }

  static getDerivedStateFromProps(nextProps: any, nextState: any) {
    return null;
  }

  componentDidMount() {
    if (this.props.store) {
      this.unsubscribeFromStore = storeDidChange(
        this.props.store,
        this.onStateDidChange.bind(this)
      );
    }
  }

  componentWillUnmount() {
    this.unsubscribeFromStore && this.unsubscribeFromStore();
  }

  onStateDidChange(prevState: LocalizeProviderState) {
    if (!this.props.store) {
      return;
    }

    const prevLocalizeState = prevState && prevState.localize;
    const curLocalizeState = this.props.store.getState().localize;

    const prevActiveLanguage =
      prevState && getActiveLanguage(prevLocalizeState);
    const curActiveLanguage = getActiveLanguage(curLocalizeState);

    const prevOptions = prevState && getOptions(prevLocalizeState);
    const curOptions = getOptions(curLocalizeState);

    const prevTranslations =
      prevState && getTranslationsForActiveLanguage(prevLocalizeState);
    const curTranslations = getTranslationsForActiveLanguage(curLocalizeState);

    const hasActiveLangaugeChanged =
      (prevActiveLanguage && prevActiveLanguage.code) !==
      (curActiveLanguage && curActiveLanguage.code);
    const hasOptionsChanged = prevOptions !== curOptions;
    const hasTranslationsChanged = prevTranslations !== curTranslations;

    if (
      hasActiveLangaugeChanged ||
      hasOptionsChanged ||
      hasTranslationsChanged
    ) {
      this.setState({ localize: curLocalizeState });
    }
  }

  dispatch(action: any) {
    console.log('action', action);

    this.setState(prevState => {
      return {
        localize: localizeReducer(prevState.localize, action)
      };
    });
  }

  render() {
    this.contextProps = this.getContextPropsSelector(this.state.localize);

    return (
      <LocalizeContext.Provider value={this.contextProps}>
        {this.props.children}
      </LocalizeContext.Provider>
    );
  }
}<commit_msg>remove stray console.log<commit_after>// @flow
import React, { Component } from 'react';
import { type Store } from 'redux';
import {
  localizeReducer,
  getActiveLanguage,
  getOptions,
  getTranslationsForActiveLanguage,
  type LocalizeState,
  type Action
} from './localize';
import {
  LocalizeContext,
  type LocalizeContextProps,
  getContextPropsFromState
} from './LocalizeContext';
import { storeDidChange } from './utils';

type LocalizeProviderState = {
  localize: LocalizeState
};

export type LocalizeProviderProps = {
  store?: Store<any, any>,
  children: any
};

export class LocalizeProvider extends Component<
  LocalizeProviderProps,
  LocalizeProviderState
> {
  unsubscribeFromStore: Function;
  getContextPropsSelector: any;
  contextProps: LocalizeContextProps;

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

    const dispatch = this.props.store
      ? this.props.store.dispatch
      : this.dispatch.bind(this);

    this.getContextPropsSelector = getContextPropsFromState(dispatch);

    this.state = {
      localize: localizeReducer(undefined, ({}: any))
    };
  }

  static getDerivedStateFromProps(nextProps: any, nextState: any) {
    return null;
  }

  componentDidMount() {
    if (this.props.store) {
      this.unsubscribeFromStore = storeDidChange(
        this.props.store,
        this.onStateDidChange.bind(this)
      );
    }
  }

  componentWillUnmount() {
    this.unsubscribeFromStore && this.unsubscribeFromStore();
  }

  onStateDidChange(prevState: LocalizeProviderState) {
    if (!this.props.store) {
      return;
    }

    const prevLocalizeState = prevState && prevState.localize;
    const curLocalizeState = this.props.store.getState().localize;

    const prevActiveLanguage =
      prevState && getActiveLanguage(prevLocalizeState);
    const curActiveLanguage = getActiveLanguage(curLocalizeState);

    const prevOptions = prevState && getOptions(prevLocalizeState);
    const curOptions = getOptions(curLocalizeState);

    const prevTranslations =
      prevState && getTranslationsForActiveLanguage(prevLocalizeState);
    const curTranslations = getTranslationsForActiveLanguage(curLocalizeState);

    const hasActiveLangaugeChanged =
      (prevActiveLanguage && prevActiveLanguage.code) !==
      (curActiveLanguage && curActiveLanguage.code);
    const hasOptionsChanged = prevOptions !== curOptions;
    const hasTranslationsChanged = prevTranslations !== curTranslations;

    if (
      hasActiveLangaugeChanged ||
      hasOptionsChanged ||
      hasTranslationsChanged
    ) {
      this.setState({ localize: curLocalizeState });
    }
  }

  dispatch(action: any) {
    this.setState(prevState => {
      return {
        localize: localizeReducer(prevState.localize, action)
      };
    });
  }

  render() {
    this.contextProps = this.getContextPropsSelector(this.state.localize);

    return (
      <LocalizeContext.Provider value={this.contextProps}>
        {this.props.children}
      </LocalizeContext.Provider>
    );
  }
}
{'fraction_non_alphanumeric': 0.09981940568051223, 'fraction_numerical': 0.0, 'mean_word_length': 3.781789638932496, 'pattern_counts': {'":': 0, '<': 11, '<?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': '6779638', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1739, 'n_words': 322}