Skip to content

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

7.2 KB
Raw Blame
// Works because styles.css keys skins on [data-skin], not :root[data-skin], so any element opens a skin context for its
// subtree.
import { Check } from "lucide-react";
import { SKINS, applySkin, resolveSkin, type SkinId } from "@/lib/skins";
import { takeOffTheme } from "@/lib/theme/runtime";
import { cn } from "@/lib/cn";
import { t, tFromServer } from "@/lib/i18n";
import { setDressUp, useDressUp } from "@/lib/festival-dress";
import { FESTIVALS, isFestival, type CostumeColor, type FestivalId } from "../../shared/bot-body";
import { MausAvatar } from "./Avatar";
import { useContext } from "react";
import { SettingsLook, Switch } from "./SettingsPrimitives";
import { useSkinChoice } from "./ThemeRow";

function Miniature({ skin }: { skin: SkinId }) {
  return (
    <div
      data-skin={skin}
      data-skin-preview=""
      aria-hidden="true"
      className="flex h-[78px] w-full overflow-hidden rounded-lg bg-app ring-1 ring-hairline/60"
    >
      <div className="flex w-[11px] shrink-0 flex-col items-center gap-[3px] bg-panel pt-[5px]">
        <span className="size-[5px] rounded-full bg-accent" />
        <span className="size-[5px] rounded-full bg-ink-secondary/40" />
        <span className="size-[5px] rounded-full bg-ink-secondary/40" />
      </div>
      <div className="flex w-[30px] shrink-0 flex-col gap-[3px] border-r border-hairline bg-panel p-[4px]">
        <span className="flex h-[9px] w-full items-center gap-[2px] rounded-sm bg-raised px-[2px]">
          <span className="size-[4px] shrink-0 rounded-full bg-accent" />
          <span className="h-[2px] flex-1 rounded-full bg-ink/50" />
        </span>
        <span className="h-[3px] w-[80%] rounded-full bg-ink-secondary/30" />
        <span className="h-[3px] w-[62%] rounded-full bg-ink-secondary/30" />
        <span className="h-[3px] w-[74%] rounded-full bg-ink-secondary/30" />
      </div>
      <div className="flex min-w-0 flex-1 flex-col gap-[4px] p-[6px]">
        <span className="h-[13px] w-[62%] self-end rounded-md bg-bubble-user" />
        <div className="flex w-[88%] flex-col gap-[3px] rounded-md bg-card p-[4px]">
          <span className="h-[2px] w-full rounded-full bg-ink/45" />
          <span className="h-[2px] w-[85%] rounded-full bg-ink/45" />
          <span className="h-[2px] w-[60%] rounded-full bg-ink-secondary/40" />
        </div>
        <div className="mt-auto flex items-center gap-[4px]">
          <span className="h-[11px] flex-1 rounded-full bg-inset ring-1 ring-hairline" />
          <span className="flex size-[11px] items-center justify-center rounded-full bg-accent">
            <span
              className="h-[1.5px] w-[5px] rounded-full"
              style={{ background: "var(--color-accent-ink)" }}
            />
          </span>
        </div>
      </div>
    </div>
  );
}

const DRESS_UP_PREVIEW: Record<FestivalId, readonly CostumeColor[]> = {
  halloween: ["orange", "purple", "teal", "red"],
  diwali: ["red", "orange", "pink", "coral"],
};

export function DressUpRow({ festival }: { festival: FestivalId }) {
  const dressed = useDressUp();
  const title = t("settings.skin.dressUp.title");
  return (
    <div data-dress-up className="mt-4 flex items-center gap-4 rounded-xl border border-accent-border/40 bg-accent/[0.06] p-3">
      <div className="flex shrink-0 -space-x-1.5" aria-hidden="true">
        {DRESS_UP_PREVIEW[festival].map((color) => (
          <MausAvatar key={color} color={color} bodyId={FESTIVALS[festival].costumes[color]} size={30} animated={false} trackPointer={false} />
        ))}
      </div>
      <div className="min-w-0 flex-1">
        <div className="text-[14px] font-medium text-ink">{title}</div>
        <div className="mt-1 text-[12px] leading-relaxed text-ink-secondary">{t("settings.skin.dressUp.subtitle")}</div>
      </div>
      <Switch checked={dressed} aria-label={title} onClick={() => setDressUp(!dressed)} />
    </div>
  );
}

export function SkinPicker() {
  const active = resolveSkin(useSkinChoice());
  const settings = useContext(SettingsLook);
  if (settings) {
    // Settings: no tile borders; an 80 px preview, the name under it, and the chosen one ringed in ink.
    return (
      <>
        <div className="grid grid-cols-2 gap-x-3 gap-y-4 sm:grid-cols-4" role="radiogroup" aria-label={t("settings.theme.title")}>
          {SKINS.map((skin) => {
            const selected = skin.id === active;
            return (
              <button key={skin.id} type="button" role="radio" aria-checked={selected} onClick={() => { applySkin(skin.id); takeOffTheme(); }}
                title={tFromServer(`skins.tagline.${skin.id}`, skin.tagline)}
                className="group flex cursor-pointer flex-col gap-2 rounded-lg text-left focus-visible:outline-none">
                {/* Every preview: a 0.5 px frame. The chosen one adds the 2 px ink ring and a 16 px check at its top right. */}
                <span className={cn("relative block rounded-lg ring-offset-2 ring-offset-(--color-menu) [&>[data-skin-preview]]:h-20 [&>[data-skin-preview]]:ring-0",
                  "after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:shadow-[inset_0_0_0_0.5px_var(--settings-edge)] after:content-['']",
                  selected ? "ring-2 ring-ink" : "group-hover:ring-[0.5px] group-hover:ring-ink/25 group-focus-visible:ring-2 group-focus-visible:ring-focus")}>
                  <Miniature skin={skin.id} />
                  {selected && <span aria-hidden="true" data-skin-check="" className="absolute -right-2 -top-2 flex size-4 items-center justify-center rounded-full bg-ink text-app"><Check size={12} strokeWidth={2.5} /></span>}
                </span>
                <span className="truncate text-[13px] leading-[18px] text-ink">{skin.name}</span>
              </button>
            );
          })}
        </div>
        {isFestival(active) && <DressUpRow festival={active} />}
      </>
    );
  }

  return (
    <>
      <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
        {SKINS.map((skin) => {
          const selected = skin.id === active;
          return (
            <button
              key={skin.id}
              type="button"
              onClick={() => {
                applySkin(skin.id);
                takeOffTheme();
              }}
              aria-pressed={selected}
              className={cn(
                "cursor-pointer flex flex-col gap-2 rounded-xl border p-2 text-left transition-colors",
                selected
                  ? "border-accent-border bg-control"
                  : "border-hairline/60 hover:border-hairline hover:bg-control/50",
              )}
            >
              <Miniature skin={skin.id} />
              <div className="flex items-start gap-1.5 px-0.5 pb-0.5">
                <div className="min-w-0 flex-1">
                  <div className="text-[13px] font-medium text-ink">{skin.name}</div>
                  <div className="mt-0.5 text-[11px] leading-snug text-ink-secondary">
                    {tFromServer(`skins.tagline.${skin.id}`, skin.tagline)}
                  </div>
                </div>
                {selected && <Check size={13} className="mt-0.5 shrink-0 text-accent-text" />}
              </div>
            </button>
          );
        })}
      </div>
      {isFestival(active) && <DressUpRow festival={active} />}
    </>
  );
}