diff --git a/pkgs/clan-app/ui/src/App.tsx b/pkgs/clan-app/ui/src/App.tsx deleted file mode 100644 index 894da8432..000000000 --- a/pkgs/clan-app/ui/src/App.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { createSignal } from "solid-js"; -import { makePersisted } from "@solid-primitives/storage"; -import { callApi } from "./api"; - -const [activeURI, setActiveURI] = makePersisted( - createSignal(null), - { - name: "activeURI", - storage: localStorage, - }, -); - -export { activeURI, setActiveURI }; - -const [clanList, setClanList] = makePersisted(createSignal([]), { - name: "clanList", - storage: localStorage, -}); - -export { clanList, setClanList }; - -(async function () { - const curr = activeURI(); - if (curr) { - const result = await callApi("show_clan_meta", { - flake: { identifier: curr }, - }); - console.log("refetched meta for ", curr); - if (result.status === "error") { - result.errors.forEach((error) => { - if (error.description === "clan directory does not exist") { - setActiveURI(null); - setClanList((clans) => clans.filter((clan) => clan !== curr)); - } - }); - } - } -})(); - -// ensure to null out activeURI on startup if the clan was deleted -// => throws user back to the view for selecting a clan diff --git a/pkgs/clan-app/ui/src/components/Sidebar/index.tsx b/pkgs/clan-app/ui/src/components/Sidebar/index.tsx index d841559b7..f527e8847 100644 --- a/pkgs/clan-app/ui/src/components/Sidebar/index.tsx +++ b/pkgs/clan-app/ui/src/components/Sidebar/index.tsx @@ -1,14 +1,12 @@ -import { For, createEffect, Show, type JSX, children } from "solid-js"; -import { A, RouteSectionProps } from "@solidjs/router"; -import { activeURI } from "@/src/App"; -import { createQuery } from "@tanstack/solid-query"; -import { callApi } from "@/src/api"; -import { AppRoute, routes } from "@/src/index"; +import { For, type JSX, Show } from "solid-js"; +import { RouteSectionProps } from "@solidjs/router"; +import { AppRoute, routes } from "@/src"; import { SidebarHeader } from "./SidebarHeader"; import { SidebarListItem } from "./SidebarListItem"; import { Typography } from "../Typography"; import "./css/sidebar.css"; import Icon, { IconVariant } from "../icon"; +import { clanMetaQuery } from "@/src/queries/clan-meta"; export const SidebarSection = (props: { title: string; @@ -42,26 +40,7 @@ export const SidebarSection = (props: { }; export const Sidebar = (props: RouteSectionProps) => { - createEffect(() => { - console.log("machines"); - console.log(routes); - }); - - const query = createQuery(() => ({ - queryKey: [activeURI(), "meta"], - queryFn: async () => { - const curr = activeURI(); - if (curr) { - const result = await callApi("show_clan_meta", { - flake: { identifier: curr }, - }); - console.log("refetched meta for ", curr); - if (result.status === "error") throw new Error("Failed to fetch data"); - - return result.data; - } - }, - })); + const query = clanMetaQuery(); return (
- + {(value) => }
diff --git a/pkgs/clan-app/ui/src/routes/disk/view.tsx b/pkgs/clan-app/ui/src/routes/disk/view.tsx index 5f90d0b9e..6c5ded434 100644 --- a/pkgs/clan-app/ui/src/routes/disk/view.tsx +++ b/pkgs/clan-app/ui/src/routes/disk/view.tsx @@ -1,13 +1,14 @@ import { callApi } from "@/src/api"; -import { activeURI } from "@/src/App"; -import { createQuery } from "@tanstack/solid-query"; -import { createEffect } from "solid-js"; +import { useQuery } from "@tanstack/solid-query"; +import { useClanContext } from "@/src/contexts/clan"; export function DiskView() { - const query = createQuery(() => ({ - queryKey: ["disk", activeURI()], + const { activeClanURI } = useClanContext(); + + const query = useQuery(() => ({ + queryKey: ["disk", activeClanURI()], queryFn: async () => { - const currUri = activeURI(); + const currUri = activeClanURI(); if (currUri) { // Example of calling an API const result = await callApi("get_inventory", { diff --git a/pkgs/clan-app/ui/src/routes/machines/create.tsx b/pkgs/clan-app/ui/src/routes/machines/create.tsx index 8941710f8..e6719e251 100644 --- a/pkgs/clan-app/ui/src/routes/machines/create.tsx +++ b/pkgs/clan-app/ui/src/routes/machines/create.tsx @@ -1,5 +1,4 @@ import { callApi, OperationArgs } from "@/src/api"; -import { activeURI } from "@/src/App"; import { Button } from "@/src/components/button"; import Icon from "@/src/components/icon"; import { TextInput } from "@/src/Form/fields/TextInput"; @@ -13,16 +12,19 @@ import { MachineAvatar } from "./avatar"; import { DynForm } from "@/src/Form/form"; import Fieldset from "@/src/Form/fieldset"; import Accordion from "@/src/components/accordion"; +import { useClanContext } from "@/src/contexts/clan"; type CreateMachineForm = OperationArgs<"create_machine">; export function CreateMachine() { const navigate = useNavigate(); + const { activeClanURI } = useClanContext(); + const [formStore, { Form, Field }] = createForm({ initialValues: { opts: { clan_dir: { - identifier: activeURI() || "", + identifier: activeClanURI() || "", }, machine: { tags: ["all"], @@ -39,7 +41,7 @@ export function CreateMachine() { const queryClient = useQueryClient(); const handleSubmit = async (values: CreateMachineForm) => { - const active_dir = activeURI(); + const active_dir = activeClanURI(); if (!active_dir) { toast.error("Open a clan to create the machine within"); return; @@ -60,9 +62,10 @@ export function CreateMachine() { toast.success(`Successfully created ${values.opts.machine.name}`); reset(formStore); - queryClient.invalidateQueries({ - queryKey: [activeURI(), "list_inv_machines"], + await queryClient.invalidateQueries({ + queryKey: [active_dir, "list_inv_machines"], }); + navigate("/machines"); } else { toast.error( diff --git a/pkgs/clan-app/ui/src/routes/machines/details.tsx b/pkgs/clan-app/ui/src/routes/machines/details.tsx index e33e0f4a2..64e0d7794 100644 --- a/pkgs/clan-app/ui/src/routes/machines/details.tsx +++ b/pkgs/clan-app/ui/src/routes/machines/details.tsx @@ -7,10 +7,9 @@ import { setValue, } from "@modular-forms/solid"; import { useNavigate, useParams, useSearchParams } from "@solidjs/router"; -import { createQuery, useQueryClient } from "@tanstack/solid-query"; +import { createQuery, useQuery, useQueryClient } from "@tanstack/solid-query"; import { createEffect, createSignal, For, Match, Show, Switch } from "solid-js"; -import { activeURI } from "@/src/App"; import { Button } from "@/src/components/button"; import Icon from "@/src/components/icon"; import { TextInput } from "@/src/Form/fields/TextInput"; @@ -29,9 +28,11 @@ import cx from "classnames"; import { VarsStep, VarsValues } from "./install/vars-step"; import Fieldset from "@/src/Form/fieldset"; import { - FileSelectorField, type FileDialogOptions, + FileSelectorField, } from "@/src/components/fileSelect"; +import { useClanContext } from "@/src/contexts/clan"; + type MachineFormInterface = MachineData & { sshKey?: File; disk?: string; @@ -81,7 +82,9 @@ interface InstallMachineProps { machine: MachineData; } const InstallMachine = (props: InstallMachineProps) => { - const curr = activeURI(); + const { activeClanURI } = useClanContext(); + + const curr = activeClanURI(); const { name } = props; if (!curr || !name) { return No Clan selected; @@ -95,7 +98,7 @@ const InstallMachine = (props: InstallMachineProps) => { const handleInstall = async (values: AllStepsValues) => { console.log("Installing", values); - const curr_uri = activeURI(); + const curr_uri = activeClanURI(); const target = values["1"].target; const diskValues = values["2"]; @@ -257,7 +260,7 @@ const InstallMachine = (props: InstallMachineProps) => { // @ts-expect-error: This cannot be undefined in this context. machine_id={props.name} // @ts-expect-error: This cannot be undefined in this context. - dir={activeURI()} + dir={activeClanURI()} handleNext={(data) => { const prev = getValue(formStore, "1"); setValue(formStore, "1", { ...prev, ...data }); @@ -277,7 +280,7 @@ const InstallMachine = (props: InstallMachineProps) => { // @ts-expect-error: This cannot be undefined in this context. machine_id={props.name} // @ts-expect-error: This cannot be undefined in this context. - dir={activeURI()} + dir={activeClanURI()} footer={