feat(ui): task logs as a table with expandable fields
This commit is contained in:
1 parent
e143362479
commit
849e24757d
4 files changed
+175
-126
No files matched your search
@@ -19,7 +19,8 @@ import type { DataTableColumn } from "./types";
|
||||
* under `storageKey`. Cells render through `#cell-<id>` slots, given the row;
|
||||
* `#actions` fills a trailing cell and `#empty` replaces the body when there
|
||||
* are no rows. With `rowLink`, the whole row opens that route, so controls
|
||||
* inside a cell must stop their click and keydown events.
|
||||
* inside a cell must stop their click and keydown events. With a `#detail`
|
||||
* slot and no `rowLink`, a row opens in place to show its detail beneath it.
|
||||
*/
|
||||
const props = defineProps<{
|
||||
columns: DataTableColumn<Row>[];
|
||||
@@ -29,7 +30,7 @@ const props = defineProps<{
|
||||
rowLink?: (row: Row) => string;
|
||||
rowLabel?: (row: Row) => string;
|
||||
}>();
|
||||
const slots = defineSlots<Record<string, (props: { row: Row }) => unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown }>();
|
||||
const slots = defineSlots<Record<string, (props: { row: Row }) => unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown; detail?: (props: { row: Row }) => unknown }>();
|
||||
|
||||
const router = useRouter();
|
||||
const chosen = useLocalStorage<string[]>(
|
||||
@@ -62,8 +63,18 @@ const sorted = computed(() => {
|
||||
const ariaSort = (id: string) => (sortBy.value?.id !== id ? "none" : sortBy.value.desc ? "descending" : "ascending");
|
||||
const sortIcon = (id: string) => (sortBy.value?.id !== id ? ArrowUpDown : sortBy.value.desc ? ArrowDown : ArrowUp);
|
||||
|
||||
const expandable = computed(() => !props.rowLink && !!slots.detail);
|
||||
const expanded = ref(new Set<string>());
|
||||
function open(row: Row): void {
|
||||
if (props.rowLink) void router.push(props.rowLink(row));
|
||||
if (props.rowLink) {
|
||||
void router.push(props.rowLink(row));
|
||||
return;
|
||||
}
|
||||
if (!expandable.value) return;
|
||||
const key = props.rowKey(row);
|
||||
const next = new Set(expanded.value);
|
||||
if (!next.delete(key)) next.add(key);
|
||||
expanded.value = next;
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -105,23 +116,29 @@ function open(row: Row): void {
|
||||
<TableRow v-if="!rows.length && slots.empty">
|
||||
<TableCell :colspan="shown.length + (trailing ? 1 : 0)"><slot name="empty" /></TableCell>
|
||||
</TableRow>
|
||||
<TableRow
|
||||
v-for="row in sorted"
|
||||
:key="rowKey(row)"
|
||||
:data-row-key="rowKey(row)"
|
||||
:tabindex="rowLink ? 0 : undefined"
|
||||
:role="rowLink ? 'link' : undefined"
|
||||
:aria-label="rowLink && rowLabel ? rowLabel(row) : undefined"
|
||||
:class="['align-top', rowLink && 'cursor-pointer']"
|
||||
@click="open(row)"
|
||||
@keydown.enter.self.prevent="open(row)"
|
||||
@keydown.space.self.prevent="open(row)"
|
||||
>
|
||||
<TableCell v-for="column in shown" :key="column.id" :class="column.class">
|
||||
<slot :name="`cell-${column.id}`" :row="row" />
|
||||
</TableCell>
|
||||
<TableCell v-if="trailing"><slot name="actions" :row="row" /></TableCell>
|
||||
</TableRow>
|
||||
<template v-for="row in sorted" :key="rowKey(row)">
|
||||
<TableRow
|
||||
:data-row-key="rowKey(row)"
|
||||
:tabindex="rowLink || expandable ? 0 : undefined"
|
||||
:role="rowLink ? 'link' : undefined"
|
||||
:aria-label="rowLink && rowLabel ? rowLabel(row) : undefined"
|
||||
:aria-expanded="expandable ? expanded.has(rowKey(row)) : undefined"
|
||||
:class="['align-top', (rowLink || expandable) && 'cursor-pointer']"
|
||||
@click="open(row)"
|
||||
@keydown.enter.self.prevent="open(row)"
|
||||
@keydown.space.self.prevent="open(row)"
|
||||
>
|
||||
<TableCell v-for="column in shown" :key="column.id" :class="column.class">
|
||||
<slot :name="`cell-${column.id}`" :row="row" />
|
||||
</TableCell>
|
||||
<TableCell v-if="trailing"><slot name="actions" :row="row" /></TableCell>
|
||||
</TableRow>
|
||||
<TableRow v-if="expandable && expanded.has(rowKey(row))" class="hover:bg-transparent">
|
||||
<TableCell :colspan="shown.length + (trailing ? 1 : 0)" class="bg-muted/40">
|
||||
<slot name="detail" :row="row" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</template>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</template>
|
||||
@@ -2,7 +2,9 @@
|
||||
import { computed, nextTick, ref, watch } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
|
||||
import LogRow from "@/base/LogRow.vue";
|
||||
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
|
||||
import { MetadataList } from "@/components/ui/metadata-list";
|
||||
import { fmtValue, LEVEL_COLOR, levelKind, shortTime, type LogEntry } from "@/lib/log";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Empty,
|
||||
@@ -191,13 +193,28 @@ watch([() => route.hash, current], async () => {
|
||||
if (focusedHash === route.hash) return;
|
||||
if (page.value) { page.value = null; newerPages.value = []; }
|
||||
await nextTick();
|
||||
const line = logFrame.value?.querySelector<HTMLElement>(`[data-log-id="${match[1]}"]`);
|
||||
const line = logFrame.value?.querySelector<HTMLElement>(`[data-row-key="${match[1]}"]`);
|
||||
if (!line) return;
|
||||
focusedHash = route.hash;
|
||||
line?.scrollIntoView({ block: "center" });
|
||||
line?.focus({ preventScroll: true });
|
||||
}, { immediate: true, flush: "post" });
|
||||
const TASK_FIELDS = ["task", "task_id"];
|
||||
// Fields the columns or the page already show: the time, the task this page
|
||||
// is about, and the stage and service columns.
|
||||
const SHOWN_FIELDS = new Set(["received_at", "task", "task_id", "stage", "service"]);
|
||||
const columns: DataTableColumn<LogEntry>[] = [
|
||||
{ id: "time", label: "Time", fixed: true, class: "w-0 whitespace-nowrap" },
|
||||
{ id: "level", label: "Level", fixed: true, class: "w-0" },
|
||||
{ id: "stage", label: "Stage", class: "w-0 whitespace-nowrap" },
|
||||
{ id: "service", label: "Service", hidden: true, class: "w-0 whitespace-nowrap" },
|
||||
{ id: "message", label: "Message", fixed: true },
|
||||
];
|
||||
const rowKey = (entry: LogEntry) => String(entry.id ?? `${entry.time}|${entry.message ?? entry.msg}`);
|
||||
const field = (entry: LogEntry, name: string) => fmtValue(entry.fields?.[name] ?? "");
|
||||
const details = (entry: LogEntry) =>
|
||||
Object.entries(entry.fields ?? {})
|
||||
.filter(([k]) => !SHOWN_FIELDS.has(k))
|
||||
.map(([label, value]) => ({ label, value: fmtValue(value), mono: true }));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -250,9 +267,24 @@ const TASK_FIELDS = ["task", "task_id"];
|
||||
read surface of its own and the tallest one on the page. -->
|
||||
<div
|
||||
ref="logFrame"
|
||||
class="max-h-[62vh] overflow-y-auto rounded-sm border border-border bg-card px-2"
|
||||
class="max-h-[62vh] overflow-y-auto rounded-sm border border-border bg-card"
|
||||
>
|
||||
<LogRow v-for="(entry, i) in view.entries" :key="entry.id ?? i" :entry="entry" :omit="TASK_FIELDS" :data-log-id="entry.id" tabindex="-1" />
|
||||
<DataTable :columns="columns" :rows="view.entries" :row-key="rowKey" storage-key="task-log">
|
||||
<template #cell-time="{ row }"><span class="font-mono text-xs text-fg-subtle">{{ shortTime(row.time) }}</span></template>
|
||||
<template #cell-level="{ row }">
|
||||
<span class="text-micro font-semibold tracking-[0.04em]" :class="LEVEL_COLOR[levelKind(row.level)]">{{ (row.level || "info").toUpperCase() }}</span>
|
||||
</template>
|
||||
<template #cell-stage="{ row }"><span class="font-mono text-xs text-fg-muted">{{ field(row, "stage") }}</span></template>
|
||||
<template #cell-service="{ row }"><span class="font-mono text-xs text-fg-muted">{{ field(row, "service") }}</span></template>
|
||||
<template #cell-message="{ row }">
|
||||
<span class="font-mono text-xs [overflow-wrap:anywhere]">{{ row.message || row.msg || "" }}</span>
|
||||
<span v-if="details(row).length" class="ml-2 text-xs text-fg-subtle">{{ details(row).length }} fields</span>
|
||||
</template>
|
||||
<template #detail="{ row }">
|
||||
<MetadataList v-if="details(row).length" :rows="details(row)" class="text-xs" />
|
||||
<span v-else class="text-xs text-fg-subtle">No fields</span>
|
||||
</template>
|
||||
</DataTable>
|
||||
</div>
|
||||
<div v-if="canPage" class="mt-2 flex flex-wrap items-center gap-2 px-1">
|
||||
<Button variant="outline" size="sm" :disabled="!canOlder" @click="loadOlder">Older</Button>
|
||||
|
||||
Reference in new issue
Block a user