2019-02-03 17:23:33 +07:00
|
|
|
/* eslint react/jsx-max-depth:0 */
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import React, { Component, ReactElement } from 'react';
|
2019-03-28 05:39:06 +07:00
|
|
|
import { Router, Route, Switch } from 'react-router-dom';
|
2019-07-14 18:35:57 +07:00
|
|
|
import { createBrowserHistory } from 'history';
|
2019-06-26 06:10:15 +07:00
|
|
|
import { AppContextConsumer, AppStateInterface } from './App/App';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-03-28 05:39:06 +07:00
|
|
|
import Header from './components/Header';
|
|
|
|
|
2019-07-14 18:35:57 +07:00
|
|
|
const history = createBrowserHistory({
|
|
|
|
basename: window.__VERDACCIO_BASENAME_UI_OPTIONS && window.__VERDACCIO_BASENAME_UI_OPTIONS.url_prefix,
|
|
|
|
});
|
|
|
|
|
2019-10-06 01:13:36 +07:00
|
|
|
const NotFound = React.lazy(() => import('./components/NotFound'));
|
|
|
|
const VersionPackage = React.lazy(() => import('./pages/Version'));
|
|
|
|
const HomePage = React.lazy(() => import('./pages/home'));
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-22 16:43:59 +07:00
|
|
|
interface RouterAppProps {
|
|
|
|
onLogout: () => void;
|
|
|
|
onToggleLoginModal: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class RouterApp extends Component<RouterAppProps> {
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLDivElement> {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<Router history={history}>
|
2019-10-06 01:13:36 +07:00
|
|
|
<React.Suspense fallback={null}>
|
2019-02-03 17:23:33 +07:00
|
|
|
{this.renderHeader()}
|
|
|
|
<Switch>
|
2019-03-28 05:39:06 +07:00
|
|
|
<Route exact={true} path={'/'} render={this.renderHomePage} />
|
|
|
|
<Route exact={true} path={'/-/web/detail/@:scope/:package'} render={this.renderVersionPage} />
|
|
|
|
<Route exact={true} path={'/-/web/detail/:package'} render={this.renderVersionPage} />
|
2019-09-01 18:09:23 +07:00
|
|
|
<Route exact={true} path={'/-/web/detail/:package/v/:version'} render={this.renderVersionPage} />
|
|
|
|
<Route exact={true} path={'/-/web/detail/@:scope/:package/v/:version'} render={this.renderVersionPage} />
|
2019-02-03 17:23:33 +07:00
|
|
|
<Route component={NotFound} />
|
|
|
|
</Switch>
|
2019-10-06 01:13:36 +07:00
|
|
|
</React.Suspense>
|
2019-02-03 17:23:33 +07:00
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
public renderHeader = (): ReactElement<HTMLDivElement> => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { onLogout, onToggleLoginModal } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AppContextConsumer>
|
2019-06-26 06:10:15 +07:00
|
|
|
{function renderConsumerVersionPage({ logoUrl, scope = '', user }: Partial<AppStateInterface>) {
|
|
|
|
return <Header logo={logoUrl} onLogout={onLogout} onToggleLoginModal={onToggleLoginModal} scope={scope} username={user && user.username} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</AppContextConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
public renderHomePage = (): ReactElement<HTMLDivElement> => {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<AppContextConsumer>
|
2019-06-26 06:10:15 +07:00
|
|
|
{function renderConsumerVersionPage({ isUserLoggedIn, packages }: Partial<AppStateInterface>) {
|
2019-06-20 19:37:28 +07:00
|
|
|
// @ts-ignore
|
2019-03-28 05:39:06 +07:00
|
|
|
return <HomePage isUserLoggedIn={isUserLoggedIn} packages={packages} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</AppContextConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-26 06:10:15 +07:00
|
|
|
public renderVersionPage = (routerProps): ReactElement<HTMLDivElement> => {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
|
|
|
<AppContextConsumer>
|
2019-06-26 06:10:15 +07:00
|
|
|
{function renderConsumerVersionPage({ isUserLoggedIn }: Partial<AppStateInterface>) {
|
2019-03-28 05:39:06 +07:00
|
|
|
return <VersionPackage {...routerProps} isUserLoggedIn={isUserLoggedIn} />;
|
2019-02-03 17:23:33 +07:00
|
|
|
}}
|
|
|
|
</AppContextConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RouterApp;
|