fix(site-builder): serialise all saves; honest pill for conflicts and new sites
A manual Save during Publish sent a second save with the same base_rev; the loser 409'd against its own tab (and could abort the publish). - useWhpApi: every save runs through one in-flight promise (SaveGuard saveInFlight). A save requested while one is out waits, then sends only if still dirty, with the rev the first returned; payload is read from the editor at send time. Forced (empty/overwrite) saves always send. - Save buttons are disabled while publishing. - Pill: a conflict reads 'Unsaved changes' (modal open) from Save and from Publish alike; a never-saved site (load rev null) reads 'Not saved yet'. - Autosave interval reads runSave through a ref, so editing can't keep restarting it and starve autosave. - Fix stale 'Throws like exportBodyHtml' comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
572e736f1f
commit
97ccbee461
7 files changed
+335
-20
No files matched your search
@@ -46,6 +46,12 @@ interface SaveGuardContextValue {
|
||||
disarmForceOverwriteNextSave: () => void;
|
||||
/** "Reload latest": skip the unsaved-changes prompt, then reload the whole editor. */
|
||||
reloadLatest: () => void;
|
||||
/** The one save request in flight (shared by every useWhpApi instance), or null.
|
||||
* useWhpApi chains each new save behind it so two saves never race on one base_rev. */
|
||||
saveInFlight: React.MutableRefObject<Promise<unknown> | null>;
|
||||
/** True while the server has no copy of this site (load reported rev null) and no save
|
||||
* has gone through since. Drives the "Not saved yet" pill. */
|
||||
neverSaved: boolean;
|
||||
}
|
||||
|
||||
/** Identifies the editor state a save was built from. */
|
||||
@@ -78,6 +84,8 @@ const defaultValue: SaveGuardContextValue = {
|
||||
armForceOverwriteNextSave: () => {},
|
||||
disarmForceOverwriteNextSave: () => {},
|
||||
reloadLatest: () => {},
|
||||
saveInFlight: { current: null },
|
||||
neverSaved: false,
|
||||
};
|
||||
|
||||
const SaveGuardContext = createContext<SaveGuardContextValue>(defaultValue);
|
||||
@@ -92,8 +100,13 @@ export const SaveGuardProvider: React.FC<{ children: ReactNode; /** Injectable f
|
||||
const [conflict, setConflict] = useState<string | null>(null);
|
||||
const forceOverwriteNextSave = useRef(false);
|
||||
const revRef = useRef<string | null | undefined>(undefined);
|
||||
const [neverSaved, setNeverSaved] = useState(false);
|
||||
const saveInFlight = useRef<Promise<unknown> | null>(null);
|
||||
const getRev = useCallback(() => revRef.current, []);
|
||||
const setRev = useCallback((rev: string | null) => { revRef.current = rev; }, []);
|
||||
const setRev = useCallback((rev: string | null) => {
|
||||
revRef.current = rev;
|
||||
setNeverSaved(rev === null);
|
||||
}, []);
|
||||
|
||||
// Dirty tracking. `versionRef` counts changes so a save that raced with an
|
||||
// edit cannot mark that edit clean.
|
||||
@@ -127,6 +140,7 @@ export const SaveGuardProvider: React.FC<{ children: ReactNode; /** Injectable f
|
||||
}, []);
|
||||
const endSave = useCallback((ticket: SaveTicket, succeeded: boolean) => {
|
||||
setSavesInFlight((n) => Math.max(0, n - 1));
|
||||
if (succeeded) setNeverSaved(false);
|
||||
if (succeeded && ticket.version === versionRef.current) {
|
||||
dirtyRef.current = false;
|
||||
setDirty(false);
|
||||
@@ -175,7 +189,9 @@ export const SaveGuardProvider: React.FC<{ children: ReactNode; /** Injectable f
|
||||
armForceOverwriteNextSave: () => { forceOverwriteNextSave.current = true; },
|
||||
disarmForceOverwriteNextSave: () => { forceOverwriteNextSave.current = false; },
|
||||
reloadLatest,
|
||||
}), [loadState, emptyRefusal, dirty, saving, isDirty, getVersion, markDirty, beginSave, endSave, suppressBeforeUnload, getRev, setRev, conflict, reloadLatest]);
|
||||
saveInFlight,
|
||||
neverSaved,
|
||||
}), [loadState, emptyRefusal, dirty, saving, isDirty, getVersion, markDirty, beginSave, endSave, suppressBeforeUnload, getRev, setRev, conflict, reloadLatest, neverSaved]);
|
||||
|
||||
return <SaveGuardContext.Provider value={value}>{children}</SaveGuardContext.Provider>;
|
||||
};
|
||||
Reference in new issue
Block a user