Skip to content

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

Give the compact page card a title, a taller preview and a footer tile

luishron123e1e021b92031
diff --git a/scripts/testing/inline-page-height-ui.e2e.test.ts b/scripts/testing/inline-page-height-ui.e2e.test.ts
index 21aee2a47e..3682e1affa 100644
--- a/scripts/testing/inline-page-height-ui.e2e.test.ts
+++ b/scripts/testing/inline-page-height-ui.e2e.test.ts
@@ -60,7 +60,7 @@ h2{margin:0;height:40px;font-size:18px;line-height:24px}
<div class="row">Monday: design review</div><div class="row">Tuesday: ship the beta</div>
<div class="row">Wednesday: customer calls</div><div class="row">Thursday: retro</div></body></html>`;
const FLOWING_HEIGHT = 16 + 40 + 4 * 56 + 16;
-const PREVIEW_HEIGHT = 220;
+const PREVIEW_HEIGHT = 296;
const PAGES = [
{ key: "map-layer", input: { title: "Ferry map", html: MAP_LAYER } },
diff --git a/src/components/app/AppChrome.tsx b/src/components/app/AppChrome.tsx
index 38fea0a7d9..30021d3190 100644
--- a/src/components/app/AppChrome.tsx
+++ b/src/components/app/AppChrome.tsx
@@ -81,11 +81,13 @@ export function PageCardFooter({ title, detail, updated, infoId, infoOpen, onInf
reloading?: boolean;
}) {
return (
- <div data-page-footer className="flex items-center gap-3 border-t border-ink/[0.08] px-4 py-3">
- <AppWindow aria-hidden="true" size={16} className="shrink-0 text-ink-tertiary" />
+ <div data-page-footer className="flex min-h-18 items-center gap-3 border-t border-ink/[0.08] px-5 py-4">
+ <span data-page-icon className="flex size-8 shrink-0 items-center justify-center rounded-xl bg-inset text-ink-secondary">
+ <AppWindow aria-hidden="true" size={16} />
+ </span>
<div className="min-w-0 flex-1">
- <div title={title} className="truncate text-[13px] font-medium leading-[18px] text-ink"><bdi dir="auto">{title}</bdi></div>
- <div className="truncate text-xs leading-4 text-ink-tertiary">{detail}</div>
+ <div title={title} className="truncate text-sm font-medium leading-[18px] text-ink"><bdi dir="auto">{title}</bdi></div>
+ <div className="truncate text-[13px] leading-[18px] text-ink-secondary">{detail}</div>
</div>
{updated && <span role="status" data-page-updated className="shrink-0 rounded-full bg-accent/15 px-2 py-px text-[13px] font-medium text-accent-text">{t("chat.preview.updated")}</span>}
<div className={cn(
diff --git a/src/components/app/AppMessage.compact.test.ts b/src/components/app/AppMessage.compact.test.ts
index 32df600737..eec95ac1ce 100644
--- a/src/components/app/AppMessage.compact.test.ts
+++ b/src/components/app/AppMessage.compact.test.ts
@@ -9,6 +9,7 @@ import { botHtmlDocument } from "../../../shared/app-frame/document";
import { createAppPool } from "./app-pool";
import { AppMessage, forgetAppRows } from "./AppMessage";
import { PAGE_PREVIEW_HEIGHT, PagePaneContext } from "./PageTools";
+import portuguese from "@/locales/pt-br.json";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -35,8 +36,11 @@ const card = () => host.querySelector<HTMLElement>("[data-app=a1]")!;
const opener = () => card().querySelector<HTMLButtonElement>("[data-page-open]")!;
const press = (element: HTMLElement) => act(async () => { element.click(); await new Promise((resolve) => setTimeout(resolve, 0)); });
+let html: string;
+
beforeEach(() => {
- vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify(botHtmlDocument({ messageId: "a1", rev: 0, html: "<p>page</p>" })),
+ html = "<p>page</p>";
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify(botHtmlDocument({ messageId: "a1", rev: 0, html })),
{ status: 200, headers: { "content-type": "application/json" } })));
vi.stubGlobal("IntersectionObserver", undefined);
forgetAppRows();
@@ -74,12 +78,55 @@ describe("a bot's page as a compact card", () => {
expect(Array.from(card().querySelectorAll("button"), (button) => button.getAttribute("aria-label"))).toEqual(["About this app", "Reload app", "Open page"]);
});
- it("leaves the preview bare and parts it from the footer with a rule", async () => {
+ it("is narrower than a full page, start-aligned in the message column", async () => {
+ await render(row(payload()));
+ expect(card().className).toContain("max-w-[560px]");
+ expect(card().className).not.toContain("max-w-[760px]");
+ });
+
+ it("puts the title above a page that has no heading of its own", async () => {
+ await render(row(payload()));
+ const box = card().querySelector<HTMLElement>("[data-app-box]")!;
+ expect(box.firstElementChild!.matches("[data-page-title]")).toBe(true);
+ expect(box.firstElementChild!.textContent).toBe("Pricing");
+ expect(box.querySelector("[data-page-title] ~ [data-app-frame-box]")).not.toBeNull();
+ });
+
+ it("leaves the top of the preview to a page that opens with its own heading", async () => {
+ html = "<h1>Pricing</h1><p>page</p>";
+ await render(row(payload()));
+ expect(card().querySelector("[data-page-title]")).toBeNull();
+ expect(card().querySelector("[data-app-box]")!.firstElementChild!.matches("[data-app-frame-box]")).toBe(true);
+ expect(card().querySelector("[data-page-footer]")!.textContent).toBe("PricingPage");
+ });
+
+ 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()));
- expect(card().querySelector("[data-app-box]")!.children).toHaveLength(1);
+ const fade = card().querySelector<HTMLElement>("[data-app-box] > [data-page-fade]")!;
+ expect(fade.className).toContain("bg-gradient-to-t from-composer to-transparent");
+ expect(fade.className).toContain("h-8");
+ expect(fade.className).not.toMatch(/blur|mask|backdrop/);
expect(card().querySelector("[data-page-footer]")!.className).toContain("border-t");
});
+ it("shows a short page whole, with nothing over its foot", async () => {
+ await render(row(payload({ height: 120 })));
+ expect(card().querySelector<HTMLElement>("[data-app-box]")!.style.height).toBe("164px");
+ expect(card().querySelector("[data-page-fade]")).toBeNull();
+ });
+
+ it("gives the footer the page's icon on a tile, the title and what it is", async () => {
+ await render(row(payload()));
+ const footer = card().querySelector<HTMLElement>("[data-page-footer]")!;
+ expect(footer.className).toContain("min-h-18");
+ expect(footer.firstElementChild!.matches("[data-page-icon]")).toBe(true);
+ expect(footer.querySelector("[data-page-icon] svg")!.getAttribute("aria-hidden")).toBe("true");
+ });
+
+ it("has a Brazilian Portuguese word for what the card is", () => {
+ expect((portuguese as Record<string, string>)["chat.preview.kind"]).toBe("Página");
+ });
+
it("names the version once the page has been updated", async () => {
await render(row(payload({ rev: 1, doc: "d1", history: ["d0"] })));
expect(card().querySelector("[data-page-footer]")!.textContent).toBe("PricingPage · v2");
diff --git a/src/components/app/AppMessage.tsx b/src/components/app/AppMessage.tsx
index f8214a63f5..78be64cd35 100644
--- a/src/components/app/AppMessage.tsx
+++ b/src/components/app/AppMessage.tsx
@@ -14,9 +14,13 @@ import { PageCitationButton } from "../CitationUI";
import { appendDraftAttachments } from "@/lib/drafts";
import { useCanWriteIn } from "@/lib/cloud-guest";
import { closePage, onPageOpened, openPage, openPageFor } from "@/lib/open-page";
+import { pageOpensWithHeading } from "./page-heading";
import { PAGE_HEIGHT, PAGE_PREVIEW_HEIGHT, PageFullScreen, PageNotes, PagePaneContext, PageToolbar, usePagePin, usePagePoint, usePageSave, usePageShare, usePageVersion } from "./PageTools";
const WIDTH = "max-w-[760px]";
+const CARD_WIDTH = "max-w-[560px]";
+const CARD_TITLE = "truncate px-5 pt-5 text-[17px] font-semibold leading-6 text-ink";
+const CARD_TITLE_HEIGHT = 44;
const DOC_REUSE_MS = 60_000;
const heights = new Map<string, number>();
@@ -223,8 +227,11 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
const state = collapsed ? "collapsed" : current ? "live" : noPage ? "unavailable" : slot.live ? "loading" : "idle";
const infoId = `${uid}-info`;
+ const srcdoc = compact ? current?.srcdoc : undefined;
+ const titled = useMemo(() => srcdoc !== undefined && !pageOpensWithHeading(srcdoc), [srcdoc]);
// The compact card clips the page at its natural size, so it reads as a preview of the real thing.
- const boxHeight = compact ? Math.min(height, PAGE_PREVIEW_HEIGHT) : height;
+ const previewHeight = height + (titled ? CARD_TITLE_HEIGHT : 0);
+ const boxHeight = compact ? Math.min(previewHeight, PAGE_PREVIEW_HEIGHT) : height;
const body = collapsed ? null : fullScreen && current ? (
<div data-app-box className={cn("relative w-full", bordered && !compact && "border-t border-ink/[0.08]")} style={{ height: boxHeight }}>
{compact
@@ -238,6 +245,7 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
// The whole card opens the page, so the keyboard must not reach what the preview shows underneath it.
inert={(compact && Boolean(current) && !point.pointing) || undefined}
style={{ height: boxHeight }}>
+ {titled && <div data-page-title title={payload.title} className={CARD_TITLE}><bdi dir="auto">{payload.title}</bdi></div>}
{current ? (
<AppFrame
key={loaded!.key}
@@ -256,6 +264,9 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
) : (
<AppPlaceholder height={boxHeight} loading={slot.live} open={slot.live ? undefined : reloadNow} />
)}
+ {compact && current && previewHeight > PAGE_PREVIEW_HEIGHT && (
+ <div data-page-fade aria-hidden="true" className="pointer-events-none absolute inset-x-0 bottom-0 h-8 bg-gradient-to-t from-composer to-transparent" />
+ )}
</div>
);
const info = infoOpen && <AppInfo id={infoId} network={payload.network ?? { connect: [], resources: [] }} bytes={payload.bytes ?? 0} bordered={bordered || compact} />;
@@ -276,7 +287,7 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {
data-app-source={payload.source}
{...(page ? { "data-app-rev": rev } : {})}
{...(room ? { "data-app-room": "" } : {})}
- className={cn("w-full text-start", WIDTH)}
+ className={cn("w-full text-start", compact ? CARD_WIDTH : WIDTH)}
>
{compact ? (
<div className={APP_SURFACE}>
diff --git a/src/components/app/PageTools.tsx b/src/components/app/PageTools.tsx
index 38ffcea744..9d55b0b28e 100644
--- a/src/components/app/PageTools.tsx
+++ b/src/components/app/PageTools.tsx
@@ -315,8 +315,8 @@ function fullScreenLabel({ open, pane }: PageToolsProps["fullScreen"]): string {
/** The chat's page pane: the messages area, above the transcript and below the message box. null outside a bot chat. */
export const PagePaneContext = createContext<HTMLElement | null>(null);
-/** How tall the compact card in the chat is when a page opens in the pane. */
-export const PAGE_PREVIEW_HEIGHT = 220;
+/** How tall the preview on a page's compact card is, at most. */
+export const PAGE_PREVIEW_HEIGHT = 296;
function typing(target: EventTarget | null): boolean {
return target instanceof HTMLElement && (target.isContentEditable || target.matches("input, textarea, select"));
diff --git a/src/components/app/page-heading.test.ts b/src/components/app/page-heading.test.ts
new file mode 100644
index 0000000000..4318ae2d3d
--- /dev/null
+++ b/src/components/app/page-heading.test.ts
@@ -0,0 +1,29 @@
+// @vitest-environment happy-dom
+import { describe, expect, it } from "vitest";
+
+import { botHtmlDocument } from "../../../shared/app-frame/document";
+import { pageOpensWithHeading } from "./page-heading";
+
+const opens = (html: string) => pageOpensWithHeading(botHtmlDocument({ messageId: "a1", rev: 0, html }).srcdoc);
+
+describe("whether a page opens with a heading of its own", () => {
+ it("says yes when the first words are a heading, however deep it sits", () => {
+ expect(opens("<!DOCTYPE html><html><head><title>Sales</title><style>h1{margin:0}</style></head><body><h1>Weekly sales</h1><p>Numbers</p></body></html>")).toBe(true);
+ expect(opens("<main><header><h2>Weekly <em>sales</em></h2></header><p>Numbers</p></main>")).toBe(true);
+ expect(opens("<div role=\"heading\" aria-level=\"1\">Weekly sales</div>")).toBe(true);
+ });
+
+ it("says no when something else comes first", () => {
+ expect(opens("<div class=\"tiles\"><div>0.1.161</div></div><h1>Weekly sales</h1>")).toBe(false);
+ expect(opens("<p>Numbers</p>")).toBe(false);
+ });
+
+ it("looks past what a page does not show", () => {
+ expect(opens("<script>var title = 'x';</script><template>Draft</template><p hidden>Old</p><h1>Weekly sales</h1>")).toBe(true);
+ });
+
+ it("says no for a page a script draws", () => {
+ expect(opens("<div id=\"root\"></div><script>document.getElementById('root').textContent = 'Weekly sales';</script>")).toBe(false);
+ expect(pageOpensWithHeading(botHtmlDocument({ messageId: "a1", rev: 0, format: "jsx", title: "Sales", html: "export default () => <h1>Weekly sales</h1>;" }).srcdoc)).toBe(false);
+ });
+});
diff --git a/src/components/app/page-heading.ts b/src/components/app/page-heading.ts
new file mode 100644
index 0000000000..a6741e99f2
--- /dev/null
+++ b/src/components/app/page-heading.ts
@@ -0,0 +1,20 @@
+const UNSEEN = new Set(["SCRIPT", "STYLE", "TEMPLATE", "NOSCRIPT", "TITLE"]);
+
+function firstText(node: Node): Text | null {
+ for (const child of Array.from(node.childNodes)) {
+ if (child.nodeType === 3) {
+ if (child.textContent?.trim()) return child as Text;
+ } else if (child.nodeType === 1 && !UNSEEN.has((child as Element).tagName) && !(child as Element).hasAttribute("hidden")) {
+ const found = firstText(child);
+ if (found) return found;
+ }
+ }
+ return null;
+}
+
+/** Whether the first words of a page's markup are a heading of its own. A page that draws itself with a script has none. */
+export function pageOpensWithHeading(srcdoc: string): boolean {
+ if (typeof DOMParser === "undefined") return false;
+ const body = new DOMParser().parseFromString(srcdoc, "text/html").body;
+ return Boolean(body && firstText(body)?.parentElement?.closest("h1, h2, h3, [role=heading]"));
+}
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 9fc8856145..4effeec3af 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -7723,6 +7723,7 @@
"chat.preview.exitFullScreen": "Sair da tela cheia",
"chat.preview.openPage": "Abrir página",
"chat.preview.closePage": "Fechar página",
+ "chat.preview.kind": "Página",
"chat.preview.pointedPart": "uma parte da página",
"chat.preview.removePoint": "Remover {label}",
"chat.preview.notePoint": "Escrever uma nota sobre {label}",