| 1 | import { parseResponse } from "hono/client"; |
| 2 | import { createSignal, For, Show } from "solid-js"; |
| 3 | import { api, reason } from "../api.ts"; |
| 4 | import type { Health } from "../types/model.ts"; |
| 5 | import type { Domain, GpuShare } from "../types/vms.ts"; |
| 6 | import { showConfirmDialog } from "./Dialog.tsx"; |
| 7 | import { type StatusKind, StatusLabel } from "./Status.tsx"; |
| 8 | import { TabBar } from "./TabBar.tsx"; |
| 9 | import { toast } from "./Toast.tsx"; |
| 10 | import "./GpuSwitch.css"; |
| 11 | |
| 12 | const loading = (health: Health) => health === "starting" || health === "deploying" || health === "restarting"; |
| 13 | |
| 14 | /** Gives the GPU to `vm` for games or to one service. Who has it comes from the guest's power state and the services' health. */ |
| 15 | export function GpuSwitch(props: { vm: Domain & { gpu: GpuShare }; refetch: () => unknown }) { |
| 16 | const [sending, setSending] = createSignal(false); |
| 17 | const off = () => props.vm.state === "shutoff" || props.vm.state === "crashed"; |
| 18 | const services = () => props.vm.gpu.services ?? []; |
| 19 | const holder = () => services().find((service) => service.health !== "stopped"); |
| 20 | const name = (side: string) => services().find((service) => service.id === side)?.name ?? side; |
| 21 | const side = () => props.vm.gpu.switching ?? (!off() ? "games" : holder()?.id ?? null); |
| 22 | const busy = () => sending() || props.vm.gpu.switching !== null; |
| 23 | const progress = (): [StatusKind, string] | null => { |
| 24 | const { switching, failed } = props.vm.gpu; |
| 25 | const running = holder(); |
| 26 | if (switching === "games") return ["starting", running ? `stopping ${running.name}…` : `starting ${props.vm.name}…`]; |
| 27 | if (switching) { |
| 28 | if (!off()) return ["starting", `shutting down ${props.vm.name}…`]; |
| 29 | return ["starting", running && running.id !== switching ? `stopping ${running.name}…` : `loading ${name(switching)}…`]; |
| 30 | } |
| 31 | if (failed) return ["down", failed]; |
| 32 | if (!off() || props.vm.gpu.services === null) return null; |
| 33 | if (!running) return ["stopped", "idle"]; |
| 34 | if (loading(running.health)) return ["starting", `loading ${running.name}…`]; |
| 35 | if (running.health === "down" || running.health === "degraded") return [running.health, `${running.name} is ${running.health}`]; |
| 36 | return null; |
| 37 | }; |
| 38 | const send = async (to: string) => { |
| 39 | setSending(true); |
| 40 | try { |
| 41 | await parseResponse(api.vms[":name"].gpu.$post({ param: { name: props.vm.name }, json: { side: to } })); |
| 42 | await props.refetch(); |
| 43 | } finally { |
| 44 | setSending(false); |
| 45 | } |
| 46 | }; |
| 47 | const choose = (to: string) => { |
| 48 | const label = to === "games" ? "games" : name(to); |
| 49 | const running = holder(); |
| 50 | if (to !== "games" && !off()) { |
| 51 | showConfirmDialog({ |
| 52 | title: `Switch the GPU to ${label}?`, |
| 53 | description: `${props.vm.name} shuts down first. Anything unsaved in it is lost.`, |
| 54 | confirmLabel: `switch to ${label}`, |
| 55 | destructive: true, |
| 56 | onConfirm: () => send(to), |
| 57 | }); |
| 58 | } else if (running && running.id !== to) { |
| 59 | showConfirmDialog({ |
| 60 | title: `Switch the GPU to ${label}?`, |
| 61 | description: `${running.name} stops, along with anything it's working on.`, |
| 62 | confirmLabel: `switch to ${label}`, |
| 63 | onConfirm: () => send(to), |
| 64 | }); |
| 65 | } else { |
| 66 | send(to).catch((failure) => toast(`Couldn't switch the GPU. ${reason(failure)}`)); |
| 67 | } |
| 68 | }; |
| 69 | return ( |
| 70 | <span class="gpu-switch" aria-busy={busy()}> |
| 71 | <TabBar label="GPU for"> |
| 72 | <For each={[{ id: "games", name: "games" }, ...services()]}> |
| 73 | {(option) => ( |
| 74 | <button aria-pressed={side() === option.id} disabled={busy() || side() === option.id} onClick={() => choose(option.id)}> |
| 75 | {option.name} |
| 76 | </button> |
| 77 | )} |
| 78 | </For> |
| 79 | </TabBar> |
| 80 | <Show when={progress()}> |
| 81 | {(now) => <span class="gpu-progress" role="status" data-tip={now()[1]}><StatusLabel health={now()[0]}>{now()[1]}</StatusLabel></span>} |
| 82 | </Show> |
| 83 | </span> |
| 84 | ); |
| 85 | } |