From 031521550a235e7667b539203c165eb04e92ba83 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 8 Nov 2026 15:09:06 +1100 Subject: [PATCH] fix(ui): keep unchanged rows across live reloads on the dashboard and workflows (#1798) --- ui/src/lib/keep-unchanged.ts | 38 ++++++++++++++++++++++++++++++ ui/src/stores/tasks.ts | 25 ++++++++++++-------- ui/src/tasks/TasksPage.vue | 28 ++-------------------- ui/src/workflows/WorkflowsPage.vue | 23 +++++++++++------- ui/src/workflows/workflow-runs.ts | 7 +++--- 5 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 ui/src/lib/keep-unchanged.ts diff --git a/ui/src/lib/keep-unchanged.ts b/ui/src/lib/keep-unchanged.ts new file mode 100644 index 000000000..464c0da7c --- /dev/null +++ b/ui/src/lib/keep-unchanged.ts @@ -0,0 +1,38 @@ +// Live events re-read whole lists, and most of what comes back is unchanged. +// Keeping the previous object for an unchanged value lets Vue skip it: an +// unchanged row keeps its component, and a reload that changed nothing +// triggers nothing at all. + +const serializedCache = new WeakMap(); + +function serialized(value: object): string { + let json = serializedCache.get(value); + if (json === undefined) { + json = JSON.stringify(value); + serializedCache.set(value, json); + } + return json; +} + +/** The previous value when the incoming one has the same content. */ +export function keepValue(previous: T, incoming: T): T { + if (previous === incoming) return previous; + if (previous === null || incoming === null || typeof previous !== "object" || typeof incoming !== "object") + return incoming; + return serialized(previous) === serialized(incoming) ? previous : incoming; +} + +/** The incoming rows, each replaced by its previous object when unchanged, and + * the previous array itself when nothing changed. */ +export function keepRows(previous: readonly T[] | null, incoming: T[]): T[] { + if (!previous) return incoming; + const byId = new Map(previous.map((row) => [row.id, row])); + let changed = previous.length !== incoming.length; + const merged = incoming.map((row, index) => { + const old = byId.get(row.id); + const kept = old && serialized(old) === serialized(row) ? old : row; + if (kept !== previous[index]) changed = true; + return kept; + }); + return changed ? merged : (previous as T[]); +} diff --git a/ui/src/stores/tasks.ts b/ui/src/stores/tasks.ts index 15fbe5ec5..06d815ec1 100644 --- a/ui/src/stores/tasks.ts +++ b/ui/src/stores/tasks.ts @@ -1,9 +1,10 @@ import { defineStore } from "pinia"; -import { ref } from "vue"; +import { shallowRef } from "vue"; import type { ActivityDetailInput } from "../dashboard/activity-detail.ts"; import type { Setup } from "../dashboard/setup-preference.ts"; import { api } from "../lib/api.ts"; +import { keepRows, keepValue } from "../lib/keep-unchanged.ts"; export interface Summary { statuses?: Record; @@ -40,12 +41,14 @@ export function taskIDForEvent(event: ActivityEvent, bySource: Map { - const summary = ref(null); - const setup = ref(null); + // Shallow and replaced only on change: every task event reloads the board, + // and a reload that changed nothing must not re-render it. + const summary = shallowRef(null); + const setup = shallowRef(null); // null means loading, [] means loaded. - const tasks = ref(null); - const error = ref(null); - const taskIDsBySource = ref(new Map()); + const tasks = shallowRef(null); + const error = shallowRef(null); + const taskIDsBySource = shallowRef(new Map()); async function load(): Promise { error.value = null; @@ -57,16 +60,18 @@ export const useTasksStore = defineStore("tasks", () => { // which a failed fetch must never claim. api.tasks(), ]); + const kept = keepRows(tasks.value, nextTasks); + summary.value = keepValue(summary.value, nextSummary); + setup.value = keepValue(setup.value, nextSetup); + if (kept === tasks.value) return; const map = new Map(); - for (const task of nextTasks) { + for (const task of kept) { // A run that handed off yields its work item to its successor. if (task.owner && task.repo && task.issue_number && !task.handoff_to_task_id) map.set(`${task.owner}/${task.repo}#${task.issue_number}`, Number(task.id)); } taskIDsBySource.value = map; - summary.value = nextSummary; - setup.value = nextSetup; - tasks.value = nextTasks; + tasks.value = kept; } catch (err) { error.value = String((err as Error).message || err); } diff --git a/ui/src/tasks/TasksPage.vue b/ui/src/tasks/TasksPage.vue index b45ff9e81..9bfdf3c92 100644 --- a/ui/src/tasks/TasksPage.vue +++ b/ui/src/tasks/TasksPage.vue @@ -21,6 +21,7 @@ import { computed, nextTick, onMounted, ref, shallowRef, watch } from "vue"; import { useRoute, useRouter } from "vue-router"; import PageHeader from "@/base/PageHeader.vue"; +import { keepRows } from "@/lib/keep-unchanged"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { api } from "@/lib/api"; @@ -102,31 +103,6 @@ const state = computed<"loading" | "error" | "empty" | "no-match">(() => { const PAGE = 100; -// Each live event re-reads the board, and most rows come back unchanged. A row -// that is unchanged keeps its object, so the table skips it; a reload that -// changed nothing keeps the whole array, so nothing re-renders. -const rowJSON = new WeakMap(); -function serialized(task: Task): string { - let json = rowJSON.get(task); - if (json === undefined) { - json = JSON.stringify(task); - rowJSON.set(task, json); - } - return json; -} -function mergeRows(previous: Task[] | null, incoming: Task[]): Task[] { - if (!previous) return incoming; - const byId = new Map(previous.map((task) => [task.id, task])); - let changed = previous.length !== incoming.length; - const merged = incoming.map((task, index) => { - const old = byId.get(task.id); - const kept = old && serialized(old) === serialized(task) ? old : task; - if (kept !== previous[index]) changed = true; - return kept; - }); - return changed ? merged : previous; -} - // Every read bumps the generation; a response that settles after a newer read // started is dropped, so the previous filter's rows or next cursor never land // under the current filter. @@ -141,7 +117,7 @@ async function load() { try { const page = await api.taskPage({ status: status.value || undefined, limit }); if (current !== loadGeneration) return; - tasks.value = mergeRows(tasks.value, page.tasks); + tasks.value = keepRows(tasks.value, page.tasks); next.value = page.next; moreError.value = null; } catch (err) { diff --git a/ui/src/workflows/WorkflowsPage.vue b/ui/src/workflows/WorkflowsPage.vue index e194b5c08..164ab2f48 100644 --- a/ui/src/workflows/WorkflowsPage.vue +++ b/ui/src/workflows/WorkflowsPage.vue @@ -1,6 +1,6 @@