Skip to content

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

6.0 KB
Raw Blame
import type { ReactNode } from "react";
import { ChevronLeft } from "lucide-react";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { productName } from "@/lib/brand";
import { desktopAppVersion } from "@/lib/app-links";
import { versionLine, type SettingsPlace } from "./settings-context";
import type { SettingsContext, SettingsPage } from "./settings-index";
import type { SettingsSub } from "./settings-shell";
import { AppIcon, PlaceBadge, YouAvatar, pageLabel } from "./SettingsNav";
import { AdvancedSettingTag } from "./AdvancedSection";

const INTRO: Partial<Record<SettingsPage["id"], Parameters<typeof t>[0]>> = {
  bots: "settings.bots.intro",
  desktops: "settings.desktops.intro",
  vault: "settings.vault.intro",
  app: "settings.app.intro",
};

/** "For My Cloud" with "For" quiet and the place's name in ink, in any word order a language needs. */
function forPlace(place: string): ReactNode {
  const mark = "⁣";
  const [before = "", after = ""] = t("settings.scope.for", { place: mark }).split(mark);
  return <>{before && <span className="text-ink-secondary">{before}</span>}<span className="text-ink">{place}</span>{after && <span className="text-ink-secondary">{after}</span>}</>;
}

function everyPlace(ctx: SettingsContext): ReactNode {
  return ctx.places && ctx.desktop ? <span className="text-ink-secondary"> · {t("settings.scope.everyPlace")}</span> : null;
}

export function overviewStatus(place: SettingsPlace): string {
  if (place.kind === "computer") return `${versionLine(desktopAppVersion())} · ${t("settings.overview.updatesWithApp")}`;
  return `${place.kind === "server" ? t("settings.place.running") : t("settings.place.alwaysOn")} · ${versionLine()}`;
}

export function ScopeLine({ page, ctx, place }: { page: SettingsPage; ctx: SettingsContext; place: SettingsPlace }) {
  if (page.id === "overview") {
    return (
      <p data-settings-scope="" className="flex items-center gap-1.5 text-[13px] leading-[18px] text-ink-secondary">
        {place.kind !== "computer" && <span aria-hidden="true" className="size-1.5 shrink-0 rounded-full bg-success" />}
        {overviewStatus(place)}
      </p>
    );
  }
  const badge = page.owner === "place" ? <PlaceBadge place={place} size={14} /> : page.owner === "account" ? <YouAvatar size={14} /> : <AppIcon size={14} />;
  const text = page.owner === "place" ? forPlace(place.name)
    : page.owner === "account"
      ? ctx.desktop ? <><span className="text-ink">{t("settings.scope.account", { app: productName() })}</span>{everyPlace(ctx)}</> : <span className="text-ink-secondary">{t("settings.scope.browserAccount", { place: place.name })}</span>
      // This app's settings are still kept per place's page until they follow the device (S1), so its line makes no
      // "same on every place" promise yet; Your account's does, because the account is one.
      : ctx.desktop ? <span className="text-ink">{t("settings.scope.app", { app: productName() })}</span> : <span className="text-ink-secondary">{t("settings.scope.browser")}</span>;
  return (
    <p data-settings-scope="" className="flex min-w-0 items-center gap-1.5 text-[13px] leading-[18px]">
      <span className="flex size-3.5 shrink-0 items-center justify-center overflow-hidden rounded-[4px]">{badge}</span>
      <span className="min-w-0 truncate">{text}</span>
    </p>
  );
}

export function SettingsPageHeader({ page, ctx, place, narrow, missing, peek = false, sub = null, action = null }: {
  page: SettingsPage; ctx: SettingsContext; place: SettingsPlace; narrow: boolean; missing?: string;
  /** a page only Advanced lists, opened whole from Simple */
  peek?: boolean;
  /** A step inside the page: its title in the page's place, with the way back above it. */
  sub?: SettingsSub | null;
  /** The page's one action: on the title's line, or beside the scope line where the window's own header holds the title. */
  action?: ReactNode;
}) {
  // A browser has no updates of its own, so its This app intro leaves them out.
  const intro = page.id !== "app" ? INTRO[page.id]
    : !ctx.desktop ? ctx.advanced ? "settings.app.introBrowserAdvanced" : "settings.app.introBrowser"
    : ctx.advanced ? INTRO.app : "settings.app.introSimple";
  const title = page.id === "account" ? t("settings.page.account") : pageLabel(page, place, ctx.desktop);
  const shownAction = action && !sub ? <span data-settings-header-action="" className={cn("ms-auto flex shrink-0 items-center", !narrow && "pe-4")}>{action}</span> : null;
  return (
    // No fill and not sticky: on a glass skin any opaque fill reads as a white block over the sheet.
    <header data-settings-header="" className={cn("flex flex-col gap-1 pb-5", narrow ? "px-8 pt-1" : "pl-4 pt-4")}>
      {!narrow && sub && (
        <button type="button" data-settings-sub-back="" onClick={sub.onBack}
          className="-ms-1.5 -mt-1 mb-0.5 inline-flex h-7 w-fit cursor-pointer items-center gap-0.5 rounded-full pe-2 ps-1 text-[13px] font-medium leading-5 text-ink-secondary hover:bg-ink/[0.05] hover:text-ink">
          <ChevronLeft size={16} aria-hidden="true" className="rtl:-scale-x-100" />{sub.back}
        </button>
      )}
      {!narrow && (
        <div className="flex h-6 min-w-0 items-center gap-2">
          <h2 id="settings-page-title" className="min-w-0 truncate text-[17px] font-semibold leading-6 tracking-[-0.01em] text-ink">{sub?.title ?? title}</h2>
          {peek && <AdvancedSettingTag />}
          {shownAction}
        </div>
      )}
      {narrow && peek && <div className="flex"><AdvancedSettingTag /></div>}
      {narrow && shownAction
        ? <div className="flex min-w-0 items-center gap-3"><ScopeLine page={page} ctx={ctx} place={place} />{shownAction}</div>
        : <ScopeLine page={page} ctx={ctx} place={place} />}
      {intro && <p data-settings-intro={intro} className="text-[13px] leading-[18px] text-ink-secondary">{t(intro, { place: place.name, app: productName() })}</p>}
      {missing && <p role="status" className="text-[13px] leading-[18px] text-ink-secondary">{t("settings.missingHere", { page: missing, place: place.name })}</p>}
    </header>
  );
}