<commit_before>import * as React from 'react'
import { IMenu, ISubmenuItem } from '../../models/app-menu'
import { AppMenuBarButton } from './app-menu-bar-button'
import { Dispatcher } from '../../lib/dispatcher'
import { FoldoutType } from '../../lib/app-state'

interface IAppMenuBarProps {
  readonly appMenu: ReadonlyArray<IMenu>
  readonly dispatcher: Dispatcher
  readonly highlightAppMenuToolbarButton: boolean
  readonly foldoutState: { type: FoldoutType.AppMenu, enableAccessKeyNavigation: boolean, openedWithAccessKey?: boolean } | null
}

interface IAppMenuBarState {
  readonly menuItems: ReadonlyArray<ISubmenuItem>
}

/**
 * A Windows-style application menu bar which renders in the title
 * bar section of the app and utilizes foldouts for displaying interactive
 * menus.
 */
export class AppMenuBar extends React.Component<IAppMenuBarProps, IAppMenuBarState> {

  public constructor(props: IAppMenuBarProps) {
    super(props)
    this.state = {
      menuItems: this.buildMenuItems(props.appMenu),
    }
  }

  public componentWillReceiveProps(nextProps: IAppMenuBarProps) {
    this.setState({ menuItems: this.buildMenuItems(nextProps.appMenu) })

    // If the app menu foldout is open but...
    if (nextProps.foldoutState) {
      // ...only the root menu is open
      if (nextProps.appMenu.length <= 1) {
        // Let's make sure to close the foldout
        this.props.dispatcher.closeFoldout()
      }
    }
  }

  /**
   * Build a list of top-level menu items which have child menus of their
   * own (ie submenu items).
   */
  private buildMenuItems(appMenu: ReadonlyArray<IMenu>): ReadonlyArray<ISubmenuItem> {
    if (!this.props.appMenu.length) {
      return []
    }

    const topLevelMenu = this.props.appMenu[0]
    const items = topLevelMenu.items

    const submenuItems = new Array<ISubmenuItem>()

    for (const item of items) {
      if (item.type === 'submenuItem') {
        submenuItems.push(item)
      }
    }

    return submenuItems
  }
<commit_msg>:art: Extract creating state<commit_after>import * as React from 'react'
import { IMenu, ISubmenuItem } from '../../models/app-menu'
import { AppMenuBarButton } from './app-menu-bar-button'
import { Dispatcher } from '../../lib/dispatcher'
import { FoldoutType } from '../../lib/app-state'

interface IAppMenuBarProps {
  readonly appMenu: ReadonlyArray<IMenu>
  readonly dispatcher: Dispatcher
  readonly highlightAppMenuToolbarButton: boolean
  readonly foldoutState: { type: FoldoutType.AppMenu, enableAccessKeyNavigation: boolean, openedWithAccessKey?: boolean } | null
}

interface IAppMenuBarState {
  readonly menuItems: ReadonlyArray<ISubmenuItem>
}

/**
 * Creates menu bar state given props. This is intentionally not
 * an instance member in order to avoid mistakenly using any other
 * input data or state than the received props.
 * 
 * The state consists of a list of top-level menu items which have
 * child menus of their own (ie submenu items).
 */
function createState(props: IAppMenuBarProps): IAppMenuBarState {
    if (!props.appMenu.length) {
      return { menuItems: [ ] }
    }

    const topLevelMenu = props.appMenu[0]
    const items = topLevelMenu.items

    const menuItems = new Array<ISubmenuItem>()

    for (const item of items) {
      if (item.type === 'submenuItem') {
        menuItems.push(item)
      }
    }

    return { menuItems }
}

/**
 * A Windows-style application menu bar which renders in the title
 * bar section of the app and utilizes foldouts for displaying interactive
 * menus.
 */
export class AppMenuBar extends React.Component<IAppMenuBarProps, IAppMenuBarState> {

  public constructor(props: IAppMenuBarProps) {
    super(props)
    this.state = createState(props)
  }

  public componentWillReceiveProps(nextProps: IAppMenuBarProps) {
    if (nextProps.appMenu !== this.props.appMenu) {
      this.setState(createState(nextProps))
    }

    // If the app menu foldout is open but...
    if (nextProps.foldoutState) {
      // ...only the root menu is open
      if (nextProps.appMenu.length <= 1) {
        // Let's make sure to close the foldout
        this.props.dispatcher.closeFoldout()
      }
    }
  }


{'fraction_non_alphanumeric': 0.1029940119760479, 'fraction_numerical': 0.0009580838323353293, 'mean_word_length': 3.3636363636363638, 'pattern_counts': {'":': 0, '<': 15, '<?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': '3087346', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1222, 'n_words': 356}