import { FunctionComponent } from "react"; import { Redirect, Route, Switch, useHistory } from "react-router"; import { useDidMount } from "rooks"; import { useNavigationItems } from "../Navigation"; import { Navigation } from "../Navigation/nav"; import { RouterEmptyPath } from "../special-pages/404"; import { BuildKey, ScrollToTop } from "../utilities"; const Router: FunctionComponent = () => { const navItems = useNavigationItems(); const history = useHistory(); useDidMount(() => { history.listen(() => { // This is a hack to make sure ScrollToTop will be triggered in the next frame (When everything are loaded) setTimeout(ScrollToTop); }); }); return (
{navItems.map((v, idx) => { if ("routes" in v) { return ( ); } else if (v.enabled !== false) { return ( ); } else { return null; } })}
); }; export default Router; const ParentRouter: FunctionComponent = ({ path, enabled, component, routes, }) => { if (enabled === false || (component === undefined && routes.length === 0)) { return null; } const ParentComponent = component ?? (() => ); return ( {routes .filter((v) => v.enabled !== false) .map((v, idx) => ( ))} ); };