desktop: Key timeline runs and replies by id

So that a comment arriving above them doesn't re-render the rest of the
timeline and lose its state.
This commit is contained in:
Rūdolfs Ošiņš
2026-11-20 19:32:16 +01:00
parent ee80f7d6f4
commit f9f9009aea
4 changed files with 39 additions and 4 deletions
+2 -2
View File
@@ -14,7 +14,7 @@
import partial from "lodash/partial";
import * as roles from "@app/lib/roles";
import { mergeTimeline, timelineRuns } from "@app/lib/timelineRuns";
import { mergeTimeline, runKey, timelineRuns } from "@app/lib/timelineRuns";
import { authorForNodeId } from "@app/lib/utils";
import ExtendedTextarea from "@app/components/ExtendedTextarea.svelte";
@@ -182,7 +182,7 @@
<div class="discussion" class:flush>
<div class="timeline-rail">
<div class="activity-stream" class:has-runs={runs.length > 0}>
{#each runs as run, runIndex (runIndex)}
{#each runs as run (runKey(run))}
{#if run.kind === "thread"}
<ThreadComponent
thread={run.entry.thread}
+1 -1
View File
@@ -206,7 +206,7 @@
{#snippet repliesSnippet()}
<div class="replies-list">
{#each replies as reply}
{#each replies as reply (reply.id)}
<div
class="reply-box"
class:highlighted={highlightedCommentId === reply.id}>
+7
View File
@@ -114,3 +114,10 @@ export function timelineRuns<A>(
}
return result;
}
/// A run's identity across updates, so that a thread keeps its open reply
/// draft when comments are added or removed around it.
export function runKey<A>(run: Run<A>): string {
if (run.kind === "group") return `group:${run.entries[0].key}`;
return `${run.kind}:${run.entry.key}`;
}
+29 -1
View File
@@ -4,7 +4,7 @@ import type { Thread } from "@bindings/cob/thread/Thread";
import { describe, expect, test } from "vitest";
import type { Run, TimelineEntry } from "@app/lib/timelineRuns";
import { mergeTimeline, timelineRuns } from "@app/lib/timelineRuns";
import { mergeTimeline, runKey, timelineRuns } from "@app/lib/timelineRuns";
import { author, comment } from "./support/cobs";
@@ -148,3 +148,31 @@ describe("timelineRuns", () => {
).toEqual(["x", "y"]);
});
});
describe("runKey", () => {
test("a thread keeps its key when a run before it goes away", () => {
const authorOf = (data: Data) => data.by;
const before = timelineRuns(
[threadEntry("t1", alice), activity("a1", bob), threadEntry("t2", bob)],
authorOf,
);
const after = timelineRuns([threadEntry("t2", bob)], authorOf);
expect(runKey(after[0])).toBe(runKey(before[2]));
});
test("keys are unique within a timeline", () => {
const runs = timelineRuns(
[
activity("x", alice),
threadEntry("x", bob),
activity("y", bob),
activity("z", bob),
],
(data: Data) => data.by,
);
const keys = runs.map(runKey);
expect(new Set(keys).size).toBe(keys.length);
});
});