<commit_before>import React from 'react';
import ReactDOM from 'react-dom';
import Relay from 'react-relay';
import { RelayNetworkLayer, urlMiddleware, authMiddleware } from 'react-relay-network-layer';
import { Router, Route, IndexRoute, IndexRedirect, hashHistory, applyRouterMiddleware } from 'react-router';
import useRelay from 'react-router-relay';
import AuthService from './auth/AuthService';
import App from './App';
import { HomeContainer } from './components/Home';
import SimpleLogin from './components/SimpleLogin';

import 'bootstrap/dist/css/bootstrap.css';
// import 'bootstrap/dist/css/bootstrap-theme.css';
import './override-bootstrap.css';
import './index.css';

import { TimelineContainer } from './components/Timeline';

const auth = new AuthService(process.env.AUTH0_CLIENT_ID, process.env.AUTH0_DOMAIN);

function setupRelayNetworkLayer() {
    Relay.injectNetworkLayer(new RelayNetworkLayer([
        urlMiddleware({
            url: process.env.REACT_APP_GRAPHQL_ENDPOINT || '/graphql'
        }),
        authMiddleware({
            token: () => auth.getToken()
        })
    ], { disableBatchQuery:  true }));
}

// onEnter callback to validate authentication in private routes
const requireAuth = (nextState, replace) => {
  if (!auth.loggedIn()) {
    replace({ pathname: '/login' });
  }
};
// OnEnter for callback url to parse access_token
const parseAuthHash = (nextState, replace) => {
    auth.parseHash(nextState.location.hash);
    replace('/home');
}

const ViewerQueries = {
    viewer: () => Relay.QL`query { viewer }`,
};

setupRelayNetworkLayer();
ReactDOM.render(
  // TODO: Shift to browserHistory only blocked by auth0 access_token handling
  // see: https://auth0.com/forum/t/having-trouble-with-login-following-the-react-guide/3084
  <Router history={hashHistory} environment={Relay.Store} render={applyRouterMiddleware(useRelay)}>
    <Route path="/" component={App} auth={auth}>
        <IndexRedirect to="/home" />
        <Route path="home" component={HomeContainer} queries={ViewerQueries} onEnter={requireAuth}>
            <IndexRoute component={TimelineContainer} queries={ViewerQueries} onEnter={requireAuth} />
        </Route>
        <Route path="login" component={SimpleLogin} />
        <Route path="access_token=:token" onEnter={parseAuthHash} />
    </Route>
  </Router>,
  document.getElementById('root')
);
<commit_msg>Shift to browserHistory<commit_after>import React from 'react';
import ReactDOM from 'react-dom';
import Relay from 'react-relay';
import { RelayNetworkLayer, urlMiddleware, authMiddleware } from 'react-relay-network-layer';
import { Router, Route, IndexRoute, IndexRedirect, browserHistory, applyRouterMiddleware } from 'react-router';
import useRelay from 'react-router-relay';
import AuthService from './auth/AuthService';
import App from './App';
import { HomeContainer } from './components/Home';
import SimpleLogin from './components/SimpleLogin';

import 'bootstrap/dist/css/bootstrap.css';
// import 'bootstrap/dist/css/bootstrap-theme.css';
import './override-bootstrap.css';
import './index.css';

import { TimelineContainer } from './components/Timeline';

const auth = new AuthService(process.env.AUTH0_CLIENT_ID, process.env.AUTH0_DOMAIN);

function setupRelayNetworkLayer() {
    Relay.injectNetworkLayer(new RelayNetworkLayer([
        urlMiddleware({
            url: process.env.REACT_APP_GRAPHQL_ENDPOINT || '/graphql'
        }),
        authMiddleware({
            token: () => auth.getToken()
        })
    ], { disableBatchQuery:  true }));
}

// onEnter callback to validate authentication in private routes
const requireAuth = (nextState, replace) => {
  if (nextState.location.hash) {
     auth.parseHash(nextState.location.hash);
  }
  if (!auth.loggedIn()) {
      replace({ pathname: '/login' });
      return false;
  }
  return true;
};

const parseAuth = (nextState, replace) => {
    //because the page can't set the id token fast enough, check the nextState to see if the hash exists
    //if it does exist then grab the id token from the hash and then set it to local storage
    if (nextState.location.hash) {
        auth.parseHash(nextState.location.hash);
    }
    if (auth.loggedIn()) {
        replace({ pathname: '/' });
        return true;
    }
}

const ViewerQueries = {
    viewer: () => Relay.QL`query { viewer }`,
};

setupRelayNetworkLayer();
ReactDOM.render(
  <Router history={browserHistory} environment={Relay.Store} render={applyRouterMiddleware(useRelay)}>
    <Route path="/" component={App} auth={auth}>
        <IndexRedirect to="/home" />
        <Route path="home" component={HomeContainer} queries={ViewerQueries} onEnter={requireAuth}>
            <IndexRoute component={TimelineContainer} queries={ViewerQueries} onEnter={requireAuth} />
        </Route>
        <Route path="login" component={SimpleLogin} onEnter={parseAuth}/>
        <Route path="access_token=:token" component={SimpleLogin} onEnter={parseAuth}/>
    </Route>
  </Router>,
  document.getElementById('root')
);

{'fraction_non_alphanumeric': 0.13910969793322733, 'fraction_numerical': 0.001987281399046105, 'mean_word_length': 4.0481444332999, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 1, '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': '4252295', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1452, 'n_words': 347}