Skip to content

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

3.6 KB
Raw Blame
import { EyeOff, UserMinus } from "lucide-react";

import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { DEFAULT_BOT_NAME, myDefaultBot, useViewerEmail } from "@/lib/default-bot";
import { type Action, type Bot } from "@/state/store";
import { Card, NavRow } from "./bot-panel/rows";
import { SettingRow, Switch } from "./SettingsPrimitives";
import { useStoreDispatch } from "@/state/store-select";
import { useBotApartFromTurns } from "@/state/apart-from-turns";

/** Hiding MausBot is the Settings switch turned off: one place says whether it is in the sidebar. */
export function showDefaultBot(dispatch: (action: Action) => void, bot: Pick<Bot, "id" | "name">, shown: boolean): void {
  dispatch({ type: "updateBot", botId: bot.id, patch: { sidebarHidden: !shown } });
  if (!shown) dispatch({ type: "notice", notice: { kind: "note", botName: null, text: t("defaultBot.hidden", { name: bot.name }) } });
}

export function HideDefaultBotItem({ bot, onDone, className = "flex w-full items-center gap-3 px-3.5 py-2 text-left text-[14px]" }: { bot: Bot; onDone: () => void; className?: string }) {
  const dispatch = useStoreDispatch();
  return (
    <button
      type="button"
      role="menuitem"
      data-default-bot-hide=""
      onClick={() => { showDefaultBot(dispatch, bot, false); onDone(); }}
      title={t("defaultBot.hideHint")}
      className={cn(className, "text-ink hover:bg-menu-hover")}
    >
      <EyeOff size={16} className="text-ink-secondary" />
      {t("defaultBot.hide")}
    </button>
  );
}

/** Demotes the default bot to a regular bot: the mark is cleared server-side, so it can then be archived or deleted. */
export function DemoteDefaultBotItem({ bot, onDone, className = "flex w-full items-center gap-3 px-3.5 py-2 text-left text-[14px]" }: { bot: Bot; onDone: () => void; className?: string }) {
  const dispatch = useStoreDispatch();
  return (
    <button
      type="button"
      role="menuitem"
      data-default-bot-demote=""
      onClick={() => {
        dispatch({ type: "updateBot", botId: bot.id, patch: { defaultBot: null } });
        onDone();
      }}
      title={t("defaultBot.demoteHint")}
      className={cn(className, "text-ink hover:bg-menu-hover")}
    >
      <UserMinus size={16} className="text-ink-secondary" />
      {t("defaultBot.demote")}
    </button>
  );
}

/** Settings → General: whether the viewer's own MausBot has a row in the sidebar. */
export function DefaultBotSidebarRow({ simple = false }: { simple?: boolean }) {
  const viewer = useViewerEmail(), dispatch = useStoreDispatch();
  const bot = useBotApartFromTurns((bots) => myDefaultBot(bots, viewer));
  if (!bot) return null;
  const shown = !bot.sidebarHidden;
  return (
    <SettingRow title={t("defaultBot.show.title", { name: bot.name || DEFAULT_BOT_NAME })} subtitle={t(simple ? "settings.simple.defaultBot" : "defaultBot.show.subtitle")}>
      <Switch
        checked={shown}
        data-default-bot-shown=""
        aria-label={t("defaultBot.show.title", { name: bot.name || DEFAULT_BOT_NAME })}
        onClick={() => showDefaultBot(dispatch, bot, !shown)}
      />
    </SettingRow>
  );
}

/** Bot settings: MausBot is never deleted, only taken off the sidebar. */
export function DefaultBotHideRow({ bot }: { bot: Bot }) {
  const dispatch = useStoreDispatch();
  if (!bot.defaultBot || bot.sidebarHidden) return null;
  return (
    <Card anchor="default-bot">
      <NavRow
        data-panel-row="default-bot-hide"
        icon={EyeOff}
        title={t("defaultBot.hide")}
        hint={t("defaultBot.hideHint")}
        drill={false}
        onClick={() => showDefaultBot(dispatch, bot, false)}
      />
    </Card>
  );
}