fix(site-builder): honest save state (dirty tracking, status pill, beforeunload, error notice)

Track dirty from Craft node changes and page/design/header/footer state, cleared
only by a successful save. Persistent Unsaved/Saving/Saved/Published/Save failed
pill, beforeunload prompt while dirty or saving, one persistent error notice,
resp.ok/non-JSON handling for save and publish, and publish refuses to run
after a failed save. Draft badge no longer flips on a no-op autosave.

Refs #30

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-10-07 10:07:03 -07:00
1 parent a406ff8548
commit 9418444916
12 files changed
+842 -170

No files matched your search

+87 -2
View File
@@ -1,4 +1,4 @@
import React, { createContext, useContext, useMemo, useRef, useState, ReactNode } from 'react';
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, ReactNode } from 'react';
import { useEditorConfig } from './EditorConfigContext';
export type LoadState = 'loading' | 'loaded' | 'failed';
@@ -15,6 +15,26 @@ interface SaveGuardContextValue {
/** One-shot: the next save sends force_empty (consumed by the next successful save). */
forceEmptyNextSave: React.MutableRefObject<boolean>;
armForceEmptyNextSave: () => void;
/** True when the editor holds changes (canvas, pages, design, header/footer)
* made after the last successful save or load. Cleared only by a save. */
dirty: boolean;
/** Synchronous read of `dirty`, for handlers that must not see a stale render. */
isDirty: () => boolean;
/** Record a change. Ignored until the site has loaded (load itself rewrites state). */
markDirty: () => void;
/** Call right before serializing for a save; pass the ticket to `endSave`. */
beginSave: () => SaveTicket;
/** Finish a save. Clears dirty only if it succeeded AND nothing changed since `beginSave`. */
endSave: (ticket: SaveTicket, succeeded: boolean) => void;
/** True while at least one save request is in flight. */
saving: boolean;
/** Skip the unsaved-changes prompt for the next unload (e.g. a deliberate "Reload latest"). */
suppressBeforeUnload: () => void;
}
/** Identifies the editor state a save was built from. */
export interface SaveTicket {
version: number;
}
// Default (no provider: standalone mode and isolated unit tests) is
@@ -26,6 +46,13 @@ const defaultValue: SaveGuardContextValue = {
setEmptyRefusal: () => {},
forceEmptyNextSave: { current: false },
armForceEmptyNextSave: () => {},
dirty: false,
isDirty: () => false,
markDirty: () => {},
beginSave: () => ({ version: 0 }),
endSave: () => {},
saving: false,
suppressBeforeUnload: () => {},
};
const SaveGuardContext = createContext<SaveGuardContextValue>(defaultValue);
@@ -38,6 +65,57 @@ export const SaveGuardProvider: React.FC<{ children: ReactNode }> = ({ children
const [emptyRefusal, setEmptyRefusal] = useState<string | null>(null);
const forceEmptyNextSave = useRef(false);
// Dirty tracking. `versionRef` counts changes so a save that raced with an
// edit cannot mark that edit clean.
const [dirty, setDirty] = useState(false);
const [savesInFlight, setSavesInFlight] = useState(0);
const dirtyRef = useRef(false);
const versionRef = useRef(0);
const loadedRef = useRef(loadState === 'loaded');
const suppressRef = useRef(false);
// A (re)load replaces the whole editor state: it is the new clean baseline.
useEffect(() => {
loadedRef.current = loadState === 'loaded';
if (loadState === 'loaded') {
dirtyRef.current = false;
setDirty(false);
}
}, [loadState]);
const markDirty = useCallback(() => {
if (!loadedRef.current) return;
versionRef.current += 1;
dirtyRef.current = true;
setDirty(true);
}, []);
const isDirty = useCallback(() => dirtyRef.current, []);
const beginSave = useCallback((): SaveTicket => {
setSavesInFlight((n) => n + 1);
return { version: versionRef.current };
}, []);
const endSave = useCallback((ticket: SaveTicket, succeeded: boolean) => {
setSavesInFlight((n) => Math.max(0, n - 1));
if (succeeded && ticket.version === versionRef.current) {
dirtyRef.current = false;
setDirty(false);
}
}, []);
const suppressBeforeUnload = useCallback(() => { suppressRef.current = true; }, []);
const saving = savesInFlight > 0;
const needsPrompt = isWHP && (dirty || saving);
useEffect(() => {
if (!needsPrompt) return;
const onBeforeUnload = (e: BeforeUnloadEvent) => {
if (suppressRef.current) return;
e.preventDefault();
e.returnValue = '';
};
window.addEventListener('beforeunload', onBeforeUnload);
return () => window.removeEventListener('beforeunload', onBeforeUnload);
}, [needsPrompt]);
const value = useMemo<SaveGuardContextValue>(() => ({
loadState,
setLoadState,
@@ -45,7 +123,14 @@ export const SaveGuardProvider: React.FC<{ children: ReactNode }> = ({ children
setEmptyRefusal,
forceEmptyNextSave,
armForceEmptyNextSave: () => { forceEmptyNextSave.current = true; },
}), [loadState, emptyRefusal]);
dirty,
isDirty,
markDirty,
beginSave,
endSave,
saving,
suppressBeforeUnload,
}), [loadState, emptyRefusal, dirty, saving, isDirty, markDirty, beginSave, endSave, suppressBeforeUnload]);
return <SaveGuardContext.Provider value={value}>{children}</SaveGuardContext.Provider>;
};