<commit_before>/**
 *  Copyright (C) 2017 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { memoize } from 'lodash';
import React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';

import { ROUTES } from '../../constants/routes';
import { getCookie, setCookie } from '../../helpers/cookies';
import { renderWhenTrue } from '../../helpers/rendering';
import { WebGLChecker } from '../../helpers/webglChecker';
import { analyticsService } from '../../services/analytics';
import { clientConfigService } from '../../services/clientConfig';
import { isStaticRoute, STATIC_ROUTES } from '../../services/staticPages';
import { DialogContainer } from '../components/dialogContainer';
import { LiveChat } from '../components/liveChat';
import { PrivateRoute } from '../components/privateRoute';
import { SnackbarContainer } from '../components/snackbarContainer';
import StaticPageRoute from '../components/staticPageRoute/staticPageRoute.component';
import TopMenu from '../components/topMenu/topMenu.container';
import { Dashboard } from '../dashboard';
import { Login } from '../login';
import { NotFound } from '../notFound';
import { PasswordChange } from '../passwordChange';
import { PasswordForgot } from '../passwordForgot';
import RegisterRequest from '../registerRequest/registerRequest.container';
import { RegisterVerify } from '../registerVerify';
import { SignUp } from '../signUp';
import { ViewerCanvas } from '../viewerCanvas';
import { ViewerGui } from '../viewerGui';
import { AppContainer } from './app.styles';

interface IProps {
	location: any;
	history: any;
	isAuthenticated: boolean;
	currentUser: any;
	isAuthPending: boolean;
	showNewUpdateDialog: (config) => void;
	authenticate: () => void;
	logout: () => void;
	startup: () => void;
	hideDialog: () => void;
	onLoggedOut: () => void;
	subscribeToDm: (event, handler) => void;
	showDialog: (config) => void;
	dialogs: any[];
}

interface IState {
	referrer?: string;
	autologoutInterval?: number;
}

const DEFAULT_REDIRECT = ROUTES.TEAMSPACES;

const ANALYTICS_REFERER_ROUTES = [
	'sign-up',
	'register-request'
] as any;

export class App extends React.PureComponent<IProps, IState> {

	get WebGLVersion() {
		return WebGLChecker();
	}

	public state = {
		referrer: DEFAULT_REDIRECT,
		autologoutInterval: clientConfigService.login_check_interval || 4
	};

	public renderStaticRoutes = memoize(() => STATIC_ROUTES.map(({ title, path, fileName }) => (
		<Route key={path} path={path} render={() => <StaticPageRoute title={title} fileName={fileName} />} />
	)));

	public renderLoginRoute = memoize(() => {
		return <Route exact path={ROUTES.LOGIN} component={Login} />;
	});

	public renderHeader = renderWhenTrue(() => (
		<TopMenu />
	));

	constructor(props) {
		super(props);
		props.startup();
	}

	public isRefererRoute(path) {
		return ANALYTICS_REFERER_ROUTES.includes(path.replace('/', ''));
	}

	public componentDidMount() {
		this.props.authenticate();
		this.sendAnalyticsPageView(location);

		if ('serviceWorker' in navigator) {
			navigator.serviceWorker.addEventListener('message', (event) => {
				if (event.data.type === 'UPDATE') {
					this.props.showNewUpdateDialog({
						onConfirm: () => {
							location.reload();
						}
					});
				}
			});
		}

		this.props.subscribeToDm('loggedOut', this.props.onLoggedOut);
	}

	public componentDidUpdate(prevProps) {
		if (decodeURIComponent(location.pathname) !== decodeURIComponent(prevProps.location.pathname)) {
			this.sendAnalyticsPageView(location);
			this.props.hideDialog();
		}

		if (!this.props.dialogs.length && this.props.isAuthenticated && this.WebGLVersion !== 2) {
			const { currentUser: { username }, showDialog } = this.props;
			const cookieName = `unsupportedViewerWarning_${username}`;
			const cookie = getCookie(cookieName);

			if (!cookie) {
				showDialog({
					title: `3D Repo Error`,
					content: `
						Your browser does not support WebGL 2.0 therefore the 3D Viewer will be unavailable.
						However, you can still other functionalities we offer.<br><br>
						To get the full experience, please update to the latest Chrome, Firefox or Edge.
					`,
					onCancel: () => setCookie(cookieName, true),
				});
			}
		}
	}

	public sendAnalyticsPageView(location) {
		const isAnalyticsRefererRoute = this.isRefererRoute(location.pathname);
		analyticsService.sendPageView(location);

		if (isAnalyticsRefererRoute) {
			analyticsService.sendPageViewReferer(location);
		}
	}

	public render() {
		return (
			<AppContainer>
				<Route component={ViewerCanvas} />
				{this.renderHeader(!isStaticRoute(location.pathname))}
				<Switch>
					{this.renderLoginRoute()}
					<Route exact path={ROUTES.SIGN_UP} component={SignUp} />
					<Route exact path={ROUTES.PASSWORD_FORGOT} component={PasswordForgot} />
					<Route exact path={ROUTES.PASSWORD_CHANGE} component={PasswordChange} />
					<Route exact path={ROUTES.REGISTER_REQUEST} component={RegisterRequest} />
					<Route exact path={ROUTES.REGISTER_VERIFY} component={RegisterVerify} />
					<Redirect exact from={ROUTES.HOME} to={ROUTES.TEAMSPACES} />
					<PrivateRoute path={ROUTES.DASHBOARD} component={Dashboard} />
					<PrivateRoute
						path={`${ROUTES.VIEWER}/:teamspace/:model/:revision?`}
						component={ViewerGui}
					/>
					{this.renderStaticRoutes()}
					<Route component={NotFound} />
				</Switch>
				<DialogContainer />
				<SnackbarContainer />
				<LiveChat />
			</AppContainer>
		);
	}
}
<commit_msg>ISSUE #2215 - Fixed problem when navigating and using back and foward buttons from the browser (#2302)<commit_after>/**
 *  Copyright (C) 2017 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { memoize } from 'lodash';
import React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';

import { ROUTES } from '../../constants/routes';
import { getCookie, setCookie } from '../../helpers/cookies';
import { renderWhenTrue } from '../../helpers/rendering';
import { WebGLChecker } from '../../helpers/webglChecker';
import { analyticsService } from '../../services/analytics';
import { clientConfigService } from '../../services/clientConfig';
import { isStaticRoute, STATIC_ROUTES } from '../../services/staticPages';
import { DialogContainer } from '../components/dialogContainer';
import { LiveChat } from '../components/liveChat';
import { PrivateRoute } from '../components/privateRoute';
import { SnackbarContainer } from '../components/snackbarContainer';
import StaticPageRoute from '../components/staticPageRoute/staticPageRoute.component';
import TopMenu from '../components/topMenu/topMenu.container';
import { Dashboard } from '../dashboard';
import { Login } from '../login';
import { NotFound } from '../notFound';
import { PasswordChange } from '../passwordChange';
import { PasswordForgot } from '../passwordForgot';
import RegisterRequest from '../registerRequest/registerRequest.container';
import { RegisterVerify } from '../registerVerify';
import { SignUp } from '../signUp';
import { ViewerCanvas } from '../viewerCanvas';
import { ViewerGui } from '../viewerGui';
import { AppContainer } from './app.styles';

interface IProps {
	location: any;
	history: any;
	isAuthenticated: boolean;
	currentUser: any;
	isAuthPending: boolean;
	showNewUpdateDialog: (config) => void;
	authenticate: () => void;
	logout: () => void;
	startup: () => void;
	hideDialog: () => void;
	onLoggedOut: () => void;
	subscribeToDm: (event, handler) => void;
	showDialog: (config) => void;
	dialogs: any[];
}

interface IState {
	referrer?: string;
	autologoutInterval?: number;
}

const DEFAULT_REDIRECT = ROUTES.TEAMSPACES;

const ANALYTICS_REFERER_ROUTES = [
	'sign-up',
	'register-request'
] as any;

export class App extends React.PureComponent<IProps, IState> {

	get WebGLVersion() {
		return WebGLChecker();
	}

	public state = {
		referrer: DEFAULT_REDIRECT,
		autologoutInterval: clientConfigService.login_check_interval || 4
	};

	public renderStaticRoutes = memoize(() => STATIC_ROUTES.map(({ title, path, fileName }) => (
		<Route key={path} path={path} render={() => <StaticPageRoute title={title} fileName={fileName} />} />
	)));

	public renderLoginRoute = memoize(() => {
		return <Route exact path={ROUTES.LOGIN} component={Login} />;
	});

	public renderHeader = renderWhenTrue(() => (
		<TopMenu />
	));

	constructor(props) {
		super(props);
		props.startup();
	}

	public isRefererRoute(path) {
		return ANALYTICS_REFERER_ROUTES.includes(path.replace('/', ''));
	}

	public componentDidMount() {
		this.props.authenticate();
		this.sendAnalyticsPageView(location);

		if ('serviceWorker' in navigator) {
			navigator.serviceWorker.addEventListener('message', (event) => {
				if (event.data.type === 'UPDATE') {
					this.props.showNewUpdateDialog({
						onConfirm: () => {
							location.reload();
						}
					});
				}
			});
		}

		this.props.subscribeToDm('loggedOut', this.props.onLoggedOut);
	}

	public componentDidUpdate(prevProps) {
		if (location.pathname !== prevProps.location.pathname) {
			this.sendAnalyticsPageView(location);
			if (this.props.dialogs.length) {
				this.props.hideDialog();
			}
		}

		if (!this.props.dialogs.length && this.props.isAuthenticated && this.WebGLVersion !== 2) {
			const { currentUser: { username }, showDialog } = this.props;
			const cookieName = `unsupportedViewerWarning_${username}`;
			const cookie = getCookie(cookieName);

			if (!cookie) {
				showDialog({
					title: `3D Repo Error`,
					content: `
						Your browser does not support WebGL 2.0 therefore the 3D Viewer will be unavailable.
						However, you can still other functionalities we offer.<br><br>
						To get the full experience, please update to the latest Chrome, Firefox or Edge.
					`,
					onCancel: () => setCookie(cookieName, true),
				});
			}
		}
	}

	public sendAnalyticsPageView(location) {
		const isAnalyticsRefererRoute = this.isRefererRoute(location.pathname);
		analyticsService.sendPageView(location);

		if (isAnalyticsRefererRoute) {
			analyticsService.sendPageViewReferer(location);
		}
	}

	public render() {
		return (
			<AppContainer>
				<Route component={ViewerCanvas} />
				{this.renderHeader(!isStaticRoute(location.pathname))}
				<Switch>
					{this.renderLoginRoute()}
					<Route exact path={ROUTES.SIGN_UP} component={SignUp} />
					<Route exact path={ROUTES.PASSWORD_FORGOT} component={PasswordForgot} />
					<Route exact path={ROUTES.PASSWORD_CHANGE} component={PasswordChange} />
					<Route exact path={ROUTES.REGISTER_REQUEST} component={RegisterRequest} />
					<Route exact path={ROUTES.REGISTER_VERIFY} component={RegisterVerify} />
					<Redirect exact from={ROUTES.HOME} to={ROUTES.TEAMSPACES} />
					<PrivateRoute path={ROUTES.DASHBOARD} component={Dashboard} />
					<PrivateRoute
						path={`${ROUTES.VIEWER}/:teamspace/:model/:revision?`}
						component={ViewerGui}
					/>
					{this.renderStaticRoutes()}
					<Route component={NotFound} />
				</Switch>
				<DialogContainer />
				<SnackbarContainer />
				<LiveChat />
			</AppContainer>
		);
	}
}

{'fraction_non_alphanumeric': 0.13924152987790087, 'fraction_numerical': 0.0025875313333872402, 'mean_word_length': 4.060556464811784, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1065865', 'n_tokens_mistral': 4158, 'n_tokens_neox': 3759, 'n_words': 945}