Skip to content

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

5.5 KB
Raw Blame
import { Puzzle } from "lucide-react";
import { useEffect, useState } from "react";

import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { useStoreDispatch, useStoreSelector } from "@/state/store-select";

import { loadAppCards } from "./bot-settings/useBotApps";
import { preloadConnectedApps } from "@/lib/connected-apps";
import type { ToolkitCard } from "./ServiceIcon";
import { ELEVATED_CONTROL, ELEVATED_HOVER } from "./elevated";
import { SIMPLE_TOP_BUTTON } from "./SidebarSimpleTop";

export type AppFace = Pick<ToolkitCard, "slug" | "label" | "logo" | "domain">;

export const POPULAR_APPS: readonly AppFace[] = [
  { slug: "gmail", label: "Gmail", logo: null, domain: "gmail.com" },
  { slug: "slack", label: "Slack", logo: null, domain: "slack.com" },
  { slug: "notion", label: "Notion", logo: null, domain: "notion.so" },
];

export const APP_FACES = 3;

export function appFaces(connected: readonly string[], cards: ReadonlyMap<string, AppFace>): AppFace[] {
  const own = connected.map((slug) => cards.get(slug)).filter((card): card is AppFace => Boolean(card));
  return [...own, ...POPULAR_APPS.filter((app) => !own.some((card) => card.slug === app.slug))].slice(0, APP_FACES);
}

interface SidebarApps {
  faces: AppFace[];
  connected: boolean;
}

function useSidebarApps(): SidebarApps {
  const pluginsOpen = useStoreSelector((state) => state.pluginsOpen);
  const [apps, setApps] = useState<SidebarApps>(() => ({ faces: appFaces([], new Map()), connected: false }));
  useEffect(() => {
    if (pluginsOpen) return;
    let alive = true;
    void preloadConnectedApps()
      .then(async (inventory) => {
        const connected = Object.entries(inventory.services).filter(([, status]) => status.connected).map(([slug]) => slug).sort();
        const cards = connected.length > 0 ? await loadAppCards() : new Map<string, AppFace>();
        if (alive) setApps({ faces: appFaces(connected, cards), connected: connected.length > 0 });
      })
      .catch(() => {});
    return () => { alive = false; };
  }, [pluginsOpen]);
  return apps;
}

function AppFaceIcon({ face, className }: { face: AppFace; className?: string }) {
  const sources = [face.logo, face.domain ? `https://www.google.com/s2/favicons?domain=${face.domain}&sz=64` : null].filter((src): src is string => Boolean(src));
  const [failed, setFailed] = useState(0);
  const src = sources[failed];
  return (
    <span data-app-face={face.slug} className={cn("flex size-[18px] shrink-0 items-center justify-center overflow-hidden rounded-md p-px motion-safe:transition-colors group-hover/apps:bg-white group-focus-visible/apps:bg-white", className)}>
      {src
        ? <img src={src} alt="" draggable={false} className="size-4 object-contain" onError={() => setFailed((count) => count + 1)} />
        : <span className="text-[10px] font-semibold leading-none text-neutral-700">{face.label.slice(0, 1).toUpperCase()}</span>}
    </span>
  );
}

/** The outer logos fan out while the pill is hovered. */
const FAN = ["motion-safe:group-hover/apps:-translate-x-[3px] motion-safe:group-hover/apps:-rotate-[8deg]", "", "motion-safe:group-hover/apps:translate-x-[3px] motion-safe:group-hover/apps:rotate-[8deg]"];

/** `circle` is the collapsed rail's round button. */
export function SidebarConnectApps({ circle = false }: { circle?: boolean }) {
  const pluginsOpen = useStoreSelector((state) => state.pluginsOpen);
  const dispatch = useStoreDispatch();
  const { faces, connected } = useSidebarApps();
  const label = connected ? t("sidebar.nav.apps") : t("sidebar.connectApps");
  return (
    <button
      type="button"
      data-tour="nav-apps"
      data-sidebar-nav="apps"
      aria-pressed={pluginsOpen}
      aria-label={circle ? label : undefined}
      title={circle ? label : undefined}
      onClick={() => dispatch({ type: "togglePlugins", open: true })}
      className={circle ? SIMPLE_TOP_BUTTON : cn("group/apps flex h-9 min-w-0 flex-1 items-center justify-center gap-1.5 rounded-full pl-4 pr-3 text-[14px] font-medium leading-5 text-ink", ELEVATED_CONTROL, ELEVATED_HOVER)}
    >
      {circle ? <Puzzle size={16} aria-hidden="true" /> : <>
        <span data-apps-label="" className="shrink-0 whitespace-nowrap">{label}</span>
        {/* The logos rest grey and apart, with no tile, so the sidebar's loudest colour is never an ad; hover brings the tiles and colour back.
            In a tight row the label stays whole and whole logos drop instead: they wrap onto a second line that the one-line
            height hides; the empty first item, a logo high and no wider than nothing, lets even the first logo wrap rather than show cut. The
            padding, taken back by the negative margin, leaves room for the hover fan. */}
        <span aria-hidden="true" data-app-faces="" className="-mx-1 -my-0.5 flex h-[22px] min-w-0 flex-wrap content-start items-center gap-1 overflow-hidden px-1 py-0.5 opacity-60 grayscale motion-safe:transition-[filter,opacity] group-hover/apps:opacity-100 group-hover/apps:grayscale-0 group-hover/apps:invert-0 group-hover/apps:mix-blend-normal group-focus-visible/apps:opacity-100 group-focus-visible/apps:grayscale-0 group-focus-visible/apps:invert-0 group-focus-visible/apps:mix-blend-normal">
          <span data-app-faces-start="" className="-me-1 h-[18px] w-0" />
          {faces.map((face, index) => <AppFaceIcon key={face.slug} face={face}
            className={cn(faces.length > 1 && FAN[index === 0 ? 0 : index === faces.length - 1 ? 2 : 1], "motion-safe:transition-transform motion-safe:duration-150")} />)}
        </span>
      </>}
    </button>
  );
}