Files
archie/ui/src/bindings/use-bindings.ts
T
sam 1def531437 feat(webui): port the dashboard (Phases 3-5)
Every page from the deleted Preact dashboard, rebuilt in Vue 3 + shadcn-vue.
The navigation restructure landed earlier on this branch; this is the surfaces
behind it, and the two are now one app.

Dashboard, Tasks (list and detail), Configuration split six ways, Logs,
Inspector, Mappings, Bindings, Workflows, Skills, Curators, Channels, chat.

Components, not files: docs/prds/dashboard-port-brief.md requires one
component, one job, one file, with shared state in a module beside it. So a
page file composes and holds nothing, and a shape repeated across panes becomes
its own component (LogRow, PageHeader, PanelLoading, WorkflowBar).

Behaviour that had to survive, because each was a fix:

- Live activity's detail is clamped to one line with the payload on the title.
  Unclamped, one row made the dashboard 7,481px tall.
- Each health card takes its own height. Equalising left a mostly-empty box
  taking a quarter of the first screen once setup was done.
- Tasks: `tab`/`attempt` live in the query string and are written with
  router.replace, so a tab click adds no history entry and does not remount.
  The list's deep-link reveal honours prefers-reduced-motion.
- LOG_LEVELS is a wire contract (the server matches that CSV server-side), used
  unchanged by both log panes.
- The chat panel's Phase 5 list: a FAB that is the only toggle, a morph whose
  scale, radius and position interpolate together, no scrim, no close button,
  Escape returning focus to the launcher, Stop only while streaming.
- Unconfigured is not broken. A 501 on /api/version or the log stream renders
  what is true ("Not configured on this deployment") rather than blank, and a
  read-only Configuration says so instead of offering controls that cannot work.

Three things the port decided rather than carried:

- Button gains an `attention` variant and Badge the five status tones, in the
  project's own cva. The pill's dot did not come across: the Badge's shape is
  the affordance, and the same tone now covers a trend indicator.
- The icon module is not ported. Its name-to-glyph lookup is the pattern
  shadcn-vue's rules call out as wrong, so icons come from @lucide/vue at the
  call site. The five bespoke glyphs have no consumer yet and become components
  if a page draws them.
- The theme control left the topbar for /system/appearance, per the approved
  navigation design. ThemeToggle.vue and Placeholder.vue are deleted rather
  than orphaned: every route now resolves to a real page.

Also fixes the bundle freshness check failing on the first build after every
change. ui/dist is committed, so nothing ignores it and Tailwind's content
detection scanned it, making each build an input to the next.
2026-09-20 20:04:55 +10:00

127 lines
3.6 KiB
TypeScript

import { ref } from "vue";
import { api, ApiError } from "@/lib/api";
import {
bindingPayload,
type Binding,
type BindingDraft,
type MappingOption,
type WorkflowOption,
} from "./binding-draft";
/**
* Why the page has nothing to show. "unconfigured" is a daemon with no
* bindings store wired (501/503) -- the feature is off, which reads very
* differently from a daemon that answered badly.
*/
export type LoadFailureKind = "unconfigured" | "failed";
export interface LoadFailure {
kind: LoadFailureKind;
message: string;
}
function failureFor(err: unknown): LoadFailure {
const message = err instanceof Error && err.message ? err.message : String(err);
const status = err instanceof ApiError ? err.status : undefined;
return { kind: status === 501 || status === 503 ? "unconfigured" : "failed", message };
}
/**
* The bindings page's data: the three lists the list and the editor read, the
* load that fills them, and the three mutations. Every mutation goes through
* api.* so the CSRF and Content-Type headers are sent -- a bare fetch is
* refused 403/415 by archied's mutation guard.
*/
export function useBindings() {
// null means loading, [] means loaded and empty.
const bindings = ref<Binding[] | null>(null);
const mappings = ref<MappingOption[]>([]);
const workflows = ref<WorkflowOption[]>([]);
const failure = ref<LoadFailure | null>(null);
// A failed mutation is not a failed load: the list on screen is still
// accurate, so it keeps rendering and the failure lands beside it.
const actionFailure = ref<LoadFailure | null>(null);
const saving = ref(false);
const saveFailure = ref<string | null>(null);
async function load(): Promise<void> {
failure.value = null;
try {
const [b, m, w] = await Promise.all([
api.bindings<{ bindings?: Binding[] }>(),
api.mappings<{ mappings?: MappingOption[] }>(),
api.workflows<{ definitions?: WorkflowOption[] }>(),
]);
bindings.value = b.bindings || [];
mappings.value = m.mappings || [];
// Only enabled definitions are offerable: binding to a disabled one
// would persist a binding that can never dispatch.
workflows.value = (w.definitions || []).filter((d) => d.enabled);
} catch (err) {
failure.value = failureFor(err);
bindings.value = null;
}
}
function resetSaveFailure(): void {
saveFailure.value = null;
}
/** True when the binding was written, so the caller knows to close. */
async function save(draft: BindingDraft): Promise<boolean> {
saving.value = true;
saveFailure.value = null;
try {
const body = bindingPayload(draft);
if (draft.id) {
await api.bindingUpdate(String(draft.id), body);
} else {
await api.bindingCreate(body);
}
await load();
return true;
} catch (err) {
saveFailure.value = err instanceof Error && err.message ? err.message : String(err);
return false;
} finally {
saving.value = false;
}
}
async function approve(binding: Binding): Promise<void> {
actionFailure.value = null;
try {
await api.bindingApprove(String(binding.id));
await load();
} catch (err) {
actionFailure.value = failureFor(err);
}
}
async function remove(binding: Binding): Promise<void> {
actionFailure.value = null;
try {
await api.bindingDelete(String(binding.id));
await load();
} catch (err) {
actionFailure.value = failureFor(err);
}
}
return {
bindings,
mappings,
workflows,
failure,
actionFailure,
saving,
saveFailure,
load,
resetSaveFailure,
save,
approve,
remove,
};
}