Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit 6783b05

Browse files
refactor: updated routes
1 parent d1ce828 commit 6783b05

File tree

6 files changed

+82
-81
lines changed

6 files changed

+82
-81
lines changed

src/App/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface AppStateInterface {
2828
scope: string;
2929
showLoginModal: boolean;
3030
isUserLoggedIn: boolean;
31-
packages: any[];
31+
packages: [];
3232
isLoading: boolean;
3333
}
3434
export default class App extends Component<{}, AppStateInterface> {
@@ -85,6 +85,7 @@ export default class App extends Component<{}, AppStateInterface> {
8585
public loadOnHandler = async () => {
8686
try {
8787
const packages = await API.request<any[]>('packages', 'GET');
88+
// @ts-ignore: FIX THIS TYPE: Type 'any[]' is not assignable to type '[]'
8889
this.setState({
8990
packages,
9091
isLoading: false,

src/App/AppContext.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createContext } from 'react';
2+
3+
import { FormError } from '../components/Login/Login';
4+
5+
export interface AppProps {
6+
error?: FormError;
7+
logoUrl: string;
8+
user: {
9+
username?: string;
10+
};
11+
scope: string;
12+
showLoginModal: boolean;
13+
isUserLoggedIn: boolean;
14+
packages: [];
15+
isLoading: boolean;
16+
}
17+
18+
export const AppContext = createContext<Partial<AppProps>>({});
19+
export const AppContextProvider = AppContext.Provider;
20+
export const AppContextConsumer = AppContext.Consumer;

src/App/AppRoute.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { lazy, useContext, Suspense } from 'react';
2+
import { Route, Switch } from 'react-router-dom';
3+
4+
import { Route as Routes } from '../routers';
5+
import Loading from '../components/Loading';
6+
7+
import { AppContext } from './AppContext';
8+
9+
const NotFound = lazy(() => import('../components/NotFound'));
10+
const VersionContextProvider = lazy(() => import('../pages/Version/VersionContextProvider'));
11+
const VersionPage = lazy(() => import('../pages/Version'));
12+
const HomePage = lazy(() => import('../pages/home'));
13+
14+
/* eslint react/jsx-max-depth: 0 */
15+
const AppRoute: React.FC = () => {
16+
const appContext = useContext(AppContext);
17+
const { isUserLoggedIn, packages } = appContext;
18+
19+
return (
20+
<Suspense fallback={<Loading />}>
21+
<Switch>
22+
<Route exact={true} path={Routes.ROOT}>
23+
<HomePage isUserLoggedIn={!!isUserLoggedIn} packages={packages || []} />
24+
</Route>
25+
<Route exact={true} path={Routes.PACKAGE}>
26+
<VersionContextProvider>
27+
<VersionPage />
28+
</VersionContextProvider>
29+
</Route>
30+
<Route exact={true} path={Routes.PACKAGE_VERSION}>
31+
<VersionContextProvider>
32+
<VersionPage />
33+
</VersionContextProvider>
34+
</Route>
35+
<Route exact={true} path={Routes.SCOPE_PACKAGE_VERSION}>
36+
<VersionContextProvider>
37+
<VersionPage />
38+
</VersionContextProvider>
39+
</Route>
40+
<Route exact={true} path={Routes.SCOPE_PACKAGE}>
41+
<VersionContextProvider>
42+
<VersionPage />
43+
</VersionContextProvider>
44+
</Route>
45+
<Route>
46+
<NotFound />
47+
</Route>
48+
</Switch>
49+
</Suspense>
50+
);
51+
};
52+
53+
export default AppRoute;

src/router.tsx

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/routers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum Route {
2+
ROOT = '/',
3+
SCOPE_PACKAGE = '/-/web/detail/@:scope/:package',
4+
SCOPE_PACKAGE_VERSION = '/-/web/detail/@:scope/:package/v/:version',
5+
PACKAGE = '/-/web/detail/:package',
6+
PACKAGE_VERSION = '/-/web/detail/:package/v/:version',
7+
}

src/utils/package.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ export function formatDateDistance(lastUpdate): string {
5757
return distanceInWordsToNow(new Date(lastUpdate));
5858
}
5959

60-
export function buildScopePackage(scope: string, packageName: string): string {
61-
return `@${scope}/${packageName}`;
62-
}
63-
6460
/**
6561
* For <LastSync /> component
6662
* @param {array} uplinks

0 commit comments

Comments
 (0)