<commit_before>import React from 'react';
import PropTypes from 'prop-types';

import { action } from '@kadira/storybook';
import { MemoryRouter, Route, matchPath } from 'react-router';


const StoryRouter = ({story, links, routerProps}) => (
  <MemoryRouter {...routerProps}>
    <Route render={({history}) => {
      const replaceLinks = (name, links, originalAction) => {
        return path => {
          for (const link in links) {
            if (matchPath(path, {path: link, exact: true})) {
              links[link]();
              return;
            }
          }
          originalAction(path);
          action(name)(path);
        };
      };

      history.push = replaceLinks('history.push', links, history.push);
      history.replace = replaceLinks('history.replace', links, history.replace);

      const replaceAction = (name, originalAction) => (
        arg => {
          originalAction(arg);
          action(name)(arg);
        }
      );

      history.go = replaceAction('history.go', history.go);
      history.goBack = replaceAction('history.goBack', history.goBack);
      history.goForward = replaceAction('history.goForward', history.goForward);

      return (
        <div>
          {story()}
        </div>
      );
    }} />
  </MemoryRouter>
);

StoryRouter.propTypes = {
  story: PropTypes.func.isRequired,
  links: PropTypes.object,
  routerProps: PropTypes.object
};

const storyRouterDecorator = (links, routerProps) => {
  const s = story => (
    <StoryRouter<commit_msg>Refactor the component rendered by the StoryRouter to avoid unnecessary renderings<commit_after>import React, { Component } from 'react';
import PropTypes from 'prop-types';

import { action } from '@kadira/storybook';
import { MemoryRouter, Route, matchPath } from 'react-router';


class InnerComponent extends Component {
  constructor(props) {
    super(props);
    this.onHistoryChanged = this.onHistoryChanged.bind(this);
  }

  componentDidMount() {
    // React on every change to the history
    this.unlisten = this.props.history.listen(this.onHistoryChanged);
  }

  shouldComponentUpdate(nextProps) {
    // As the history object is mutable (see https://goo.gl/lusQ2H) this check prevents
    // unnecessary re-renderings.
    return nextProps.location.pathname !== this.props.location.pathname;
  }

  componentWillUnmount() {
    this.unlisten();
  }

  onHistoryChanged(location, historyAction) {
    const path = location.pathname;
    const {links} = this.props;

    for (const link in links) {
      // If the new path matches with one of the keys defined in the links object, then
      // executes the given corresponding callback value with the path as argument.
      // As behind the scene matchProps uses path-to-regexp (https://goo.gl/xgzOaL)
      // you can use parameter names and regexp within the link keys.
      if (matchPath(path, {path: link, exact: true})) {
        links[link](path);
        return;
      }
    }
    action(historyAction)(path);
  }

  render() {
    return (
      <div>{this.props.story()}</div>
    );
  }
}

InnerComponent.propTypes = {
  story: PropTypes.func.isRequired,
  history: PropTypes.object.isRequired,
  location: PropTypes.object.isRequired,
  links: PropTypes.object
};


const StoryRouter = ({story, links, routerProps}) => (
  // Limitation: as MemoryRouter creates a new history object, you cannot pass it from
  // a story to another one and so you cannot implement a back or forward button which
  // works among stories.
  <MemoryRouter {...routerProps}>
    <Route render={({history, location}) => (
      <InnerComponent
        story={story}
        history={history}
        location={location}
        links={links}/>
    )} />
  </MemoryRouter>
);

StoryRouter.propTypes = {
  story: PropTypes.func.isRequired,
  links: PropTypes.object,
  routerProps: PropTypes.object
};

const storyRouterDecorator = (links, routerProps) => {
  const s = story => (
    <StoryRouter
{'fraction_non_alphanumeric': 0.12443324937027708, 'fraction_numerical': 0.00025188916876574307, 'mean_word_length': 2.916173570019724, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 26, 'https://': 2, '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': '3697610', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1124, 'n_words': 303}