Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix title not updating in real time sync

Abdullah Atta0d99367a9319
diff --git a/apps/web/__e2e__/models/note-item.model.ts b/apps/web/__e2e__/models/note-item.model.ts
index 3a30e7062..7840a69a8 100644
--- a/apps/web/__e2e__/models/note-item.model.ts
+++ b/apps/web/__e2e__/models/note-item.model.ts
@@ -38,8 +38,8 @@ export class NoteItemModel extends BaseItemModel {
this.editor = new EditorModel(this.page);
}
- async openNote() {
- await this.click();
+ async openNote(openInNewTab?: boolean) {
+ await this.click({ middleClick: openInNewTab });
const title = await this.getTitle();
await this.editor.waitForLoading(title);
}
diff --git a/apps/web/__e2e__/sync.test.ts b/apps/web/__e2e__/sync.test.ts
index d37d7e5a4..d52c8b73e 100644
--- a/apps/web/__e2e__/sync.test.ts
+++ b/apps/web/__e2e__/sync.test.ts
@@ -20,6 +20,9 @@ import { test, Browser, expect } from "@playwright/test";
import { AppModel } from "./models/app.model";
import { USER } from "./utils";
+// run this test file sequentially
+test.describe.configure({ mode: "serial" });
+
async function createDevice(browser: Browser) {
// Create two isolated browser contexts
const context = await browser.newContext();
@@ -48,13 +51,13 @@ async function actAndSync<T>(
return results.slice(0, actions.length) as T[];
}
-const NOTE = {
- title: `Note ${makeid(20)}`
-};
-
-test(`edits in a note opened on 2 devices should sync in real-time`, async ({
+test(`content edits in a note opened on 2 devices in multiple tabs should sync in real-time`, async ({
browser
}, info) => {
+ const NOTE = {
+ title: `Note ${makeid(20)}`
+ };
+
info.setTimeout(70 * 1000);
const newContent = makeid(24).repeat(2);
@@ -71,6 +74,7 @@ test(`edits in a note opened on 2 devices should sync in real-time`, async ({
)[0];
const noteA = await notesA.findNote(NOTE);
await Promise.all([noteA, noteB].map((note) => note?.openNote()));
+ await noteA?.openNote(true);
if ((await notesB.editor.getContent("text")) !== "")
await actAndSync([deviceA, deviceB], notesB.editor.clear());
@@ -82,6 +86,52 @@ test(`edits in a note opened on 2 devices should sync in real-time`, async ({
expect(noteB).toBeDefined();
await expect(notesA.editor.content).toHaveText(newContent);
await expect(notesB.editor.content).toHaveText(newContent);
+ const tabsA = await notesA.editor.getTabs();
+ await tabsA[0].click();
+ await expect(notesA.editor.content).toHaveText(newContent);
+
+ await (await deviceA.goToSettings())?.logout();
+ await (await deviceB.goToSettings())?.logout();
+});
+
+test(`title edits in a note opened on 2 devices in multiple tabs should sync in real-time`, async ({
+ browser
+}, info) => {
+ const NOTE = {
+ title: `Note ${makeid(20)}`
+ };
+
+ info.setTimeout(70 * 1000);
+ const newContent = makeid(24).repeat(2);
+
+ const [deviceA, deviceB] = await Promise.all([
+ createDevice(browser),
+ createDevice(browser)
+ ]);
+
+ const [notesA, notesB] = await Promise.all(
+ [deviceA, deviceB].map((d) => d.goToNotes())
+ );
+ const noteB = (
+ await actAndSync([deviceA, deviceB], notesB.createNote(NOTE))
+ )[0];
+ const noteA = await notesA.findNote(NOTE);
+ await Promise.all([noteA, noteB].map((note) => note?.openNote()));
+ await noteA?.openNote(true);
+
+ if ((await notesB.editor.getContent("text")) !== "")
+ await actAndSync([deviceA, deviceB], notesB.editor.clear());
+ await expect(notesA.editor.content).toBeEmpty();
+ await expect(notesB.editor.content).toBeEmpty();
+ await actAndSync([deviceA, deviceB], notesB.editor.setTitle(newContent));
+
+ expect(noteA).toBeDefined();
+ expect(noteB).toBeDefined();
+ expect(await notesA.editor.getTitle()).toBe(newContent);
+ expect(await notesB.editor.getTitle()).toBe(newContent);
+ const tabsA = await notesA.editor.getTabs();
+ await tabsA[0].click();
+ expect(await notesA.editor.getTitle()).toBe(newContent);
await (await deviceA.goToSettings())?.logout();
await (await deviceB.goToSettings())?.logout();
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 751084a15..17a3ab311 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -302,6 +302,7 @@ function EditorView({
editor.updateContent(result.data);
} else if (isNote && session.note.title !== item.title) {
AppEventManager.publish(AppEvents.changeNoteTitle, {
+ sessionId: session.id,
title: item.title,
preventSave: true
});
diff --git a/apps/web/src/components/editor/title-box.tsx b/apps/web/src/components/editor/title-box.tsx
index b8d5c26f4..222c16d04 100644
--- a/apps/web/src/components/editor/title-box.tsx
+++ b/apps/web/src/components/editor/title-box.tsx
@@ -17,7 +17,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React, { useCallback, useEffect, useMemo, useRef } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState
+} from "react";
import { Input } from "@theme-ui/components";
import { useEditorStore } from "../../stores/editor-store";
import { debounceWithId } from "@notesnook/common";
@@ -40,7 +47,7 @@ function TitleBox(props: TitleBoxProps) {
const inputRef = useRef<HTMLInputElement>(null);
const pendingChanges = useRef(false);
// const id = useStore((store) => store.session.id);
- const sessionType = useEditorStore((store) => store.getActiveSession()?.type);
+ const sessionType = useEditorStore((store) => store.getSession(id)?.type);
const isMobile = useMobile();
const isTablet = useTablet();
const { editorConfig } = useEditorConfig();
@@ -64,13 +71,15 @@ function TitleBox(props: TitleBoxProps) {
[isMobile, isTablet]
);
- useEffect(() => {
+ useLayoutEffect(() => {
const session = useEditorStore.getState().getSession(id);
if (!session || !("note" in session) || !session.note || !inputRef.current)
return;
if (pendingChanges.current) return;
const { title } = session.note;
+ if (inputRef.current.value === title) return;
+
withSelectionPersist(
inputRef.current,
(input) => (input.value = title || "")
@@ -86,24 +95,24 @@ function TitleBox(props: TitleBoxProps) {
useEffect(() => {
const { unsubscribe } = AppEventManager.subscribe(
AppEvents.changeNoteTitle,
- ({ preventSave, title }: { title: string; preventSave: boolean }) => {
- if (!inputRef.current) return;
+ ({
+ preventSave,
+ title,
+ sessionId
+ }: {
+ title: string;
+ preventSave: boolean;
+ sessionId: string;
+ }) => {
+ if (!inputRef.current || sessionId !== id) return;
withSelectionPersist(
inputRef.current,
(input) => (input.value = title)
);
updateFontSize(title.length);
if (!preventSave) {
- const { getActiveTab } = useEditorStore.getState();
- const activeTab = getActiveTab();
- if (!activeTab) return;
pendingChanges.current = true;
- debouncedOnTitleChange(
- activeTab.sessionId,
- activeTab.sessionId,
- title,
- pendingChanges
- );
+ debouncedOnTitleChange(sessionId, sessionId, title, pendingChanges);
}
}
);
@@ -111,7 +120,7 @@ function TitleBox(props: TitleBoxProps) {
return () => {
unsubscribe();
};
- }, [updateFontSize]);
+ }, [updateFontSize, id]);
return (
<Input