Files
offscale/web/useOffscaleServers.ts
pastilhasandClaude Opus 5 373d67e0a0 finish the rebrand: the parts a user reads, and the manifest that was wrong
The extraction landed months ago and the platform side is clean. What was left was
inside the plugin, and it was the half that shows.

── Names a user actually reads ──

The three registered panels were still called "Headscale servers", "Headscale" and
"Headscale" in the panel picker, and the server picker rendered "Headscale" as its
own heading. Those name OUR panels, not the upstream server, so they were simply
missed.

Deliberately NOT a find-and-replace. Most of the ~175 "headscale" strings in here
are correct — "no active Headscale server", "headscale timed out", "is this a
Headscale server?" all refer to the thing this plugin drives, and renaming them
would make the errors lie. What was fixed is the plugin's own identity, plus nine
stale `/headscale` route paths that are wrong rather than merely old (the route is
`/offscale`) and one reference to `queries/headscale.ts`, a file the extraction
deleted.

The console's pty session key moves from `headscale-console-<id>` to
`offscale-console-<id>`. That one has a real cost: a console open at the moment of
the change keeps running in the sidecar under the old key with nothing pointing at
it, until that sidecar restarts. Paid once, deliberately, rather than carrying the
pre-extraction name in persisted state forever.

── The manifest was missing a dependency it has always had ──

`sidecar/ssh.ts` spawns `ssh`, and so does the reachability probe. The manifest
said "Offscale needed nothing", so install checked for nothing and a host without
an ssh client failed at RUNTIME with a spawn error — precisely the failure
`osDependencies` exists to turn into a refusal at install time. PLUGIN.md listed
ssh as a host dependency all along; the manifest was the file that was wrong.

Missed because ssh is on effectively every Linux box, which is also why declaring
it costs nothing there and is the whole difference where it is absent.

── Documentation that said the opposite of the code ──

PLUGIN.md instructed the reader to carry `assist.ts` in "but do not wire it up",
as a marker. It has been wired for some time: `policy.ts` routes
`POST /_officer/policy/assist`, `PolicyView.tsx` renders `<PolicyAssistant />`, and
the manifest declares the route in `readOnlyWrites`. Corrected rather than deleted,
because that instruction is exactly the kind someone would follow — a person
tidying "unused" code would have found it wired and assumed the document knew
something they did not.

Present-tense identifiers corrected to the shipped names; the "before" columns of
the migration tables still say headscale, because that is what they record. A note
at the top says which is which.

tsgo clean, 21 plugin tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 04:23:28 +00:00

103 lines
3.8 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useClient } from 'hooks/useClient';
import type { OffscaleServer, OffscaleHealth, OffscaleSshTest } from './shared';
// The registered-servers cache. Every panel in the /offscale workspace reads this one query, so switching
// the active server anywhere updates the whole screen at once.
//
// Registration is validated server-side before anything is saved (reachable, >=0.29, key accepted), which
// means a POST can fail for perfectly ordinary reasons — a typo'd URL, a revoked key. Those are not
// exceptional here, so the mutations surface their message rather than swallowing it.
const SERVERS_KEY = ['offscale', 'servers'] as const;
const EMPTY: OffscaleServer[] = [];
const BASE = '/offscale/_officer/servers';
/**
* Readable message from a useClient rejection. It throws `{status, message}` where `message` is the raw
* body text — JSON `{error}` from our sidecar, but plain text from the platform's own 401/503 paths.
*/
export function offscaleErrorMessage(err: unknown): string {
const raw = (err as { message?: unknown } | null)?.message;
if (typeof raw !== 'string' || !raw) return 'Something went wrong';
try {
const parsed = JSON.parse(raw) as { error?: unknown };
if (typeof parsed.error === 'string' && parsed.error) return parsed.error;
} catch {
/* plain text */
}
return raw.slice(0, 300);
}
export type RegisterServerInput = { name?: string; url: string; apiKey: string; sshHost?: string };
/** `sshHost: ''` clears the console target; omitting it leaves whatever is stored alone. */
export type UpdateServerInput = { id: number; name?: string; url?: string; apiKey?: string; sshHost?: string };
export function useOffscaleServers() {
const { get, post, patch, delete: del } = useClient();
const qc = useQueryClient();
const query = useQuery({
queryKey: SERVERS_KEY,
queryFn: () => get<{ servers: OffscaleServer[] }>(BASE),
staleTime: 30_000,
});
const invalidate = () => qc.invalidateQueries({ queryKey: SERVERS_KEY });
const register = useMutation({
mutationFn: (input: RegisterServerInput) => post<{ server: OffscaleServer }>(BASE, input),
onSuccess: invalidate,
});
const update = useMutation({
mutationFn: ({ id, ...rest }: UpdateServerInput) => patch<{ server: OffscaleServer }>(`${BASE}/${id}`, rest),
onSuccess: invalidate,
});
const remove = useMutation({
mutationFn: (id: number) => del(`${BASE}/${id}`),
onSuccess: invalidate,
});
const activate = useMutation({
mutationFn: (id: number) => post<{ server: OffscaleServer }>(`${BASE}/${id}/activate`),
// Deleting or switching reshuffles which server is active, and every domain query is scoped to it.
onSuccess: () => qc.invalidateQueries({ queryKey: ['offscale'] }),
});
const servers = query.data?.servers ?? EMPTY;
return {
servers,
active: servers.find((s) => s.isActive) ?? null,
isLoading: query.isLoading,
error: query.error,
refetch: query.refetch,
register,
update,
remove,
activate,
};
}
/**
* Try to open a shell on a console target with the keys already on this machine. Takes the host rather than a
* server id so the form can test a value before it is saved — which is when a typo is still cheap to fix.
*/
export function useOffscaleSshTest() {
const { post } = useClient();
return useMutation({
mutationFn: (host: string) => post<OffscaleSshTest>('/offscale/_officer/ssh-test', { host }),
});
}
/** On-demand reachability probe for one server. Never automatic — it costs two upstream round trips. */
export function useOffscaleHealth() {
const { get } = useClient();
return useMutation({
mutationFn: (id: number) => get<OffscaleHealth>(`${BASE}/${id}/health`),
});
}