From 9206e00a99cf6855233cffca151ddfaccdcb4495 Mon Sep 17 00:00:00 2001 From: karamvir Date: Mon, 24 Jul 2023 09:07:32 -0700 Subject: [PATCH] refactor component layout --- frontend/src/components/header/index.tsx | 2 +- frontend/src/hooks.ts | 16 ++++- frontend/src/layouts/resources.tsx | 59 +++++++++++------ frontend/src/pages/build/index.tsx | 63 ------------------ .../dashboard/components/recently-viewed.tsx | 18 +++-- frontend/src/pages/dashboard/index.tsx | 65 +------------------ frontend/src/pages/deployments/index.tsx | 10 ++- .../resources/build/components/actions.tsx | 18 +++++ frontend/src/resources/build/page.tsx | 35 ++++++++++ frontend/src/resources/build/util.tsx | 14 ++++ frontend/src/resources/deployment/card.tsx | 21 ++++++ frontend/src/resources/deployment/new.tsx | 56 ++++++++++++++++ frontend/src/resources/server/page.tsx | 40 ++++++++++++ .../index.tsx => resources/server/util.tsx} | 40 +----------- frontend/src/router.tsx | 4 +- 15 files changed, 262 insertions(+), 199 deletions(-) delete mode 100644 frontend/src/pages/build/index.tsx create mode 100644 frontend/src/resources/build/components/actions.tsx create mode 100644 frontend/src/resources/build/page.tsx create mode 100644 frontend/src/resources/build/util.tsx create mode 100644 frontend/src/resources/deployment/card.tsx create mode 100644 frontend/src/resources/deployment/new.tsx create mode 100644 frontend/src/resources/server/page.tsx rename frontend/src/{pages/server/index.tsx => resources/server/util.tsx} (73%) diff --git a/frontend/src/components/header/index.tsx b/frontend/src/components/header/index.tsx index e5d7819c3..edf177557 100644 --- a/frontend/src/components/header/index.tsx +++ b/frontend/src/components/header/index.tsx @@ -5,7 +5,7 @@ import { ChevronRight, Circle, LogOut } from "lucide-react"; import { Link, useLocation, useParams } from "react-router-dom"; import { useUser } from "@hooks"; -import { ServerName } from "@pages/server"; +import { ServerName } from "@resources/server/util"; import { DeploymentName } from "@resources/deployment/util"; export const Paths = () => { diff --git a/frontend/src/hooks.ts b/frontend/src/hooks.ts index 68f40e390..7aa2a5bf1 100644 --- a/frontend/src/hooks.ts +++ b/frontend/src/hooks.ts @@ -1,15 +1,25 @@ import { Types } from "@monitor/client"; import { client } from "./main"; -import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; +import { useQuery, useMutation } from "@tanstack/react-query"; import { useAtomValue, useSetAtom } from "jotai"; import { atomWithStorage } from "jotai/utils"; import { useNavigate } from "react-router-dom"; +import { WriteResponses } from "@monitor/client/dist/responses"; export const useRead = (req: T) => useQuery([req], () => client.read(req)); -export const useWrite = () => - useMutation((req: T) => client.write(req)); +export const useWrite = < + T extends Types.WriteRequest["type"], + P = Extract["params"] +>( + type: T +) => + useMutation( + [type], + async (params: P) => + (await client.write({ type, params } as any)) as WriteResponses[T] + ); export const useExecute = () => useMutation((req: T) => client.execute(req)); diff --git a/frontend/src/layouts/resources.tsx b/frontend/src/layouts/resources.tsx index f6eba3cf2..b5cf14aab 100644 --- a/frontend/src/layouts/resources.tsx +++ b/frontend/src/layouts/resources.tsx @@ -1,24 +1,43 @@ -import { Card, CardContent, CardHeader, CardTitle } from "@ui/card"; -import { ReactNode } from "react"; +import { useRead } from "@hooks"; +import { DeploymentCard } from "@resources/deployment/card"; +import { Button } from "@ui/button"; +import { Input } from "@ui/input"; +import { PlusCircle } from "lucide-react"; +import { useState } from "react"; + +export const Deployments = () => { + const deployments = useRead({ type: "ListDeployments", params: {} }).data; + const [search, set] = useState(""); -export const Resources = ({ - title, - newButton, - children, -}: { - title: string; - newButton: ReactNode; - children: ReactNode; -}) => { return ( - - - {title} - {newButton} - - -
{children}
-
-
+
+
+

Deployments

+
+ set(e.target.value)} + /> + +
+
+
+ {deployments?.map( + ({ id, name }) => + (search.includes(name) || name.includes(search)) && ( + + ) + )} +
+
); }; diff --git a/frontend/src/pages/build/index.tsx b/frontend/src/pages/build/index.tsx deleted file mode 100644 index 7f913e814..000000000 --- a/frontend/src/pages/build/index.tsx +++ /dev/null @@ -1,63 +0,0 @@ -import { Resource } from "@layouts/resource"; -import { useParams } from "react-router-dom"; -import { useExecute, useRead, useSetRecentlyViewed } from "@hooks"; -import { ActionButton } from "@components/util"; -import { Hammer } from "lucide-react"; -import { version_to_string } from "@util/helpers"; - -export const BuildName = ({ id }: { id: string }) => { - const builds = useRead({ type: "ListBuilds", params: {} }).data; - const build = builds?.find((b) => b.id === id); - return <>{build?.name ?? "..."}; -}; - -export const BuildVersion = ({ id }: { id: string }) => { - const builds = useRead({ type: "ListBuilds", params: {} }).data; - const build = builds?.find((b) => b.id === id); - return <>{version_to_string(build?.version) ?? "..."}; -}; - -export const RebuildBuild = ({ buildId }: { buildId: string }) => { - const { mutate, isLoading } = useExecute(); - return ( - } - onClick={() => - mutate({ type: "RunBuild", params: { build_id: buildId } }) - } - disabled={isLoading} - /> - ); -}; - -export const Build = () => { - const { buildId } = useParams(); - const push = useSetRecentlyViewed(); - - if (!buildId) return null; - push("Build", buildId); - - return ( - } - info={} - actions={} - tabs={[ - { - title: "Config", - component: "config", - }, - { - title: "Builder", - component: "builder", - }, - { - title: "Updates", - component: "updates", - }, - ]} - /> - ); -}; diff --git a/frontend/src/pages/dashboard/components/recently-viewed.tsx b/frontend/src/pages/dashboard/components/recently-viewed.tsx index a56ce4298..1c1117f4e 100644 --- a/frontend/src/pages/dashboard/components/recently-viewed.tsx +++ b/frontend/src/pages/dashboard/components/recently-viewed.tsx @@ -1,5 +1,5 @@ import { useGetRecentlyViewed } from "@hooks"; -import { BuildCard, DeploymentCard, NewDeployment, ServerCard } from ".."; +import { BuildCard, ServerCard } from ".."; import { useState } from "react"; import { DropdownMenu, @@ -12,6 +12,8 @@ import { } from "@ui/dropdown"; import { Button } from "@ui/button"; import { ChevronDown, PlusCircle } from "lucide-react"; +import { DeploymentCard } from "@resources/deployment/card"; +import { NewDeployment } from "@resources/deployment/new"; const NewButton = () => { const [open, set] = useState<"deployment" | "build" | "server" | boolean>( @@ -38,13 +40,21 @@ const NewButton = () => { - set("deployment")}> + set("deployment")} + > Deployment - set("build")}> + set("build")} + > Build - Server + + Server + diff --git a/frontend/src/pages/dashboard/index.tsx b/frontend/src/pages/dashboard/index.tsx index 9359500b7..bbbaf99a9 100644 --- a/frontend/src/pages/dashboard/index.tsx +++ b/frontend/src/pages/dashboard/index.tsx @@ -21,52 +21,7 @@ import { } from "@ui/dialog"; import { useState } from "react"; import { RecentlyViewed } from "./components/recently-viewed"; -import { ServerStats, ServerStatusIcon } from "@pages/server"; - -export const NewDeployment = ({ - open, - set, -}: { - open: boolean; - set: (b: boolean) => void; -}) => { - const { mutate } = useWrite(); - const [name, setName] = useState(""); - - return ( - - - - New Deployment - -
-
Deployment Name
- setName(e.target.value)} - /> -
- - - -
-
- ); -}; +import { ServerStats, ServerStatusIcon } from "@resources/server/util"; const NewBuild = ({ open, @@ -113,24 +68,6 @@ const NewBuild = ({ ); }; -export const DeploymentCard = ({ id }: { id: string }) => { - const deployments = useRead({ type: "ListDeployments", params: {} }).data; - const deployment = deployments?.find((d) => d.id === id); - if (!deployment) return null; - return ( - - - - {deployment.name} - - {deployment.status ?? "not deployed"} - - - - - ); -}; - export const ServerCard = ({ id }: { id: string }) => { const servers = useRead({ type: "ListServers", params: {} }).data; const server = servers?.find((server) => server.id === id); diff --git a/frontend/src/pages/deployments/index.tsx b/frontend/src/pages/deployments/index.tsx index e3c568281..97ba84944 100644 --- a/frontend/src/pages/deployments/index.tsx +++ b/frontend/src/pages/deployments/index.tsx @@ -1,5 +1,6 @@ import { useRead } from "@hooks"; -import { DeploymentCard } from "@pages/dashboard"; +import { DeploymentCard } from "@resources/deployment/card"; +import { NewDeployment } from "@resources/deployment/new"; import { Button } from "@ui/button"; import { Input } from "@ui/input"; import { PlusCircle } from "lucide-react"; @@ -7,7 +8,8 @@ import { useState } from "react"; export const Deployments = () => { const deployments = useRead({ type: "ListDeployments", params: {} }).data; - const [search, set] = useState(""); + const [search, setSearch] = useState(""); + const [open, setOpen] = useState(false); return (
@@ -18,16 +20,18 @@ export const Deployments = () => { className="w-[300px]" placeholder="Search" value={search} - onChange={(e) => set(e.target.value)} + onChange={(e) => setSearch(e.target.value)} /> +
diff --git a/frontend/src/resources/build/components/actions.tsx b/frontend/src/resources/build/components/actions.tsx new file mode 100644 index 000000000..7e961dcff --- /dev/null +++ b/frontend/src/resources/build/components/actions.tsx @@ -0,0 +1,18 @@ +import { ActionButton } from "@components/util"; +import { useExecute } from "@hooks"; +import { Hammer } from "lucide-react"; + +export const RebuildBuild = ({ buildId }: { buildId: string }) => { + const { mutate, isLoading } = useExecute(); + return ( + } + onClick={() => + mutate({ type: "RunBuild", params: { build_id: buildId } }) + } + disabled={isLoading} + /> + ); +}; diff --git a/frontend/src/resources/build/page.tsx b/frontend/src/resources/build/page.tsx new file mode 100644 index 000000000..47ce89b37 --- /dev/null +++ b/frontend/src/resources/build/page.tsx @@ -0,0 +1,35 @@ +import { useSetRecentlyViewed } from "@hooks"; +import { Resource } from "@layouts/resource"; +import { BuildName, BuildVersion } from "./util"; +import { useParams } from "react-router-dom"; +import { RebuildBuild } from "./components/actions"; + +export const Build = () => { + const { buildId } = useParams(); + const push = useSetRecentlyViewed(); + + if (!buildId) return null; + push("Build", buildId); + + return ( + } + info={} + actions={} + tabs={[ + { + title: "Config", + component: "config", + }, + { + title: "Builder", + component: "builder", + }, + { + title: "Updates", + component: "updates", + }, + ]} + /> + ); +}; diff --git a/frontend/src/resources/build/util.tsx b/frontend/src/resources/build/util.tsx new file mode 100644 index 000000000..7df5425aa --- /dev/null +++ b/frontend/src/resources/build/util.tsx @@ -0,0 +1,14 @@ +import { useRead } from "@hooks"; +import { version_to_string } from "@util/helpers"; + +export const BuildName = ({ id }: { id: string }) => { + const builds = useRead({ type: "ListBuilds", params: {} }).data; + const build = builds?.find((b) => b.id === id); + return <>{build?.name ?? "..."}; +}; + +export const BuildVersion = ({ id }: { id: string }) => { + const builds = useRead({ type: "ListBuilds", params: {} }).data; + const build = builds?.find((b) => b.id === id); + return <>{version_to_string(build?.version) ?? "..."}; +}; diff --git a/frontend/src/resources/deployment/card.tsx b/frontend/src/resources/deployment/card.tsx new file mode 100644 index 000000000..b37f55492 --- /dev/null +++ b/frontend/src/resources/deployment/card.tsx @@ -0,0 +1,21 @@ +import { useRead } from "@hooks"; +import { Card, CardDescription, CardHeader, CardTitle } from "@ui/card"; +import { Link } from "react-router-dom"; + +export const DeploymentCard = ({ id }: { id: string }) => { + const deployments = useRead({ type: "ListDeployments", params: {} }).data; + const deployment = deployments?.find((d) => d.id === id); + if (!deployment) return null; + return ( + + + + {deployment.name} + + {deployment.status ?? "not deployed"} + + + + + ); +}; diff --git a/frontend/src/resources/deployment/new.tsx b/frontend/src/resources/deployment/new.tsx new file mode 100644 index 000000000..14ef3b99f --- /dev/null +++ b/frontend/src/resources/deployment/new.tsx @@ -0,0 +1,56 @@ +import { useState } from "react"; +import { Button } from "@ui/button"; +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@ui/dialog"; +import { Input } from "@ui/input"; +import { useWrite } from "@hooks"; +import { useNavigate } from "react-router-dom"; + +export const NewDeployment = ({ + open, + set, +}: { + open: boolean; + set: (b: boolean) => void; +}) => { + const nav = useNavigate(); + const { mutateAsync } = useWrite("CreateDeployment"); + const [name, setName] = useState(""); + + return ( + + + + New Deployment + +
+
Deployment Name
+ setName(e.target.value)} + /> +
+ + + +
+
+ ); +}; diff --git a/frontend/src/resources/server/page.tsx b/frontend/src/resources/server/page.tsx new file mode 100644 index 000000000..2819c6e44 --- /dev/null +++ b/frontend/src/resources/server/page.tsx @@ -0,0 +1,40 @@ +import { useSetRecentlyViewed } from "@hooks"; +import { Resource } from "@layouts/resource"; +import { useParams } from "react-router-dom"; +import { ServerInfo, ServerName } from "./util"; + +export const Server = () => { + const { serverId } = useParams(); + const push = useSetRecentlyViewed(); + + // if (!serverId) return null; + // push("Server", serverId!); + if (!serverId) return null; + push("Server", serverId); + + return ( + } + info={} + actions="" + tabs={[ + { + title: "Config", + component: <>config, + }, + { + title: "Deployments", + component: <>server deployments, + }, + { + title: "Stats", + component: "server stats", + }, + { + title: "Updates", + component: <>updates, + }, + ]} + /> + ); +}; diff --git a/frontend/src/pages/server/index.tsx b/frontend/src/resources/server/util.tsx similarity index 73% rename from frontend/src/pages/server/index.tsx rename to frontend/src/resources/server/util.tsx index 0b8050cba..7bcf063b4 100644 --- a/frontend/src/pages/server/index.tsx +++ b/frontend/src/resources/server/util.tsx @@ -1,11 +1,9 @@ -import { useRead, useSetRecentlyViewed } from "@hooks"; -import { Resource } from "@layouts/resource"; +import { useRead } from "@hooks"; import { ServerStatus } from "@monitor/client/dist/types"; import { CardDescription } from "@ui/card"; import { cn } from "@util/helpers"; import { Circle, Cpu, Database, MemoryStick } from "lucide-react"; import { useEffect } from "react"; -import { useParams } from "react-router-dom"; export const ServerName = ({ serverId }: { serverId: string | undefined }) => { const servers = useRead({ type: "ListServers", params: {} }).data; @@ -80,39 +78,3 @@ export const ServerStatusIcon = ({ /> ); }; - -export const Server = () => { - const { serverId } = useParams(); - const push = useSetRecentlyViewed(); - - // if (!serverId) return null; - // push("Server", serverId!); - if (!serverId) return null; - push("Server", serverId); - - return ( - } - info={} - actions="" - tabs={[ - { - title: "Config", - component: <>config, - }, - { - title: "Deployments", - component: <>server deployments, - }, - { - title: "Stats", - component: "server stats", - }, - { - title: "Updates", - component: <>updates, - }, - ]} - /> - ); -}; diff --git a/frontend/src/router.tsx b/frontend/src/router.tsx index 5f6e327c2..da90ca947 100644 --- a/frontend/src/router.tsx +++ b/frontend/src/router.tsx @@ -3,12 +3,12 @@ import { Layout } from "@layouts/layout"; import { Login } from "@pages/auth/login"; import { Signup } from "@pages/auth/signup"; import { Dashboard } from "@pages/dashboard"; -import { Server } from "@pages/server"; import { Servers } from "@pages/servers"; import { Deployments } from "@pages/deployments"; import { Builds } from "@pages/builds"; -import { Build } from "@pages/build"; import { Deployment } from "@resources/deployment/page"; +import { Server } from "@resources/server/page"; +import { Build } from "@resources/build/page"; const router = createBrowserRouter([ {