feat(ui): task log opens newest first

This commit is contained in:
sam committed 2026-10-25 17:41:21 +11:00
1 parent f2b8681611
commit 33da08bce5
4 files changed
+30 -8

No files matched your search

+19 -3
View File
@@ -1,5 +1,7 @@
<script setup lang="ts">
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { computed } from "vue";
import { MetadataList } from "@/components/ui/metadata-list";
import { fmtValue, LEVEL_COLOR, levelKind, rowClass, shortTime, type LogEntry } from "@/lib/log";
@@ -18,12 +20,18 @@ const props = withDefaults(
/** Shows service and component columns by default, for logs spanning
* services; a task's own log shows its stage instead. */
services?: boolean;
/** Opens with the newest entry first; the Time header flips it. */
newestFirst?: boolean;
}>(),
{ omit: () => [], services: false },
{ omit: () => [], services: false, newestFirst: false },
);
// Entries arrive in log order, which is the order to sort time by: equal
// timestamps keep the order they were written in.
const position = computed(() => new Map(props.entries.map((entry, i) => [entry, i])));
const columns: DataTableColumn<LogEntry>[] = [
{ id: "time", label: "Time", fixed: true, class: "w-0 whitespace-nowrap" },
{ id: "time", label: "Time", fixed: true, class: "w-0 whitespace-nowrap", sort: (e) => position.value.get(e) ?? 0 },
{ id: "level", label: "Level", fixed: true, class: "w-0" },
{ id: "service", label: "Service", hidden: !props.services, class: "w-0 whitespace-nowrap" },
{ id: "component", label: "Component", hidden: !props.services, class: "w-0 whitespace-nowrap" },
@@ -40,7 +48,15 @@ const details = (entry: LogEntry) =>
</script>
<template>
<DataTable :columns="columns" :rows="entries" :row-key="rowKey" :storage-key="storageKey" :row-class="(e) => rowClass(e.level)" sticky-header>
<DataTable
:columns="columns"
:rows="entries"
:row-key="rowKey"
:storage-key="storageKey"
:row-class="(e) => rowClass(e.level)"
:initial-sort="newestFirst ? { id: 'time', desc: true } : undefined"
sticky-header
>
<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>
@@ -32,6 +32,8 @@ const props = defineProps<{
rowClass?: (row: Row) => string;
/** Keeps the header in view inside a scrolling container. */
stickyHeader?: boolean;
/** The sort the table opens with, on a column that has one. */
initialSort?: { id: string; desc: boolean };
}>();
const slots = defineSlots<Record<string, (props: { row: Row }) => unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown; detail?: (props: { row: Row }) => unknown }>();
@@ -48,10 +50,14 @@ function toggle(id: string, on: boolean): void {
}
// Sorting covers the loaded rows only.
const sortBy = ref<{ id: string; desc: boolean } | null>(null);
const sortBy = ref<{ id: string; desc: boolean } | null>(props.initialSort ?? null);
// The column a table opens sorted by only flips direction: its unsorted order
// is the order it was sorted from.
function cycleSort(id: string): void {
const current = sortBy.value;
sortBy.value = current?.id !== id ? { id, desc: false } : current.desc ? null : { id, desc: true };
if (current?.id !== id) sortBy.value = { id, desc: false };
else if (!current.desc) sortBy.value = { id, desc: true };
else sortBy.value = props.initialSort?.id === id ? { id, desc: false } : null;
}
const sorted = computed(() => {
const key = sortBy.value && props.columns.find((c) => c.id === sortBy.value?.id)?.sort;
+1 -1
View File
@@ -255,7 +255,7 @@ const rowKey = (entry: LogEntry) => String(entry.id ?? `${entry.time}|${entry.me
ref="logFrame"
class="max-h-[62vh] overflow-y-auto rounded-sm border border-border bg-card"
>
<LogTable :entries="view.entries" :row-key="rowKey" :omit="TASK_FIELDS" storage-key="task-log" />
<LogTable :entries="view.entries" :row-key="rowKey" :omit="TASK_FIELDS" storage-key="task-log" newest-first />
</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>