preview-demo: seed settings before the settings module reads them, short banner, quick first poll

Found driving Chrome: the pose setting was seeded after settings.ts had already read defaults;
the Connecting bar stayed until the first 15 s long poll returned.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-07 18:57:07 +00:00
commit 0020f7f003
5 changed files with 27 additions and 32 deletions

View file

@ -1,4 +1,5 @@
/* eslint-disable import/first */
import './preview-demo/seed';
import React from 'react';
import { createRoot } from 'react-dom/client';
import { enableMapSet } from 'immer';

View file

@ -26,9 +26,9 @@ export const routeOf = (url: string, pageOrigin: string): Route => {
const refusal = (what: string, url: string) =>
new Error(`preview: pretend server only; ${what} to ${url} refused`);
const BANNER_TEXT = `preview: pretend server (${DEMO_SERVER}). Log in as demo with any password; nothing leaves this page.`;
const BANNER_TEXT = 'preview: pretend server. Any password works.';
const FOREIGN_SESSION_TEXT =
'preview: pretend server. A login for another server is stored on this origin and cannot be used here.';
'preview: pretend server. A login for another server is stored here and cannot be used.';
const showBanner = (text: string) => {
const el = document.createElement('div');
@ -36,18 +36,11 @@ const showBanner = (text: string) => {
el.setAttribute('role', 'status');
el.style.cssText =
'position:fixed;top:0;left:50%;transform:translateX(-50%);z-index:2147483647;max-width:96vw;' +
'padding:2px 10px;font:11px/1.4 system-ui,sans-serif;color:#1b1b1b;background:#ffc46b;' +
'padding:0 8px;font:10px/1.5 system-ui,sans-serif;white-space:nowrap;opacity:0.9;color:#1b1b1b;background:#ffc46b;' +
'border-radius:0 0 6px 6px;pointer-events:none;text-align:center';
document.body.appendChild(el);
};
// The pose portraits are a setting that defaults off; a first visit turns it on so the demo shows them.
const seedSettings = (storage: Storage) => {
if (storage.getItem('settings') === null) {
storage.setItem('settings', JSON.stringify({ poseBubbles: true }));
}
};
// The preview host's CSP sandbox has no allow-forms, so the browser cancels a native form
// submission before its submit event fires and no login button works. Deliver the event ourselves;
// nothing is submitted natively. Remove this when the host grants allow-forms to /void/rat-*.
@ -146,7 +139,6 @@ export const installDemo = (win: Window & typeof globalThis = window) => {
routeOf(String(url), origin) === 'own' ? realBeacon(url, data) : false;
}
seedSettings(win.localStorage);
deliverSubmitEvents(document);
const stored = win.localStorage.getItem('cinny_hs_base_url');
showBanner(stored !== null && stored !== DEMO_ORIGIN ? FOREIGN_SESSION_TEXT : BANNER_TEXT);

10
src/preview-demo/seed.ts Normal file
View file

@ -0,0 +1,10 @@
// Imported first by src/index.tsx: the settings module reads localStorage when it is first
// imported, so a demo build's first-visit defaults must be stored before anything else loads.
// Pose portraits default off; the demo exists to show them. Does nothing without the build flag.
export const seedDemoSettings = (storage: Storage) => {
if (storage.getItem('settings') === null) {
storage.setItem('settings', JSON.stringify({ poseBubbles: true }));
}
};
if (import.meta.env.VITE_PREVIEW_DEMO === '1') seedDemoSettings(localStorage);

View file

@ -4,6 +4,7 @@ import { ClientEvent, MatrixClient, SyncState, createClient } from 'matrix-js-sd
import { DEMO_ORIGIN, ME, ROOMS, GENERAL, COMMONS } from './fixtures';
import { guardedFetch, installDemo, routeOf } from './install';
import { createDemoServer, DemoServer } from './server';
import { seedDemoSettings } from './seed';
import { mindOfSender, POSE_KEY, resolvePose } from '../app/components/message/pose/poses';
import { parseRoomNotice, roomTrainingLine } from '../app/utils/trainingNotice';
@ -249,28 +250,16 @@ describe('the guard', () => {
win.XMLHttpRequest.prototype.open('GET', '/same-origin');
expect(open).toHaveBeenCalledTimes(1);
expect(win.navigator.sendBeacon('https://matrix.org/x')).toBe(false);
expect(JSON.parse(store.get('settings')!)).toEqual({ poseBubbles: true });
vi.unstubAllGlobals();
});
it('keeps stored settings a visitor already chose', () => {
const store = new Map<string, string>([['settings', '{"poseBubbles":false}']]);
const win = {
location: { origin: PAGE },
fetch: never,
WebSocket: class {},
XMLHttpRequest: { prototype: { open: vi.fn() } },
navigator: {},
localStorage: { getItem: (k: string) => store.get(k) ?? null, setItem: vi.fn() },
} as unknown as Window & typeof globalThis;
vi.stubGlobal('document', {
createElement: () => ({ style: {}, setAttribute: vi.fn() }),
body: { appendChild: vi.fn() },
addEventListener: vi.fn(),
});
const server = installDemo(win);
live.push({ server, mx: clientFor(server) });
expect(win.localStorage.setItem).not.toHaveBeenCalled();
vi.unstubAllGlobals();
it('seeds pose portraits on for a first visit and keeps what a visitor already chose', () => {
const fresh = new Map<string, string>();
seedDemoSettings({ getItem: (k: string) => fresh.get(k) ?? null, setItem: (k: string, v: string) => void fresh.set(k, v) } as Storage);
expect(JSON.parse(fresh.get('settings')!)).toEqual({ poseBubbles: true });
const chosen = new Map([['settings', '{"poseBubbles":false}']]);
seedDemoSettings({ getItem: (k: string) => chosen.get(k) ?? null, setItem: (k: string, v: string) => void chosen.set(k, v) } as Storage);
expect(chosen.get('settings')).toBe('{"poseBubbles":false}');
});
});
});

View file

@ -50,6 +50,7 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => {
let replyCounter = 0;
let accountDataVersion = 0;
let syncedJoined = new Set(joined);
let polled = false;
let closed = false;
const mk = (
@ -246,7 +247,9 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => {
resolve();
};
waiters.add(done);
later(Math.min(timeoutMs, MAX_LONG_POLL_MS), done);
// The first poll answers quickly so the client leaves its Connecting state.
later(polled ? Math.min(timeoutMs, MAX_LONG_POLL_MS) : 200, done);
polled = true;
signal?.addEventListener('abort', done);
});
}