Skip to content

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

8.4 KB
Raw Blame
import { AppWindow, FolderOpen, FolderTree, Globe, Puzzle, Rocket, UsersRound, Zap } from "lucide-react";
import { useMemo, type ReactNode } from "react";

import { useAppTriggers } from "@/lib/app-triggers";
import { automationsFrom, hasUnseenProblemRun } from "@/lib/automations";
import { cn } from "@/lib/cn";
import { useCloudFilesReady } from "@/lib/cloud-files";
import { t } from "@/lib/i18n";
import { useAdvancedMode } from "@/lib/interface-mode";
import { useWebPanels } from "@/lib/web-panel-preferences";
import { useSidebarToolsLayout, type SidebarDensity } from "@/lib/sidebar-preferences";
import { sameFields, useStoreDispatch, useStoreSelector } from "@/state/store-select";

import { SidebarSectionHeader } from "./SidebarSectionHeader";
import { UpdateIndicator } from "./UpdateIndicator";


function NavRow({
  id,
  label,
  icon,
  active = false,
  attention = false,
  iconsOnly,
  rail = false,
  tourId,
  onClick,
}: {
  id: string;
  label: string;
  icon: (active: boolean) => ReactNode;
  active?: boolean;
  attention?: boolean;
  iconsOnly: boolean;
  rail?: boolean;
  tourId?: string;
  onClick: () => void;
}) {
  return (
    <button
      type="button"
      data-tour={tourId}
      data-sidebar-nav={id}
      onClick={onClick}
      aria-label={iconsOnly ? label : undefined}
      title={iconsOnly ? label : undefined}
      aria-current={active ? "page" : undefined}
      className={cn(
        "relative flex items-center rounded-xl transition-colors",
        // A labelled row follows the bot rows: a 16px icon, a 10px gap, a regular 14/20 label and the ink hover.
        rail ? "size-9 shrink-0 justify-center" : iconsOnly ? "mx-auto size-[46px] shrink-0 justify-center" : "w-full min-h-9 gap-2.5 px-3 py-1.5 text-left",
        active ? "bg-raised text-ink" : "text-ink hover:bg-ink/[0.06]",
      )}
    >
      {icon(active)}
      {!iconsOnly && <span className="flex-1 truncate text-[14px] leading-5">{label}</span>}
      {attention && (
        <span
          data-testid="routines-attention"
          className={cn("size-2 shrink-0 rounded-full bg-danger", iconsOnly && (rail ? "absolute right-1 top-1" : "absolute right-2 top-2"))}
        />
      )}
    </button>
  );
}

export function SidebarAppsButton() {
  const pluginsOpen = useStoreSelector((state) => state.pluginsOpen);
  const dispatch = useStoreDispatch();
  const label = t("sidebar.nav.apps");
  return (
    <button
      type="button"
      data-tour="nav-apps"
      data-sidebar-nav="apps"
      onClick={() => dispatch({ type: "togglePlugins", open: true })}
      aria-label={label}
      title={label}
      aria-pressed={pluginsOpen}
      className={cn(
        "flex size-9 shrink-0 items-center justify-center rounded-xl transition-colors",
        pluginsOpen ? "bg-raised text-accent" : "text-ink-secondary hover:bg-raised/50 hover:text-ink",
      )}
    >
      <Puzzle size={16} />
    </button>
  );
}

/** An unseen failed run of an automation the Automations page lists (the same list, so opening it clears the dot), or an app trigger in trouble. */
export function useAutomationsNeedYou(): boolean {
  const { routines, webhooks, routineRuns, bots } = useStoreSelector((state) => ({ routines: state.routines, webhooks: state.webhooks, routineRuns: state.routineRuns, bots: state.bots }), sameFields);
  const appTriggers = useAppTriggers();
  const routinesOnly = typeof window !== "undefined" && window.ogb?.remoteClient?.active === true;
  const unseenProblem = useMemo(() => hasUnseenProblemRun(automationsFrom({
    routines, webhooks: routinesOnly ? [] : webhooks, appTriggers: appTriggers.triggers, runs: routineRuns, bots,
  })), [routines, webhooks, appTriggers.triggers, routineRuns, bots, routinesOnly]);
  return unseenProblem || appTriggers.triggers.some((trigger) => trigger.enabled && trigger.problem);
}

export function SidebarBottomButtons() {
  return (
    <div data-sidebar-bottom-buttons="" className="flex shrink-0 items-center gap-0.5">
      <SidebarAppsButton />
      <UpdateIndicator />
    </div>
  );
}

export function SidebarFooterNav({
  density,
  rail = false,
  collapsed = false,
  onToggleCollapsed,
}: {
  density: SidebarDensity;
  rail?: boolean;
  collapsed?: boolean;
  onToggleCollapsed?: () => void;
}) {
  const { activeView, pluginsOpen, hasTeam, webPanelId } = useStoreSelector((state) => ({ activeView: state.activeView, pluginsOpen: state.pluginsOpen, hasTeam: Boolean(state.config?.team), webPanelId: state.webPanelId }), sameFields);
  const dispatch = useStoreDispatch();
  const advanced = useAdvancedMode();
  const webPanels = useWebPanels();
  const iconsOnly = density === "icons";
  const toolsLayout = useSidebarToolsLayout();
  const toolbar = advanced && !iconsOnly && toolsLayout === "toolbar";
  // 16 in rows, toolbars and 36px buttons; the avatars-only column's 46px buttons keep 20.
  const iconSize = iconsOnly && !rail ? 20 : 16;
  const tone = (active: boolean) => (active ? "text-accent" : "text-ink-secondary");
  const automationsNeedYou = useAutomationsNeedYou();
  const automationsRow = <NavRow
    id="automations"
    label={t("sidebar.nav.automations")}
    tourId="nav-automations"
    iconsOnly={iconsOnly || toolbar}
    active={activeView === "routines"}
    attention={automationsNeedYou}
    icon={(active) => <Zap size={iconSize} className={tone(active)} />}
    onClick={() => dispatch({ type: "showRoutines", view: "list" })}
  />;
  const filesRow = useCloudFilesReady() && <NavRow
    id="files"
    label={t("sidebar.nav.files")}
    iconsOnly={iconsOnly}
    rail={rail}
    active={activeView === "files"}
    icon={(active) => <FolderOpen size={iconSize} className={tone(active)} />}
    onClick={() => dispatch({ type: "showFiles" })}
  />;
  if (!advanced && !iconsOnly) return null;
  const appsRow = iconsOnly && <NavRow
    id="apps"
    label={t("sidebar.nav.apps")}
    tourId="nav-apps"
    iconsOnly={iconsOnly}
    rail={rail}
    active={pluginsOpen}
    icon={(active) => <Puzzle size={iconSize} className={tone(active)} />}
    onClick={() => dispatch({ type: "togglePlugins", open: true })}
  />;
  if (rail) return <nav data-tour="tools" aria-label={t("sidebar.tools")} className="flex flex-col items-center gap-0.5">{appsRow}</nav>;

  const rows = (
    <>
      {automationsRow}
      {appsRow}
      {(advanced || iconsOnly) && (
        <NavRow
          id="team-map"
          label={t("sidebar.nav.teamMap")}
          tourId="team-tools"
          iconsOnly={iconsOnly || toolbar}
          active={activeView === "team-map"}
          icon={(active) => <UsersRound size={iconSize} className={tone(active)} />}
          onClick={() => dispatch({ type: "showTeamMap" })}
        />
      )}
      {filesRow}
      <NavRow
        id="published-apps"
        label={t("sidebar.nav.publishedApps")}
        iconsOnly={iconsOnly || toolbar}
        active={activeView === "published-apps"}
        icon={(active) => <Rocket size={iconSize} className={tone(active)} />}
        onClick={() => dispatch({ type: "showPublishedApps" })}
      />
      {hasTeam && (
        <NavRow
          id="team-files"
          label={t("sidebar.nav.teamFiles")}
          iconsOnly={iconsOnly || toolbar}
          active={activeView === "team-files"}
          icon={(active) => <FolderTree size={iconSize} className={tone(active)} />}
          onClick={() => dispatch({ type: "showTeamFiles" })}
        />
      )}
      {advanced && webPanels.map((panel) => (
        <NavRow
          key={panel.id}
          id={`web-panel-${panel.id}`}
          label={panel.name}
          iconsOnly={iconsOnly || toolbar}
          active={activeView === "web-panel" && webPanelId === panel.id}
          icon={(active) => (panel.page ? <AppWindow size={iconSize} className={tone(active)} /> : <Globe size={iconSize} className={tone(active)} />)}
          onClick={() => dispatch({ type: "showWebPanel", id: panel.id })}
        />
      ))}
    </>
  );

  if (iconsOnly) {
    if (collapsed) return null;
    return <nav data-tour="tools" aria-label={t("sidebar.tools")} className="flex flex-col items-center gap-1">{rows}</nav>;
  }
  return (
    <div data-tour="tools">
      <SidebarSectionHeader
        name={t("sidebar.tools")}
        collapsed={collapsed}
        alert={collapsed && automationsNeedYou}
        onToggle={onToggleCollapsed}
        reorderable={false}
        dragging={false}
      />
      {!collapsed && <nav aria-label={t("sidebar.tools")} className={toolbar ? "flex flex-row gap-1" : "flex flex-col gap-0.5"}>{rows}</nav>}
    </div>
  );
}