Skip to content

refactor(app): carve the edit-song modal into static/js/edit-modal.js (R3d) - #919

Merged
byrongamatos merged 1 commit into
mainfrom
r3d/edit-modal
Jul 12, 2026
Merged

refactor(app): carve the edit-song modal into static/js/edit-modal.js (R3d)#919
byrongamatos merged 1 commit into
mainfrom
r3d/edit-modal

Conversation

@byrongamatos

@byrongamatos byrongamatos commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

4 functions, 234 lines. app.js 4,452 → 4,218. Bodies verbatim.

Interface width: zero

Nothing in app.js calls into this cluster. app.js needs only the names on the window contract, so the markup's onclick= handlers resolve. That's what makes it the cleanest slice left.

And it only became clean because the library came out first (#896). Every dependency the modal has is a module now: it reads six bindings out of ./library.js (loadLibrary, loadFavorites, loadTreeView, _removeLibCardsForFilename, libView, _lastLibSelected), plus dom.js and the L container. Before that carve, extracting this would have dragged the whole library with it.

Checked, and it matters: the modal never writes any of those six. An imported binding is read-only, so a single write would have forced a setter or a state container. Every use is a read, so plain imports suffice.

Acyclic: edit-modal → { dom, library-state, library }, and library imports none of them back.

Verification

A/B against origin/main in two browsers — identical, no new page errors:

the window contract (openEditModal et al.)
the modal actually opens off a real library row
its title and year fields render
the data-edit-save wiring holds — rather than an inline onclick embedding the filename, which is the fix this cluster's harness exists to guard

node 1045 · pytest 2425 · ESLint 0 (no-cycle clean) · host contract 2/2 · Codex 0.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved the Edit Song dialog’s keyboard navigation, focus restoration, and close behavior.
    • Updated library and Favorites views more reliably after saving metadata, uploading artwork, or removing a song.
    • Improved error handling during metadata updates, artwork uploads, and song deletion.
    • Ensured deleted songs are removed immediately from displayed library views.

… (R3d)

4 functions, 234 lines. app.js 4,452 -> 4,218. Bodies VERBATIM.

INTERFACE WIDTH ZERO — nothing in app.js calls into this cluster. app.js needs only the names
on the window contract, so the markup's onclick= handlers resolve. That is what makes it the
cleanest slice left.

AND IT ONLY BECAME CLEAN BECAUSE THE LIBRARY CAME OUT FIRST (#896). Every dependency the modal
has is a module now: it reads six bindings out of ./library.js (loadLibrary, loadFavorites,
loadTreeView, _removeLibCardsForFilename, libView, _lastLibSelected) plus dom.js and the L
container. Before that carve, extracting this would have dragged the whole library with it.

Checked, and it matters: the modal never WRITES any of those six. An imported binding is
READ-ONLY, so a single write would have forced a setter or a state container. Every use is a
read, so plain imports suffice.

Acyclic: edit-modal -> { dom, library-state, library }, and library imports none of them back.

VERIFIED. A/B against origin/main in two browsers: the window contract, the modal actually
OPENING off a real library row, its title and year fields rendering, and the data-edit-save
wiring (rather than an inline onclick embedding the filename — the fix this cluster's harness
exists to guard). IDENTICAL, no new page errors.

node 1045, pytest 2425, ESLint 0 (no-cycle clean), host contract 2/2, Codex 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The edit-song modal handlers were extracted from static/app.js into static/js/edit-modal.js. Application imports and regression tests were updated to use the new module location.

Changes

Edit modal flow

Layer / File(s) Summary
Extracted modal implementation
static/js/edit-modal.js
Adds modal creation, focus management, dismissal rules, metadata saving, artwork upload, deletion, cache updates, and library refresh behavior.
Application wiring and tests
static/app.js, tests/js/edit_metadata_modal.test.js
Imports the extracted handlers and updates regression tests to load static/js/edit-modal.js.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main refactor: moving the edit-song modal into static/js/edit-modal.js.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch r3d/edit-modal

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/js/edit_metadata_modal.test.js (1)

66-72: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider covering the art-upload path.

edit-art-file always mocks { files: null }, so saveEditModal's art-upload branch (and its unawaited-FileReader/missing-error-handling issue flagged in edit-modal.js) is never exercised by this regression suite.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/js/edit_metadata_modal.test.js` around lines 66 - 72, The test fixture
for edit-art-file only covers the no-file path, leaving saveEditModal’s
art-upload branch untested. Extend the mock and regression tests in the edit
metadata modal suite to provide a file and exercise upload behavior, including
the FileReader flow and its error handling; preserve the existing null-files
coverage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@static/js/edit-modal.js`:
- Around line 162-205: Update saveEditModal to await and validate both the
metadata and art-upload requests, treating non-OK responses and network errors
as failures and surfacing them consistently with deleteSongFromModal. Wrap the
FileReader operation in a Promise so the upload completes before modal removal
and view refresh; only close and refresh after all saves succeed, while
preserving the existing focus restoration and favorites/library refresh
behavior.

---

Nitpick comments:
In `@tests/js/edit_metadata_modal.test.js`:
- Around line 66-72: The test fixture for edit-art-file only covers the no-file
path, leaving saveEditModal’s art-upload branch untested. Extend the mock and
regression tests in the edit metadata modal suite to provide a file and exercise
upload behavior, including the FileReader flow and its error handling; preserve
the existing null-files coverage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 998cde45-0c3d-45fd-a4b0-86b8db87ce34

📥 Commits

Reviewing files that changed from the base of the PR and between 0a6e030 and 31f76bc.

📒 Files selected for processing (3)
  • static/app.js
  • static/js/edit-modal.js
  • tests/js/edit_metadata_modal.test.js

Comment thread static/js/edit-modal.js
Comment on lines +162 to +205
export async function saveEditModal(encodedFilename) {
const filename = decodeURIComponent(encodedFilename);

// Save metadata
await fetch(`/api/song/${encodeURIComponent(filename)}/meta`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: document.getElementById('edit-title').value.trim(),
artist: document.getElementById('edit-artist').value.trim(),
album: document.getElementById('edit-album').value.trim(),
// Year is normalised server-side (non-numeric/empty → ""), so a
// blank or cleared field round-trips safely.
year: document.getElementById('edit-year').value.trim(),
}),
});

// Upload art if changed
const fileInput = document.getElementById('edit-art-file');
if (fileInput.files && fileInput.files[0]) {
const reader = new FileReader();
reader.onload = async (e) => {
await fetch(`/api/song/${encodeURIComponent(filename)}/art/upload`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: e.target.result }),
});
};
reader.readAsDataURL(fileInput.files[0]);
}

const modal = document.getElementById('edit-modal');
const opener = modal ? modal._opener : null;
if (modal) modal.remove();
// Restore focus to the entry the modal was opened from so subsequent
// keyboard navigation resumes correctly (same as Esc / Cancel paths).
const focusTarget = (opener && document.body.contains(opener)) ? opener
: (_lastLibSelected && document.body.contains(_lastLibSelected) ? _lastLibSelected : null);
if (focusTarget) focusTarget.focus({ preventScroll: true });
// Refresh current view
const activeScreen = document.querySelector('.screen.active');
if (activeScreen?.id === 'favorites') loadFavorites();
else loadLibrary();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

saveEditModal swallows fetch failures and doesn't await the art upload before refreshing.

Two issues in this function, unlike deleteSongFromModal below which does this correctly:

  • Neither the metadata fetch (166-177) nor the art-upload fetch (184-188) checks resp.ok or is wrapped in try/catch. A failed save (network error or non-2xx response) is silently ignored — the modal still closes and the view refreshes as if the save succeeded, misleading the user into thinking their edit was persisted.
  • The art upload runs inside reader.onload, which is never awaited. The code falls through to modal.remove() and loadLibrary()/loadFavorites() (193-205) immediately, so the grid/tree refresh can render before the new art has actually been uploaded to the server — a race condition that shows stale artwork.

Wrapping the FileReader read in a Promise (the standard pattern for combining FileReader with async/await) and awaiting it, plus adding basic error surfacing consistent with deleteSongFromModal, would fix both.

🛡️ Proposed fix
 export async function saveEditModal(encodedFilename) {
     const filename = decodeURIComponent(encodedFilename);
 
-    // Save metadata
-    await fetch(`/api/song/${encodeURIComponent(filename)}/meta`, {
-        method: 'POST',
-        headers: { 'Content-Type': 'application/json' },
-        body: JSON.stringify({
-            title: document.getElementById('edit-title').value.trim(),
-            artist: document.getElementById('edit-artist').value.trim(),
-            album: document.getElementById('edit-album').value.trim(),
-            // Year is normalised server-side (non-numeric/empty → ""), so a
-            // blank or cleared field round-trips safely.
-            year: document.getElementById('edit-year').value.trim(),
-        }),
-    });
-
-    // Upload art if changed
-    const fileInput = document.getElementById('edit-art-file');
-    if (fileInput.files && fileInput.files[0]) {
-        const reader = new FileReader();
-        reader.onload = async (e) => {
-            await fetch(`/api/song/${encodeURIComponent(filename)}/art/upload`, {
-                method: 'POST',
-                headers: { 'Content-Type': 'application/json' },
-                body: JSON.stringify({ image: e.target.result }),
-            });
-        };
-        reader.readAsDataURL(fileInput.files[0]);
-    }
+    try {
+        const metaResp = await fetch(`/api/song/${encodeURIComponent(filename)}/meta`, {
+            method: 'POST',
+            headers: { 'Content-Type': 'application/json' },
+            body: JSON.stringify({
+                title: document.getElementById('edit-title').value.trim(),
+                artist: document.getElementById('edit-artist').value.trim(),
+                album: document.getElementById('edit-album').value.trim(),
+                year: document.getElementById('edit-year').value.trim(),
+            }),
+        });
+        if (!metaResp.ok) throw new Error(metaResp.statusText);
+
+        const fileInput = document.getElementById('edit-art-file');
+        if (fileInput.files && fileInput.files[0]) {
+            const dataUrl = await new Promise((resolve, reject) => {
+                const reader = new FileReader();
+                reader.onload = (e) => resolve(e.target.result);
+                reader.onerror = reject;
+                reader.readAsDataURL(fileInput.files[0]);
+            });
+            const artResp = await fetch(`/api/song/${encodeURIComponent(filename)}/art/upload`, {
+                method: 'POST',
+                headers: { 'Content-Type': 'application/json' },
+                body: JSON.stringify({ image: dataUrl }),
+            });
+            if (!artResp.ok) throw new Error(artResp.statusText);
+        }
+    } catch (e) {
+        alert(`Save failed: ${e.message}`);
+        return;
+    }
 
     const modal = document.getElementById('edit-modal');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export async function saveEditModal(encodedFilename) {
const filename = decodeURIComponent(encodedFilename);
// Save metadata
await fetch(`/api/song/${encodeURIComponent(filename)}/meta`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: document.getElementById('edit-title').value.trim(),
artist: document.getElementById('edit-artist').value.trim(),
album: document.getElementById('edit-album').value.trim(),
// Year is normalised server-side (non-numeric/empty → ""), so a
// blank or cleared field round-trips safely.
year: document.getElementById('edit-year').value.trim(),
}),
});
// Upload art if changed
const fileInput = document.getElementById('edit-art-file');
if (fileInput.files && fileInput.files[0]) {
const reader = new FileReader();
reader.onload = async (e) => {
await fetch(`/api/song/${encodeURIComponent(filename)}/art/upload`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: e.target.result }),
});
};
reader.readAsDataURL(fileInput.files[0]);
}
const modal = document.getElementById('edit-modal');
const opener = modal ? modal._opener : null;
if (modal) modal.remove();
// Restore focus to the entry the modal was opened from so subsequent
// keyboard navigation resumes correctly (same as Esc / Cancel paths).
const focusTarget = (opener && document.body.contains(opener)) ? opener
: (_lastLibSelected && document.body.contains(_lastLibSelected) ? _lastLibSelected : null);
if (focusTarget) focusTarget.focus({ preventScroll: true });
// Refresh current view
const activeScreen = document.querySelector('.screen.active');
if (activeScreen?.id === 'favorites') loadFavorites();
else loadLibrary();
}
export async function saveEditModal(encodedFilename) {
const filename = decodeURIComponent(encodedFilename);
try {
const metaResp = await fetch(`/api/song/${encodeURIComponent(filename)}/meta`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: document.getElementById('edit-title').value.trim(),
artist: document.getElementById('edit-artist').value.trim(),
album: document.getElementById('edit-album').value.trim(),
year: document.getElementById('edit-year').value.trim(),
}),
});
if (!metaResp.ok) throw new Error(metaResp.statusText);
const fileInput = document.getElementById('edit-art-file');
if (fileInput.files && fileInput.files[0]) {
const dataUrl = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => resolve(e.target.result);
reader.onerror = reject;
reader.readAsDataURL(fileInput.files[0]);
});
const artResp = await fetch(`/api/song/${encodeURIComponent(filename)}/art/upload`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: dataUrl }),
});
if (!artResp.ok) throw new Error(artResp.statusText);
}
} catch (e) {
alert(`Save failed: ${e.message}`);
return;
}
const modal = document.getElementById('edit-modal');
const opener = modal ? modal._opener : null;
if (modal) modal.remove();
// Restore focus to the entry the modal was opened from so subsequent
// keyboard navigation resumes correctly (same as Esc / Cancel paths).
const focusTarget = (opener && document.body.contains(opener)) ? opener
: (_lastLibSelected && document.body.contains(_lastLibSelected) ? _lastLibSelected : null);
if (focusTarget) focusTarget.focus({ preventScroll: true });
// Refresh current view
const activeScreen = document.querySelector('.screen.active');
if (activeScreen?.id === 'favorites') loadFavorites();
else loadLibrary();
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@static/js/edit-modal.js` around lines 162 - 205, Update saveEditModal to
await and validate both the metadata and art-upload requests, treating non-OK
responses and network errors as failures and surfacing them consistently with
deleteSongFromModal. Wrap the FileReader operation in a Promise so the upload
completes before modal removal and view refresh; only close and refresh after
all saves succeed, while preserving the existing focus restoration and
favorites/library refresh behavior.

@byrongamatos
byrongamatos merged commit 69aac32 into main Jul 12, 2026
5 checks passed
@byrongamatos
byrongamatos deleted the r3d/edit-modal branch July 12, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant