Skip to content

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

6.3 KB
Raw Blame
import { FileText, Loader2, Repeat2 } from "lucide-react";
import { showBotLimit } from "@/lib/bot-limit";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import type { Routine, RoutineRun } from "@/lib/routines";
import { scheduleLabel } from "@/lib/schedule-label";
import { latestRoutineRun, routineDateTime, routineFinished, routineHeldNote, routineNextLabel, routineRunLabel, routineRunText, routineRunTone, routineScheduleState } from "@/lib/routine-display";
import type { Bot } from "@/state/store";
import { Switch } from "../SettingsPrimitives";

export function RoutineList({ routines, runs, bots, pausedBots, heldBots, loading, error, unreadable, onOpen, onLogs, onToggle }: {
  routines: Routine[];
  runs: RoutineRun[];
  bots?: Bot[];
  /** Bots a Starter Cloud has paused: a routine on one runs nothing until it is swapped in. */
  pausedBots?: readonly string[];
  /** Bots the person paused, by themselves or with their department. */
  heldBots?: readonly string[];
  loading?: boolean;
  error?: boolean;
  /** The server cannot read its saved routines. */
  unreadable?: boolean;
  onOpen: (routine: Routine) => void;
  onLogs: (routine: Routine) => void;
  onToggle?: (routine: Routine) => void;
}) {
  const sorted = [...routines].sort((a, b) => Number(b.enabled) - Number(a.enabled) || (a.nextRunAt ?? Infinity) - (b.nextRunAt ?? Infinity) || a.name.localeCompare(b.name));
  return <div className="space-y-3" aria-label={t("routines.list")}>
    {error && <div role="alert" className="rounded-lg bg-danger/10 p-3 text-[12px] text-danger">{t("routines.loadError")}</div>}
    {unreadable && <div role="alert" className="rounded-lg bg-danger/10 p-3 text-[12px] leading-relaxed text-danger" data-routines-unreadable="">{t("routines.unreadable")}</div>}
    {loading && <p role="status" className="flex items-center gap-2 p-3 text-[12px] text-ink-secondary"><Loader2 size={14} className="animate-spin" />{t("routines.loading")}</p>}
    {!loading && !error && !unreadable && sorted.length === 0 && <div className="rounded-xl border border-dashed border-hairline/50 p-5 text-center text-[13px] text-ink-secondary"><Repeat2 size={20} className="mx-auto mb-2 opacity-60" />{t("routines.empty")}</div>}
    {sorted.map((routine) => {
      const latest = latestRoutineRun(routine.id, runs);
      const said = latest && (latest.attention ? "attention" : latest.error ? "error" : latest.output ? "output" : null);
      const bot = bots?.find((candidate) => candidate.id === routine.botId);
      const switched = onToggle !== undefined && !routineFinished(routine);
      return <article key={routine.id} className="rounded-xl border border-hairline/40 bg-card p-3.5" aria-label={routine.name}>
        <div className="flex items-start gap-2">
          <button type="button" onClick={() => onOpen(routine)} className="min-w-0 flex-1 text-left hover:text-accent">
            <span className="block truncate text-[13px] font-semibold text-ink">{routine.name}</span>
            <span className="mt-1 block text-[12px] leading-relaxed text-ink-secondary">{bot && `${bot.name} · `}{scheduleLabel(routine.schedule)}</span>
          </button>
          {switched
            ? <Switch checked={routine.enabled} aria-label={t("routines.toggle", { name: routine.name })} onClick={() => onToggle(routine)} />
            : <span className={cn("shrink-0 rounded-full bg-inset px-2 py-0.5 text-[10px]", routine.enabled && routine.nextRunAt != null ? "text-accent" : "text-ink-secondary")}>{routineScheduleState(routine)}</span>}
        </div>
        {(switched || routineNextLabel(routine) !== routineScheduleState(routine)) && <div className="mt-2 text-[12px] text-ink-secondary">{routineNextLabel(routine)}</div>}
        <div className="mt-1 flex items-start justify-between gap-2">
          <div className="min-w-0 text-[12px]">
            {latest ? <><span className={routineRunTone(latest)}>{t("routines.latestRun", { status: routineRunLabel(latest) })}</span><span className="block text-[11px] text-ink-secondary">{routineDateTime(latest.finishedAt ?? latest.startedAt ?? latest.scheduledFor)}</span></> : <span className="text-ink-secondary">{t("routines.neverRun")}</span>}
          </div>
          <button type="button" onClick={() => onLogs(routine)} aria-label={t("routines.logsFor", { name: routine.name })} className="flex shrink-0 items-center gap-1 rounded-md px-2 py-1 text-[11px] text-ink-secondary hover:bg-raised hover:text-ink"><FileText size={12} />{t("routines.logs")}</button>
        </div>
        {latest && said && <p className={cn("mt-2 line-clamp-2 whitespace-pre-wrap text-[12px] leading-relaxed", latest.error ? "text-danger" : "text-ink-secondary")}>{routineRunText(latest, said)}</p>}
        {!!routine.failureStreak && <p className="mt-2 text-[12px] text-danger">{t("routines.failureStreak", { count: routine.failureStreak })}</p>}
        {!!routine.noStartStreak && <p className="mt-2 text-[12px] leading-relaxed text-warning" data-routine-no-start="">{t("routines.noStartStreak", { count: routine.noStartStreak })}</p>}
        {routine.weekRuns && <p className="mt-2 text-[12px] text-ink-secondary">{t(routine.weekRuns.total === 1 ? "routines.weekRunsOne" : "routines.weekRuns", routine.weekRuns)}</p>}
        {routine.warning && <p className="mt-2 text-[12px] leading-relaxed text-warning">{routine.warning}</p>}
        {routine.enabled && pausedBots?.includes(routine.botId) && !heldBots?.includes(routine.botId) && <p className="mt-2 text-[12px] leading-relaxed text-warning" data-routine-bot-paused="">
          {t("routines.botPausedRuns")}{" "}
          <button type="button" className="cursor-pointer text-accent hover:underline" onClick={() => showBotLimit("paused")}>{t("routines.botPausedSwap")}</button>
        </p>}
        {routine.enabled && heldBots?.includes(routine.botId) && <p className="mt-2 text-[12px] leading-relaxed text-warning" data-routine-bot-held="">{t("routines.botHeld")}</p>}
        {routine.heldFor && <p className="mt-2 text-[12px] leading-relaxed text-ink-secondary">{routineHeldNote(routine)}</p>}
        {!!routine.skippedRuns && <p className="mt-2 text-[12px] text-ink-secondary">
          {t("routines.skippedRuns", { count: routine.skippedRuns })}
          {routine.lastSkippedAt != null && <> · {t("routines.lastSkipped", { date: routineDateTime(routine.lastSkippedAt) })}</>}
        </p>}
      </article>;
    })}
  </div>;
}