Skip to content

Commit d0246bb

Browse files
committed
Fix crash while loading the user who resolved a thread
1 parent d074655 commit d0246bb

3 files changed

Lines changed: 159 additions & 10 deletions

File tree

‎packages/react/src/components/Comments/Comments.tsx‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ export const Comments = ({
3030

3131
// Adds "resolved by" comment if needed.
3232
if (thread.resolved && thread.resolvedUpdatedAt && thread.resolvedBy) {
33-
const resolvedByUser = users.get(thread.resolvedBy);
34-
if (!resolvedByUser) {
35-
throw new Error(
36-
`User ${thread.resolvedBy} resolved thread ${thread.id}, but their data could not be found.`,
37-
);
38-
}
39-
4033
const resolvedCommentIndex =
4134
thread.comments.findLastIndex(
4235
(comment) =>
@@ -49,9 +42,7 @@ export const Comments = ({
4942
<Components.Comments.Comment
5043
key={"resolved-comment"}
5144
className={"bn-thread-comment"}
52-
authorInfo={
53-
(thread.resolvedBy && users.get(thread.resolvedBy)) || "loading"
54-
}
45+
authorInfo={users.get(thread.resolvedBy) ?? "loading"}
5546
timeString={thread.resolvedUpdatedAt.toLocaleDateString(undefined, {
5647
month: "short",
5748
day: "numeric",
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { type PartialBlock, type User } from "@blocknote/core";
2+
import {
3+
CommentsExtension,
4+
DefaultThreadStoreAuth,
5+
} from "@blocknote/core/comments";
6+
import { YjsThreadStore } from "@blocknote/core/y";
7+
import * as Y from "@y/y";
8+
import { describe, expect } from "vite-plus/test";
9+
import { page } from "vite-plus/test/browser";
10+
import { ThreadsSidebar } from "./ThreadsSidebar.js";
11+
12+
const user: User = { id: "user", username: "Test user", avatarUrl: "" };
13+
14+
function setupThreadStore(onTestFinished: (cleanup: () => void) => void) {
15+
const doc = new Y.Doc();
16+
onTestFinished(() => doc.destroy());
17+
const threads = doc.get("threads");
18+
const threadStore = new YjsThreadStore(
19+
user.id,
20+
threads,
21+
new DefaultThreadStoreAuth(user.id, "editor"),
22+
);
23+
24+
return {
25+
threadStore,
26+
clearComments(this: void, threadId: string, createdAt?: Date) {
27+
// Publish an empty comment list without deleting the thread, as a custom
28+
// store can do while synchronizing comments separately from threads.
29+
doc.transact(() => {
30+
const thread = threads.getAttr(threadId);
31+
if (createdAt) {
32+
thread.setAttr("createdAt", createdAt.getTime());
33+
}
34+
thread.getAttr("comments").delete(0, 1);
35+
});
36+
},
37+
};
38+
}
39+
40+
function commentBody(content: string): PartialBlock[] {
41+
return [{ type: "paragraph", content }];
42+
}
43+
44+
describe("ThreadsSidebar", () => {
45+
describe("integration", () => {
46+
it("keeps an empty thread selectable until its comments arrive", async ({
47+
setupEditor,
48+
selectText,
49+
onTestFinished,
50+
}) => {
51+
const { threadStore, clearComments } = setupThreadStore(onTestFinished);
52+
const { editor } = await setupEditor({
53+
initialContent: commentBody("First anchor and Second anchor"),
54+
extensions: [
55+
CommentsExtension({
56+
threadStore,
57+
resolveUsers: async () => [user],
58+
}),
59+
],
60+
children: <ThreadsSidebar filter="all" sort="recent-activity" />,
61+
});
62+
const comments = editor.getExtension(CommentsExtension)!;
63+
selectText(editor, "First anchor");
64+
await comments.createThread({
65+
initialComment: { body: commentBody("Initial comment") },
66+
});
67+
const [threadId] = threadStore.getThreads().keys();
68+
selectText(editor, "Second anchor");
69+
await comments.createThread({
70+
initialComment: { body: commentBody("Another thread") },
71+
});
72+
73+
clearComments(threadId, new Date("2025-01-01"));
74+
75+
function cards() {
76+
return Array.from(
77+
document.querySelectorAll(".bn-threads-sidebar .bn-thread"),
78+
(card) => card.textContent,
79+
);
80+
}
81+
82+
await expect
83+
.poll(cards)
84+
.toEqual([
85+
expect.stringContaining("Second anchor"),
86+
expect.stringContaining("First anchor"),
87+
]);
88+
await expect
89+
.element(page.getByText("Initial comment", { exact: true }))
90+
.not.toBeInTheDocument();
91+
function firstMark() {
92+
return Array.from(document.querySelectorAll(".bn-thread-mark")).find(
93+
(mark) => mark.textContent === "First anchor",
94+
);
95+
}
96+
await page.elementLocator(firstMark()!).click();
97+
function floatingThread() {
98+
return document.querySelector(
99+
".bn-thread:not(.bn-threads-sidebar .bn-thread)",
100+
);
101+
}
102+
await expect
103+
.poll(() => floatingThread()?.querySelector(".bn-thread-composer"))
104+
.toBeTruthy();
105+
106+
await threadStore.addComment({
107+
threadId,
108+
comment: { body: commentBody("Comment arrived") },
109+
});
110+
111+
await expect.poll(() => cards()[0]).toContain("Comment arrived");
112+
await expect
113+
.poll(() => floatingThread()?.textContent)
114+
.toContain("Comment arrived");
115+
116+
await threadStore.deleteThread({ threadId });
117+
118+
await expect
119+
.poll(() => firstMark()?.getAttribute("data-orphan"))
120+
.toBe("true");
121+
await expect.poll(floatingThread).toBeNull();
122+
await expect.poll(cards).toHaveLength(1);
123+
});
124+
125+
it("shows a resolved empty thread while its resolving user loads", async ({
126+
setupEditor,
127+
onTestFinished,
128+
}) => {
129+
const { threadStore, clearComments } = setupThreadStore(onTestFinished);
130+
const thread = await threadStore.createThread({
131+
initialComment: { body: commentBody("Initial comment") },
132+
});
133+
clearComments(thread.id);
134+
await threadStore.resolveThread({ threadId: thread.id });
135+
const users = Promise.withResolvers<User[]>();
136+
await setupEditor({
137+
extensions: [
138+
CommentsExtension({
139+
threadStore,
140+
resolveUsers: () => users.promise,
141+
}),
142+
],
143+
children: <ThreadsSidebar filter="resolved" sort="recent-activity" />,
144+
});
145+
146+
await expect
147+
.poll(() =>
148+
document.querySelector(".bn-threads-sidebar .bn-resolved-text"),
149+
)
150+
.not.toBeNull();
151+
152+
users.resolve([user]);
153+
154+
await expect.element(page.getByText(user.username)).toBeVisible();
155+
});
156+
});
157+
});

‎tests/vite.config.browser.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const LOCAL_BROWSER_FILES = [
7979
"../packages/core/src/editor/managers/EventManager.browser.test.ts",
8080
"../packages/core/src/extensions/FormattingToolbar/FormattingToolbar.browser.test.ts",
8181
"../packages/core/src/extensions/SuggestionMenu/SuggestionMenu.browser.test.ts",
82+
"../packages/react/src/components/Comments/ThreadsSidebar.browser.test.tsx",
8283
"../packages/react/src/components/FormattingToolbar/DefaultButtons/BasicTextStyleButton.browser.test.tsx",
8384
"../packages/react/src/components/FormattingToolbar/DefaultButtons/ColorStyleButton.browser.test.tsx",
8485
"../packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.browser.test.tsx",

0 commit comments

Comments
 (0)