Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: keep edits typed during a note save visible and unsaved (#13157)
Adam Eivyd942866f4f5c
diff --git a/client/src/components/brain/tabs/NotesTab.jsx b/client/src/components/brain/tabs/NotesTab.jsxindex 8464f56b21..bcf0c3d624 100644--- a/client/src/components/brain/tabs/NotesTab.jsx+++ b/client/src/components/brain/tabs/NotesTab.jsx@@ -108,7 +108,7 @@ export default function NotesTab() {const { isConfirming: isConfirmingDelete, requestDelete, cancelDelete } = useConfirmDelete();// Owns the write plus the iCloud force-save escape hatch (#3717).- const { saving, save, forceOffered, dismissForce } = useNoteSave({+ const { saving, save, forceOffered, dismissForce, bufferChangedSince } = useNoteSave({vaultId: selectedVaultId,notePath: selectedNote?.path || null,content: noteContent@@ -314,10 +314,13 @@ export default function NotesTab() {const handleSaveNote = async (options) => {const scope = vaultScopeRef.current;const sequence = selectSeqRef.current;+ const submitted = noteContent;const data = await save(options);if (!data || !isCurrentScope(scope) || sequence !== selectSeqRef.current) return;setSelectedNote(data);- setEditing(false);+ // Text typed while the write was in flight was never submitted: keep it+ // visible and unsaved instead of closing the editor over it.+ if (!bufferChangedSince(submitted)) setEditing(false);toast.success('Note saved');loadNotes(); // Refresh metadata};diff --git a/client/src/components/brain/tabs/NotesTab.test.jsx b/client/src/components/brain/tabs/NotesTab.test.jsxindex 2b9e6f6672..ebc6115200 100644--- a/client/src/components/brain/tabs/NotesTab.test.jsx+++ b/client/src/components/brain/tabs/NotesTab.test.jsx@@ -368,6 +368,36 @@ describe('NotesTab request lifetimes', () => {expect(mockToast.success).not.toHaveBeenCalledWith('Note saved');});+ it('keeps text typed during a deferred save visible and unsaved, and the next save sends it', async () => {+ const saved = deferred();+ api.updateNote.mockReturnValueOnce(saved.promise);+ await renderTab();+ await act(async () => { fireEvent.click(screen.getByText('first')); });+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }));+ const editor = screen.getByRole('textbox', { name: 'Note content' });+ fireEvent.change(editor, { target: { value: 'Submitted body' } });+ fireEvent.click(screen.getByRole('button', { name: 'Save' }));+ fireEvent.change(editor, { target: { value: 'Later unsaved body' } });+ await act(async () => { saved.resolve(note('first', 'Submitted body')); });+ expect(screen.getByRole('textbox', { name: 'Note content' })).toHaveValue('Later unsaved body');+ api.updateNote.mockResolvedValueOnce(note('first', 'Later unsaved body'));+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'Save' })); });+ expect(api.updateNote).toHaveBeenLastCalledWith('a', 'first.md', 'Later unsaved body', { force: false });+ expect(screen.queryByRole('textbox', { name: 'Note content' })).toBeNull();+ });++ it('keeps text typed during a deferred save when the save fails', async () => {+ const saved = deferred();+ api.updateNote.mockReturnValueOnce(saved.promise);+ await renderTab();+ await act(async () => { fireEvent.click(screen.getByText('first')); });+ fireEvent.click(screen.getByRole('button', { name: 'Edit' }));+ fireEvent.click(screen.getByRole('button', { name: 'Save' }));+ fireEvent.change(screen.getByRole('textbox', { name: 'Note content' }), { target: { value: 'typed later' } });+ await act(async () => { saved.reject(new Error('nope')); });+ expect(screen.getByRole('textbox', { name: 'Note content' })).toHaveValue('typed later');+ });+it('does not remove the previous vault row when a delete finishes after browser back', async () => {const deleted = deferred();api.deleteNote.mockReturnValueOnce(deleted.promise);diff --git a/client/src/components/wiki/tabs/BrowseTab.jsx b/client/src/components/wiki/tabs/BrowseTab.jsxindex a1669c6eb1..c411bff8c2 100644--- a/client/src/components/wiki/tabs/BrowseTab.jsx+++ b/client/src/components/wiki/tabs/BrowseTab.jsx@@ -36,7 +36,7 @@ export default function BrowseTab({ vaultId, notes, rawNotes, allNotes, onRefresconst mountedRef = useMounted();// Owns the write plus the iCloud force-save escape hatch (#3717).- const { saving, save, forceOffered, dismissForce } = useNoteSave({+ const { saving, save, forceOffered, dismissForce, bufferChangedSince } = useNoteSave({vaultId,notePath: selectedNote?.path || null,content: noteContent@@ -59,10 +59,13 @@ export default function BrowseTab({ vaultId, notes, rawNotes, allNotes, onRefresconst handleSaveNote = async (options) => {if (!selectedNote) return;const sequence = noteSequence.current;+ const submitted = noteContent;const data = await save(options);if (!data || !mountedRef.current || sequence !== noteSequence.current) return;setSelectedNote(data);- setEditing(false);+ // Text typed while the write was in flight was never submitted: keep it+ // visible and unsaved instead of closing the editor over it.+ if (!bufferChangedSince(submitted)) setEditing(false);toast.success('Note saved');onRefresh();};diff --git a/client/src/components/wiki/tabs/BrowseTab.test.jsx b/client/src/components/wiki/tabs/BrowseTab.test.jsxindex 8df56192ea..f53bdb02dd 100644--- a/client/src/components/wiki/tabs/BrowseTab.test.jsx+++ b/client/src/components/wiki/tabs/BrowseTab.test.jsx@@ -1,4 +1,4 @@-import { render, screen, fireEvent, waitFor } from '@testing-library/react';+import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';import { MemoryRouter, useLocation } from 'react-router';import { describe, it, expect, vi, beforeEach } from 'vitest';import BrowseTab from './BrowseTab';@@ -186,3 +186,42 @@ describe('BrowseTab iCloud force save', () => {expect(screen.queryByRole('button', { name: 'Save anyway' })).toBeNull();});});++describe('BrowseTab save lifetimes', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ api.getNote.mockResolvedValue(sampleNote);+ });++ const openEditor = async () => {+ renderTab();+ fireEvent.click(screen.getByText('Example Source'));+ fireEvent.click(await screen.findByRole('button', { name: 'Edit' }));+ return screen.findByRole('textbox', { name: 'Note content' });+ };++ it('keeps text typed during a deferred save visible and unsaved, and the next save sends it', async () => {+ let resolveSave;+ api.updateNote.mockReturnValueOnce(new Promise(resolve => { resolveSave = resolve; }));+ const editor = await openEditor();+ fireEvent.change(editor, { target: { value: 'Submitted body' } });+ fireEvent.click(screen.getByRole('button', { name: /Save/ }));+ fireEvent.change(editor, { target: { value: 'Later unsaved body' } });+ await act(async () => { resolveSave({ ...sampleNote, content: 'Submitted body' }); });+ expect(screen.getByRole('textbox', { name: 'Note content' })).toHaveValue('Later unsaved body');+ api.updateNote.mockResolvedValueOnce({ ...sampleNote, content: 'Later unsaved body' });+ fireEvent.click(screen.getByRole('button', { name: /Save/ }));+ await waitFor(() => expect(api.updateNote).toHaveBeenLastCalledWith(+ 'v1', 'wiki/sources/example.md', 'Later unsaved body', { force: false },+ ));+ await waitFor(() => expect(screen.queryByRole('textbox', { name: 'Note content' })).toBeNull());+ });++ it('closes the editor when the buffer is unchanged during the save', async () => {+ api.updateNote.mockResolvedValueOnce({ ...sampleNote, content: 'Submitted body' });+ const editor = await openEditor();+ fireEvent.change(editor, { target: { value: 'Submitted body' } });+ fireEvent.click(screen.getByRole('button', { name: /Save/ }));+ await waitFor(() => expect(screen.queryByRole('textbox', { name: 'Note content' })).toBeNull());+ });+});diff --git a/client/src/hooks/useNoteSave.js b/client/src/hooks/useNoteSave.jsindex de905b4bb3..b51a2c0e84 100644--- a/client/src/hooks/useNoteSave.js+++ b/client/src/hooks/useNoteSave.js@@ -43,10 +43,14 @@ const CLEARED = { target: null, count: 0 };* @param {string|null} params.notePath - vault-relative path of the open note.* @param {string} params.content - editor buffer to write.* @returns {{ saving: boolean, save: (opts?: { force?: boolean }) => Promise<object|null>,- * forceOffered: boolean, dismissForce: () => void }}+ * forceOffered: boolean, dismissForce: () => void,+ * bufferChangedSince: (submitted: string) => boolean }}* `save` resolves the updated note, or `null` when the write did not happen —* either because it was refused (the API layer has already toasted the reason)* or because there was nothing to save / a save was already in flight.+ * The textarea stays editable while a write is in flight, so a completion+ * handler passes the content it submitted to `bufferChangedSince` and keeps+ * the editor open (rather than discarding newer text) when it returns true.*/export function useNoteSave({ vaultId, notePath, content }) {const [saving, setSaving] = useState(false);@@ -57,6 +61,8 @@ export function useNoteSave({ vaultId, notePath, content }) {// each extra forced write strands another libuv threadpool thread for the life// of the process. Same reason ⌘S must not stack.const inFlight = useRef(false);+ const contentRef = useRef(content);+ contentRef.current = content;// Identity of the open note. The path ALONE is not enough: vaults routinely// share basenames (`index.md`, `README.md`, daily notes), so a refusal recorded@@ -97,7 +103,9 @@ export function useNoteSave({ vaultId, notePath, content }) {&& refusal.target === target&& refusal.count >= REFUSALS_BEFORE_FORCE;- return { saving, save, forceOffered, dismissForce: clearRefusals };+ const bufferChangedSince = useCallback(submitted => contentRef.current !== submitted, []);++ return { saving, save, forceOffered, dismissForce: clearRefusals, bufferChangedSince };}export default useNoteSave;