Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Page card: the wait for the page's heading ends once

luishron1238db9ae62ba37
diff --git a/src/components/app/AppMessage.compact.test.ts b/src/components/app/AppMessage.compact.test.ts
index 601b75321b..cd099c8e21 100644
--- a/src/components/app/AppMessage.compact.test.ts
+++ b/src/components/app/AppMessage.compact.test.ts
@@ -135,6 +135,28 @@ describe("a bot's page as a compact card", () => {
expect(card().querySelector<HTMLElement>("[data-app-box]")!.style.height).toBe("164px");
});
+ it("keeps the title and the card's height while it has no frame, the page still on its way", async () => {
+ await render(row(payload({ height: 120 })));
+ await pageSays(false);
+ act(() => root.unmount());
+ // past the minute a fetched page is reused for, so the card has to ask for it again
+ const later = Date.now() + 61_000;
+ vi.spyOn(Date, "now").mockReturnValue(later);
+ const answer = vi.mocked(fetch).getMockImplementation()!;
+ let arrive = () => {};
+ vi.mocked(fetch).mockImplementationOnce((...args) => new Promise((resolve) => { arrive = () => resolve(answer(...args)); }));
+ root = createRoot(host);
+ await render(row(payload({ height: 120 })));
+ const shown = () => [card().dataset.appState, card().querySelector("[data-page-title]")?.textContent, card().querySelector<HTMLElement>("[data-app-box]")!.style.height];
+ expect(card().querySelector("iframe")).toBeNull();
+ expect(shown()).toEqual(["loading", "Pricing", "164px"]);
+ await act(async () => { arrive(); });
+ await settle();
+ vi.mocked(Date.now).mockRestore();
+ expect(card().querySelector("iframe")).not.toBeNull();
+ expect(shown()).toEqual(["live", "Pricing", "164px"]);
+ });
+
it("shows the preview with the title after a while when the page never says", async () => {
await render(row(payload()));
await act(async () => { await new Promise((resolve) => setTimeout(resolve, 1_600)); });
@@ -153,6 +175,39 @@ describe("a bot's page as a compact card", () => {
expect(card().querySelector("[data-page-title]")).toBeNull();
});
+ it("does not cover the preview again once the wait has run out, and still follows a later answer", async () => {
+ await render(row(payload()));
+ vi.useFakeTimers();
+ try {
+ await pageSays(null);
+ await act(async () => { await vi.advanceTimersByTimeAsync(10_001); });
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ expect(card().querySelector("[data-page-title]")!.textContent).toBe("Pricing");
+ await pageSays(null);
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ await pageSays(false);
+ await pageSays(null);
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ expect(card().querySelector("[data-page-title]")!.textContent).toBe("Pricing");
+ } finally {
+ vi.useRealTimers();
+ }
+ await pageSays(true);
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ expect(card().querySelector("[data-page-title]")).toBeNull();
+ });
+
+ it("keeps the wait's outcome for the next time the card is drawn", async () => {
+ await render(row(payload()));
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 1_600)); });
+ act(() => root.unmount());
+ root = createRoot(host);
+ await render(row(payload()));
+ await pageSays(null);
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ expect(card().querySelector("[data-page-title]")!.textContent).toBe("Pricing");
+ });
+
it("fades the foot of a clipped preview with a plain gradient and parts it from the footer with a rule", async () => {
await render(row(payload()));
await pageSays(false);
@@ -163,6 +218,11 @@ describe("a bot's page as a compact card", () => {
expect(card().querySelector("[data-page-footer]")!.className).toContain("border-t");
});
+ it("sits flat on the page, with no shadow", async () => {
+ await render(row(payload()));
+ expect(card().firstElementChild!.className).not.toContain("shadow");
+ });
+
it("shows a short page whole, with nothing over its foot", async () => {
await render(row(payload({ height: 120 })));
await pageSays(false);
diff --git a/src/components/app/AppMessage.tsx b/src/components/app/AppMessage.tsx
index ef2c3a2dfb..927efc48ac 100644
--- a/src/components/app/AppMessage.tsx
+++ b/src/components/app/AppMessage.tsx
@@ -25,12 +25,18 @@ const DOC_REUSE_MS = 60_000;
const heights = new Map<string, number>();
/** Whether a page opens with a heading of its own, as it last said: by message id and version. */
const headings = new Map<string, boolean>();
-/** How long a card waits for its page to say, before it shows the preview with the title above it. */
+/** How long a card waits for its page to say, before it shows the preview with the title above it for good. */
const HEADING_WAIT_MS = 1_500;
/** The same once the page has said it is still drawing itself. */
const HEADING_DRAWING_MS = 10_000;
const docs = new Map<string, { doc: AppDocument; at: number }>();
+function rememberHeading(key: string, opens: boolean) {
+ headings.delete(key);
+ headings.set(key, opens);
+ while (headings.size > 200) headings.delete(headings.keys().next().value!);
+}
+
export function forgetAppRows(): void {
heights.clear();
headings.clear();
@@ -173,8 +179,11 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
if (mounted.current) setHeight(next);
}, [message.id, rev]);
const onHeading = useCallback((next: boolean | null) => {
- if (next !== null) headings.set(`${message.id}:${rev}`, next);
- if (mounted.current) setHeading({ key: `${message.id}:${rev}`, opens: next });
+ const key = `${message.id}:${rev}`;
+ if (next !== null) rememberHeading(key, next);
+ // Once the card has an answer, or stopped waiting for one, "still drawing" cannot take the preview away again.
+ else if (headings.has(key)) return;
+ if (mounted.current) setHeading((was) => (next === null && was?.key === key && was.opens !== null ? was : { key, opens: next }));
}, [message.id, rev]);
const current = slot.live ? loaded?.doc : undefined;
@@ -248,7 +257,10 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
useEffect(() => {
if (!undecided) return;
const key = `${message.id}:${rev}`;
- const timer = setTimeout(() => setHeading({ key, opens: false }), drawing ? HEADING_DRAWING_MS : HEADING_WAIT_MS);
+ const timer = setTimeout(() => {
+ rememberHeading(key, false);
+ setHeading({ key, opens: false });
+ }, drawing ? HEADING_DRAWING_MS : HEADING_WAIT_MS);
return () => clearTimeout(timer);
}, [undecided, drawing, frameKey, message.id, rev]);
// The title stays while the frame is away (the pool took its slot), so the card keeps its height.
diff --git a/src/components/app/app-frame-host.test.ts b/src/components/app/app-frame-host.test.ts
index 7a1e737af7..f008d2f9cf 100644
--- a/src/components/app/app-frame-host.test.ts
+++ b/src/components/app/app-frame-host.test.ts
@@ -193,6 +193,15 @@ describe("a page's word on whether it opens with a heading", () => {
expect(onHeading.mock.calls).toEqual([[true], [false], [null]]);
host.destroy();
});
+
+ it("is passed on once however often the page repeats it", () => {
+ const onHeading = vi.fn();
+ const host = mountAppFrame({ container, doc, title: "Coffee counter", height: 200, hostContext: {}, hostVersion: "1", adapter: adapter(), onHeading });
+ const say = (heading: boolean | null) => window.dispatchEvent(new MessageEvent("message", { data: { jsonrpc: "2.0", method: HEADING, params: { heading } }, source: host.iframe.contentWindow as MessageEventSource }));
+ for (const heading of [null, null, true, true, true, false, false, null, true]) say(heading);
+ expect(onHeading.mock.calls).toEqual([[null], [true], [false], [null], [true]]);
+ host.destroy();
+ });
});
describe("a row leaving the page", () => {
diff --git a/src/components/app/app-frame-host.ts b/src/components/app/app-frame-host.ts
index f045a6e229..d057582cbd 100644
--- a/src/components/app/app-frame-host.ts
+++ b/src/components/app/app-frame-host.ts
@@ -194,6 +194,7 @@ export function mountAppFrame(options: AppFrameHostOptions): AppFrameHost {
const engagement = trackAppEngagement(iframe);
let gone = false;
let leaving = false;
+ let said: boolean | null | undefined;
const bridge = createAppBridge({
doc,
hostContext: options.hostContext,
@@ -216,7 +217,9 @@ export function mountAppFrame(options: AppFrameHostOptions): AppFrameHost {
const onMessage = (event: MessageEvent) => {
const heading = event.source === iframe.contentWindow ? readPageHeading(event.data) : undefined;
if (heading !== undefined) {
- if (!leaving) options.onHeading?.(heading);
+ if (leaving || heading === said) return;
+ said = heading;
+ options.onHeading?.(heading);
return;
}
const selection = event.source === iframe.contentWindow ? readPageSelection(event.data) : undefined;