From 2953aefe9d7f5f939cdc499a7095ffa5ba7c977e Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 13 Sep 2024 16:44:35 +0200 Subject: [PATCH] UI: fix breakage in disk id api --- pkgs/webview-ui/app/src/api/disk.ts | 16 +++++++---- pkgs/webview-ui/app/src/api/index.ts | 10 ++++++- pkgs/webview-ui/app/src/api/inventory.ts | 27 +++++++++++++------ .../app/src/routes/machines/details.tsx | 14 ++++++++-- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/pkgs/webview-ui/app/src/api/disk.ts b/pkgs/webview-ui/app/src/api/disk.ts index 592451ff1..37ad908fb 100644 --- a/pkgs/webview-ui/app/src/api/disk.ts +++ b/pkgs/webview-ui/app/src/api/disk.ts @@ -1,20 +1,26 @@ +import { QueryClient } from "@tanstack/query-core"; import { get_inventory } from "./inventory"; export const instance_name = (machine_name: string) => `${machine_name}-single-disk` as const; export async function set_single_disk_id( + client: QueryClient, base_path: string, machine_name: string, disk_id: string, ) { - const inventory = await get_inventory(base_path); - if (!inventory.services) { + const r = await get_inventory(client, base_path); + if (r.status === "error") { + return r; + } + if (!r.data.services) { return new Error("No services found in inventory"); } - if (!inventory.services["single-disk"]) { - inventory.services["single-disk"] = {}; - } + const inventory = r.data; + inventory.services = inventory.services || {}; + inventory.services["single-disk"] = inventory.services["single-disk"] || {}; + inventory.services["single-disk"][instance_name(machine_name)] = { meta: { name: instance_name(machine_name), diff --git a/pkgs/webview-ui/app/src/api/index.ts b/pkgs/webview-ui/app/src/api/index.ts index 9bf104c68..2ad32591b 100644 --- a/pkgs/webview-ui/app/src/api/index.ts +++ b/pkgs/webview-ui/app/src/api/index.ts @@ -1,5 +1,5 @@ import schema from "@/api/API.json" assert { type: "json" }; -import { API } from "@/api/API"; +import { API, Error } from "@/api/API"; import { nanoid } from "nanoid"; import { Schema as Inventory } from "@/api/Inventory"; @@ -7,6 +7,14 @@ export type OperationNames = keyof API; export type OperationArgs = API[T]["arguments"]; export type OperationResponse = API[T]["return"]; +export type ApiEnvelope = + | { + status: "success"; + data: T; + op_key: string; + } + | Error; + export type Services = NonNullable; export type ServiceNames = keyof Services; export type ClanService = Services[T]; diff --git a/pkgs/webview-ui/app/src/api/inventory.ts b/pkgs/webview-ui/app/src/api/inventory.ts index 9554988d7..d85985ffb 100644 --- a/pkgs/webview-ui/app/src/api/inventory.ts +++ b/pkgs/webview-ui/app/src/api/inventory.ts @@ -1,12 +1,21 @@ import { QueryClient } from "@tanstack/solid-query"; -import { callApi, ClanServiceInstance, ServiceNames, Services } from "."; +import { + ApiEnvelope, + callApi, + ClanServiceInstance, + ServiceNames, + Services, +} from "."; +import { Schema as Inventory } from "@/api/Inventory"; export async function get_inventory(client: QueryClient, base_path: string) { const data = await client.ensureQueryData({ queryKey: [base_path, "inventory"], queryFn: () => { console.log("Refreshing inventory"); - return callApi("get_inventory", { base_path }); + return callApi("get_inventory", { base_path }) as Promise< + ApiEnvelope + >; }, revalidateIfStale: true, staleTime: 60 * 1000, @@ -51,7 +60,7 @@ export async function get_single_service( client: QueryClient, base_path: string, service_name: T, -) { +): Promise> { const instance_key = await get_first_instance_name( client, base_path, @@ -59,7 +68,7 @@ export async function get_single_service( ); if (!instance_key) { - return {}; + throw new Error("No instance found"); } const service: Services[T] | null = await get_service( client, @@ -67,10 +76,10 @@ export async function get_single_service( service_name, ); if (service) { - const clanServiceInstance = service[instance_key]; - return clanServiceInstance || {}; + const clanServiceInstance = service[instance_key] as ClanServiceInstance; + return clanServiceInstance; } - return {}; + throw new Error("No service found"); } export async function set_single_service( @@ -88,10 +97,12 @@ export async function set_single_service( const inventory = r.data; inventory.services = inventory.services || {}; inventory.services[service_name] = inventory.services[service_name] || {}; - // @ts-expect-error: This doesn't check + + // @ts-expect-error: This cannot be undefined, because of the line above inventory.services[service_name][instance_key] = service_config; console.log("saving inventory", inventory); return callApi("set_inventory", { + // @ts-expect-error: This doesn't check inventory, message: `update_single_service ${service_name}`, flake_dir: base_path, diff --git a/pkgs/webview-ui/app/src/routes/machines/details.tsx b/pkgs/webview-ui/app/src/routes/machines/details.tsx index 6e5499735..c9af7b02a 100644 --- a/pkgs/webview-ui/app/src/routes/machines/details.tsx +++ b/pkgs/webview-ui/app/src/routes/machines/details.tsx @@ -21,7 +21,11 @@ import { setValue, } from "@modular-forms/solid"; import { useParams } from "@solidjs/router"; -import { createQuery, QueryObserver } from "@tanstack/solid-query"; +import { + createQuery, + QueryObserver, + useQueryClient, +} from "@tanstack/solid-query"; import { createSignal, For, @@ -115,6 +119,7 @@ const InstallMachine = (props: InstallMachineProps) => { toast.success("Machine installed successfully"); } }; + const queryClient = useQueryClient(); const handleDiskConfirm = async (e: Event) => { e.preventDefault(); @@ -125,7 +130,12 @@ const InstallMachine = (props: InstallMachineProps) => { return; } - const r = await set_single_disk_id(curr_uri, props.name, disk_id); + const r = await set_single_disk_id( + queryClient, + curr_uri, + props.name, + disk_id, + ); if (!r) { toast.success("Disk set successfully"); setConfirmDisk(true);