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: discard a SongBook URL import that resolves after the form moved on (#3903)

[._.]/ Adam Eivyd8202e709159
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index b4874cb05e..d7621b6f21 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -65,4 +65,4 @@
- An AI generate or refine started in the Story Builder now survives clicking another step on the left rail. The step panel is keyed by the active step, so navigating away unmounted it — which tore down the SSE progress stream and threw away the completion handlers, leaving the run to finish on the server with no phase updates, no success or failure toast, and a stale view until a manual refresh. Each run's stream, phase and handlers now live in a provider mounted above the rail (`client/src/hooks/useStoryStepRuns.jsx`), one slot per step id, so runs on different steps can be in flight at once and the panel picks its own run back up — including which character a refine targets — when the user navigates back to it.
- `docs/SELF_UPDATE.md` now documents that `update.sh` / `update.ps1` check out `main` before pulling, stashing a dirty working tree first so the checkout can proceed. The doc described only the `git pull --rebase --autostash`, so anyone running an in-app or CLI update from a feature branch or a detached HEAD was surprised to find their checkout moved to `main` with their work parked in `git stash` — the scripts print the restore commands on completion, but nothing said so up front.
- The Vite dev server example in `docs/PM2.md` no longer tells you to point PM2 at `node_modules/.bin/vite`. PM2 resolves a relative `script` against its own working directory rather than the app's `cwd`, and the `.bin` entry itself varies by platform and package manager (symlink, `.cmd`/`.ps1` wrapper, generated shell script), none of which the Node interpreter launches uniformly — so a config copy-pasted from the doc crashed on startup. Both the standalone snippet and the complete monorepo example now use the absolute `path.join(__dirname, 'client', 'node_modules', 'vite', 'bin', 'vite.js')` form that PortOS's own `portos-ui` app has always used, with a note explaining why.
-- A failed URL import in the SongBook importer no longer leaves the form looking untouched. The error had only a transient toast behind it, so once it faded the URL still sat in the input with no preview, no explanation, and a Save button that answered with the unrelated "Nothing to save — paste or fetch a tab first". The From URL tab now keeps an inline alert under the input naming the failure with a Retry button next to it, clears that alert as soon as the URL is edited, and drops any previously fetched draft (and the title/artist it auto-filled) so a failed fetch can't leave the earlier song previewed under a URL that never loaded. The disabled Save button now says why it's disabled instead of waiting for the click.
+- A failed URL import in the SongBook importer no longer leaves the form looking untouched. The error had only a transient toast behind it, so once it faded the URL still sat in the input with no preview, no explanation, and a Save button that answered with the unrelated "Nothing to save — paste or fetch a tab first". The From URL tab now keeps an inline alert under the input naming the failure with a Retry button next to it, clears that alert as soon as the URL is edited, and drops any previously fetched draft (and the title/artist it auto-filled) so a failed fetch can't leave the earlier song previewed under a URL that never loaded. The disabled Save button now says why it's disabled instead of waiting for the click. A result that lands after the URL has been edited (or after the user has left the tab) is discarded rather than written under whatever is on screen now.
diff --git a/client/src/pages/SongBookImport.jsx b/client/src/pages/SongBookImport.jsx
index 063d206a87..f15bc53af3 100644
--- a/client/src/pages/SongBookImport.jsx
+++ b/client/src/pages/SongBookImport.jsx
@@ -116,23 +116,37 @@ export default function SongBookImport() {
applyMetaDefaults(parseTabSheet(normalizePastedTab(text)).meta);
}, [applyMetaDefaults]);
+ // The input stays editable while a fetch is in flight, so the request that
+ // resolves may no longer be the one the form is showing. Refs (not the stale
+ // closure values) say what's on screen when the promise settles.
+ const urlRef = useRef(url);
+ urlRef.current = url;
+ const tabRef = useRef(tab);
+ tabRef.current = tab;
+
// Error mapping lives in the .catch alone — it swallows every rejection, so
// a useAsyncAction errorMessage layer could never fire (single toast layer).
const [fetchUrl, fetching] = useAsyncAction(async () => {
- const normalizedUrl = normalizeUrl(url);
+ const submittedUrl = url;
+ const normalizedUrl = normalizeUrl(submittedUrl);
if (!normalizedUrl || !isUrl(normalizedUrl)) { toast.error('Enter a valid URL'); return null; }
+ // A result for a URL the user has since edited away from — or for a tab
+ // they've left — must not write itself over what's on screen now.
+ const isCurrent = () => urlRef.current === submittedUrl && tabRef.current === 'url';
const data = await importSongFromUrl(normalizedUrl, { silent: true }).catch((err) => {
const message = importErrorMessage(err);
toast.error(message);
- // Drop any earlier draft: leaving the previous song previewed under a
- // failed URL is exactly the stale-form confusion this guards against.
- setFetched(null);
- applyMetaDefaults({});
- setFetchError(message);
+ if (isCurrent()) {
+ // Drop any earlier draft: leaving the previous song previewed under a
+ // failed URL is exactly the stale-form confusion this guards against.
+ setFetched(null);
+ applyMetaDefaults({});
+ setFetchError(message);
+ }
return null;
});
const draft = data?.draft;
- if (!draft) return null;
+ if (!draft || !isCurrent()) return null;
setFetchError(null);
setFetched(draft);
applyMetaDefaults(draft);
diff --git a/client/src/pages/SongBookImport.test.jsx b/client/src/pages/SongBookImport.test.jsx
index 3d8729997a..82c49232b8 100644
--- a/client/src/pages/SongBookImport.test.jsx
+++ b/client/src/pages/SongBookImport.test.jsx
@@ -233,6 +233,23 @@ describe('SongBookImport', () => {
});
});
+ it('ignores a fetch that resolves after the user has edited the URL away', async () => {
+ // The input stays editable during the fetch, so a late result must not
+ // write a draft (or an error) under a URL that never produced it.
+ let rejectFetch;
+ api.importSongFromUrl.mockReturnValue(new Promise((_, reject) => { rejectFetch = reject; }));
+ renderPage();
+ fireEvent.click(screen.getByRole('tab', { name: 'From URL' }));
+ const input = screen.getByLabelText('Tab / chord-sheet URL');
+ fireEvent.change(input, { target: { value: 'https://example.com/slow' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Fetch' }));
+
+ fireEvent.change(input, { target: { value: 'https://example.com/typed-more' } });
+ rejectFetch(Object.assign(new Error('Bad gateway'), { code: 'SONG_IMPORT_FETCH_FAILED' }));
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Fetch' }).textContent).toContain('Fetch'));
+ expect(screen.queryByRole('alert')).toBeNull();
+ });
+
it('sends an in-range pasted capo through unchanged', async () => {
renderPage();
fireEvent.change(screen.getByLabelText('Pasted tab content'), {