Skip to content

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

7.4 KB
Raw Blame
import { ArrowUpRight } from "lucide-react";
import { t } from "@/lib/i18n";
import { CLOUD_PLAN_URL } from "@/lib/app-links";
import { productName } from "@/lib/brand";
import { api } from "@/state/store";
import { useStoreDispatch, useStoreSelector } from "@/state/store-select";
import { useState } from "react";
import { InertRow, useSwitchToComputer } from "../inert";
import { PlaceBadge, YouAvatar } from "../SettingsNav";
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";
import { useAccountSummary, useYou } from "../account-summary";
import { useSettingsShell } from "../settings-shell";
import { SpentRow } from "../plan-parts";
import { profileLabel } from "../../SidebarProfileMenu";

export function AccountPage() {
  const { ctx, place, go } = useSettingsShell();
  const linkRequest = useStoreSelector((state) => state.appSettingsCloudLink), dispatch = useStoreDispatch();
  const { profile, session } = useYou();
  const summary = useAccountSummary();
  const computer = useSwitchToComputer();
  const [signingOut, setSigningOut] = useState(false);
  const bridge = ctx.client ? undefined : window.ogb?.cloudAccount;
  // A Cloud's page in the desktop window that isn't the app's home: the account lives on This computer until main
  // shares it (S2). The same `home` decides This app's rows, so the two never disagree.
  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">
      <SettingsRows>
        {elsewhere && computer.open
          ? <SettingsLinkRow title={t("settings.account.nameRow")} badge={computerBadge} value={t("settings.account.onPlace", { place: computer.name })} external onClick={computer.open} data={{ "data-account-name-row": "" }} />
          : <SettingsLinkRow title={t("settings.account.nameRow")} badge={<PlaceBadge place={place} size={14} />} value={profileLabel(profile)} onClick={() => go("bots", "about-you")} data={{ "data-account-name-row": "" }} />}
      </SettingsRows>
    </SettingsGroup>
    {bridge && <CloudAccountSettings part="account" advanced={ctx.advanced} linkRequest={linkRequest} avatar={<YouAvatar size={32} />}
      onSignIn={() => dispatch({ type: "openCloudAdd", source: "app_settings", signIn: true })} />}
    {elsewhere && (
      <SettingsGroup label={t("settings.account.mausbot", { app: productName() })} block="mausbot-account">
        <SettingsRows icons>
          {/* The same card as on This computer: who is signed in (live, it is the person's own), then the way to sign out
              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 && 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>
    )}
    {!ctx.desktop && (
      <SettingsGroup block="mausbot-account">
        <SettingsRows>
          <SettingRow title={t("settings.account.signedInTo", { place: place.name })} subtitle={profile?.email}>{null}</SettingRow>
          {session?.kind === "session" && (
            <SettingRow title={t("settings.account.signOutBrowser")}>
              <button type="button" data-danger="" className="settings-quiet cursor-pointer" onClick={() => setSigningOut(true)}>{t("settings.account.signOutShort")}</button>
            </SettingRow>
          )}
        </SettingsRows>
        {signingOut && <SignOutConfirm browser here={place.name} onConfirm={() => api("/api/auth/logout", { method: "POST" }).finally(() => location.reload())} onClose={() => setSigningOut(false)} />}
      </SettingsGroup>
    )}
    {ctx.organization ? (
      <SettingsGroup label={t("settings.section.organization")} block="organization">
        <OrganizationSettings simple={!ctx.advanced} />
      </SettingsGroup>
    ) : elsewhere && ctx.advanced && (
      // The organization sign-in is This computer's: it stays in place on a Cloud's page, inert, with the way there.
      <SettingsGroup label={t("settings.section.organization")} block="organization">
        <SettingsRows><InertRow title={t("settings.account.orgSignIn")} control={<button type="button" className="ui-button" tabIndex={-1}>{t("settings.account.signIn")}</button>} /></SettingsRows>
      </SettingsGroup>
    )}
    {ctx.desktop && !ctx.client && window.ogb?.updatesConsent && (
      <SettingsGroup label={t("settings.account.emails", { app: productName() })} block="emails">
        <SettingsRows><UpdatesConsentRow /></SettingsRows>
      </SettingsGroup>
    )}
  </>;
}

export function PlanPage() {
  const { ctx, place } = useSettingsShell();
  const linkRequest = useStoreSelector((state) => state.appSettingsCloudLink), teamHome = useStoreSelector((state) => state.config?.team), dispatch = useStoreDispatch();
  const { usage, upgrade, dialog } = useCloudUsageSettings();
  if (!ctx.desktop) {
    return (
      <SettingsGroup block="plan">
        <SettingsRows>
          <SettingRow title={t("settings.plan.browserTitle")} subtitle={t("settings.plan.browserLine", { app: productName() })}>
            <a className="ui-button ui-button-primary inline-flex items-center gap-1" href={CLOUD_PLAN_URL} target="_blank" rel="noreferrer">{t("settings.plan.openBilling")}<ArrowUpRight size={12} aria-hidden="true" /></a>
          </SettingRow>
        </SettingsRows>
      </SettingsGroup>
    );
  }
  return <>
    <CloudAccountSettings part="plan" advanced={ctx.advanced} cloudHome={ctx.cloud} teamHome={teamHome} linkRequest={linkRequest}
      usage={usage} upgrade={upgrade}
      // A Cloud's own cost stays off its paid plan's card (§1.3: it must never read as a second bill).
      extra={!ctx.advanced && !ctx.client && !(ctx.team && !ctx.admin) && (place.kind === "computer" || place.kind === "server") ? <SpentRow place={place.name} /> : undefined}
      onAddCloud={() => dispatch({ type: "openCloudAdd", source: "app_settings" })}
      onSignIn={() => dispatch({ type: "openCloudAdd", source: "app_settings", signIn: true })} />
    <PlacesTogetherSwitch />
    {dialog}
  </>;
}