Skip to content

Commit

Permalink
Add NotFound component and 404 redirect (#440)
Browse files Browse the repository at this point in the history
* Add NotFound component and 404 redirect

* fix TS

* use theme colors and fix catch-all path for MemoryRouter
  • Loading branch information
gabalafou authored Nov 19, 2024
1 parent e356081 commit e0d8cac
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
prefDefault,
prefGlobal
} from "./preferences";
import { routes } from "./routes";
import { createRoutes } from "./routes";

import { store } from "./store";
import { condaStoreTheme, grayscaleTheme } from "./theme";
Expand Down Expand Up @@ -72,8 +72,8 @@ export class App<

const router =
routerType === "memory"
? createMemoryRouter(routes, { initialEntries: ["/"] })
: createBrowserRouter(routes, { basename });
? createMemoryRouter(createRoutes("memory"), { initialEntries: ["/"] })
: createBrowserRouter(createRoutes("browser"), { basename });

return (
<PrefContext.Provider value={this.state.pref}>
Expand Down
22 changes: 22 additions & 0 deletions src/components/NoEnvironmentSelected.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";

export function NoEnvironmentSelected() {
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
height: "100%"
}}
data-testid="no-environment-selected"
>
<Typography sx={{ fontSize: "18px", color: "secondary.dark" }}>
Select an environment to show details
</Typography>
</Box>
);
}
24 changes: 24 additions & 0 deletions src/components/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";

export function NotFound() {
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
height: "100%"
}}
>
<Typography
variant="h1"
sx={{ fontSize: "18px", color: "secondary.dark" }}
>
404 - Not found
</Typography>
</Box>
);
}
24 changes: 2 additions & 22 deletions src/layouts/PageLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState, useRef, useEffect, RefObject } from "react";
import { Outlet, useOutletContext, useParams } from "react-router-dom";
import { Outlet, useOutletContext } from "react-router-dom";
import Box from "@mui/material/Box";
import { Typography } from "@mui/material";
import { Popup } from "../components";
import { Environments } from "../features/environments";
import { StyledScrollContainer } from "../styles";
Expand All @@ -21,8 +20,6 @@ export const PageLayout = () => {
setRefreshEnvironments(true);
}, [notification]);

const { namespaceName } = useParams();

return (
<Box
sx={{
Expand All @@ -46,24 +43,7 @@ export const PageLayout = () => {
overflowY: "scroll"
}}
>
{namespaceName ? (
<Outlet context={scrollRef} />
) : (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
height: "100%"
}}
data-testid="no-environment-selected"
>
<Typography sx={{ fontSize: "18px", color: "#333" }}>
Select an environment to show details
</Typography>
</Box>
)}
<Outlet context={scrollRef} />
</StyledScrollContainer>
<Popup
isVisible={notification.show}
Expand Down
64 changes: 48 additions & 16 deletions src/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
import React from "react";
import { useHref, Navigate } from "react-router-dom";

import { PageLayout } from "./layouts";
import { EnvironmentDetails } from "./features/environmentDetails";
import { EnvironmentCreate } from "./features/environmentCreate";
import { NoEnvironmentSelected } from "./components/NoEnvironmentSelected";
import { NotFound } from "./components/NotFound";

/**
* Define URL routes for the single page app
*/

export const routes = [
{
path: "/",
element: <PageLayout />,
children: [
{
path: ":namespaceName/new-environment",
element: <EnvironmentCreate />
},
{
path: ":namespaceName/:environmentName",
element: <EnvironmentDetails />
}
]
}
];
const HardRedirectNotFound = () => {
// useHref takes into account React Router basename option. So if basename is
// set to "/conda-store", the hook will return "/conda-store/not-found"
const url = useHref("/not-found");
window.location.replace(url);
return null;
};

export function createRoutes(routerType: "browser" | "memory") {
return [
{
path: "/",
element: <PageLayout />,
children: [
{
index: true,
element: <NoEnvironmentSelected />
},
{
path: ":namespaceName/new-environment",
element: <EnvironmentCreate />
},
{
path: ":namespaceName/:environmentName",
element: <EnvironmentDetails />
},
{
path: "not-found",
element: <NotFound />
}
]
},
// If no route matches, send the user to a route on the server that will
// return a proper 404 HTTP status code.
{
path: "*",
element:
routerType === "browser" ? (
<HardRedirectNotFound />
) : (
<Navigate to="/not-found" replace={true} />
)
}
];
}

0 comments on commit e0d8cac

Please sign in to comment.