<commit_before>import * as React from "react";

import { AppSettingsMixinProps, AppSettingsMixinState, AppSettingsMixin } from "../stores/app-settings-store";
import { CodapMixinProps, CodapMixinState, CodapMixin } from "../stores/codap-store";
import { UndoRedoUIMixin, UndoRedoUIMixinProps, UndoRedoUIMixinState } from "../stores/undo-redo-ui-store";

import { AboutView } from "./about-view";
import { SimulationRunPanelView } from "./simulation-run-panel-view";
import { Mixer } from "../mixins/components";

import { CodapConnect, CODAPDataContextListItem } from "../models/codap-connect";

interface CODAPTableMenuProps {
  toggleMenu: (override?: boolean) => void;
}

interface CODAPTableMenuState {
  dataContexts: CODAPDataContextListItem[];
}

class CODAPTableMenu extends React.Component<CODAPTableMenuProps, CODAPTableMenuState> {
  public state: CODAPTableMenuState = {
    dataContexts: []
  };

  private codapConnect: CodapConnect;

  public componentWillMount() {
    this.codapConnect = CodapConnect.instance("building-models");
    this.codapConnect.getDataContexts((dataContexts) => {
      this.setState({dataContexts});
    });
  }

  public render() {
    return (
      <div className="codap-table-menu" onMouseLeave={this.handleMouseLeave}>
        {this.state.dataContexts.map((dataContext) => {
          return (
            <div key={dataContext.id} className="codap-table-menu-item" onClick={this.handleLoadTable(dataContext)}>
              {dataContext.name}
              {dataContext.name !== this.codapConnect.dataContextName ? <i className="moonicon-icon-trash" onClick={this.handleDeleteTable(dataContext)} /> : undefined}
            </div>
          );
        })}
        <div className="codap-table-menu-item">
          <button onClick={this.handleCreateNewTable}>New Table</button>
        </div>
      </div>
    );
  }

  private handleLoadTable(dataContext: CODAPDataContextListItem) {
    return () => {
      this.codapConnect.showTable(dataContext.name);
      this.props.toggleMenu(false);
    };
  }

  private handleDeleteTable(dataContext: CODAPDataContextListItem) {
    return (e: React.MouseEvent<HTMLDivElement>) => {
      e.stopPropagation();
      if (confirm(`Are you sure you want to delete "${dataContext.name}"?`)) {
        this.codapConnect.deleteDataContext(dataContext.name);
        this.props.toggleMenu(false);
      }
    };
  }

  private handleCreateNewTable = () => {
    // trigger the same blank csv import method that CODAP uses internally
    window.parent.postMessage({
      type: "cfm::event",
      eventType: "importedData",
      eventData: {
        text: "AttributeName",
        name: "New Table"
      }
    }, "*");
    this.props.toggleMenu(false);
  }

  private handleMouseLeave = () => {
    this.props.toggleMenu(false);
  }
}<commit_msg>Changed table menu to use data context title instead of name<commit_after>import * as React from "react";

import { AppSettingsMixinProps, AppSettingsMixinState, AppSettingsMixin } from "../stores/app-settings-store";
import { CodapMixinProps, CodapMixinState, CodapMixin } from "../stores/codap-store";
import { UndoRedoUIMixin, UndoRedoUIMixinProps, UndoRedoUIMixinState } from "../stores/undo-redo-ui-store";

import { AboutView } from "./about-view";
import { SimulationRunPanelView } from "./simulation-run-panel-view";
import { Mixer } from "../mixins/components";

import { CodapConnect, CODAPDataContextListItem } from "../models/codap-connect";

interface CODAPTableMenuProps {
  toggleMenu: (override?: boolean) => void;
}

interface CODAPTableMenuState {
  dataContexts: CODAPDataContextListItem[];
}

class CODAPTableMenu extends React.Component<CODAPTableMenuProps, CODAPTableMenuState> {
  public state: CODAPTableMenuState = {
    dataContexts: []
  };

  private codapConnect: CodapConnect;

  public componentWillMount() {
    this.codapConnect = CodapConnect.instance("building-models");
    this.codapConnect.getDataContexts((dataContexts) => {
      this.setState({dataContexts});
    });
  }

  public render() {
    return (
      <div className="codap-table-menu" onMouseLeave={this.handleMouseLeave}>
        {this.state.dataContexts.map((dataContext) => {
          return (
            <div key={dataContext.id} className="codap-table-menu-item" onClick={this.handleLoadTable(dataContext)}>
              {dataContext.title}
              {dataContext.name !== this.codapConnect.dataContextName ? <i className="moonicon-icon-trash" onClick={this.handleDeleteTable(dataContext)} /> : undefined}
            </div>
          );
        })}
        <div className="codap-table-menu-item">
          <button onClick={this.handleCreateNewTable}>New Table</button>
        </div>
      </div>
    );
  }

  private handleLoadTable(dataContext: CODAPDataContextListItem) {
    return () => {
      this.codapConnect.showTable(dataContext.name);
      this.props.toggleMenu(false);
    };
  }

  private handleDeleteTable(dataContext: CODAPDataContextListItem) {
    return (e: React.MouseEvent<HTMLDivElement>) => {
      e.stopPropagation();
      if (confirm(`Are you sure you want to delete "${dataContext.title}"?`)) {
        this.codapConnect.deleteDataContext(dataContext.name);
        this.props.toggleMenu(false);
      }
    };
  }

  private handleCreateNewTable = () => {
    // trigger the same blank csv import method that CODAP uses internally
    window.parent.postMessage({
      type: "cfm::event",
      eventType: "importedData",
      eventData: {
        text: "AttributeName",
        name: "New Table"
      }
    }, "*");
    this.props.toggleMenu(false);
  }

  private handleMouseLeave = () => {
    this.props.toggleMenu(false);
  }
}
{'fraction_non_alphanumeric': 0.13193350831146106, 'fraction_numerical': 0.0, 'mean_word_length': 3.469116497263487, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, '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': '2752927', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1735, 'n_words': 304}