Files
archie/ui/src/controls/ControlsPage.vue
T

122 lines
6.7 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import PageHeader from "@/base/PageHeader.vue";
import { Badge } from "@/components/ui/badge";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { MoreMenu } from "@/components/ui/more-menu";
import { ReadError } from "@/components/ui/read-error";
import { Skeleton } from "@/components/ui/skeleton";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Timestamp } from "@/components/ui/timestamp";
import { errors } from "@/copy/errors";
import { api, describeError, type ActionClass, type Judge } from "@/lib/api";
import { useRead } from "@/lib/use-read";
import { stateLabel } from "@/runs/state";
import ActionClassDetail from "./ActionClassDetail.vue";
import PolicyText from "./PolicyText.vue";
/** What bounds every action: hard rules, change-request rules, the guards'
* judges, and the approval level each class of action has earned. */
const judges = useRead(() => api.judges());
const classes = useRead(() => api.actionClasses());
onMounted(() => {
void judges.reload();
void classes.reload();
});
const route = useRoute();
const router = useRouter();
const TABS = [
{ id: "rules", label: "Rules" },
{ id: "guards", label: "Guards" },
{ id: "classes", label: "Action classes" },
];
const tab = computed(() => (TABS.some((t) => t.id === route.query.tab) ? String(route.query.tab) : "rules"));
const show = (id: unknown) => void router.replace({ query: { ...route.query, tab: id === "rules" ? undefined : String(id) } });
const judgeColumns: DataTableColumn<Judge>[] = [
{ id: "name", label: "Guard", fixed: true, width: "auto", sort: (j) => j.name },
{ id: "mode", label: "Mode", fixed: true, width: "8rem" },
{ id: "latest", label: "Last evaluation", hide: "md", width: "28rem" },
];
const classColumns: DataTableColumn<ActionClass>[] = [
{ id: "action", label: "Action", fixed: true, width: "auto", sort: (c) => `${c.connector}.${c.action}` },
{ id: "target", label: "Target", hide: "md", width: "14rem", sort: (c) => c.target_kind ?? "" },
{ id: "level", label: "Level", fixed: true, width: "13rem", sort: (c) => c.level },
{ id: "count", label: "Approved in a row", hide: "md", width: "11rem", sort: (c) => c.count },
{ id: "decisions", label: "Decisions", hidden: true, hide: "md", width: "7rem", sort: (c) => c.decisions },
{ id: "last", label: "Last decided", hide: "sm", width: "9rem", sort: (c) => (c.last_decided_at ? Date.parse(c.last_decided_at) : 0) },
];
const resetError = ref("");
async function reset(c: ActionClass): Promise<void> {
resetError.value = "";
try {
await api.resetActionClass(c.id);
} catch (cause) {
resetError.value = describeError(cause, errors.common.unknown);
}
await classes.reload();
}
</script>
<template>
<div>
<PageHeader title="Controls" />
<Tabs :model-value="tab" class="mb-4" @update:model-value="show">
<TabsList variant="line" class="w-full justify-start">
<TabsTrigger v-for="t in TABS" :key="t.id" :value="t.id" class="flex-none px-3">{{ t.label }}</TabsTrigger>
</TabsList>
</Tabs>
<template v-if="tab === 'rules'">
<PolicyText kind="hard-rules" title="Hard rules" />
<PolicyText kind="change-request" title="Change-request rules" />
</template>
<div v-else-if="tab === 'guards'">
<ReadError v-if="judges.error.value" :message="judges.error.value" retry-label="Retry" @retry="judges.reload" />
<Skeleton v-else-if="!judges.data.value" class="h-16 w-full" />
<DataTable v-else :columns="judgeColumns" :rows="judges.data.value" :row-key="(j) => j.name" storage-key="judges">
<template #cell-name="{ row }"><span class="font-medium">{{ row.name }}</span> <span class="text-caption text-fg-subtle">{{ row.version }}</span></template>
<template #cell-mode="{ row }">
<Badge :variant="row.advisory ? 'idle' : 'ok'" :title="row.latest?.advisory_reason || undefined">{{ row.advisory ? "Advisory" : "Enforcing" }}</Badge>
</template>
<template #cell-latest="{ row }">
<span v-if="row.latest" class="text-caption">
<Timestamp :at="row.latest.at" /><template v-if="row.latest.model"> · {{ row.latest.model }}</template><template v-if="row.latest.answer"> · {{ row.latest.answer }}</template>
<Badge v-if="row.latest.trusted === false" variant="warn" class="ml-1">Not trusted</Badge>
</span>
</template>
<template #empty><Empty><EmptyHeader><EmptyTitle>No guards</EmptyTitle></EmptyHeader></Empty></template>
</DataTable>
<p class="mt-2 text-caption text-fg-muted"><RouterLink class="underline" :to="{ path: '/settings/extensions', query: { kind: 'guards' } }">Manage guards</RouterLink></p>
</div>
<div v-else>
<p v-if="resetError" role="alert" class="mb-2 text-caption text-danger">{{ resetError }}</p>
<ReadError v-if="classes.error.value" :message="classes.error.value" retry-label="Retry" @retry="classes.reload" />
<Skeleton v-else-if="!classes.data.value" class="h-16 w-full" />
<DataTable v-else :columns="classColumns" :rows="classes.data.value" :row-key="(c) => c.id" storage-key="action-classes">
<template #cell-action="{ row }"><span class="font-mono text-caption">{{ row.connector }}.{{ row.action }}</span></template>
<template #cell-target="{ row }">{{ row.target_kind }}<span v-if="row.environment" class="text-caption text-fg-muted"> · {{ row.environment }}</span></template>
<template #cell-level="{ row }">
<Badge variant="outline" :title="row.tightened_because || undefined">{{ stateLabel(row.level) }}</Badge>
<span v-if="row.level !== row.default" class="text-caption text-fg-muted"> from {{ stateLabel(row.default) }}</span>
</template>
<template #cell-count="{ row }">{{ row.count }}</template>
<template #cell-decisions="{ row }">{{ row.decisions }}</template>
<template #cell-last="{ row }"><Timestamp :at="row.last_decided_at" /></template>
<template #detail="{ row }"><ActionClassDetail :id="row.id" /></template>
<template #actions="{ row }">
<MoreMenu :label="`${row.connector}.${row.action} actions`">
<DropdownMenuItem :disabled="row.level === row.default" @select="reset(row)">Reset to {{ stateLabel(row.default) }}</DropdownMenuItem>
</MoreMenu>
</template>
<template #empty><Empty><EmptyHeader><EmptyTitle>No action classes</EmptyTitle></EmptyHeader></Empty></template>
</DataTable>
</div>
</div>
</template>