Files
archie/ui/src/settings/VersionsCard.vue
T

186 lines
5.7 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Section } from "@/components/ui/section";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { ApiError, api } from "@/lib/api";
/**
* Per-component versions and the update that applies them: /api/version reports
* what is running, what the check command claims, what is available and the
* status between those (ok, update_available, drift, unknown).
*/
interface ComponentVersion {
id: string;
label: string;
install_type?: string;
reference?: string;
running_version?: string;
installed_claim?: string;
latest_available?: string;
status: "ok" | "update_available" | "drift" | "unknown";
}
const components = ref<ComponentVersion[]>([]);
const snapshot = ref<unknown>(null);
const available = ref<unknown[]>([]);
const canInstall = ref(false);
const notConfigured = ref(false);
const loading = ref(true);
const loadError = ref<string | null>(null);
const installing = ref(false);
const installProgress = ref<string[]>([]);
const installResult = ref<string | null>(null);
const installError = ref<string | null>(null);
const updateable = computed(() =>
components.value.filter((c) => c.status === "update_available"),
);
const statusKind: Record<
ComponentVersion["status"],
"ok" | "warn" | "danger" | "idle"
> = {
ok: "ok",
update_available: "warn",
drift: "danger",
unknown: "idle",
};
const statusLabel: Record<ComponentVersion["status"], string> = {
ok: "",
update_available: "Update available",
drift: "Version drift",
unknown: "Unknown",
};
async function load(): Promise<void> {
loading.value = true;
loadError.value = null;
try {
const [report, update] = await Promise.all([
api.version<{ components: ComponentVersion[] }>(),
api
.updateSnapshot<{ snapshot: unknown; available: unknown[] | null; can_install: boolean }>()
.catch((err) => {
if (err instanceof ApiError && err.status === 501) {
notConfigured.value = true;
return null;
}
throw err;
}),
]);
components.value = report.components;
if (update) {
snapshot.value = update.snapshot;
available.value = update.available ?? [];
canInstall.value = update.can_install;
}
} catch (err) {
loadError.value = err instanceof Error ? err.message : String(err);
} finally {
loading.value = false;
}
}
// The snapshot the install endpoint demands is the one the operator saw: the
// endpoint re-checks freshness and answers 409 when releases moved.
function installableSnapshot(): unknown {
return snapshot.value && available.value.length ? snapshot.value : null;
}
async function install(): Promise<void> {
const snap = installableSnapshot();
if (!snap || installing.value) return;
installing.value = true;
installError.value = null;
installProgress.value = [];
installResult.value = null;
try {
const result = await api.updateInstall<{
progress?: string[];
result?: { installed?: Record<string, string> };
}>(snap);
installProgress.value = result.progress ?? [];
const installed = result.result?.installed;
installResult.value = installed
? Object.entries(installed)
.map(([id, version]) => `${id} → ${version}`)
.join(", ")
: "update finished";
await load();
} catch (err) {
installError.value = err instanceof Error ? err.message : String(err);
} finally {
installing.value = false;
}
}
onMounted(load);
</script>
<template>
<p v-if="notConfigured" class="text-sm text-fg-muted">Update checks not configured.</p>
<Section v-else title="Versions">
<p v-if="loadError" class="text-sm text-danger">{{ loadError }}</p>
<p v-else-if="loading" class="text-sm text-fg-muted">Checking…</p>
<template v-else>
<Table>
<TableHeader>
<TableRow>
<TableHead>Component</TableHead>
<TableHead>Running</TableHead>
<TableHead>Available</TableHead>
<TableHead class="w-full"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="component in components" :key="component.id">
<TableCell>{{ component.label || component.id }}</TableCell>
<TableCell class="font-mono text-fg-muted">{{
component.running_version || component.installed_claim
}}</TableCell>
<TableCell class="font-mono text-fg-muted">{{ component.latest_available }}</TableCell>
<TableCell>
<Badge v-if="component.status !== 'ok'" :variant="statusKind[component.status]">{{
statusLabel[component.status]
}}</Badge>
</TableCell>
</TableRow>
</TableBody>
</Table>
<Button class="mt-4 self-start" :disabled="installing" @click="install">
{{
installing
? "Installing…"
: `Install ${updateable.map((c) => c.label || c.id).join(", ")}`
}}
</Button>
<div
v-if="installProgress.length"
class="mt-3 rounded-sm border border-border bg-muted p-3 font-mono text-xs"
>
<p v-for="(line, i) in installProgress" :key="i">{{ line }}</p>
</div>
<p v-if="installResult" class="mt-2 text-sm text-ok">
{{ installResult }}
</p>
<p v-if="installError" class="mt-2 text-sm text-danger">
{{ installError }}
</p>
</template>
</Section>
</template>