Skip to content

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

18.5 KB
Raw Blame
import { useEffect, useMemo, useRef, useState } from "react";
import { Pause, Play, Plus, Zap } from "lucide-react";

import { automationsFrom, automationSentence, automationStatus, orderAutomations, runLine, statusLine, type Automation } from "@/lib/automations";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { isRoutineProblemRun } from "@/lib/routines";
import { saveWebhookCredential, webhookCredentialStore } from "@/lib/webhook-credentials";
import { botInDepartment, filterableDepartments, GENERAL_DEPARTMENT } from "@/lib/department-filter";
import { type Bot } from "@/state/store";
import { useBotsApartFromTurns } from "@/state/apart-from-turns";
import { sameFields, useStoreDispatch, useStoreSelector } from "@/state/store-select";
import { useAppTriggers } from "@/lib/app-triggers";
import { useMinuteTick } from "@/lib/minute-tick";
import { AppEventEditor } from "./automations/AppEventEditor";
import { type AutomationActions, SET_ALL_BUSY, useAllAutomationActions } from "./automations/app-actions";
import { AutomationPanel } from "./automations/AutomationPanel";
import { AutomationsCalendar } from "./automations/AutomationsCalendar";
import { LimitWarnings } from "./automations/LimitWarnings";
import { NewAutomation, type SetUpKind } from "./automations/NewAutomation";
import { FixButton, StatusMark } from "./automations/parts";
import { usePausedByAll } from "./automations/paused-by-all";
import { RoutineEditor } from "./RoutineCalendarPage";
import { Select } from "./Select";
import { Switch } from "./SettingsPrimitives";
import { TOOL_PAGE_TITLE, ToolPage, ToolPageActions, ToolPageHeader } from "./ToolPageShell";
import { UndoToast, useUndoToast } from "./UndoToast";
import { WebhookEditor } from "./WebhookTrigger";
import { schedulableBots, useTeamPeople } from "@/state/team-people";

/** The bot filter earns its place only on a long list shared by several bots. */
const FILTER_FROM = 9;
const NO_BOTS: Bot[] = [];

/** Automations: every schedule and every "when another app sends something", each as one plain sentence. */
export function TriggersPage({ onBack, onOpenRoom }: { onBack: () => void; onOpenRoom: (id: string) => void }) {
  const state = useStoreSelector((s) => ({
    routinesFocus: s.routinesFocus, routines: s.routines, webhooks: s.webhooks, routineRuns: s.routineRuns, sections: s.sections,
    routinesUnreadable: s.config?.routinesUnreadable, routinesLoadState: s.routinesLoadState, hydrated: s.hydrated,
  }), sameFields), bots = useBotsApartFromTurns(), dispatch = useStoreDispatch();
  const actions = useAllAutomationActions();
  useMinuteTick();
  const appTriggers = useAppTriggers();
  const backButtonRef = useRef<HTMLButtonElement>(null);
  const focus = state.routinesFocus;
  const view = focus?.view === "calendar" ? "calendar" : "list";
  const [openId, setOpenId] = useState<string | null>(focus?.section !== "logs" ? focus?.routineId ?? null : null);
  const [botFilter, setBotFilter] = useState(focus?.botId ?? "all");
  const [department, setDepartment] = useState("all");
  const [newText, setNewText] = useState<string | null>(null);
  const [editing, setEditing] = useState<{ kind: SetUpKind; id?: string; botId?: string } | null>(null);
  const routinesOnly = window.ogb?.remoteClient?.active === true;
  const visibleBots = bots.filter((bot) => !bot.hidden);
  // An editor reads each bot's threads, so while one is open the page follows every change to the bots.
  const editorBots = useStoreSelector((s) => editing ? s.bots : NO_BOTS).filter((bot) => !bot.hidden);
  const team = useTeamPeople();
  const botName = (botId: string) => bots.find((bot) => bot.id === botId)?.name ?? t("routines.unavailableBot");

  useEffect(() => {
    setBotFilter(focus?.botId ?? "all");
    // "Open" from a chat card or a notice names the automation: open its panel.
    if (focus?.routineId && focus.section !== "logs") setOpenId(focus.routineId);
  }, [focus]);
  useEffect(() => { backButtonRef.current?.focus({ preventScroll: true }); }, []);

  const all = useMemo(() => automationsFrom({ routines: state.routines, webhooks: routinesOnly ? [] : state.webhooks, appTriggers: appTriggers.triggers, runs: state.routineRuns, bots }),
    [state.routines, state.webhooks, appTriggers.triggers, state.routineRuns, bots, routinesOnly]);
  const departments = filterableDepartments(bots, [...new Set([...(state.sections ?? []), ...bots.flatMap((bot) => bot.section ? [bot.section] : [])])]);
  const showDepartments = departments.names.length + (departments.hasGeneral ? 1 : 0) > 1;
  const activeDepartment = department === "all" || (department === GENERAL_DEPARTMENT ? departments.hasGeneral : departments.names.includes(department)) ? department : "all";
  useEffect(() => {
    if (activeDepartment === department) return;
    setDepartment("all");
    setBotFilter("all");
  }, [activeDepartment, department]);
  const inDepartment = (botId: string) => botInDepartment(bots.find((bot) => bot.id === botId), activeDepartment);
  const departmentBots = visibleBots.filter((bot) => botInDepartment(bot, activeDepartment));
  const showFilter = all.length >= FILTER_FROM && new Set(all.map((automation) => automation.botId)).size > 1;
  const listed = orderAutomations(all.filter((automation) => inDepartment(automation.botId) && (!showFilter || botFilter === "all" || automation.botId === botFilter)));
  const open = all.find((automation) => automation.id === openId) ?? null;
  const sentenceOf = (automation: Automation) => automationSentence(automation, botName(automation.botId));
  const setAll = usePauseAll(view === "list" ? listed : [], JSON.stringify([activeDepartment, showFilter ? botFilter : "all"]), actions);

  const editRoutine = editing?.kind === "schedule" ? state.routines.find((routine) => routine.id === editing.id) : undefined;
  const editWebhook = editing?.kind === "webhook" ? state.webhooks.find((webhook) => webhook.id === editing.id) : undefined;
  const editApp = editing?.kind === "app" ? appTriggers.triggers.find((trigger) => trigger.id === editing.id) : undefined;

  return (
    <ToolPage>
      <ToolPageHeader backLabel={t("automations.back")} onBack={onBack} backRef={backButtonRef} besideSidebar={view === "list"}>
          <h1 data-tour="automations-page" className={TOOL_PAGE_TITLE}>{t("automations.title")}</h1>
          <ToolPageActions>
            {showDepartments && (
              <Select size="sm" aria-label={t("automations.allDepartments")} value={activeDepartment} onChange={(event) => { setDepartment(event.target.value); setBotFilter("all"); }} className="max-w-[180px]">
                <option value="all">{t("automations.allDepartments")}</option>
                {departments.names.map((name) => <option key={name} value={name}>{name}</option>)}
                {departments.hasGeneral && <option value={GENERAL_DEPARTMENT}>{t("automations.generalDepartment")}</option>}
              </Select>
            )}
            {showFilter && view === "list" && (
              <Select size="sm" aria-label={t("automations.allBots")} value={botFilter} onChange={(event) => setBotFilter(event.target.value)} className="max-w-[180px]">
                <option value="all">{t("automations.allBots")}</option>
                {departmentBots.map((bot) => <option key={bot.id} value={bot.id}>{bot.name}</option>)}
              </Select>
            )}
            {setAll.offer && (
              <button type="button" className="ui-button ui-button-sm" data-automations-set-all="" disabled={actions.busy !== null} onClick={() => void setAll.run()}>
                {setAll.offer === "pause" ? <Pause size={14} aria-hidden="true" /> : <Play size={14} aria-hidden="true" />}
                {t(setAll.offer === "pause" ? "automations.pauseAll" : "automations.resumeAll")}
              </button>
            )}
            <div role="group" aria-label={t("automations.view")} className="flex items-center rounded-lg border border-hairline/50 bg-panel p-0.5">
              {(["list", "calendar"] as const).map((option) => (
                <button key={option} type="button" aria-pressed={view === option} onClick={() => dispatch({ type: "showRoutines", view: option, botId: botFilter === "all" ? undefined : botFilter })}
                  className={cn("rounded-md px-3 py-1 text-[13px] font-medium", view === option ? "bg-raised text-ink shadow-sm" : "text-ink-secondary hover:text-ink")}>
                  {option === "list" ? t("automations.list") : t("automations.calendar")}
                </button>
              ))}
            </div>
            <button type="button" onClick={() => setNewText("")} className="flex items-center gap-1.5 rounded-lg bg-accent px-3.5 py-2 text-[13px] font-semibold text-accent-ink hover:brightness-110"><Plus size={15} aria-hidden="true" />{t("automations.new")}</button>
          </ToolPageActions>
      </ToolPageHeader>

      <div className="relative flex min-h-0 flex-1">
        {view === "calendar" ? <AutomationsCalendar botFilter={botFilter} department={activeDepartment} selectedAutomationId={openId} onOpenAutomation={setOpenId} onOpenRoom={onOpenRoom} /> : (
          <div className="min-h-0 flex-1 overflow-y-auto">
            <div className="mx-auto w-full max-w-3xl p-4 sm:p-6">
              <LimitWarnings botFilter={showFilter ? botFilter : "all"} />
              {state.routinesUnreadable && <div role="alert" className="mb-4 rounded-lg bg-danger/10 p-3 text-[12px] leading-relaxed text-danger" data-routines-unreadable="">{t("routines.unreadable")}</div>}
              {all.length === 0 && !state.routinesUnreadable && state.routinesLoadState !== "loading" && state.hydrated ? (
                <section aria-label={t("automations.empty.title")} className="px-6 py-16 text-center">
                  <div className="mx-auto mb-4 flex size-12 items-center justify-center rounded-2xl bg-accent/10 text-accent"><Zap size={22} /></div>
                  <h2 className="text-[17px] font-semibold text-ink">{t("automations.empty.title")}</h2>
                  <button type="button" onClick={() => setNewText(t("automations.empty.example"))} className="mt-3 text-[14px] text-accent-text underline-offset-2 hover:underline">{t("automations.empty.try")}</button>
                  <div><button type="button" onClick={() => setNewText("")} className="mt-6 inline-flex items-center gap-2 rounded-xl bg-accent px-4 py-2.5 text-[13px] font-medium text-accent-ink hover:brightness-110"><Plus size={15} />{t("automations.new")}</button></div>
                </section>
              ) : (
                <ul aria-label={t("automations.title")} className="divide-y divide-hairline/30 overflow-hidden rounded-2xl border border-hairline/40 bg-card">
                  {listed.map((automation) => {
                    const status = automationStatus(automation);
                    const name = automation.name.trim();
                    const unseenProblem = automation.runs.find((run) => isRoutineProblemRun(run) && !run.seenAt);
                    const problemLine = unseenProblem && runLine(unseenProblem);
                    return (
                      <li key={automation.id} data-automation={automation.id} data-automation-status={status.state} className={cn("relative flex items-start gap-3 px-4 py-3.5", openId === automation.id && "bg-raised/60")}>
                        <StatusMark status={status} className="mt-[5px]" />
                        <div className="min-w-0 flex-1">
                          <button type="button" onClick={() => setOpenId(automation.id)} className="block text-left text-[15px] font-medium leading-snug text-ink after:absolute after:inset-0 hover:text-accent-text">{name || sentenceOf(automation)}</button>
                          {name && <div className="mt-0.5 text-[13px] text-ink">{sentenceOf(automation)}</div>}
                          {status.state === "needs-you"
                            ? <div className="mt-1.5 flex flex-wrap items-center gap-2 text-[13px] text-warning">{t("automations.needsYou", { reason: status.reason ?? "" })}<FixButton automation={automation} status={status} actions={actions} onEdit={() => setEditing({ kind: automation.kind, id: automation.id })} className="relative z-10" /></div>
                            : <div className="mt-0.5 text-[13px] text-ink-secondary">{statusLine(automation)}</div>}
                          {problemLine && <button type="button" onClick={() => setOpenId(automation.id)} className="mt-1 flex items-baseline gap-2 text-left text-[13px] text-danger">
                            <span aria-hidden="true" className="size-2 shrink-0 self-center rounded-full bg-danger" />
                            <span>{problemLine.when} {problemLine.mark} {problemLine.text}</span>
                          </button>}
                          {!!automation.noStartStreak && <div className="mt-1 text-[13px] text-warning" data-automation-no-start="">{t("routines.noStartStreak", { count: automation.noStartStreak })}</div>}
                          {!open && actions.error && actions.errorFor?.endsWith(`:${automation.id}`) && <p role="alert" className="mt-1 text-[12px] text-danger">{actions.error}</p>}
                        </div>
                        {status.state !== "done" && <Switch checked={automation.enabled} disabled={actions.busy === `toggle:${automation.id}` || actions.busy === SET_ALL_BUSY} aria-label={t("automations.toggle", { name: automation.name })} onClick={() => { setAll.forget(automation.id); void actions.toggle(automation); }} className="relative z-10 mt-0.5 shrink-0" />}
                      </li>
                    );
                  })}
                </ul>
              )}
              {!open && actions.error && !listed.some((automation) => actions.errorFor?.endsWith(`:${automation.id}`)) && <p role="alert" className="mt-3 text-[12px] text-danger">{actions.error}</p>}
              <RoutineWakeBar />
            </div>
          </div>
        )}
        {open && (
          <div className="absolute inset-y-0 right-0 z-30 flex shadow-2xl lg:static lg:shadow-none">
            <AutomationPanel key={open.id} automation={open} sentence={sentenceOf(open)} actions={actions} onClose={() => setOpenId(null)} onDeleted={() => setOpenId(null)} onEdit={() => setEditing({ kind: open.kind, id: open.id })} />
          </div>
        )}
      </div>

      {newText !== null && (
        <NewAutomation initialText={newText} onClose={() => setNewText(null)} onSetUpYourself={(kind, botId) => { setNewText(null); setEditing({ kind, botId }); }} />
      )}
      {editing?.kind === "schedule" && (editRoutine || !editing.id) && <RoutineEditor routine={editRoutine} bots={schedulableBots(editorBots, team, editRoutine?.botId)} initialBotId={editing.botId} plain onClose={() => setEditing(null)} />}
      {editing?.kind === "webhook" && (editWebhook || !editing.id) && (
        <WebhookEditor webhook={editWebhook} bots={editorBots} initialBotId={editing.botId} onClose={() => setEditing(null)} onCredential={(credential, id) => { saveWebhookCredential(webhookCredentialStore(), id, credential); setOpenId(id); }} />
      )}
      {editing?.kind === "app" && (editApp || !editing.id) && <AppEventEditor trigger={editApp} bots={editorBots} initialBotId={editing.botId} onClose={() => setEditing(null)} onSaved={(trigger) => setOpenId(trigger.id)} />}
      <UndoToast toast={setAll.toast} onDone={setAll.hideToast} />
    </ToolPage>
  );
}

/** "Pause all" turns off every automation on in the list shown, with Undo; "Resume all" then turns back on only the
 * ones it turned off and that are still off. One the person switches by hand is theirs again. Shown for two or more
 * automations with a switch. */
function usePauseAll(listed: readonly Automation[], list: string, actions: AutomationActions) {
  const [pausedByAll, remember] = usePausedByAll(list);
  const [toast, showToast] = useUndoToast();
  const latest = useRef({ listed, actions });
  latest.current = { listed, actions };
  const switchable = listed.filter((automation) => automationStatus(automation).state !== "done");
  const toResume = switchable.filter((automation) => !automation.enabled && pausedByAll.includes(automation.id));
  const toPause = switchable.filter((automation) => automation.enabled);
  const offer = switchable.length < 2 ? null : toResume.length ? "resume" : toPause.length ? "pause" : null;

  /** Turns back on those of `ids` still off, and keeps in the list's set only the ones that could not be. */
  const resume = async (ids: readonly string[]) => {
    const off = latest.current.listed.filter((automation) => !automation.enabled && ids.includes(automation.id) && automationStatus(automation).state !== "done");
    const resumed = await latest.current.actions.setAllEnabled(off, true);
    remember(off.map((automation) => automation.id).filter((id) => !resumed.includes(id)));
  };
  const pause = async () => {
    const paused = await actions.setAllEnabled(toPause, false);
    if (!paused.length) return;
    remember(paused);
    showToast({
      text: paused.length === 1 ? t("automations.pausedAll.one") : t("automations.pausedAll.other", { count: paused.length }),
      undo: () => void resume(paused),
    });
  };
  return {
    offer,
    run: () => offer === "resume" ? resume(toResume.map((automation) => automation.id)) : pause(),
    forget: (id: string) => { if (pausedByAll.includes(id)) remember(pausedByAll.filter((kept) => kept !== id)); },
    toast,
    hideToast: () => showToast(null),
  };
}

/** While a Mac sleeps no schedule runs: the shell can hold it awake the hour before one is due, plugged in only. */
function RoutineWakeBar() {
  const bridge = typeof window !== "undefined" ? window.ogb?.routines : undefined;
  const [state, setState] = useState<DesktopRoutineWake | null>(null);
  const [busy, setBusy] = useState(false);
  useEffect(() => {
    if (!bridge) return;
    let cancelled = false;
    const load = () => bridge.wakeState().then((next) => { if (!cancelled) setState(next); }).catch(() => {});
    void load();
    const timer = window.setInterval(load, 30_000);
    return () => { cancelled = true; window.clearInterval(timer); };
  }, [bridge]);
  if (!bridge || !state) return null;
  return (
    <div className="mt-6 flex items-center justify-between gap-4 px-1 text-[12px] text-ink-secondary">
      <span>{t("automations.wake")}{state.keepAwake && state.onBattery ? ` ${t("automations.wakeBattery")}` : ""}</span>
      <Switch
        checked={state.keepAwake}
        disabled={busy}
        aria-label={t("automations.wake")}
        onClick={() => {
          setBusy(true);
          bridge.keepAwake(!state.keepAwake).then(setState).catch(() => {}).finally(() => setBusy(false));
        }}
        className="shrink-0 disabled:cursor-wait disabled:opacity-50"
      />
    </div>
  );
}