Skip to content

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

web: fix tags & colors not updating/loading on opening a note

Abdullah Atta5a346aa7deea
diff --git a/apps/web/src/components/editor/header.tsx b/apps/web/src/components/editor/header.tsx
index 9c3d3195d..85a499f52 100644
--- a/apps/web/src/components/editor/header.tsx
+++ b/apps/web/src/components/editor/header.tsx
@@ -34,36 +34,28 @@ import { Tag } from "@notesnook/core";
type HeaderProps = { readonly: boolean; id: string };
function Header(props: HeaderProps) {
const { readonly, id } = props;
- const session = useEditorStore((store) =>
- store.getActiveSession(["default", "readonly"])
+ const tags = useEditorStore(
+ (store) => store.getActiveSession(["default", "readonly"])?.tags
);
- const tags = session?.tags || [];
- const refreshTags = useEditorStore((store) => store.refreshTags);
- useEffect(() => {
- refreshTags();
- }, [id, refreshTags, session?.type]);
-
- const setTag = useCallback(
- async function (noteId: string, tags: Tag[], value: string) {
- const oldTag = tags.find((t) => t.title === value);
- if (oldTag) {
- await db.relations.unlink(oldTag, { type: "note", id: noteId });
- } else {
- const id =
- (await db.tags.find(value))?.id ??
- (await db.tags.add({ title: value }));
- await db.relations.add(
- { id, type: "tag" },
- { type: "note", id: noteId }
- );
- await useTagStore.getState().refresh();
- }
- await refreshTags();
- await useNoteStore.getState().refresh();
- },
- [refreshTags]
- );
+ const setTag = useCallback(async function (
+ noteId: string,
+ tags: Tag[],
+ value: string
+ ) {
+ const oldTag = tags.find((t) => t.title === value);
+ if (oldTag) {
+ await db.relations.unlink(oldTag, { type: "note", id: noteId });
+ } else {
+ const id =
+ (await db.tags.find(value))?.id ??
+ (await db.tags.add({ title: value }));
+ await db.relations.add({ id, type: "tag" }, { type: "note", id: noteId });
+ await useTagStore.getState().refresh();
+ }
+ await useNoteStore.getState().refresh();
+ },
+ []);
return (
<>
@@ -82,7 +74,7 @@ function Header(props: HeaderProps) {
styles={{ container: { mr: 1 }, text: { fontSize: "body" } }}
/>
))}
- {!!session && !readonly && tags ? (
+ {!readonly && tags ? (
<Autosuggest
sessionId={id}
filter={(query) => db.lookup.tags(query).items(10)}
diff --git a/apps/web/src/components/tag/index.tsx b/apps/web/src/components/tag/index.tsx
index e7d3f973e..a00395b31 100644
--- a/apps/web/src/components/tag/index.tsx
+++ b/apps/web/src/components/tag/index.tsx
@@ -102,7 +102,6 @@ const menuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (
await db.tags.remove(...ids);
showToast("success", `${pluralize(ids.length, "tag")} deleted`);
await appStore.refreshNavItems();
- await useEditorStore.getState().refreshTags();
await tagStore.refresh();
await noteStore.refresh();
},
diff --git a/apps/web/src/stores/app-store.ts b/apps/web/src/stores/app-store.ts
index 2c058d5c7..13b97fdec 100644
--- a/apps/web/src/stores/app-store.ts
+++ b/apps/web/src/stores/app-store.ts
@@ -23,7 +23,6 @@ import { store as noteStore } from "./note-store";
import { store as notebookStore } from "./notebook-store";
import { store as trashStore } from "./trash-store";
import { store as tagStore } from "./tag-store";
-import { useEditorStore } from "./editor-store";
import { store as attachmentStore } from "./attachment-store";
import { store as monographStore } from "./monograph-store";
import { store as reminderStore } from "./reminder-store";
@@ -154,7 +153,6 @@ class AppStore extends BaseStore<AppStore> {
await attachmentStore.refresh();
await monographStore.refresh();
await settingStore.refresh();
- await useEditorStore.getState().refresh();
await this.refreshNavItems();
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 7d05540d4..ecca2da77 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -291,7 +291,7 @@ class EditorStore extends BaseStore<EditorStore> {
db.eventManager.subscribe(
EVENTS.databaseUpdated,
- (event: DatabaseUpdatedEvent) => {
+ async (event: DatabaseUpdatedEvent) => {
const { sessions, openSession, closeSessions, updateSession } =
this.get();
const clearIds: string[] = [];
@@ -380,37 +380,71 @@ class EditorStore extends BaseStore<EditorStore> {
});
} else if (
event.type === "unlink" &&
- event.reference.type === "color" &&
- event.types.includes("note")
+ ((event.reference.type === "color" &&
+ event.types.includes("note")) ||
+ (event.reference.type === "note" &&
+ event.types.includes("color")))
) {
for (const session of sessions) {
- if (!("color" in session) || !session.color) continue;
- const hasSameColor =
+ const ids =
"id" in event.reference
- ? session.color === event.reference.id
- : event.reference.ids.includes(session.color);
- if (!hasSameColor) continue;
+ ? [event.reference.id]
+ : event.reference.ids;
+ if (!("color" in session) || !session.color) continue;
+ if (
+ !ids.includes(session.id) &&
+ !ids.includes(session.note.id) &&
+ !ids.includes(session.color)
+ )
+ continue;
updateSession(session.id, undefined, {
color: undefined
});
}
} else if (
event.type === "unlink" &&
- event.reference.type === "note" &&
- event.types.includes("color")
+ ((event.reference.type === "tag" && event.types.includes("note")) ||
+ (event.reference.type === "note" && event.types.includes("tag")))
) {
for (const session of sessions) {
const ids =
"id" in event.reference
? [event.reference.id]
: event.reference.ids;
+ if (!("tags" in session) || !session.tags) continue;
if (
- session.type === "new" ||
- (!ids.includes(session.id) && !ids.includes(session.note.id))
+ !ids.includes(session.id) &&
+ !ids.includes(session.note.id) &&
+ session.tags.every((t) => !ids.includes(t.id))
)
continue;
+
updateSession(session.id, undefined, {
- color: undefined
+ tags: await getTags(session.note.id)
+ });
+ }
+ } else if (
+ event.type === "upsert" &&
+ event.item.toId &&
+ event.item.fromId &&
+ event.item.fromType === "tag" &&
+ event.item.toType === "note"
+ ) {
+ updateSession(event.item.toId, undefined, {
+ tags: await getTags(event.item.toId)
+ });
+ }
+ } else if (event.collection === "tags") {
+ if (event.type === "update") {
+ for (const session of sessions) {
+ if (
+ !("tags" in session) ||
+ session.tags?.every((t) => !event.ids.includes(t.id))
+ )
+ continue;
+ console.log("UDPATE");
+ updateSession(session.id, undefined, {
+ tags: await getTags(session.note.id)
});
}
}
@@ -437,21 +471,6 @@ class EditorStore extends BaseStore<EditorStore> {
}
};
- refreshTags = async () => {
- const { updateSession, getActiveSession } = this.get();
- const session = getActiveSession();
- if (!session || !("note" in session)) return;
- const tags = await db.relations
- .to({ id: session.note.id, type: "note" }, "tag")
- .selector.items(undefined, {
- sortBy: "dateCreated",
- sortDirection: "asc"
- });
- updateSession(session.id, ["readonly", "default"], { tags });
- };
-
- refresh = async () => {};
-
updateSession = <T extends SessionType[] = SessionType[]>(
id: string,
types: T | undefined,
@@ -646,38 +665,45 @@ class EditorStore extends BaseStore<EditorStore> {
},
!options.silent
);
- } else if (note.readonly) {
- this.addSession(
- {
- type: "readonly",
- note,
- id: note.id,
- pinned: session?.pinned,
- content,
- activeBlockId: options.activeBlockId
- },
- !options.silent
- );
} else {
const attachmentsLength = await db.attachments
.ofNote(note.id, "all")
.count();
-
- this.addSession(
- {
- type: "default",
- id: note.id,
- note,
- saveState: SaveState.Saved,
- sessionId: `${Date.now()}`,
- attachmentsLength,
- pinned: session?.pinned,
- content,
- preview: isPreview,
- activeBlockId: options.activeBlockId
- },
- !options.silent
- );
+ const tags = await getTags(note.id);
+ const colors = await db.relations.to(note, "color").get();
+ if (note.readonly) {
+ this.addSession(
+ {
+ type: "readonly",
+ note,
+ id: note.id,
+ pinned: session?.pinned,
+ content,
+ color: colors[0]?.fromId,
+ tags,
+ activeBlockId: options.activeBlockId
+ },
+ !options.silent
+ );
+ } else {
+ this.addSession(
+ {
+ type: "default",
+ id: note.id,
+ note,
+ saveState: SaveState.Saved,
+ sessionId: `${Date.now()}`,
+ attachmentsLength,
+ pinned: session?.pinned,
+ tags,
+ color: colors[0]?.fromId,
+ content,
+ preview: isPreview,
+ activeBlockId: options.activeBlockId
+ },
+ !options.silent
+ );
+ }
}
}
};
@@ -794,16 +820,7 @@ class EditorStore extends BaseStore<EditorStore> {
if (shouldRefreshNotes) useNoteStore.getState().refresh();
if (currentSession.type === "new") {
- this.addSession({
- type: "default",
- id,
- note,
- saveState: SaveState.Saved,
- sessionId,
- attachmentsLength,
- pinned: currentSession.pinned,
- content: partial.content
- });
+ await this.openSession(note, { force: true });
} else {
// update any conflicted session that has the same content opened
if (partial.content) {
@@ -1000,3 +1017,12 @@ async function waitForSync() {
db.eventManager.subscribe(EVENTS.syncCompleted, resolve, true);
});
}
+
+async function getTags(noteId: string) {
+ return await db.relations
+ .to({ id: noteId, type: "note" }, "tag")
+ .selector.items(undefined, {
+ sortBy: "dateCreated",
+ sortDirection: "asc"
+ });
+}