<commit_before>// @flow

import type { AppState } from '../redux-setup';
import type { NavigationState } from 'react-navigation';

import { createSelector } from 'reselect';
import invariant from 'invariant';

import { AppRouteName } from '../navigation-setup';
import { ChatRouteName } from '../chat/chat.react';
import { MessageListRouteName } from '../chat/message-list.react';
import {
  ThreadSettingsRouteName,
} from '../chat/settings/thread-settings.react';
import {
  assertNavigationRouteNotLeafNode,
  getThreadIDFromParams,
} from '../utils/navigation-utils';

const createIsForegroundSelector = (routeName: string) => createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState) =>
    navigationState.routes[navigationState.index].routeName === routeName,
);

const createActiveTabSelector = (routeName: string) => createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState) => {
    const innerState = navigationState.routes[navigationState.index];
    if (innerState.routeName !== AppRouteName) {
      return false;
    }
    const appRoute = assertNavigationRouteNotLeafNode(innerState);
    return appRoute.routes[appRoute.index].routeName === routeName;
  },
);

const activeThreadSelector = createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState): ?string => {
    const innerState = navigationState.routes[navigationState.index];
    if (innerState.routeName !== AppRouteName) {
      return null;
    }
    const appRoute = assertNavigationRouteNotLeafNode(innerState);
    const innerInnerState = appRoute.routes[appRoute.index];
    if (innerInnerState.routeName !== ChatRouteName) {
      return null;
    }
    const chatRoute = assertNavigationRouteNotLeafNode(innerInnerState);
    const innerInnerInnerState = chatRoute.routes[chatRoute.index];
    if (
      innerInnerInnerState.routeName !== MessageListRouteName &&
      innerInnerInnerState.routeName !== ThreadSettingsRouteName
    ) {
      return null;
    }
    return getThreadIDFromParams(innerInnerInnerState);
  },
);

export {
  createIsForegroundSelector,
  createActiveTabSelector,
  activeThreadSelector,
};
<commit_msg>Memoize native nav selectors<commit_after>// @flow

import type { AppState } from '../redux-setup';
import type { NavigationState } from 'react-navigation';

import { createSelector } from 'reselect';
import invariant from 'invariant';
import _memoize from 'lodash/memoize';

import { AppRouteName } from '../navigation-setup';
import { ChatRouteName } from '../chat/chat.react';
import { MessageListRouteName } from '../chat/message-list.react';
import {
  ThreadSettingsRouteName,
} from '../chat/settings/thread-settings.react';
import {
  assertNavigationRouteNotLeafNode,
  getThreadIDFromParams,
} from '../utils/navigation-utils';

const baseCreateIsForegroundSelector = (routeName: string) => createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState) =>
    navigationState.routes[navigationState.index].routeName === routeName,
);
const createIsForegroundSelector = _memoize(baseCreateIsForegroundSelector);

const baseCreateActiveTabSelector = (routeName: string) => createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState) => {
    const innerState = navigationState.routes[navigationState.index];
    if (innerState.routeName !== AppRouteName) {
      return false;
    }
    const appRoute = assertNavigationRouteNotLeafNode(innerState);
    return appRoute.routes[appRoute.index].routeName === routeName;
  },
);
const createActiveTabSelector = _memoize(baseCreateActiveTabSelector);

const activeThreadSelector = createSelector(
  (state: AppState) => state.navInfo.navigationState,
  (navigationState: NavigationState): ?string => {
    const innerState = navigationState.routes[navigationState.index];
    if (innerState.routeName !== AppRouteName) {
      return null;
    }
    const appRoute = assertNavigationRouteNotLeafNode(innerState);
    const innerInnerState = appRoute.routes[appRoute.index];
    if (innerInnerState.routeName !== ChatRouteName) {
      return null;
    }
    const chatRoute = assertNavigationRouteNotLeafNode(innerInnerState);
    const innerInnerInnerState = chatRoute.routes[chatRoute.index];
    if (
      innerInnerInnerState.routeName !== MessageListRouteName &&
      innerInnerInnerState.routeName !== ThreadSettingsRouteName
    ) {
      return null;
    }
    return getThreadIDFromParams(innerInnerInnerState);
  },
);

export {
  createIsForegroundSelector,
  createActiveTabSelector,
  activeThreadSelector,
};

{'fraction_non_alphanumeric': 0.11158072696534235, 'fraction_numerical': 0.0, 'mean_word_length': 4.923654568210263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '952169', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1307, 'n_words': 261}