fix(spa): clear dirty guard before saved navigation
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 11s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 11s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m29s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m44s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 11s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 11s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m29s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m44s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Fixes #344 Co-Authored-By: OpenAI Codex <codex@openai.com>
This commit is contained in:
@@ -327,6 +327,15 @@ draft to navigation. The shared module `web/src/navigationGuard.ts` is the seam:
|
||||
|
||||
Keep the guard predicate reading a **ref** (`dirtyRef`), not the `dirty` state value, so
|
||||
`canLeaveCurrentScreen()` sees the current dirtiness synchronously at click time.
|
||||
|
||||
- **Successful save + same-callback navigation:** `useDirtyGuard` returns a `markClean` callback for
|
||||
the narrow case where a successful save queues the new draft/baseline and immediately calls
|
||||
`navigateToPath(...)` in that same promise callback. React has not committed the clean render before
|
||||
the synthetic `popstate`, so call `markClean()` after the durable save succeeds and immediately before
|
||||
navigating. Still queue the real clean draft/baseline state; the callback only transfers the synchronous
|
||||
guard truth across that one event boundary. Do not replace it with a timer or a forced render. Screens
|
||||
that remain mounted after saving do not need the callback.
|
||||
|
||||
## 9. Review checklist — temporal semantics
|
||||
|
||||
- **For every effect / timer / async completion, ask: *when* does it fire (mount, dependency change,
|
||||
|
||||
@@ -8,11 +8,18 @@
|
||||
// each keystroke. A single hook keeps the three editors (connection / libraries / path-replacements)
|
||||
// consistent instead of copy-pasting the guard block into each.
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useCallback, useEffect, useRef } from 'react';
|
||||
import { registerNavigationGuard } from '../navigationGuard';
|
||||
|
||||
export function useDirtyGuard(dirty: boolean, prompt: string): void {
|
||||
export function useDirtyGuard(dirty: boolean, prompt: string): () => void {
|
||||
const dirtyRef = useRef(dirty);
|
||||
// A successful save may navigate in the same promise callback that queues the clean baseline.
|
||||
// React has not rendered that baseline yet, so let the caller transfer the guard synchronously
|
||||
// before dispatching the synthetic popstate. The normal render still owns the durable state.
|
||||
const markClean = useCallback(() => {
|
||||
dirtyRef.current = false;
|
||||
}, []);
|
||||
|
||||
// Keep the ref current for the synchronous guard predicate. Updated in an effect (committed before
|
||||
// any user pop/nav gesture can fire) rather than during render.
|
||||
useEffect(() => {
|
||||
@@ -32,4 +39,6 @@ export function useDirtyGuard(dirty: boolean, prompt: string): void {
|
||||
window.addEventListener('beforeunload', handler);
|
||||
return () => window.removeEventListener('beforeunload', handler);
|
||||
}, [dirty]);
|
||||
|
||||
return markClean;
|
||||
}
|
||||
|
||||
@@ -82,6 +82,40 @@ describe('RemoteConnectionEditScreen', () => {
|
||||
expect(body.apiKey).toBe(''); // blank = retain existing key server-side
|
||||
});
|
||||
|
||||
it.each(['jellyfin', 'emby'] as const)(
|
||||
'successful %s save navigates without triggering the dirty guard',
|
||||
async (family) => {
|
||||
const editPath = `/app/libraries/${family}/connection`;
|
||||
window.history.replaceState(null, '', editPath);
|
||||
installFetch(family, {
|
||||
connection: { address: `http://${family}:8096`, hasApiKey: true },
|
||||
saveBody: { address: `http://${family}:9000`, hasApiKey: true }
|
||||
});
|
||||
|
||||
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
const onPopState = () => {
|
||||
// Mirror App's dirty-guard veto: a rejected synthetic pop restores the editor path.
|
||||
if (!canLeaveCurrentScreen()) {
|
||||
window.history.pushState(null, '', editPath);
|
||||
}
|
||||
};
|
||||
window.addEventListener('popstate', onPopState);
|
||||
|
||||
try {
|
||||
render(<RemoteConnectionEditScreen family={family} />);
|
||||
const address = (await screen.findByPlaceholderText(/192.168/i)) as HTMLInputElement;
|
||||
|
||||
fireEvent.change(address, { target: { value: `http://${family}:9000` } });
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Save connection' }));
|
||||
|
||||
await vi.waitFor(() => expect(window.location.pathname).toBe(`/app/libraries/${family}`));
|
||||
expect(confirmSpy).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
window.removeEventListener('popstate', onPopState);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
it('first connect (no stored key) requires an api key before Save enables', async () => {
|
||||
installFetch('jellyfin', { connection: { address: '', hasApiKey: false } });
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ export function RemoteConnectionEditScreen({ family }: { family: RemoteFamilyWit
|
||||
const valid = addressValid && apiKeyValid;
|
||||
const dirty = JSON.stringify(draft) !== JSON.stringify(saved);
|
||||
|
||||
useDirtyGuard(dirty, DIRTY_PROMPT);
|
||||
const markDirtyGuardClean = useDirtyGuard(dirty, DIRTY_PROMPT);
|
||||
|
||||
if (loadError) {
|
||||
return (
|
||||
@@ -129,6 +129,7 @@ export function RemoteConnectionEditScreen({ family }: { family: RemoteFamilyWit
|
||||
setSaved(next);
|
||||
setLoaded({ hasApiKey: connection.hasApiKey });
|
||||
setSaving(false);
|
||||
markDirtyGuardClean();
|
||||
navigateToPath(familyRoute(family));
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
|
||||
Reference in New Issue
Block a user