Skip to content

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

fix: compare the SongBook draft as the save would send it, and hide the discard row mid-save (#3902)

[._.]/ Adam Eivy50137cca4be3
diff --git a/client/src/pages/SongBookViewer.jsx b/client/src/pages/SongBookViewer.jsx
index 2ac825e3b3..36fa84d40f 100644
--- a/client/src/pages/SongBookViewer.jsx
+++ b/client/src/pages/SongBookViewer.jsx
@@ -98,13 +98,25 @@ const toDraft = (song) => ({
const parseTags = (raw) => raw.split(',').map((t) => t.trim()).filter(Boolean);
-// Draft-vs-saved comparison for the unsaved-changes guard. Both sides come from
-// toDraft (flat strings), except `capo`: the number input hands back a STRING,
-// so retyping the stored value ('2' vs 2) would otherwise read as an edit. An
-// emptied capo field compares as 0, which is what the save would clamp it to.
-const draftsEqual = (a, b) => Object.keys(a).every((k) => (
- k === 'capo' ? Number(a.capo || 0) === Number(b.capo || 0) : a[k] === b[k]
-));
+// Draft-vs-saved comparison for the unsaved-changes guard. It compares what a
+// SAVE would send, not the raw form text — otherwise edits the save normalizes
+// away read as unsaved work and prompt for nothing. Each field is normalized
+// exactly as `save` normalizes it: trimmed strings, `parseTags` for the tag
+// input ('a,b' and 'a, b,' both save as ['a','b']), and a numeric capo (the
+// number input hands back a STRING, and an emptied field clamps to 0).
+// `notes`/`text` go to the server verbatim, so they compare verbatim.
+const TRIMMED_DRAFT_FIELDS = ['title', 'artist', 'key', 'tuning', 'sourceUrl'];
+const normalizeDraftField = (draft, k) => {
+ if (k === 'capo') return Number(draft.capo || 0);
+ // Joined on a comma — the one character parseTags strips from every tag, so
+ // two different tag lists can never normalize to the same string.
+ if (k === 'tags') return parseTags(draft.tags).join(',');
+ if (TRIMMED_DRAFT_FIELDS.includes(k)) return (draft[k] || '').trim();
+ return draft[k];
+};
+const draftsEqual = (a, b) => Object.keys(a).every(
+ (k) => normalizeDraftField(a, k) === normalizeDraftField(b, k),
+);
// Instrument-view toggle tabs (chord-diagram rendering — never mutates the record).
const VIEW_TABS = VOICING_INSTRUMENTS.map((viewId) => ({ id: viewId, label: instrumentLabel(viewId) }));
@@ -531,8 +543,11 @@ export default function SongBookViewer() {
/>
{/* Unsaved-edit guard — a full-width band under the header, so the
- deferred exit is confirmed where the user just clicked. */}
- {pendingExit && (
+ deferred exit is confirmed where the user just clicked. It hides
+ while a save is in flight: discarding then would reset the draft
+ under a PATCH that is still persisting those very edits. A failed
+ save leaves the draft dirty, so the band comes back. */}
+ {pendingExit && !saving && (
<InlineConfirmRow
className="shrink-0"
variant="separator"
diff --git a/client/src/pages/SongBookViewer.test.jsx b/client/src/pages/SongBookViewer.test.jsx
index 692a01ed37..104aee5e1d 100644
--- a/client/src/pages/SongBookViewer.test.jsx
+++ b/client/src/pages/SongBookViewer.test.jsx
@@ -578,8 +578,11 @@ K: o - - - - - o -`;
it('switches straight to play mode when the draft is clean', async () => {
renderPage('/songbook/abc?mode=edit');
fireEvent.click(await screen.findByRole('button', { name: 'View' }));
- expect(await screen.findByText('Nonsense words here')).toBeTruthy();
+ // The edit-mode PREVIEW renders the sheet text too, so "we left edit
+ // mode" is asserted on the form going away, not on the sheet appearing.
+ await waitFor(() => expect(screen.queryByLabelText('Content')).toBeNull());
expect(screen.queryByText(/Discard your unsaved changes/)).toBeNull();
+ expect(screen.getByText('Nonsense words here')).toBeTruthy();
});
it('confirms before the View toggle discards unsaved edits', async () => {
@@ -598,7 +601,7 @@ K: o - - - - - o -`;
// Discard → the exit runs and the draft resets to the saved song.
fireEvent.click(screen.getByRole('button', { name: 'View' }));
fireEvent.click(await screen.findByRole('button', { name: 'Discard' }));
- expect(await screen.findByText('Nonsense words here')).toBeTruthy();
+ await waitFor(() => expect(screen.queryByLabelText('Content')).toBeNull());
fireEvent.click(screen.getByRole('button', { name: 'Edit' }));
expect((await screen.findByLabelText('Content')).value).toBe(SHEET);
expect(api.updateSong).not.toHaveBeenCalled();
@@ -619,7 +622,38 @@ K: o - - - - - o -`;
fireEvent.change(capo, { target: { value: '3' } });
fireEvent.change(capo, { target: { value: '2' } });
fireEvent.click(screen.getByRole('button', { name: 'View' }));
- expect(await screen.findByText('Nonsense words here')).toBeTruthy();
+ await waitFor(() => expect(screen.queryByLabelText('Content')).toBeNull());
+ expect(screen.queryByText(/Discard your unsaved changes/)).toBeNull();
+ });
+
+ it('treats tag whitespace and a trailing comma as clean (parseTags round-trip)', async () => {
+ api.getSong.mockResolvedValue(song({ tags: ['campfire', 'fingerstyle'] }));
+ renderPage('/songbook/abc?mode=edit');
+ const tags = await screen.findByLabelText('Tags (comma-separated)');
+ expect(tags.value).toBe('campfire, fingerstyle');
+ // Same saved value — different raw text.
+ fireEvent.change(tags, { target: { value: 'campfire,fingerstyle,' } });
+ fireEvent.click(screen.getByRole('button', { name: 'View' }));
+ await waitFor(() => expect(screen.queryByLabelText('Content')).toBeNull());
+ expect(screen.queryByText(/Discard your unsaved changes/)).toBeNull();
+ });
+
+ it('hides the discard row while a save is in flight', async () => {
+ let resolveSave;
+ api.updateSong.mockImplementation((_id, patch) => new Promise((resolve) => {
+ resolveSave = () => resolve(song({ ...patch }));
+ }));
+ renderPage('/songbook/abc?mode=edit');
+ await editSheet();
+ fireEvent.click(screen.getByRole('button', { name: 'View' }));
+ expect(await screen.findByText('Discard your unsaved changes to this song?')).toBeTruthy();
+ // Save starts → the row goes away, so Discard can't reset the draft under
+ // the in-flight PATCH.
+ fireEvent.click(screen.getByRole('button', { name: 'Save' }));
+ await waitFor(() => expect(screen.queryByRole('button', { name: 'Discard' })).toBeNull());
+ expect(screen.getByRole('button', { name: 'Saving…' })).toBeTruthy();
+ resolveSave();
+ await waitFor(() => expect(api.updateSong).toHaveBeenCalled());
});
it('drops the armed confirm once a save settles the draft', async () => {