2019-10-19 15:49:04 +07:00
|
|
|
import React, { lazy, useContext, Suspense } from 'react';
|
|
|
|
import { Route as ReactRouterDomRoute, Switch, Router } from 'react-router-dom';
|
|
|
|
import { createBrowserHistory } from 'history';
|
|
|
|
|
|
|
|
import Loading from '../components/Loading';
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
import AppContext from './AppContext';
|
2019-10-19 15:49:04 +07:00
|
|
|
|
|
|
|
const NotFound = lazy(() => import('../components/NotFound'));
|
|
|
|
const VersionContextProvider = lazy(() => import('../pages/Version/VersionContextProvider'));
|
|
|
|
const VersionPage = lazy(() => import('../pages/Version'));
|
|
|
|
const HomePage = lazy(() => import('../pages/home'));
|
|
|
|
|
|
|
|
enum Route {
|
|
|
|
ROOT = '/',
|
|
|
|
SCOPE_PACKAGE = '/-/web/detail/@:scope/:package',
|
|
|
|
SCOPE_PACKAGE_VERSION = '/-/web/detail/@:scope/:package/v/:version',
|
|
|
|
PACKAGE = '/-/web/detail/:package',
|
|
|
|
PACKAGE_VERSION = '/-/web/detail/:package/v/:version',
|
|
|
|
}
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
export const history = createBrowserHistory({
|
2019-10-19 15:49:04 +07:00
|
|
|
basename: window.__VERDACCIO_BASENAME_UI_OPTIONS && window.__VERDACCIO_BASENAME_UI_OPTIONS.url_prefix,
|
|
|
|
});
|
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
const AppRoute: React.FC = () => {
|
2019-10-19 15:49:04 +07:00
|
|
|
const appContext = useContext(AppContext);
|
2019-11-25 01:21:08 +07:00
|
|
|
|
|
|
|
if (!appContext) {
|
|
|
|
throw Error('The app Context was not correct used');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { user, packages } = appContext;
|
|
|
|
|
|
|
|
const isUserLoggedIn = user && user.username;
|
2019-10-19 15:49:04 +07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Router history={history}>
|
|
|
|
<Suspense fallback={<Loading />}>
|
|
|
|
<Switch>
|
|
|
|
<ReactRouterDomRoute exact={true} path={Route.ROOT}>
|
|
|
|
<HomePage isUserLoggedIn={!!isUserLoggedIn} packages={packages || []} />
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
<ReactRouterDomRoute exact={true} path={Route.PACKAGE}>
|
|
|
|
<VersionContextProvider>
|
|
|
|
<VersionPage />
|
|
|
|
</VersionContextProvider>
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
<ReactRouterDomRoute exact={true} path={Route.PACKAGE_VERSION}>
|
|
|
|
<VersionContextProvider>
|
|
|
|
<VersionPage />
|
|
|
|
</VersionContextProvider>
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
<ReactRouterDomRoute exact={true} path={Route.SCOPE_PACKAGE_VERSION}>
|
|
|
|
<VersionContextProvider>
|
|
|
|
<VersionPage />
|
|
|
|
</VersionContextProvider>
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
<ReactRouterDomRoute exact={true} path={Route.SCOPE_PACKAGE}>
|
|
|
|
<VersionContextProvider>
|
|
|
|
<VersionPage />
|
|
|
|
</VersionContextProvider>
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
<ReactRouterDomRoute>
|
|
|
|
<NotFound />
|
|
|
|
</ReactRouterDomRoute>
|
|
|
|
</Switch>
|
|
|
|
</Suspense>
|
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AppRoute;
|