<commit_before>/**
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React, { Component } from "react";
import {
  getStatsVarInfo,
  getPlaceNames,
  getStatsVar,
  getTimelineParamsFromUrl,
  StatsVarNode,
} from "./timeline_util";
import { SearchBar } from "./timeline_search";
import { Menu } from "./statsvar_menu";
import { StatsVarInfo, TimelineParams } from "./timeline_util";
import { Info } from "./timeline_info";
import { ChartRegion } from "./timeline_chart_region";
import {
  NoopStatsVarFilter,
  TimelineStatsVarFilter,
  StatsVarFilterInterface,
} from "./commons";

interface PageStateType {
  statsVarNodes: StatsVarNode;
  statsVarInfo: { [key: string]: StatsVarInfo };
  placeIdNames: Record<string, string>; // [(placeId, placeName)]
  statsVarTitle: Record<string, string>;
  perCapita: boolean;
  statsVarValid: Set<string>;
}

class Page extends Component<Record<string, unknown>, PageStateType> {
  params: TimelineParams;

  constructor(props: Record<string, unknown>) {
    super(props);
    this.params = getTimelineParamsFromUrl();
    // set default statsVarTitle as the statsVar dcids
    const statsVarTitle = {};
    for (const statsVar of this.params.getStatsVarDcids()) {
      statsVarTitle[statsVar] = statsVar;
    }
    this.state = {
      placeIdNames: {},
      statsVarValid: new Set(),
      statsVarNodes: this.params.statsVarNodes,
      statsVarInfo: {},
      statsVarTitle: statsVarTitle,
      perCapita: this.params.pc,
    };
  }

  componentDidMount(): void {
    // Initial rendering has emmpty state. This proactively parse the url hash
    // and re-render.
    let statsVarInfoPromise = Promise.resolve({});
    if (this.params.getStatsVarDcids().length !== 0) {
      statsVarInfoPromise = getStatsVarInfo(this.params.getStatsVarDcids());
    }
    let placesPromise = Promise.resolve({});
    let validStatsVarPromise = Promise.resolve(new Set<string>());
    if (this.params.placeDcids.length !== 0) {
      placesPromise = getPlaceNames(this.params.placeDcids);
      validStatsVarPromise = getStatsVar(this.params.placeDcids);
    }

    Promise.all([
      statsVarInfoPromise,
      placesPromise,
      validStatsVarPromise,
    ]).then(([statsVarInfo, placeIdNames, statsVarValid]) => {
      this.setState({
        statsVarInfo: statsVarInfo,
        placeIdNames: placeIdNames,
        statsVarValid: statsVarValid,
      });
    });
  }

  // add one statsVar with nodePath
  private addStatsVar(statsVar: string, nodePath: string[]): void {
    if (this.params.addStatsVar(statsVar, nodePath)) {
      getStatsVarInfo(this.params.getStatsVarDcids()).then((data) => {
        this.setState({
          statsVarInfo: data,
          statsVarNodes: this.params.statsVarNodes,
        });
      });
    }
  }

  // remove one statsVar with nodePath
  private removeStatsVar(statsVar: string, nodePath: string[] = []): void {
    if (this.params.removeStatsVar(statsVar, nodePath)) {
      const tempStatsVarInfo = this.state.statsVarInfo;
      if (!(statsVar in this.params.statsVarNodes)) {
        delete tempStatsVarInfo[statsVar];
      }
      this.setState({
        statsVarNodes: this.params.statsVarNodes,
        statsVarInfo: tempStatsVarInfo,
      });
    }
  }

  // add one place
  private addPlace(place: string): void {
    if (this.params.addPlace(place)) {
      const placesPromise = getPlaceNames(this.params.placeDcids);
      const validStatsVarPromise = getStatsVar(this.params.placeDcids);
      Promise.all([placesPromise, validStatsVarPromise]).then((values) => {
        this.setState({
          placeIdNames: values[0],
          statsVarValid: values[1],<commit_msg>fix undefined title when statsVar are selected from checkbox (#259)<commit_after>/**
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React, { Component } from "react";
import {
  getStatsVarInfo,
  getPlaceNames,
  getStatsVar,
  getTimelineParamsFromUrl,
  StatsVarNode,
} from "./timeline_util";
import { SearchBar } from "./timeline_search";
import { Menu } from "./statsvar_menu";
import { StatsVarInfo, TimelineParams } from "./timeline_util";
import { Info } from "./timeline_info";
import { ChartRegion } from "./timeline_chart_region";
import {
  NoopStatsVarFilter,
  TimelineStatsVarFilter,
  StatsVarFilterInterface,
} from "./commons";
import _ from "lodash";

interface PageStateType {
  statsVarNodes: StatsVarNode;
  statsVarInfo: { [key: string]: StatsVarInfo };
  placeIdNames: Record<string, string>; // [(placeId, placeName)]
  statsVarTitle: Record<string, string>;
  perCapita: boolean;
  statsVarValid: Set<string>;
}

class Page extends Component<Record<string, unknown>, PageStateType> {
  params: TimelineParams;

  constructor(props: Record<string, unknown>) {
    super(props);
    this.params = getTimelineParamsFromUrl();
    // set default statsVarTitle as the statsVar dcids
    const statsVarTitle = {};
    for (const statsVar of this.params.getStatsVarDcids()) {
      statsVarTitle[statsVar] = statsVar;
    }
    this.state = {
      placeIdNames: {},
      statsVarValid: new Set(),
      statsVarNodes: _.cloneDeep(this.params.statsVarNodes),
      statsVarInfo: {},
      statsVarTitle: statsVarTitle,
      perCapita: this.params.pc,
    };
  }

  componentDidMount(): void {
    // Initial rendering has emmpty state. This proactively parse the url hash
    // and re-render.
    let statsVarInfoPromise = Promise.resolve({});
    if (this.params.getStatsVarDcids().length !== 0) {
      statsVarInfoPromise = getStatsVarInfo(this.params.getStatsVarDcids());
    }
    let placesPromise = Promise.resolve({});
    let validStatsVarPromise = Promise.resolve(new Set<string>());
    if (this.params.placeDcids.length !== 0) {
      placesPromise = getPlaceNames(this.params.placeDcids);
      validStatsVarPromise = getStatsVar(this.params.placeDcids);
    }

    Promise.all([
      statsVarInfoPromise,
      placesPromise,
      validStatsVarPromise,
    ]).then(([statsVarInfo, placeIdNames, statsVarValid]) => {
      this.setState({
        statsVarInfo: statsVarInfo,
        placeIdNames: placeIdNames,
        statsVarValid: statsVarValid,
      });
    });
  }

  // add one statsVar with nodePath
  private addStatsVar(statsVar: string, nodePath: string[]): void {
    if (this.params.addStatsVar(statsVar, nodePath)) {
      const statsVarNode = _.cloneDeep(this.params.statsVarNodes);
      getStatsVarInfo(this.params.getStatsVarDcids()).then((data) => {
        this.setState({
          statsVarInfo: data,
          statsVarNodes: statsVarNode,
        });
      });
    }
  }

  // remove one statsVar with nodePath
  private removeStatsVar(statsVar: string, nodePath: string[] = []): void {
    if (this.params.removeStatsVar(statsVar, nodePath)) {
      const tempStatsVarInfo = this.state.statsVarInfo;
      if (!(statsVar in this.params.statsVarNodes)) {
        delete tempStatsVarInfo[statsVar];
      }
      this.setState({
        statsVarNodes: this.params.statsVarNodes,
        statsVarInfo: tempStatsVarInfo,
      });
    }
  }

  // add one place
  private addPlace(place: string): void {
    if (this.params.addPlace(place)) {
      const placesPromise = getPlaceNames(this.params.placeDcids);
      const validStatsVarPromise = getStatsVar(this.params.placeDcids);
      Promise.all([placesPromise, validStatsVarPromise]).then((values) => {
        this.setState({
          placeIdNames: values[0],
          statsVarValid: values[1],
{'fraction_non_alphanumeric': 0.1126005361930295, 'fraction_numerical': 0.0031472199557057933, 'mean_word_length': 3.48744769874477, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '107250', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2484, 'n_words': 626}