From 22d6d57e3afd49a0d03386e664bedea374d8a979 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Wed, 14 Aug 2024 13:16:14 +0200 Subject: [PATCH] Clan-app: dynamic router concept --- pkgs/webview-ui/app/package-lock.json | 9 ++ pkgs/webview-ui/app/package.json | 1 + pkgs/webview-ui/app/src/App.tsx | 29 +----- pkgs/webview-ui/app/src/Routes.tsx | 91 ------------------ pkgs/webview-ui/app/src/Sidebar.tsx | 73 ++++++++++----- .../app/src/components/MachineListItem.tsx | 10 +- pkgs/webview-ui/app/src/index.tsx | 92 ++++++++++++++++++- pkgs/webview-ui/app/src/layout/header.tsx | 4 +- pkgs/webview-ui/app/src/layout/layout.tsx | 22 ++--- .../app/src/routes/clan/clanDetails.tsx | 6 +- pkgs/webview-ui/app/src/routes/disk/view.tsx | 1 - pkgs/webview-ui/app/src/routes/hosts/view.tsx | 19 +--- .../app/src/routes/machines/[name]/view.tsx | 6 ++ .../app/src/routes/machines/create.tsx | 6 +- .../app/src/routes/machines/view.tsx | 9 +- .../app/src/routes/settings/index.tsx | 17 +--- .../app/src/routes/welcome/index.tsx | 6 +- 17 files changed, 194 insertions(+), 207 deletions(-) delete mode 100644 pkgs/webview-ui/app/src/Routes.tsx create mode 100644 pkgs/webview-ui/app/src/routes/machines/[name]/view.tsx diff --git a/pkgs/webview-ui/app/package-lock.json b/pkgs/webview-ui/app/package-lock.json index 5f435d99c..317efb017 100644 --- a/pkgs/webview-ui/app/package-lock.json +++ b/pkgs/webview-ui/app/package-lock.json @@ -12,6 +12,7 @@ "@floating-ui/dom": "^1.6.8", "@modular-forms/solid": "^0.21.0", "@solid-primitives/storage": "^3.7.1", + "@solidjs/router": "^0.14.2", "@tanstack/eslint-plugin-query": "^5.51.12", "@tanstack/solid-query": "^5.51.2", "material-icons": "^1.13.12", @@ -1551,6 +1552,14 @@ "solid-js": "^1.6.12" } }, + "node_modules/@solidjs/router": { + "version": "0.14.2", + "resolved": "https://registry.npmjs.org/@solidjs/router/-/router-0.14.2.tgz", + "integrity": "sha512-JaJe7XJcZTyOfMOIVHmLO+3wP3akm5QQesrDU4XLn/JRMxozBzCaNXBsK7F8pBuDgxzRRxTV8RvXeS09HGXv6Q==", + "peerDependencies": { + "solid-js": "^1.8.6" + } + }, "node_modules/@tailwindcss/typography": { "version": "0.5.13", "resolved": "https://registry.npmjs.org/@tailwindcss/typography/-/typography-0.5.13.tgz", diff --git a/pkgs/webview-ui/app/package.json b/pkgs/webview-ui/app/package.json index 356215a3e..8681e2830 100644 --- a/pkgs/webview-ui/app/package.json +++ b/pkgs/webview-ui/app/package.json @@ -41,6 +41,7 @@ "@floating-ui/dom": "^1.6.8", "@modular-forms/solid": "^0.21.0", "@solid-primitives/storage": "^3.7.1", + "@solidjs/router": "^0.14.2", "@tanstack/eslint-plugin-query": "^5.51.12", "@tanstack/solid-query": "^5.51.2", "material-icons": "^1.13.12", diff --git a/pkgs/webview-ui/app/src/App.tsx b/pkgs/webview-ui/app/src/App.tsx index a30f315eb..d5ad8b40e 100644 --- a/pkgs/webview-ui/app/src/App.tsx +++ b/pkgs/webview-ui/app/src/App.tsx @@ -1,15 +1,6 @@ -import { type Component, createEffect, createSignal } from "solid-js"; -import { Layout } from "./layout/layout"; -import { Route, Router } from "./Routes"; -import { Toaster } from "solid-toast"; -import { effect } from "solid-js/web"; +import { createSignal } from "solid-js"; import { makePersisted } from "@solid-primitives/storage"; -// Some global state -const [route, setRoute] = createSignal("machines"); - -export { route, setRoute }; - const [activeURI, setActiveURI] = makePersisted( createSignal(null), { @@ -26,21 +17,3 @@ const [clanList, setClanList] = makePersisted(createSignal([]), { }); export { clanList, setClanList }; - -const App: Component = () => { - effect(() => { - if (clanList().length === 0) { - setRoute("welcome"); - } - }); - return ( -
- - - - -
- ); -}; - -export default App; diff --git a/pkgs/webview-ui/app/src/Routes.tsx b/pkgs/webview-ui/app/src/Routes.tsx deleted file mode 100644 index 420502948..000000000 --- a/pkgs/webview-ui/app/src/Routes.tsx +++ /dev/null @@ -1,91 +0,0 @@ -import { Accessor, For, Match, Switch } from "solid-js"; -import { MachineListView } from "./routes/machines/view"; -import { colors } from "./routes/colors/view"; -import { CreateClan } from "./routes/clan/view"; -import { HostList } from "./routes/hosts/view"; -import { BlockDevicesView } from "./routes/blockdevices/view"; -import { Flash } from "./routes/flash/view"; -import { Settings } from "./routes/settings"; -import { Welcome } from "./routes/welcome"; -import { Deploy } from "./routes/deploy"; -import { CreateMachine } from "./routes/machines/create"; -import { DiskView } from "./routes/disk/view"; - -export type Route = keyof typeof routes; - -export const routes = { - createClan: { - child: CreateClan, - label: "Create Clan", - icon: "groups", - }, - machines: { - child: MachineListView, - label: "Machines", - icon: "devices_other", - }, - "machines/add": { - child: CreateMachine, - label: "create Machine", - icon: "add", - }, - hosts: { - child: HostList, - label: "hosts", - icon: "devices_other", - }, - flash: { - child: Flash, - label: "create_flash_installer", - icon: "devices_other", - }, - blockdevices: { - child: BlockDevicesView, - label: "blockdevices", - icon: "devices_other", - }, - colors: { - child: colors, - label: "Colors", - icon: "color_lens", - }, - settings: { - child: Settings, - label: "Settings", - icon: "settings", - }, - welcome: { - child: Welcome, - label: "welcome", - icon: "settings", - }, - deploy: { - child: Deploy, - label: "deploy", - icon: "content_copy", - }, - diskConfig: { - child: DiskView, - label: "diskConfig", - icon: "disk", - }, - "machines/edit": { - child: CreateMachine, - label: "Edit Machine", - icon: "edit", - }, -}; - -interface RouterProps { - route: Accessor; -} -export const Router = (props: RouterProps) => { - const { route } = props; - return ( - route {route()} not found

}> - - {([key, { child }]) => {child}} - -
- ); -}; diff --git a/pkgs/webview-ui/app/src/Sidebar.tsx b/pkgs/webview-ui/app/src/Sidebar.tsx index f548ed5cc..cb9bc3f55 100644 --- a/pkgs/webview-ui/app/src/Sidebar.tsx +++ b/pkgs/webview-ui/app/src/Sidebar.tsx @@ -1,14 +1,11 @@ -import { Accessor, For, Setter } from "solid-js"; -import { Route, routes } from "./Routes"; +import { For, Show } from "solid-js"; import { activeURI } from "./App"; import { createQuery } from "@tanstack/solid-query"; import { callApi } from "./api"; +import { A, RouteSectionProps } from "@solidjs/router"; +import { AppRoute, routes } from "./index"; -interface SidebarProps { - route: Accessor; - setRoute: Setter; -} -export const Sidebar = (props: SidebarProps) => { +export const Sidebar = (props: RouteSectionProps) => { const query = createQuery(() => ({ queryKey: [activeURI(), "meta"], queryFn: async () => { @@ -20,29 +17,57 @@ export const Sidebar = (props: SidebarProps) => { } }, })); - const { route, setRoute } = props; + return ( ); }; + +const RouteMenu = (props: { + class?: string; + routes: AppRoute[]; + prefix?: string; +}) => ( +
    + !r.hidden)}> + {(route) => ( +
  • + + + + } + > + {(children) => ( +
    + + {route.icon && ( + {route.icon} + )} + {route.label} + + +
    + )} +
    +
  • + )} +
    +
+); diff --git a/pkgs/webview-ui/app/src/components/MachineListItem.tsx b/pkgs/webview-ui/app/src/components/MachineListItem.tsx index 5eca6d334..92a245918 100644 --- a/pkgs/webview-ui/app/src/components/MachineListItem.tsx +++ b/pkgs/webview-ui/app/src/components/MachineListItem.tsx @@ -1,7 +1,7 @@ -import { Accessor, createSignal, Show } from "solid-js"; +import { createSignal, Show } from "solid-js"; import { callApi, SuccessData } from "../api"; import { Menu } from "./Menu"; -import { activeURI, setRoute } from "../App"; +import { activeURI } from "../App"; import toast from "solid-toast"; type MachineDetails = SuccessData<"list_inventory_machines">["data"][string]; @@ -63,9 +63,9 @@ export const MachineListItem = (props: MachineListItemProps) => {