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
@@ -1,6 +1,7 @@
|
||||
import React from 'react';
|
||||
|
||||
export type SaveStatus = 'unsaved' | 'saving' | 'saved' | 'published' | 'failed';
|
||||
/** 'new': the server has no copy yet (brand-new site, never saved). */
|
||||
export type SaveStatus = 'unsaved' | 'saving' | 'saved' | 'published' | 'failed' | 'new';
|
||||
|
||||
const LABELS: Record<SaveStatus, string> = {
|
||||
unsaved: 'Unsaved changes',
|
||||
@@ -8,6 +9,7 @@ const LABELS: Record<SaveStatus, string> = {
|
||||
saved: 'Saved',
|
||||
published: 'Published',
|
||||
failed: 'Save failed',
|
||||
new: 'Not saved yet',
|
||||
};
|
||||
|
||||
const ICONS: Record<SaveStatus, string> = {
|
||||
@@ -16,6 +18,7 @@ const ICONS: Record<SaveStatus, string> = {
|
||||
saved: 'fa-check',
|
||||
published: 'fa-check-circle',
|
||||
failed: 'fa-exclamation-triangle',
|
||||
new: 'fa-circle-o',
|
||||
};
|
||||
|
||||
/** Persistent save state. It changes only when the state does; nothing resets it on a timer. */
|
||||
|
||||
@@ -38,6 +38,7 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
const {
|
||||
loadState, emptyRefusal, setEmptyRefusal, dirty, isDirty, getVersion,
|
||||
conflict, setConflict, armForceOverwriteNextSave, disarmForceOverwriteNextSave, reloadLatest,
|
||||
neverSaved,
|
||||
} = useSaveGuard();
|
||||
const canSave = loadState === 'loaded';
|
||||
const { headerPage, footerPage, pages, activePageId } = usePages();
|
||||
@@ -68,10 +69,17 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
if (dirty) setPublished(false);
|
||||
}, [dirty]);
|
||||
|
||||
// A conflict reads "Unsaved changes" (with the conflict modal open) whether
|
||||
// it came from Save, autosave or the save inside Publish: the edits are
|
||||
// intact locally, just not on the server. "Not saved yet": the server has
|
||||
// no copy of this site and nothing has been saved since load (an older
|
||||
// backend that reports no rev falls back to "Saved").
|
||||
const status: SaveStatus = saving || publishing ? 'saving'
|
||||
: conflict ? 'unsaved'
|
||||
: saveFailed ? 'failed'
|
||||
: dirty ? 'unsaved'
|
||||
: published ? 'published'
|
||||
: neverSaved ? 'new'
|
||||
: 'saved';
|
||||
|
||||
const messageOf = (e: unknown, what: string) =>
|
||||
@@ -112,18 +120,24 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
}
|
||||
}, [save, canSave, isDirty, setEmptyRefusal, setConflict]);
|
||||
|
||||
// Read at tick time: runSave's identity changes with editor state, and
|
||||
// restarting the interval on every change would starve autosave during
|
||||
// continuous editing.
|
||||
const runSaveRef = useRef(runSave);
|
||||
runSaveRef.current = runSave;
|
||||
|
||||
// Auto-save every 30 seconds
|
||||
useEffect(() => {
|
||||
// Paused until the site has loaded, and while the empty-save banner is up.
|
||||
if (!isWHP || !canSave || emptyRefusal || conflict) return;
|
||||
// A running publish does its own save; a second one would race it.
|
||||
// Read through refs/isDirty() at tick time, never a stale closure.
|
||||
// A running publish does its own save; a second one would only queue
|
||||
// behind it. Read through refs/isDirty() at tick time, never a stale closure.
|
||||
// Also paused while the conflict modal is up: it is the authority, the
|
||||
// effect guard above only avoids a pointless timer.
|
||||
const autosaveDue = () => isDirty() && !publishingRef.current && !conflictRef.current;
|
||||
const interval = setInterval(() => { if (autosaveDue()) void runSave(); }, 30000);
|
||||
const interval = setInterval(() => { if (autosaveDue()) void runSaveRef.current(); }, 30000);
|
||||
return () => clearInterval(interval);
|
||||
}, [isWHP, canSave, emptyRefusal, conflict, runSave, isDirty]);
|
||||
}, [isWHP, canSave, emptyRefusal, conflict, isDirty]);
|
||||
|
||||
const handleSave = runSave;
|
||||
|
||||
@@ -137,8 +151,8 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
try {
|
||||
const result = await publish();
|
||||
if (result?.error === 'conflict') {
|
||||
// Nothing was published: the conflict modal (opened by save()) takes over.
|
||||
setSaveFailed(true);
|
||||
// Nothing was published: the conflict modal (opened by save()) takes
|
||||
// over, and the pill reads "Unsaved changes" exactly as for a Save.
|
||||
} else if (result?.error === 'empty_overwrite_refused') {
|
||||
setEmptyRefusal(result.message ?? null);
|
||||
} else if (result?.success) {
|
||||
@@ -330,7 +344,7 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
<button
|
||||
className="topbar-btn icon-only primary"
|
||||
onClick={() => { void handleSave(); }}
|
||||
disabled={saving || !canSave}
|
||||
disabled={saving || publishing || !canSave}
|
||||
aria-label="Save Draft"
|
||||
data-tooltip="Save"
|
||||
>
|
||||
@@ -473,7 +487,7 @@ export const TopBar: React.FC<TopBarProps> = ({ device, onDeviceChange, showGuid
|
||||
<button
|
||||
className="topbar-btn primary"
|
||||
onClick={() => { void handleSave(); }}
|
||||
disabled={saving || !canSave}
|
||||
disabled={saving || publishing || !canSave}
|
||||
title="Save Draft"
|
||||
aria-label="Save Draft"
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user