<commit_before>import * as React from 'react'
import { IGitHubUser } from '../../lib/databases'
import { Commit } from '../../models/commit'
import {
  ComparisonView,
  ICompareState,
  CompareActionKind,
  ICompareBranch,
} from '../../lib/app-state'
import { CommitList } from './commit-list'
import { Repository } from '../../models/repository'
import { Branch } from '../../models/branch'
import { Dispatcher } from '../../lib/dispatcher'
import { ThrottledScheduler } from '../lib/throttled-scheduler'
import { Button } from '../lib/button'
import { BranchList } from '../branches'
import { TextBox } from '../lib/text-box'
import { IBranchListItem } from '../branches/group-branches'
import { TabBar } from '../tab-bar'
import { CompareBranchListItem } from './compare-branch-list-item'
import { FancyTextBox } from '../lib/fancy-text-box'
import { OcticonSymbol } from '../octicons'
import { SelectionSource } from '../lib/filter-list'
import { Ref } from '../lib/ref'

interface ICompareSidebarProps {
  readonly repository: Repository
  readonly compareState: ICompareState
  readonly gitHubUsers: Map<string, IGitHubUser>
  readonly emoji: Map<string, string>
  readonly commitLookup: Map<string, Commit>
  readonly localCommitSHAs: ReadonlyArray<string>
  readonly dispatcher: Dispatcher
  readonly currentBranch: Branch | null
  readonly sidebarHasFocusWithin: boolean
  readonly onRevertCommit: (commit: Commit) => void
  readonly onViewCommitOnGitHub: (sha: string) => void
}

interface ICompareSidebarState {
  /**
   * This branch should only be used when tracking interactions that the user is performing.
   *
   * For all other cases, use the prop
   */
  readonly focusedBranch: Branch | null
  readonly filterText: string
  readonly showBranchList: boolean
  readonly selectedCommit: Commit | null
}

/** If we're within this many rows from the bottom, load the next history batch. */
const CloseToBottomThreshold = 10

export class CompareSidebar extends React.Component<
  ICompareSidebarProps,
  ICompareSidebarState
> {
  private textbox: TextBox | null = null
  private readonly loadChangedFilesScheduler = new ThrottledScheduler(200)
  private branchList: BranchList | null = null

  public constructor(props: ICompareSidebarProps) {
    super(props)

    this.state = {
      focusedBranch: null,
      filterText: '',
      showBranchList: false,
      selectedCommit: null,
    }
  }

  public componentWillReceiveProps(nextProps: ICompareSidebarProps) {
    const hasFormStateChanged =
      nextProps.compareState.formState.kind !==
      this.props.compareState.formState.kind

    const newFormState = nextProps.compareState.formState

    if (hasFormStateChanged && newFormState.kind === ComparisonView.None) {
      // the comparison form should be reset to its default state
      this.setState({ filterText: '', focusedBranch: null })
      return
    }

    if (!hasFormStateChanged && newFormState.kind !== ComparisonView.None) {
      // ensure the filter text is in sync with the comparison branch
      const branch = newFormState.comparisonBranch

      this.setState({
        filterText: branch.name,
        focusedBranch: branch,
      })
    }

    if (nextProps.sidebarHasFocusWithin !== this.props.sidebarHasFocusWithin) {
      if (nextProps.sidebarHasFocusWithin === false) {
        this.setState({ showBranchList: false })
      }
    }
  }

  public componentWillMount() {
    this.props.dispatcher.initializeCompare(this.props.repository)
  }

  public componentWillUnmount() {
    this.textbox = null
  }

  public componentDidMount() {
    if (this.textbox !== null && this.state.showBranchList) {
      this.textbox.focus()
    }
  }

  public render() {
    const formState = this.props.compareState.formState
    const placeholderText =
      formState.kind === ComparisonView.None
        ? __DARWIN__
          ? 'Select Branch to Compare...'
          : 'Select branch to compare...'
        : undefined

    return (
      <div id="compare-view">
        <div className="compare-form">
          <FancyTextBox
            symbol={OcticonSymbol.gitBranch}
            type="search"
            placeholder={placeholderText}
            onFocus={this.onTextBoxFocused}
            value={this.state.filterText}
            onRef={this.onTextBoxRef}
            onValueChanged={this.onBranchFilterTextChanged}
            onKeyDown={this.onBranchFilterKeyDown}
          />
        </div>

        {this.state.showBranchList
          ? this.renderFilterList()
          : this.renderCommits()}
      </div>
    )
  }

  private onBranchesListRef = (branchList: BranchList | null) => {
    this.branchList = branchList
  }

  private renderCommits() {
    const formState = this.props.compareState.formState
    return (
      <div className="compare-commit-list">
        {formState.kind === ComparisonView.None<commit_msg>Disable textbox if there are no branches to compare<commit_after>import * as React from 'react'
import { IGitHubUser } from '../../lib/databases'
import { Commit } from '../../models/commit'
import {
  ComparisonView,
  ICompareState,
  CompareActionKind,
  ICompareBranch,
} from '../../lib/app-state'
import { CommitList } from './commit-list'
import { Repository } from '../../models/repository'
import { Branch } from '../../models/branch'
import { Dispatcher } from '../../lib/dispatcher'
import { ThrottledScheduler } from '../lib/throttled-scheduler'
import { Button } from '../lib/button'
import { BranchList } from '../branches'
import { TextBox } from '../lib/text-box'
import { IBranchListItem } from '../branches/group-branches'
import { TabBar } from '../tab-bar'
import { CompareBranchListItem } from './compare-branch-list-item'
import { FancyTextBox } from '../lib/fancy-text-box'
import { OcticonSymbol } from '../octicons'
import { SelectionSource } from '../lib/filter-list'
import { Ref } from '../lib/ref'

interface ICompareSidebarProps {
  readonly repository: Repository
  readonly compareState: ICompareState
  readonly gitHubUsers: Map<string, IGitHubUser>
  readonly emoji: Map<string, string>
  readonly commitLookup: Map<string, Commit>
  readonly localCommitSHAs: ReadonlyArray<string>
  readonly dispatcher: Dispatcher
  readonly currentBranch: Branch | null
  readonly sidebarHasFocusWithin: boolean
  readonly onRevertCommit: (commit: Commit) => void
  readonly onViewCommitOnGitHub: (sha: string) => void
}

interface ICompareSidebarState {
  /**
   * This branch should only be used when tracking interactions that the user is performing.
   *
   * For all other cases, use the prop
   */
  readonly focusedBranch: Branch | null
  readonly filterText: string
  readonly showBranchList: boolean
  readonly selectedCommit: Commit | null
}

/** If we're within this many rows from the bottom, load the next history batch. */
const CloseToBottomThreshold = 10

export class CompareSidebar extends React.Component<
  ICompareSidebarProps,
  ICompareSidebarState
> {
  private textbox: TextBox | null = null
  private readonly loadChangedFilesScheduler = new ThrottledScheduler(200)
  private branchList: BranchList | null = null

  public constructor(props: ICompareSidebarProps) {
    super(props)

    this.state = {
      focusedBranch: null,
      filterText: '',
      showBranchList: false,
      selectedCommit: null,
    }
  }

  public componentWillReceiveProps(nextProps: ICompareSidebarProps) {
    const hasFormStateChanged =
      nextProps.compareState.formState.kind !==
      this.props.compareState.formState.kind

    const newFormState = nextProps.compareState.formState

    if (hasFormStateChanged && newFormState.kind === ComparisonView.None) {
      // the comparison form should be reset to its default state
      this.setState({ filterText: '', focusedBranch: null })
      return
    }

    if (!hasFormStateChanged && newFormState.kind !== ComparisonView.None) {
      // ensure the filter text is in sync with the comparison branch
      const branch = newFormState.comparisonBranch

      this.setState({
        filterText: branch.name,
        focusedBranch: branch,
      })
    }

    if (nextProps.sidebarHasFocusWithin !== this.props.sidebarHasFocusWithin) {
      if (nextProps.sidebarHasFocusWithin === false) {
        this.setState({ showBranchList: false })
      }
    }
  }

  public componentWillMount() {
    this.props.dispatcher.initializeCompare(this.props.repository)
  }

  public componentWillUnmount() {
    this.textbox = null
  }

  public componentDidMount() {
    if (this.textbox !== null && this.state.showBranchList) {
      this.textbox.focus()
    }
  }

  public render() {
    const formState = this.props.compareState.formState
    const numBranches = this.props.compareState.allBranches.length
    const placeholderText =
      numBranches <= 1
        ? __DARWIN__
          ? 'There Are No Branches to Compare'
          : 'There are no branches to compare'
        : formState.kind === ComparisonView.None
          ? __DARWIN__
            ? 'Select Branch to Compare...'
            : 'Select branch to compare...'
          : undefined

    return (
      <div id="compare-view">
        <div className="compare-form">
          <FancyTextBox
            symbol={OcticonSymbol.gitBranch}
            type="search"
            placeholder={placeholderText}
            onFocus={this.onTextBoxFocused}
            value={this.state.filterText}
            disabled={numBranches <= 1}
            onRef={this.onTextBoxRef}
            onValueChanged={this.onBranchFilterTextChanged}
            onKeyDown={this.onBranchFilterKeyDown}
          />
        </div>

        {this.state.showBranchList
          ? this.renderFilterList()
          : this.renderCommits()}
      </div>
    )
  }

  private onBranchesListRef = (branchList: BranchList | null) => {
    this.branchList = branchList
  }

  private renderCommits() {
    const formState = this.props.compareState.formState
    return (
      <div className="compare-commit-list">
        {formState.kind === ComparisonView.None
{'fraction_non_alphanumeric': 0.10704001588720087, 'fraction_numerical': 0.0011915400655347036, 'mean_word_length': 3.0580177276390006, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '4307352', 'n_tokens_mistral': 2827, 'n_tokens_neox': 2817, 'n_words': 709}