fix(site-builder): fail load on unreadable page state; disable editing controls until loaded

Refs #28
This commit is contained in:
shadowdao committed 2026-10-07 10:00:11 -07:00
1 parent 66e669fa01
commit a406ff8548
10 files changed
+110 -48

No files matched your search

@@ -82,6 +82,7 @@ beforeEach(() => {
vi.stubGlobal('fetch', fetchMock);
vi.spyOn(console, 'error').mockImplementation(() => {});
undoMock.mockClear();
deserializeMock.mockReset();
});
afterEach(() => {
@@ -124,6 +125,33 @@ describe('load gate', () => {
mount(); // so afterEach has something to unmount
});
test('a page state that fails to deserialize fails the load, names the problem, and never saves', async () => {
fetchMock.mockResolvedValue(jsonResp({
success: true,
rev: 'r1',
project: {
pages_craft_state: [{ id: 'home', name: 'Home', slug: 'index', craftState: '{"ROOT":{}}' }],
},
}));
deserializeMock.mockImplementationOnce(() => { throw new Error('malformed'); });
mount();
await flush();
expect(q('[data-testid="load-gate"]')!.textContent).toContain("We couldn't read part of your saved site.");
expect(q('[data-testid="load-retry"]')).not.toBeNull();
await act(async () => { vi.advanceTimersByTime(30000); });
expect(saveCalls()).toHaveLength(0);
});
test('canvas-changing top-bar controls are disabled until the site has loaded', async () => {
fetchMock.mockRejectedValue(new Error('down'));
mount();
await flush();
for (const label of ['Undo', 'Redo', 'Templates', 'Custom Head Code']) {
expect((q(`button[aria-label="${label}"]`) as HTMLButtonElement).disabled).toBe(true);
}
});
test('shows a loading overlay while the load is in flight', async () => {
fetchMock.mockReturnValue(new Promise(() => {}));
mount();
+10 -3
View File
@@ -1,7 +1,7 @@
import React, { useCallback, useEffect, useRef } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useEditorConfig } from '../state/EditorConfigContext';
import { useSaveGuard } from '../state/SaveGuardContext';
import { useWhpApi } from '../hooks/useWhpApi';
import { useWhpApi, UnreadableSiteError } from '../hooks/useWhpApi';
/**
* Owns the initial project load (WHP mode only) and blocks the whole editor
@@ -13,13 +13,16 @@ export const LoadGate: React.FC = () => {
const { loadState, setLoadState } = useSaveGuard();
const { load } = useWhpApi();
const startedRef = useRef(false);
const [unreadable, setUnreadable] = useState(false);
const runLoad = useCallback(() => {
setUnreadable(false);
setLoadState('loading');
load()
.then(() => setLoadState('loaded'))
.catch((e) => {
console.error('Failed to load project from WHP API:', e);
setUnreadable(e instanceof UnreadableSiteError);
setLoadState('failed');
});
}, [load, setLoadState]);
@@ -38,7 +41,11 @@ export const LoadGate: React.FC = () => {
<p><i className="fa fa-spinner fa-spin" aria-hidden="true" /> Loading your site…</p>
) : (
<>
<p>We couldn't load your site. Your saved work has not been changed.</p>
<p>
{unreadable
? "We couldn't read part of your saved site. Your saved work has not been changed. Contact support if this keeps happening."
: "We couldn't load your site. Your saved work has not been changed."}
</p>
<button type="button" className="topbar-btn primary" onClick={runLoad} data-testid="load-retry">
Retry
</button>