From 0d7ec7176913ac81b56c0610cd7aeff07b77f660 Mon Sep 17 00:00:00 2001 From: Bryan Phelps Date: Wed, 26 Jan 2022 04:04:20 +0000 Subject: [PATCH 1/2] Add Redirect component; use on Index page --- site/components/Redirect.tsx | 16 ++++++++ site/components/index.tsx | 1 + site/pages/index.tsx | 79 +++++------------------------------- 3 files changed, 27 insertions(+), 69 deletions(-) create mode 100644 site/components/Redirect.tsx diff --git a/site/components/Redirect.tsx b/site/components/Redirect.tsx new file mode 100644 index 0000000000000..da2c25bf32589 --- /dev/null +++ b/site/components/Redirect.tsx @@ -0,0 +1,16 @@ +import React, { useEffect } from "react" +import { useRouter } from "next/router" + +export interface RedirectProps { + to: string +} + +export const Redirect: React.FC = ({ to }) => { + const router = useRouter() + + useEffect(() => { + void router.replace(to) + }, []) + + return null +} \ No newline at end of file diff --git a/site/components/index.tsx b/site/components/index.tsx index 5fd2a75122e23..7f66b9952dd44 100644 --- a/site/components/index.tsx +++ b/site/components/index.tsx @@ -1,3 +1,4 @@ export * from "./Button" export * from "./EmptyState" export * from "./Page" +export * from "./Redirect" \ No newline at end of file diff --git a/site/pages/index.tsx b/site/pages/index.tsx index 0de405b8a74fb..ed862da2c79e6 100644 --- a/site/pages/index.tsx +++ b/site/pages/index.tsx @@ -1,78 +1,19 @@ import React from "react" -import Box from "@material-ui/core/Box" -import { makeStyles } from "@material-ui/core/styles" -import Paper from "@material-ui/core/Paper" -import AddWorkspaceIcon from "@material-ui/icons/AddToQueue" -import { EmptyState, SplitButton } from "../components" -import { Navbar } from "../components/Navbar" -import { Footer } from "../components/Page" -import { useUser } from "../contexts/UserContext" +import { Redirect } from "../components" +import { ErrorSummary } from "../components/ErrorSummary" import { FullScreenLoader } from "../components/Loader/FullScreenLoader" +import { useUser } from "../contexts/UserContext" -const WorkspacesPage: React.FC = () => { - const styles = useStyles() - const { me, signOut } = useUser(true) - - if (!me) { - return - } - - const createWorkspace = () => { - alert("create") - } +const IndexPage: React.FC = () => { + const { me } = useUser(/* redirectOnError */ true) - const button = { - children: "New Workspace", - onClick: createWorkspace, + if (me) { + // Once the user is logged in, just redirect them to /projects as the landing page + return } - return ( -
- -
- - color="primary" - onClick={createWorkspace} - options={[ - { - label: "New workspace", - value: "custom", - }, - { - label: "New workspace from template", - value: "template", - }, - ]} - startIcon={} - textTransform="none" - /> -
- - - - - - -
-
- ) + return } -const useStyles = makeStyles((theme) => ({ - root: { - display: "flex", - flexDirection: "column", - }, - header: { - display: "flex", - flexDirection: "row-reverse", - justifyContent: "space-between", - margin: "1em auto", - maxWidth: "1380px", - padding: theme.spacing(2, 6.25, 0), - width: "100%", - }, -})) - -export default WorkspacesPage +export default IndexPage From d5e70259a232f290c7fce158942f24a47fb41974 Mon Sep 17 00:00:00 2001 From: Bryan Phelps Date: Wed, 26 Jan 2022 04:11:11 +0000 Subject: [PATCH 2/2] fix: Redirect to /projects page from / --- site/components/Redirect.test.tsx | 22 ++++++++++++++++++++++ site/components/Redirect.tsx | 9 ++++++++- site/components/index.tsx | 2 +- site/pages/index.tsx | 1 - 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 site/components/Redirect.test.tsx diff --git a/site/components/Redirect.test.tsx b/site/components/Redirect.test.tsx new file mode 100644 index 0000000000000..4b53889f3931c --- /dev/null +++ b/site/components/Redirect.test.tsx @@ -0,0 +1,22 @@ +import { render, waitFor } from "@testing-library/react" +import singletonRouter from "next/router" +import mockRouter from "next-router-mock" +import React from "react" +import { Redirect } from "./Redirect" + +describe("Redirect", () => { + // Reset the router to '/' before every test + beforeEach(() => { + mockRouter.setCurrentUrl("/") + }) + + it("performs client-side redirect on render", async () => { + // When + render() + + // Then + await waitFor(() => { + expect(singletonRouter).toMatchObject({ asPath: "/workspaces/v2" }) + }) + }) +}) diff --git a/site/components/Redirect.tsx b/site/components/Redirect.tsx index da2c25bf32589..5d9e01258cd74 100644 --- a/site/components/Redirect.tsx +++ b/site/components/Redirect.tsx @@ -2,9 +2,16 @@ import React, { useEffect } from "react" import { useRouter } from "next/router" export interface RedirectProps { + /** + * The path to redirect to + * @example '/projects' + */ to: string } +/** + * Helper component to perform a client-side redirect + */ export const Redirect: React.FC = ({ to }) => { const router = useRouter() @@ -13,4 +20,4 @@ export const Redirect: React.FC = ({ to }) => { }, []) return null -} \ No newline at end of file +} diff --git a/site/components/index.tsx b/site/components/index.tsx index 7f66b9952dd44..ebb1a90188bb8 100644 --- a/site/components/index.tsx +++ b/site/components/index.tsx @@ -1,4 +1,4 @@ export * from "./Button" export * from "./EmptyState" export * from "./Page" -export * from "./Redirect" \ No newline at end of file +export * from "./Redirect" diff --git a/site/pages/index.tsx b/site/pages/index.tsx index ed862da2c79e6..905fef83e8e55 100644 --- a/site/pages/index.tsx +++ b/site/pages/index.tsx @@ -1,7 +1,6 @@ import React from "react" import { Redirect } from "../components" -import { ErrorSummary } from "../components/ErrorSummary" import { FullScreenLoader } from "../components/Loader/FullScreenLoader" import { useUser } from "../contexts/UserContext"