Skip to content

Commit e8500c4

Browse files
author
npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta
committed
perf(desktop): flatten timeline render rows
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
1 parent feb9ac4 commit e8500c4

3 files changed

Lines changed: 375 additions & 189 deletions

File tree

‎desktop/src/features/messages/lib/videoReviewContext.test.mjs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from "node:test";
33

44
import {
55
buildVideoReviewCommentsByRootId,
6+
buildVideoReviewCommentsForRoot,
67
buildVideoReviewContextForMessage,
78
hasVideoAttachment,
89
} from "./videoReviewContext.ts";
@@ -99,6 +100,64 @@ test("buildVideoReviewCommentsByRootId includes nested descendants", () => {
99100
);
100101
});
101102

103+
test("buildVideoReviewCommentsForRoot returns descendants for one root", () => {
104+
const video = message({
105+
id: "video",
106+
body: "![video](https://relay/media/a.mp4)",
107+
createdAt: 1,
108+
});
109+
const otherVideo = message({
110+
id: "other-video",
111+
body: "![video](https://relay/media/b.mp4)",
112+
createdAt: 2,
113+
});
114+
const firstComment = message({
115+
id: "first-comment",
116+
body: "[00:01] tighten this",
117+
createdAt: 4,
118+
parentId: "video",
119+
rootId: "video",
120+
});
121+
const nestedReply = message({
122+
id: "nested-reply",
123+
body: "agreed",
124+
createdAt: 5,
125+
parentId: "first-comment",
126+
rootId: "video",
127+
});
128+
const earlierComment = message({
129+
id: "earlier-comment",
130+
body: "[00:00] opener",
131+
createdAt: 3,
132+
parentId: "video",
133+
rootId: "video",
134+
});
135+
const otherComment = message({
136+
id: "other-comment",
137+
body: "different root",
138+
createdAt: 6,
139+
parentId: "other-video",
140+
rootId: "other-video",
141+
});
142+
143+
const comments = buildVideoReviewCommentsForRoot(
144+
[
145+
video,
146+
otherVideo,
147+
firstComment,
148+
nestedReply,
149+
earlierComment,
150+
otherComment,
151+
],
152+
"video",
153+
);
154+
155+
assert.deepEqual(
156+
comments.map((comment) => comment.id),
157+
["earlier-comment", "first-comment", "nested-reply"],
158+
);
159+
});
160+
102161
test("buildVideoReviewContextForMessage posts against the source video", async () => {
103162
const video = message({
104163
id: "video",

‎desktop/src/features/messages/lib/videoReviewContext.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ export function buildVideoReviewCommentsByRootId(
6161
return commentsByRootId;
6262
}
6363

64+
export function buildVideoReviewCommentsForRoot(
65+
messages: TimelineMessage[],
66+
rootId: string,
67+
): TimelineMessage[] {
68+
const messageById = new Map(messages.map((message) => [message.id, message]));
69+
const comments: TimelineMessage[] = [];
70+
71+
for (const message of messages) {
72+
let ancestorId = message.parentId ?? null;
73+
let hops = 0;
74+
const maxHops = messages.length + 1;
75+
76+
while (ancestorId && hops < maxHops) {
77+
if (ancestorId === rootId) {
78+
comments.push(message);
79+
break;
80+
}
81+
ancestorId = messageById.get(ancestorId)?.parentId ?? null;
82+
hops += 1;
83+
}
84+
}
85+
86+
comments.sort((left, right) => {
87+
if (left.createdAt !== right.createdAt) {
88+
return left.createdAt - right.createdAt;
89+
}
90+
return left.id.localeCompare(right.id);
91+
});
92+
93+
return comments;
94+
}
95+
6496
export function buildVideoReviewContextForMessage({
6597
channelId,
6698
channelName,

0 commit comments

Comments
 (0)