Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix desktop sync on memo selection

tianma-if4099584cfb24
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index 61db1185..6bd97773 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -94,6 +94,7 @@ import {
import { createRepository } from "@/lib/repository";
import {
refreshWorkspaceData,
+ resolveCreatedMemoSelection,
resolveSyncedMemoId,
shouldNavigateHomeWhenOpeningMemo,
type WorkspaceRefreshMode,
@@ -670,6 +671,8 @@ export const WorkspaceApp = ({
const [selectedNotebookId, setSelectedNotebookId] = useState<string | null>(null);
const autoSelectedDemoNotebookRef = useRef(false);
const [selectedMemoId, setSelectedMemoId] = useState<string | null>(null);
+ const selectedMemoIdRef = useRef(selectedMemoId);
+ selectedMemoIdRef.current = selectedMemoId;
const [createdMemoEditId, setCreatedMemoEditId] = useState<string | null>(null);
const pendingCreatedMemoIdRef = useRef<string | null>(null);
const creatingMemoSelectionRef = useRef(false);
@@ -810,7 +813,14 @@ export const WorkspaceApp = ({
onSynced: async (memo, item) => {
if (item.kind === "memo.create") {
await replaceLocalMemoId(localDataScope, item.memoId, memo);
- if (selectedMemoId === item.memoId || pendingCreatedMemoIdRef.current === item.memoId) {
+ const remappedSelection = resolveCreatedMemoSelection(
+ selectedMemoIdRef.current,
+ pendingCreatedMemoIdRef.current,
+ item.memoId,
+ memo.id,
+ );
+ if (remappedSelection === memo.id) {
+ selectedMemoIdRef.current = memo.id;
setSelectedMemoId(memo.id);
// Keep the create intent attached to the remapped memo until
// the editor has consumed it. The list query may still contain
@@ -871,7 +881,7 @@ export const WorkspaceApp = ({
} finally {
setIsSyncingQueuedChanges(false);
}
- }, [localDataScope, queryClient, selectedMemoId]);
+ }, [localDataScope, queryClient]);
const discardConflictsNow = useCallback(async () => {
if (!isOnline) return;
diff --git a/apps/web/src/lib/workspace-refresh.test.mjs b/apps/web/src/lib/workspace-refresh.test.mjs
index 03d0617c..b653bf54 100644
--- a/apps/web/src/lib/workspace-refresh.test.mjs
+++ b/apps/web/src/lib/workspace-refresh.test.mjs
@@ -3,6 +3,7 @@ import { describe, it } from "node:test";
import {
BACKGROUND_WORKSPACE_REFRESH_INTERVAL_MS,
refreshWorkspaceData,
+ resolveCreatedMemoSelection,
resolveSyncedMemoId,
shouldNavigateHomeWhenOpeningMemo,
} from "./workspace-refresh.ts";
@@ -25,6 +26,12 @@ describe("refreshWorkspaceData", () => {
assert.equal(resolveSyncedMemoId(mappings, null), null);
});
+ it("remaps a created memo selection without closing over the selected memo state", () => {
+ assert.equal(resolveCreatedMemoSelection("memo_local_1", null, "memo_local_1", "memo_remote_1"), "memo_remote_1");
+ assert.equal(resolveCreatedMemoSelection("memo_other", "memo_local_1", "memo_local_1", "memo_remote_1"), "memo_remote_1");
+ assert.equal(resolveCreatedMemoSelection("memo_other", null, "memo_local_1", "memo_remote_1"), "memo_other");
+ });
+
it("pushes local changes before pulling and invalidating during a manual refresh", async () => {
const calls = [];
const result = await refreshWorkspaceData({
diff --git a/apps/web/src/lib/workspace-refresh.ts b/apps/web/src/lib/workspace-refresh.ts
index 2a434e6c..992f7139 100644
--- a/apps/web/src/lib/workspace-refresh.ts
+++ b/apps/web/src/lib/workspace-refresh.ts
@@ -10,6 +10,15 @@ export const resolveSyncedMemoId = (
memoId: string | null,
) => memoId ? memoIdMappings.get(memoId) ?? memoId : null;
+export const resolveCreatedMemoSelection = (
+ currentMemoId: string | null,
+ pendingMemoId: string | null,
+ temporaryMemoId: string,
+ remoteMemoId: string,
+) => currentMemoId === temporaryMemoId || pendingMemoId === temporaryMemoId
+ ? remoteMemoId
+ : currentMemoId;
+
export type WorkspaceRefreshResult = {
changed: number;
skipped: boolean;