add use of useRoutes with nested children rotues and outlets

This commit is contained in:
Matthew Russell 2022-02-16 20:35:46 -08:00 committed by Dexter Edwards
parent c2aaed0b9a
commit bebc767c4c
9 changed files with 131 additions and 128 deletions

View File

@ -12,7 +12,10 @@ import { DATA_SOURCES } from './config';
import { TendermintWebsocketProvider } from './contexts/websocket/tendermint-websocket-provider'; import { TendermintWebsocketProvider } from './contexts/websocket/tendermint-websocket-provider';
function App() { function App() {
const [client] = React.useState(createClient(DATA_SOURCES.dataNodeUrl)); const client = React.useMemo(
() => createClient(DATA_SOURCES.dataNodeUrl),
[]
);
return ( return (
<TendermintWebsocketProvider> <TendermintWebsocketProvider>
<ApolloProvider client={client}> <ApolloProvider client={client}>

View File

@ -6,8 +6,7 @@ export const Nav = () => {
<nav> <nav>
{routerConfig.map((r) => ( {routerConfig.map((r) => (
<div key={r.name}> <div key={r.name}>
{/* TODO stupid hack */} <Link to={r.path}>{r.name}</Link>
<Link to={r.path.replace('/*', '')}>{r.name}</Link>
<br /> <br />
</div> </div>
))} ))}

View File

@ -94,11 +94,7 @@ const Search = () => {
<h1>Vega Block Explorer</h1> <h1>Vega Block Explorer</h1>
<fieldset> <fieldset>
<label htmlFor="search">Search: </label> <label htmlFor="search">Search: </label>
<input <input name="search" value={search} onChange={(e) => onChange(e)} />
name="search"
value={search}
onChange={(e) => onChange(e)}
></input>
</fieldset> </fieldset>
</section> </section>
); );

View File

@ -1,15 +1,7 @@
import React from 'react'; import { Outlet } from 'react-router-dom';
import { Route, Routes } from 'react-router-dom';
import { Blocks } from './home';
import { Block } from './id';
const BlockPage = () => { const BlockPage = () => {
return ( return <Outlet />;
<Routes>
<Route index={true} element={<Blocks />} />
<Route path={`/:block`} element={<Block />} />
</Routes>
);
}; };
export default BlockPage; export default BlockPage;

View File

@ -1,10 +1,9 @@
import React from 'react'; import React from 'react';
import { Route, Routes } from 'react-router-dom'; import { useRoutes } from 'react-router-dom';
import { RouteErrorBoundary } from '../components/router-error-boundary'; import { RouteErrorBoundary } from '../components/router-error-boundary';
import { SplashLoader } from '../components/splash-loader'; import { SplashLoader } from '../components/splash-loader';
import { SplashScreen } from '../components/splash-screen'; import { SplashScreen } from '../components/splash-screen';
import Home from './home';
import routerConfig from './router-config'; import routerConfig from './router-config';
export interface RouteChildProps { export interface RouteChildProps {
@ -12,6 +11,8 @@ export interface RouteChildProps {
} }
export const AppRouter = () => { export const AppRouter = () => {
const routes = useRoutes(routerConfig);
const splashLoading = ( const splashLoading = (
<SplashScreen> <SplashScreen>
<SplashLoader /> <SplashLoader />
@ -20,15 +21,7 @@ export const AppRouter = () => {
return ( return (
<RouteErrorBoundary> <RouteErrorBoundary>
<React.Suspense fallback={splashLoading}> <React.Suspense fallback={splashLoading}>{routes}</React.Suspense>
<Routes>
<>
{routerConfig.map(({ path, component: Component, name }) => (
<Route key={name} path={path} element={<Component />} />
))}
</>
</Routes>
</React.Suspense>
</RouteErrorBoundary> </RouteErrorBoundary>
); );
}; };

View File

@ -1,15 +1,7 @@
import React from 'react'; import { Outlet } from 'react-router-dom';
import { Route, Routes } from 'react-router-dom';
import { Parties } from './home';
import { Party } from './id';
const PartiesPage = () => { const PartiesPage = () => {
return ( return <Outlet />;
<Routes>
<Route index={true} element={<Parties />} />
<Route path={`/:party`} element={<Party />} />
</Routes>
);
}; };
export default PartiesPage; export default PartiesPage;

View File

@ -1,79 +0,0 @@
import Assets from './assets';
import Blocks from './blocks';
import Governance from './governance';
import Home from './home';
import Markets from './markets';
import Party from './parties';
import Txs from './txs';
import Validators from './validators';
import Genesis from './genesis';
import NetworkParameters from './network-parameters';
export const Routes = {
HOME: '/',
TX: 'txs/*',
BLOCKS: 'blocks/*',
PARTIES: 'parties/*',
VALIDATORS: 'validators/*',
ASSETS: 'assets/*',
GENESIS: 'genesis/*',
GOVERNANCE: 'governance/*',
MARKETS: 'markets/*',
NETWORK_PARAMETERS: 'network-parameters/*',
};
const routerConfig = [
{
path: Routes.HOME,
name: 'Home',
component: Home,
index: true,
},
{
path: `${Routes.TX}/*`,
name: 'Txs',
component: Txs,
},
{
path: Routes.BLOCKS,
name: 'Blocks',
component: Blocks,
},
{
path: Routes.PARTIES,
name: 'Parties',
component: Party,
},
{
path: Routes.ASSETS,
name: 'Assets',
component: Assets,
},
{
path: Routes.GENESIS,
name: 'Genesis',
component: Genesis,
},
{
path: Routes.GOVERNANCE,
name: 'Governance',
component: Governance,
},
{
path: Routes.MARKETS,
name: 'Markets',
component: Markets,
},
{
path: Routes.NETWORK_PARAMETERS,
name: 'NetworkParameters',
component: NetworkParameters,
},
{
path: Routes.VALIDATORS,
name: 'Validators',
component: Validators,
},
];
export default routerConfig;

View File

@ -0,0 +1,115 @@
import Assets from './assets';
import Blocks from './blocks';
import Governance from './governance';
import Home from './home';
import Markets from './markets';
import Party from './parties';
import { Parties } from './parties/home';
import { Party as PartySingle } from './parties/id';
import Txs from './txs';
import Validators from './validators';
import Genesis from './genesis';
import NetworkParameters from './network-parameters';
import { Block } from './blocks/id';
import { Blocks as BlocksHome } from './blocks/home';
import { Tx } from './txs/id';
import { Txs as TxHome } from './txs/home';
export const Routes = {
HOME: '/',
TX: 'txs',
BLOCKS: 'blocks',
PARTIES: 'parties',
VALIDATORS: 'validators',
ASSETS: 'assets',
GENESIS: 'genesis',
GOVERNANCE: 'governance',
MARKETS: 'markets',
NETWORK_PARAMETERS: 'network-parameters',
};
const routerConfig = [
{
path: Routes.HOME,
name: 'Home',
element: <Home />,
index: true,
},
{
path: Routes.TX,
name: 'Txs',
element: <Txs />,
children: [
{
path: ':txHash',
element: <Tx />,
},
{
index: true,
element: <TxHome />,
},
],
},
{
path: Routes.BLOCKS,
name: 'Blocks',
element: <Blocks />,
children: [
{
path: ':block',
element: <Block />,
},
{
index: true,
element: <BlocksHome />,
},
],
},
{
path: Routes.PARTIES,
name: 'Parties',
element: <Party />,
children: [
{
index: true,
element: <Parties />,
},
{
path: ':party',
element: <PartySingle />,
},
],
},
{
path: Routes.ASSETS,
name: 'Assets',
element: <Assets />,
},
{
path: Routes.GENESIS,
name: 'Genesis',
element: <Genesis />,
},
{
path: Routes.GOVERNANCE,
name: 'Governance',
element: <Governance />,
},
{
path: Routes.MARKETS,
name: 'Markets',
element: <Markets />,
},
{
path: Routes.NETWORK_PARAMETERS,
name: 'NetworkParameters',
element: <NetworkParameters />,
},
{
path: Routes.VALIDATORS,
name: 'Validators',
element: <Validators />,
},
];
export default routerConfig;

View File

@ -1,15 +1,7 @@
import React from 'react'; import { Outlet } from 'react-router-dom';
import { Route, Routes } from 'react-router-dom';
import { Txs } from './home';
import { Tx } from './id';
const TxPage = () => { const TxPage = () => {
return ( return <Outlet />;
<Routes>
<Route index={true} element={<Txs />} />
<Route path={`/:txHash`} element={<Tx />} />
</Routes>
);
}; };
export default TxPage; export default TxPage;