Skip to content

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

web: fix events missed from desktop side on startup

Abdullah Atta1172a8c67b16
diff --git a/apps/web/src/app-effects.tsx b/apps/web/src/app-effects.tsx
index 55cc01131..f2ae0fd56 100644
--- a/apps/web/src/app-effects.tsx
+++ b/apps/web/src/app-effects.tsx
@@ -25,6 +25,7 @@ import { useStore as useAnnouncementStore } from "./stores/announcement-store";
import { useStore as useSettingStore } from "./stores/setting-store";
import { scheduleBackups, scheduleFullBackups } from "./common/notices";
import {
+ handleInternalLink,
introduceFeatures,
resetFeatures,
scheduleExpiredNotesDeletion
@@ -56,9 +57,10 @@ export default function AppEffects() {
useEffect(
function initializeApp() {
initStore();
- initEditorStore();
(async function () {
+ await initEditorStore();
+ await attachDesktopListeners();
await resetFeatures();
await refreshNavItems();
await updateLastSynced();
@@ -201,54 +203,6 @@ export default function AppEffects() {
})();
}, [dialogAnnouncements]);
- useEffect(() => {
- const { unsubscribe } =
- desktop?.bridge.onCreateItem.subscribe(undefined, {
- onData(itemType) {
- switch (itemType) {
- case "note":
- useEditorStore.getState().newSession();
- break;
- case "notebook":
- hashNavigate("/notebooks/create", { replace: true });
- break;
- case "reminder":
- hashNavigate("/reminders/create", { replace: true });
- break;
- }
- }
- }) || {};
-
- return () => {
- unsubscribe?.();
- };
- }, []);
-
- useEffect(() => {
- const { unsubscribe } =
- desktop?.bridge.onOpenLink.subscribe(undefined, {
- onData(url) {
- const link = parseInternalLink(url);
- if (!link) return;
- if (link.type === "note") {
- useEditorStore.getState().openSession(link.id, {
- activeBlockId: link.params?.blockId || undefined
- });
- } else if (link.type === "notebook") {
- navigate(`/notebooks/${link.id}`);
- } else if (link.type === "tag") {
- navigate(`/tags/${link.id}`);
- } else if (link.type === "color") {
- navigate(`/colors/${link.id}`);
- }
- }
- }) || {};
-
- return () => {
- unsubscribe?.();
- };
- }, []);
-
return <React.Fragment />;
}
@@ -265,3 +219,27 @@ function getProcessingStatusFromType(type: ProcessingType) {
return [];
}
}
+
+async function attachDesktopListeners() {
+ const handlers = [
+ AppEventManager.subscribe(AppEvents.onCreateItem, onCreateItem),
+ AppEventManager.subscribe(AppEvents.onOpenLink, handleInternalLink)
+ ];
+
+ await desktop?.bridge.ready.query();
+ return handlers;
+}
+
+async function onCreateItem(itemType: string) {
+ switch (itemType) {
+ case "note":
+ useEditorStore.getState().newSession();
+ break;
+ case "notebook":
+ hashNavigate("/notebooks/create", { replace: true });
+ break;
+ case "reminder":
+ hashNavigate("/reminders/create", { replace: true });
+ break;
+ }
+}
diff --git a/apps/web/src/common/app-events.ts b/apps/web/src/common/app-events.ts
index bf5bf2a1b..9fdd8467c 100644
--- a/apps/web/src/common/app-events.ts
+++ b/apps/web/src/common/app-events.ts
@@ -41,5 +41,8 @@ export const AppEvents = {
revealItemInList: "list:revealItem",
toggleSideMenu: "app:openSideMenu",
- toggleEditor: "app:toggleEditor"
+ toggleEditor: "app:toggleEditor",
+
+ onOpenLink: "onOpenLink",
+ onCreateItem: "onCreateItem"
};
diff --git a/apps/web/src/common/desktop-bridge/index.desktop.ts b/apps/web/src/common/desktop-bridge/index.desktop.ts
index 04ea973f6..64d35c7ae 100644
--- a/apps/web/src/common/desktop-bridge/index.desktop.ts
+++ b/apps/web/src/common/desktop-bridge/index.desktop.ts
@@ -24,8 +24,6 @@ import { AppEventManager, AppEvents } from "../app-events";
import { TaskScheduler } from "../../utils/task-scheduler";
import { checkForUpdate } from "../../utils/updater";
import { showToast } from "../../utils/toast";
-import { db } from "../db";
-import { logManager } from "@notesnook/core";
import { store as settingStore } from "../../stores/setting-store";
export const desktop: ReturnType<typeof createTRPCProxyClient<AppRouter>> =
@@ -67,6 +65,16 @@ function attachListeners() {
attachListener(AppEvents.updateError)
);
+ desktop.bridge.onOpenLink.subscribe(
+ undefined,
+ attachListener(AppEvents.onOpenLink)
+ );
+
+ desktop.bridge.onCreateItem.subscribe(
+ undefined,
+ attachListener(AppEvents.onCreateItem)
+ );
+
// desktop.window.onClose.subscribe(undefined, {
// async onData() {
// try {
diff --git a/apps/web/src/common/index.ts b/apps/web/src/common/index.ts
index f58402b61..e5997793e 100644
--- a/apps/web/src/common/index.ts
+++ b/apps/web/src/common/index.ts
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Config from "../utils/config";
-import { hashNavigate, getCurrentHash } from "../navigation";
+import { hashNavigate, getCurrentHash, navigate } from "../navigation";
import { db } from "./db";
import {
areFeaturesAvailable,
@@ -39,7 +39,7 @@ import { readFile, showFilePicker } from "../utils/file-picker";
import { logger } from "../utils/logger";
import { PATHS } from "@notesnook/desktop";
import { TaskManager } from "./task-manager";
-import { EVENTS } from "@notesnook/core";
+import { EVENTS, parseInternalLink } from "@notesnook/core";
import { createWritableStream } from "./desktop-bridge";
import { FeatureDialog, FeatureKeys } from "../dialogs/feature-dialog";
import { User } from "@notesnook/core";
@@ -569,3 +569,20 @@ export async function scheduleExpiredNotesDeletion() {
await db.notes.deleteExpiredNotes();
});
}
+
+export async function handleInternalLink(url: string, openInNewTab?: boolean) {
+ const link = parseInternalLink(url);
+ if (!link) return;
+ if (link.type === "note") {
+ await useEditorStore.getState().openSession(link.id, {
+ activeBlockId: link.params?.blockId || undefined,
+ openInNewTab
+ });
+ } else if (link.type === "notebook") {
+ navigate(`/notebooks/${link.id}`);
+ } else if (link.type === "tag") {
+ navigate(`/tags/${link.id}`);
+ } else if (link.type === "color") {
+ navigate(`/colors/${link.id}`);
+ }
+}
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index d32117318..a80977140 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -68,6 +68,7 @@ import { showFeatureNotAllowedToast } from "../../common/toasts";
import { UpgradeDialog } from "../../dialogs/buy-dialog/upgrade-dialog";
import { ConfirmDialog } from "../../dialogs/confirm";
import { strings } from "@notesnook/intl";
+import { handleInternalLink } from "../../common";
export type OnChangeHandler = (
content: () => string,
@@ -423,12 +424,8 @@ function TipTap(props: TipTapProps) {
getAttachmentData: onGetAttachmentData,
openLink: (url, openInNewTab) => {
const link = parseInternalLink(url);
- if (link && link.type === "note") {
- useEditorStore.getState().openSession(link.id, {
- activeBlockId: link.params?.blockId || undefined,
- openInNewTab: openInNewTab
- });
- } else window.open(url, "_blank");
+ if (link) handleInternalLink(url, openInNewTab);
+ else window.open(url, "_blank");
}
};
}, [
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 4378eecfb..c16e02d0b 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -240,7 +240,7 @@ class EditorStore extends BaseStore<EditorStore> {
);
};
- init = () => {
+ init = async () => {
useSettingStore.subscribe(
(s) => s.hideNoteTitle,
(state) => {
@@ -533,11 +533,11 @@ class EditorStore extends BaseStore<EditorStore> {
if (activeTabId) {
const tab = this.get().tabs.find((t) => t.id === activeTabId);
if (!tab) return;
- rehydrateSession(tab.sessionId);
+ await rehydrateSession(tab.sessionId);
} else newSession();
};
- private rehydrateSession = (sessionId: string) => {
+ private rehydrateSession = async (sessionId: string) => {
const { openSession, openDiffSession, getSession, activateSession } =
this.get();
@@ -549,9 +549,9 @@ class EditorStore extends BaseStore<EditorStore> {
if (!session || !session.needsHydration) return;
if (session.type === "diff")
- openDiffSession(session.note.id, session.historySessionId);
+ return openDiffSession(session.note.id, session.historySessionId);
else
- openSession(session.note.id, {
+ return openSession(session.note.id, {
force: true
});
};