Skip to content

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

CLI login card: show above Settings and other dialogs (#6565)

Milind Sonic444029f3dc9
diff --git a/src/components/CliLoginCard.dom.test.ts b/src/components/CliLoginCard.dom.test.ts
index 8a1a32bea9..54be53eb3b 100644
--- a/src/components/CliLoginCard.dom.test.ts
+++ b/src/components/CliLoginCard.dom.test.ts
@@ -1,10 +1,13 @@
// @vitest-environment happy-dom
+import { readdirSync, readFileSync, statSync } from "node:fs";
+import { join } from "node:path";
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
import { setLocale } from "@/lib/i18n";
-import { CliLoginPrompt } from "./CliLoginCard";
+import { CLI_LOGIN_LAYER, CliLoginPrompt } from "./CliLoginCard";
+import { Layer } from "./Layer";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
afterEach(() => { document.body.innerHTML = ""; vi.unstubAllGlobals(); });
@@ -29,3 +32,45 @@ it("shows the terminal's code and what it would get, and Allow answers that one
expect(document.querySelector("[data-notice='cli-login']")).toBeNull();
await act(async () => { root.unmount(); });
});
+
+const SRC = join(process.cwd(), "src");
+function* sources(dir: string): Generator<string> {
+ for (const name of readdirSync(dir)) {
+ const path = join(dir, name);
+ if (statSync(path).isDirectory()) yield* sources(path);
+ else if (/\.(tsx|css)$/.test(name) && !/\.test\./.test(name) && name !== "CliLoginCard.tsx") yield path;
+ }
+}
+const zOf = (layer: string) => Number(/^z-\[?(\d+)\]?$/.exec(layer)?.[1]);
+
+it("stays above Settings and every dialog, and its Allow still answers with one open", async () => {
+ const dialogLayers: number[] = [];
+ for (const path of sources(SRC)) {
+ const text = readFileSync(path, "utf8").replace(/\r\n/g, "\n");
+ for (const [, z] of text.matchAll(/fixed inset-0\b[^"`\n]*?\bz-\[?(\d+)\]?/g)) dialogLayers.push(Number(z));
+ for (const [, z] of text.matchAll(/(?:LAYER = |layer=)"z-\[?(\d+)\]?"/g)) dialogLayers.push(Number(z));
+ for (const [, z] of text.matchAll(/^\.glass-popup-frame \{[^}]*?z-index: (\d+)/gm)) dialogLayers.push(Number(z));
+ }
+ expect(dialogLayers).toContain(50);
+ expect(zOf(CLI_LOGIN_LAYER)).toBeGreaterThan(Math.max(...dialogLayers));
+
+ setLocale("en");
+ const waiting = { id: "req-over-settings", code: "AB12-CD34", label: "openmausbot CLI", scopes: ["client"], local: true, source: "127.0.0.1", createdAt: Date.now(), expiresAt: Date.now() + 600_000 };
+ const fetchMock = vi.fn(async (_path: string, init?: RequestInit) => new Response(JSON.stringify(
+ init?.method === "POST" ? { ok: true, state: "allowed" } : { requests: [waiting] }), { status: 200 }));
+ vi.stubGlobal("fetch", fetchMock);
+ const host = document.createElement("div"); document.body.append(host);
+ const root = createRoot(host);
+ const settings = createElement(Layer, { modal: true, children: createElement("div", { className: "glass-popup-frame", "data-settings-sheet": "" }) });
+ await act(async () => { root.render([createElement(CliLoginPrompt, { key: "card" }), createElement("div", { key: "settings" }, settings)]); });
+ await act(async () => { await Promise.resolve(); });
+ expect(document.querySelector("[data-settings-sheet]")).not.toBeNull();
+ const card = document.querySelector<HTMLElement>("[data-notice='cli-login']")!;
+ const classes = card.className.split(/\s+/);
+ expect(classes).toContain(CLI_LOGIN_LAYER);
+ expect(classes).not.toContain("z-40");
+ const allow = [...card.querySelectorAll("button")].find((button) => button.textContent === "Allow")!;
+ await act(async () => { allow.click(); });
+ expect(fetchMock).toHaveBeenCalledWith("/api/auth/cli-login/req-over-settings/allow", expect.objectContaining({ method: "POST" }));
+ await act(async () => { root.unmount(); });
+});
diff --git a/src/components/CliLoginCard.tsx b/src/components/CliLoginCard.tsx
index f4e906d230..f8ae73674c 100644
--- a/src/components/CliLoginCard.tsx
+++ b/src/components/CliLoginCard.tsx
@@ -13,6 +13,9 @@ export function cliLoginShown(requests: readonly CliLoginWire[], now: number): C
return requests.find((request) => request.expiresAt > now) ?? null;
}
+/** Above Settings and every dialog: a terminal is waiting on this one click. */
+export const CLI_LOGIN_LAYER = "z-[100]";
+
function useWaitingCliLogins(): [CliLoginWire[], (id: string) => void] {
const [requests, setRequests] = useState<CliLoginWire[]>([]);
useEffect(() => {
@@ -57,7 +60,7 @@ export function CliLoginPrompt() {
}
};
const chatOnly = !request.scopes.includes("admin");
- return <NoticeCard titleId="cli-login-title" notice="cli-login" icon={TerminalSquare} title={t("cliLogin.title")}
+ return <NoticeCard titleId="cli-login-title" notice="cli-login" layer={CLI_LOGIN_LAYER} icon={TerminalSquare} title={t("cliLogin.title")}
dismissLabel={t("cliLogin.deny")} onDismiss={() => void answer(false)}>
<p className="mt-1 text-[13px] text-ink-secondary">
{request.local ? t("cliLogin.bodyHere") : t("cliLogin.bodyElsewhere", { source: request.source })}
diff --git a/src/components/NoticeCard.tsx b/src/components/NoticeCard.tsx
index d323888688..382184b94d 100644
--- a/src/components/NoticeCard.tsx
+++ b/src/components/NoticeCard.tsx
@@ -3,15 +3,16 @@ import { ArrowUpRight, X } from "lucide-react";
import { openExternalLink } from "@/lib/app-links";
import { t } from "@/lib/i18n";
-export function NoticeCard({ titleId, icon: Icon, title, onDismiss, dismissLabel = t("notice.dismiss"), notice, children }: {
+export function NoticeCard({ titleId, icon: Icon, title, onDismiss, dismissLabel = t("notice.dismiss"), notice, layer = "z-40", children }: {
titleId: string; icon: ComponentType<{ size?: number; className?: string }>; title: string; onDismiss: () => void;
dismissLabel?: string;
notice?: string;
+ layer?: string;
children: ReactNode;
}) {
return <aside aria-labelledby={titleId} data-notice={notice} onKeyDown={event => {
if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); onDismiss(); }
- }} className="fixed bottom-4 left-[calc(var(--rail-beside,0px)_+_1rem)] z-40 max-h-[calc(100dvh-32px)] w-[300px] max-w-[calc(100vw_-_var(--rail-beside,0px)_-_32px)] overflow-y-auto rounded-xl border border-hairline/40 bg-panel p-3.5 text-ink shadow-2xl shadow-black/20">
+ }} className={`fixed bottom-4 left-[calc(var(--rail-beside,0px)_+_1rem)] ${layer} max-h-[calc(100dvh-32px)] w-[300px] max-w-[calc(100vw_-_var(--rail-beside,0px)_-_32px)] overflow-y-auto rounded-xl border border-hairline/40 bg-panel p-3.5 text-ink shadow-2xl shadow-black/20`}>
<div className="flex items-start justify-between gap-3">
<div className="flex items-start gap-2.5">
<Icon size={16} className="mt-0.5 shrink-0 text-ink-secondary" aria-hidden="true" />