Skip to content

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

fix(settings): hide Account and sign out on a page main won't open it for

ARAYN7011fd7ee6c27af3
diff --git a/src/components/AccountMenu.sheet.test.ts b/src/components/AccountMenu.sheet.test.ts
new file mode 100644
index 0000000000..5ca257ceb3
--- /dev/null
+++ b/src/components/AccountMenu.sheet.test.ts
@@ -0,0 +1,54 @@
+import { createElement, type EffectCallback } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+
+// React's hooks are replayed by hand.
+const f = vi.hoisted(() => ({ values: [] as unknown[], index: 0, effects: [] as EffectCallback[] }));
+vi.mock("react", async (original) => ({
+ ...await original<typeof import("react")>(),
+ useState: (initial: unknown) => { const index = f.index++; if (!(index in f.values)) f.values[index] = initial; return [f.values[index], (next: unknown) => { f.values[index] = next; }]; },
+ useEffect: (effect: EffectCallback) => { f.effects.push(effect); },
+}));
+import { useSheetOffered } from "./AccountMenu";
+
+let result: boolean;
+function render(enabled = true) {
+ f.index = 0; f.effects = [];
+ renderToStaticMarkup(createElement(() => { result = useSheetOffered(enabled); return null; }));
+}
+const flush = async () => { for (let i = 0; i < 5; i++) await Promise.resolve(); };
+
+beforeEach(() => { f.values = []; f.index = 0; f.effects = []; });
+afterEach(() => vi.unstubAllGlobals());
+
+it("shows the accounts row only once main says it will open the sheet from this page", async () => {
+ const offered = vi.fn().mockResolvedValue(true);
+ vi.stubGlobal("window", { ogb: { accountSheet: { offered, open: vi.fn() } } });
+ render();
+ expect(result).toBe(false);
+ f.effects[0]!(); await flush();
+ render();
+ expect(result).toBe(true);
+});
+
+it("hides the row on a page main refuses (a self-hosted VPS that isn't home), so the click never goes nowhere", async () => {
+ vi.stubGlobal("window", { ogb: { accountSheet: { offered: vi.fn().mockResolvedValue(false), open: vi.fn() } } });
+ render();
+ f.effects[0]!(); await flush();
+ render();
+ expect(result).toBe(false);
+});
+
+it("stays hidden when asking fails, and never asks when it is not wanted or the bridge is missing", async () => {
+ const offered = vi.fn().mockRejectedValue(new Error("no"));
+ vi.stubGlobal("window", { ogb: { accountSheet: { offered, open: vi.fn() } } });
+ render(); f.effects[0]!(); await flush(); render();
+ expect(result).toBe(false);
+ offered.mockClear(); f.values = [];
+ render(false); f.effects[0]!(); await flush(); render(false);
+ expect(result).toBe(false);
+ expect(offered).not.toHaveBeenCalled();
+ vi.stubGlobal("window", { ogb: {} });
+ render(); f.effects[0]!(); await flush(); render();
+ expect(result).toBe(false);
+});
diff --git a/src/components/AccountMenu.tsx b/src/components/AccountMenu.tsx
index ec7030e75b..cc06b5ef4a 100644
--- a/src/components/AccountMenu.tsx
+++ b/src/components/AccountMenu.tsx
@@ -32,6 +32,19 @@ export function switchNote(result: Pick<RailOpenResult | AccountSwitchResult, "o
}
}
+/** Whether main will open the accounts sheet from this page (a refused click would do nothing, so the row waits for a yes). */
+export function useSheetOffered(enabled = true): boolean {
+ const remote = window.ogb?.accountSheet;
+ const [offered, setOffered] = useState(false);
+ useEffect(() => {
+ if (!enabled || !remote) return;
+ let alive = true;
+ void remote.offered().then((yes) => { if (alive) setOffered(yes === true); }).catch(() => {});
+ return () => { alive = false; };
+ }, [enabled, remote]);
+ return enabled && offered && Boolean(remote);
+}
+
/** Who is signed in on this device, on this app's own page; on any other page, only whether main offers the sheet. */
export function useAccounts(): AccountsAccess & { sheet: { email: string | null; note: string | null } | null; closeSheet(): void } {
const bridge = window.ogb?.remoteClient?.active ? undefined : window.ogb?.accounts;
@@ -39,7 +52,7 @@ export function useAccounts(): AccountsAccess & { sheet: { email: string | null;
const own = window.ogb?.app?.ownPage === true && Boolean(bridge);
const places = usePlaces();
const [view, setView] = useState<AccountsView | null>(null);
- const [offered, setOffered] = useState(false);
+ const offered = useSheetOffered(!own);
const [sheet, setSheet] = useState<{ email: string | null; note: string | null } | null>(null);
useEffect(() => {
if (!own) return;
@@ -47,12 +60,6 @@ export function useAccounts(): AccountsAccess & { sheet: { email: string | null;
read();
return bridge!.onChanged(read);
}, [own, bridge]);
- useEffect(() => {
- if (own || !remote) return;
- let alive = true;
- void remote.offered().then((yes) => { if (alive) setOffered(yes === true); }).catch(() => {});
- return () => { alive = false; };
- }, [own, remote]);
return {
view: own ? view : null,
openSheet: !own && offered && remote ? () => { void remote.open().catch(() => {}); } : null,
diff --git a/src/components/settings/pages/AccountPages.tsx b/src/components/settings/pages/AccountPages.tsx
index b05fbdd455..fef619516b 100644
--- a/src/components/settings/pages/AccountPages.tsx
+++ b/src/components/settings/pages/AccountPages.tsx
@@ -10,6 +10,7 @@ import { SignOutConfirm } from "../SignOutConfirm";
import { CloudAccountSettings } from "../../CloudAccountSettings";
import { useCloudUsageSettings } from "../../CloudUsageMeter";
import { OrganizationSettings } from "../../OrganizationSettings";
+import { useSheetOffered } from "../../AccountMenu";
import { PlacesTogetherSwitch } from "../../PlacesTogetherSwitch";
import { SettingRow, SettingsGroup, SettingsLinkRow, SettingsRows } from "../../SettingsPrimitives";
import { UpdatesConsentRow } from "../../UpdatesConsent";
@@ -31,6 +32,8 @@ export function AccountPage() {
const elsewhere = ctx.desktop && !ctx.client && !ctx.here && !ctx.home && !bridge;
const email = summary.email ?? (summary.plan ? profile?.email ?? null : null);
const sheet = window.ogb?.accountSheet;
+ // Main refuses the sheet on a page that isn't the person's own (a self-hosted VPS), and the click would do nothing.
+ const sheetOffered = useSheetOffered(elsewhere);
const computerBadge = <PlaceBadge place={{ kind: "computer", name: computer.name, color: null }} size={14} />;
return <>
<SettingsGroup block="you">
@@ -49,7 +52,7 @@ export function AccountPage() {
or switch accounts, which happens on This computer, in Sign out's place. Refresh is This computer's only. */}
<SettingRow title={email ?? t("settings.account.signedIn")} leading={<YouAvatar size={32} />}
subtitle={summary.signedIn === false ? t("settings.account.notSignedIn") : t("settings.account.signedIn")}>{null}</SettingRow>
- {sheet && <SettingsLinkRow title={t("accounts.manage")} leading={<span aria-hidden="true" />} onClick={() => void sheet.open().catch(() => {})} data={{ "data-account-manage": "" }} />}
+ {sheet && sheetOffered && <SettingsLinkRow title={t("accounts.manage")} leading={<span aria-hidden="true" />} onClick={() => void sheet.open().catch(() => {})} data={{ "data-account-manage": "" }} />}
{computer.open && <SettingsLinkRow title={t("settings.account.fromHere", { place: computer.name })} leading={<span aria-hidden="true" />} external onClick={computer.open} data={{ "data-account-elsewhere": "" }} />}
</SettingsRows>
</SettingsGroup>