Skip to content

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

Page card: h4-h6 count as the page's own heading; the heading wait starts when the version has loaded

luishron123b8b964cda650
diff --git a/shared/app-frame/heading.test.ts b/shared/app-frame/heading.test.ts
index f88fcc59a8..1533a8111f 100644
--- a/shared/app-frame/heading.test.ts
+++ b/shared/app-frame/heading.test.ts
@@ -22,6 +22,7 @@ describe("whether a page opens with a heading of its own", () => {
expect(opensWithHeading(open("<h1>Pricing</h1><p>body</p>"))).toBe(true);
expect(opensWithHeading(open("<main><section><h2>Not the page's title at all</h2></section></main>"))).toBe(true);
expect(opensWithHeading(open("<div role=\"heading\" aria-level=\"1\">Pricing</div><p>body</p>"))).toBe(true);
+ for (const level of [4, 5, 6]) expect(opensWithHeading(open(`<h${level}>Summary</h${level}><p>body</p>`))).toBe(true);
});
it("is not so when the first text it shows is anything else", () => {
diff --git a/shared/app-frame/helper.ts b/shared/app-frame/helper.ts
index 578f1e29c4..4ccfcb705c 100644
--- a/shared/app-frame/helper.ts
+++ b/shared/app-frame/helper.ts
@@ -74,7 +74,7 @@ export const APP_OPENS_WITH_HEADING_SOURCE = `function (window, document) {
};
var holder = first(body);
if (!holder) return null;
- var heading = holder.closest("h1, h2, h3, [role=heading]");
+ var heading = holder.closest("h1, h2, h3, h4, h5, h6, [role=heading]");
if (!heading) return false;
return !laidOut || heading.getBoundingClientRect().top + (window.scrollY || 0) <= ${HEADING_TOP};
}`;
diff --git a/src/components/app/AppMessage.compact.test.ts b/src/components/app/AppMessage.compact.test.ts
index cd099c8e21..153c42d502 100644
--- a/src/components/app/AppMessage.compact.test.ts
+++ b/src/components/app/AppMessage.compact.test.ts
@@ -164,6 +164,23 @@ describe("a bot's page as a compact card", () => {
expect(card().querySelector("[data-page-title]")!.textContent).toBe("Pricing");
});
+ it("starts the wait for an updated page only once that version has arrived", async () => {
+ await render(row(payload()));
+ await pageSays(true);
+ const answer = vi.mocked(fetch).getMockImplementation()!;
+ let arrive = () => {};
+ vi.mocked(fetch).mockImplementationOnce((...args) => new Promise((resolve) => { arrive = () => resolve(answer(...args)); }));
+ await render(row(payload({ rev: 1, doc: "d1", history: ["d0"] })));
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 1_600)); });
+ expect(card().querySelector("[data-page-settling]")).not.toBeNull();
+ expect(card().querySelector("[data-page-title]")).toBeNull();
+ await act(async () => { arrive(); });
+ await settle();
+ await pageSays(true);
+ expect(card().querySelector("[data-page-settling]")).toBeNull();
+ expect(card().querySelector("[data-page-title]")).toBeNull();
+ });
+
it("keeps waiting for a page that says it is still drawing itself", async () => {
await render(row(payload()));
await pageSays(null);
diff --git a/src/components/app/AppMessage.tsx b/src/components/app/AppMessage.tsx
index 927efc48ac..1093d22052 100644
--- a/src/components/app/AppMessage.tsx
+++ b/src/components/app/AppMessage.tsx
@@ -252,17 +252,18 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
const said = heading?.key === `${message.id}:${rev}` ? heading.opens : undefined;
const opens = said ?? headings.get(`${message.id}:${rev}`);
const undecided = compact && Boolean(current) && opens === undefined;
+ const timing = undecided && loaded?.key === wanted;
const drawing = said === null;
const frameKey = loaded?.key;
useEffect(() => {
- if (!undecided) return;
+ if (!timing) return;
const key = `${message.id}:${rev}`;
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]);
+ }, [timing, drawing, frameKey, message.id, rev]);
// The title stays while the frame is away (the pool took its slot), so the card keeps its height.
const titled = compact && opens === false;
// The compact card clips the page at its natural size, so it reads as a preview of the real thing.