Skip to content

Your lightweight open-source personal agent.

15.4 KB
Raw Blame
import { CircleAlert, Loader2, X } from "lucide-react";
import { useId, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";

import type { AutomationRunRecord } from "@/components/settings/system/AutomationRunDialogView";
import { SkeletonStatus } from "@/components/settings/shared/SkeletonStatus";
import { Button } from "@/components/ui/button";
import { formControlFocusClassName } from "@/components/ui/form-control";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import type { SessionAutomationJob } from "@/lib/types";
import { cn } from "@/lib/utils";

type CalendarEntry = {
  id: string;
  job: SessionAutomationJob;
  startMs: number;
  kind: "planned" | "recorded";
  run: AutomationRunRecord | null;
};

type CalendarCopy = {
  planned: string;
  recorded: string;
  running: string;
  failed: string;
  more: (count: number) => string;
  close: string;
  noEntries: string;
  completed: string;
  skipped: string;
  system: string;
};

export interface AutomationCalendarRunProps {
  job: SessionAutomationJob;
  run: AutomationRunRecord;
  locale: string;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onCloseAutoFocus: (event: Event) => void;
}

interface AutomationCalendarProps {
  jobs: SessionAutomationJob[];
  loadingLabel?: string;
  month: Date;
  locale: string;
  copy: CalendarCopy;
  onInspect: (job: SessionAutomationJob, trigger: HTMLElement) => void;
  renderRunDialog: (props: AutomationCalendarRunProps) => ReactNode;
}

function startOfDay(date: Date): Date {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

function addDays(date: Date, amount: number): Date {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount);
}

function mondayIndex(date: Date): number {
  return (date.getDay() + 6) % 7;
}

function dateKey(value: Date | number): string {
  const date = typeof value === "number" ? new Date(value) : value;
  return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
}

function entriesForJobs(jobs: SessionAutomationJob[]): CalendarEntry[] {
  return jobs.flatMap((job) => {
    const history = job.state.run_history ?? [];
    const nextRun = job.enabled ? job.state.next_run_at_ms : null;
    const recordedRuns = history.length || job.state.last_run_at_ms == null || nextRun != null
      ? history
      : [{
          run_at_ms: job.state.last_run_at_ms,
          status: job.state.last_status ?? "unknown",
          error: job.state.last_error,
        }];
    const recorded = recordedRuns.map((run, index) => ({
      id: `${job.id}:recorded:${run.run_at_ms}:${index}`,
      job,
      startMs: run.run_at_ms,
      kind: "recorded" as const,
      run,
    }));
    return nextRun == null ? recorded : [
      ...recorded,
      {
        id: `${job.id}:planned:${nextRun}`,
        job,
        startMs: nextRun,
        kind: "planned" as const,
        run: null,
      },
    ];
  });
}

function entryTime(entry: CalendarEntry, locale: string): string {
  const formatter = new Intl.DateTimeFormat(locale, { hour: "2-digit", minute: "2-digit" });
  return formatter.format(entry.startMs);
}

function isSameDay(left: Date, right: Date): boolean {
  return dateKey(left) === dateKey(right);
}

function CalendarEntryRow({ entry, locale, copy, onInspect, compact = false }: {
  entry: CalendarEntry;
  locale: string;
  copy: CalendarCopy;
  onInspect: (entry: CalendarEntry, trigger: HTMLElement) => void;
  compact?: boolean;
}) {
  const name = entry.job.name || entry.job.id;
  const running = Boolean(entry.job.state.pending) && entry.kind === "planned";
  const failed = entry.run?.status === "error";
  const completed = entry.run?.status === "ok";
  const status = running
    ? copy.running
    : failed
      ? copy.failed
      : completed
        ? copy.completed
        : entry.run?.status === "skipped"
          ? copy.skipped
          : entry.kind === "planned"
            ? copy.planned
            : copy.recorded;
  const content = (
    <>
      <span className="min-w-0 flex-1">
        <span className={cn("block truncate", entry.kind === "recorded" && !failed ? "font-normal text-muted-foreground" : "font-medium text-foreground")}>{name}</span>
        <span className={cn("mt-0.5 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-[10px] leading-4", failed ? "text-destructive" : "text-muted-foreground")}>
          {running ? <Loader2 className="h-3 w-3 animate-spin motion-reduce:animate-none" aria-hidden /> : null}
          {failed ? <CircleAlert className="h-3 w-3" aria-hidden /> : null}
          <span className="tabular-nums">{entryTime(entry, locale)}</span>
          {compact || entry.kind === "recorded" ? <span>{status}</span> : null}
          {entry.job.protected ? <span>{copy.system}</span> : null}
        </span>
      </span>
    </>
  );
  const actionClass = cn(
    "flex w-full min-w-0 items-start gap-2 rounded-compact px-2 py-1.5 text-left text-[11px] leading-4",
    "transition-colors duration-150 hover:bg-foreground/[0.055] motion-reduce:transition-none",
    formControlFocusClassName,
    compact && "py-2 text-[12px]",
    failed && "bg-destructive/[0.045]",
  );

  return (
    <button
      type="button"
      className={actionClass}
      aria-label={`${name}, ${entryTime(entry, locale)}, ${status}${entry.job.protected ? `, ${copy.system}` : ""}`}
      aria-haspopup="dialog"
      onClick={(event) => onInspect(entry, event.currentTarget)}
    >
      {content}
    </button>
  );
}

function CalendarOverflowEntries({ entries, dayLabel, locale, copy, onInspect }: {
  entries: CalendarEntry[];
  dayLabel: string;
  locale: string;
  copy: CalendarCopy;
  onInspect: (entry: CalendarEntry, trigger: HTMLElement) => void;
}) {
  const [open, setOpen] = useState(false);
  const titleId = useId();
  const triggerRef = useRef<HTMLButtonElement | null>(null);
  const pendingSelection = useRef<CalendarEntry | null>(null);

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <button
          ref={triggerRef}
          type="button"
          className={cn(
            "min-h-6 w-full rounded-compact px-2 py-1 text-left text-[10px] text-muted-foreground transition-colors duration-150 hover:bg-foreground/[0.055] hover:text-foreground motion-reduce:transition-none",
            formControlFocusClassName,
          )}
        >
          {copy.more(entries.length - 3)}
        </button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        side="right"
        collisionPadding={16}
        aria-labelledby={titleId}
        className="w-80 max-w-[calc(100vw-2rem)] overscroll-contain p-2"
        onCloseAutoFocus={(event) => {
          const entry = pendingSelection.current;
          pendingSelection.current = null;
          if (!entry || !triggerRef.current?.isConnected) return;
          // Hand off after the day popover exits, retaining a mounted return target.
          event.preventDefault();
          onInspect(entry, triggerRef.current);
        }}
      >
        <div className="mb-1 flex items-center gap-2 px-2 py-1">
          <h3 id={titleId} className="min-w-0 flex-1 text-[13px] font-semibold">{dayLabel}</h3>
          <Button type="button" variant="ghost" size="icon" className="h-8 w-8 shrink-0" aria-label={copy.close} onClick={() => setOpen(false)}>
            <X className="h-4 w-4" aria-hidden />
          </Button>
        </div>
        {entries.map((entry) => (
          <CalendarEntryRow
            key={entry.id}
            entry={entry}
            locale={locale}
            copy={copy}
            onInspect={(entry) => {
              pendingSelection.current = entry;
              setOpen(false);
            }}
            compact
          />
        ))}
      </PopoverContent>
    </Popover>
  );
}

export function AutomationCalendar({ jobs, loadingLabel, month, locale, copy, onInspect, renderRunDialog }: AutomationCalendarProps) {
  const calendarRef = useRef<HTMLElement | null>(null);
  const runTrigger = useRef<HTMLElement | null>(null);
  const [selectedRun, setSelectedRun] = useState<{ job: SessionAutomationJob; run: AutomationRunRecord } | null>(null);
  const [runOpen, setRunOpen] = useState(false);
  const inspectEntry = (entry: CalendarEntry, trigger: HTMLElement) => {
    if (!entry.run) {
      onInspect(entry.job, trigger);
      return;
    }
    runTrigger.current = trigger;
    setSelectedRun({ job: entry.job, run: entry.run });
    setRunOpen(true);
  };
  const [wideCalendar, setWideCalendar] = useState(true);
  const today = startOfDay(new Date());
  const gridStart = addDays(month, -mondayIndex(month));
  const daysInMonth = new Date(month.getFullYear(), month.getMonth() + 1, 0).getDate();
  const dayCount = mondayIndex(month) + daysInMonth > 35 ? 42 : 35;
  const days = Array.from({ length: dayCount }, (_, index) => addDays(gridStart, index));
  const gridEndMs = addDays(gridStart, dayCount).getTime();
  const allEntries = useMemo(() => entriesForJobs(jobs).sort((left, right) => left.startMs - right.startMs), [jobs]);
  const visibleEntries = allEntries.filter((entry) => entry.startMs >= gridStart.getTime() && entry.startMs < gridEndMs);
  const entriesByDay = new Map<string, CalendarEntry[]>();
  visibleEntries.forEach((entry) => {
    const key = dateKey(entry.startMs);
    entriesByDay.set(key, [...(entriesByDay.get(key) ?? []), entry]);
  });
  const monthLabel = new Intl.DateTimeFormat(locale, { month: "long", year: "numeric" }).format(month);
  const weekdayFormatter = new Intl.DateTimeFormat(locale, { weekday: "short" });
  const dayFormatter = new Intl.DateTimeFormat(locale, { day: "numeric" });
  const agendaDateFormatter = new Intl.DateTimeFormat(locale, { month: "short", day: "numeric", weekday: "short" });
  const weekdayLabels = Array.from({ length: 7 }, (_, index) => weekdayFormatter.format(addDays(new Date(2026, 0, 5), index)));

  useLayoutEffect(() => {
    const calendar = calendarRef.current;
    if (!calendar) return;
    const update = (width: number) => {
      if (width > 0) setWideCalendar(width >= 704);
    };
    update(calendar.getBoundingClientRect().width);
    if (typeof ResizeObserver === "undefined") return;
    const observer = new ResizeObserver(([entry]) => update(entry?.contentRect.width ?? 0));
    observer.observe(calendar);
    return () => observer.disconnect();
  }, []);

  const content = <>
      <div className="automation-calendar-header bg-foreground/[0.025]">
        {wideCalendar ? <div className="automation-calendar-weekdays" aria-hidden>
          {weekdayLabels.map((label) => <div key={label}>{label}</div>)}
        </div> : null}
      </div>

      {wideCalendar ? <div className="automation-calendar-grid" aria-label={monthLabel}>
        <div className="automation-calendar-month">
          {days.map((day) => {
            const entries = entriesByDay.get(dateKey(day)) ?? [];
            const currentMonth = day.getMonth() === month.getMonth();
            const currentDay = isSameDay(day, today);
            const dayLabel = new Intl.DateTimeFormat(locale, { dateStyle: "full" }).format(day);
            return (
              <div
                key={dateKey(day)}
                className={cn("automation-calendar-day", !currentMonth && "bg-muted/15")}
                aria-label={dayLabel}
              >
                <div className="flex h-8 items-center justify-end px-2 pt-1">
                  <time
                    dateTime={`${day.getFullYear()}-${String(day.getMonth() + 1).padStart(2, "0")}-${String(day.getDate()).padStart(2, "0")}`}
                    aria-current={currentDay ? "date" : undefined}
                    className={cn(
                      "inline-flex h-6 min-w-6 items-center justify-center rounded-full px-1 text-[11px] tabular-nums",
                      currentDay ? "bg-foreground font-medium text-background" : currentMonth ? "text-foreground" : "text-muted-foreground/55",
                    )}
                  >
                    {dayFormatter.format(day)}
                  </time>
                </div>
                <div className="space-y-0.5 px-1 pb-1">
                  {loadingLabel ? <div className="flex h-7 items-center px-2"><div className="h-2.5 w-3/4 rounded bg-muted-foreground/15" /></div> : null}
                  {entries.slice(0, 3).map((entry) => (
                    <CalendarEntryRow
                      key={entry.id}
                      entry={entry}
                      locale={locale}
                      copy={copy}
                      onInspect={inspectEntry}
                    />
                  ))}
                  {entries.length > 3 ? (
                    <CalendarOverflowEntries
                      entries={entries}
                      dayLabel={dayLabel}
                      locale={locale}
                      copy={copy}
                      onInspect={inspectEntry}
                    />
                  ) : null}
                </div>
              </div>
            );
          })}
        </div>
      </div> : null}

      {!wideCalendar ? <div className="automation-calendar-agenda px-3 py-2 sm:px-4">
        {loadingLabel ? <div>
          {[0, 1, 2].map((index) => <div key={index} data-agenda-skeleton-row className="grid grid-cols-[5.25rem_minmax(0,1fr)] gap-2 py-2.5">
            <div className="pt-2"><div className="mt-1 h-2.5 w-16 rounded bg-muted-foreground/15" /></div>
            <div className="px-2 py-2">
              <div className="flex h-4 items-center"><div className="h-3 w-2/3 rounded bg-muted-foreground/20" /></div>
              <div className="mt-1 flex h-4 items-center"><div className="h-2.5 w-1/2 rounded bg-muted-foreground/15" /></div>
            </div>
          </div>)}
        </div> : visibleEntries.length ? (
          <ol>
            {days.filter((day) => entriesByDay.has(dateKey(day))).map((day) => (
              <li key={dateKey(day)} className="grid grid-cols-[5.25rem_minmax(0,1fr)] gap-2 py-2.5">
                <time className={cn("pt-2 text-[11px] leading-4 text-muted-foreground", isSameDay(day, today) && "font-semibold text-foreground")}>{agendaDateFormatter.format(day)}</time>
                <div className="min-w-0 space-y-1">
                  {(entriesByDay.get(dateKey(day)) ?? []).map((entry) => (
                    <CalendarEntryRow
                      key={entry.id}
                      entry={entry}
                      locale={locale}
                      copy={copy}
                      onInspect={inspectEntry}
                      compact
                    />
                  ))}
                </div>
              </li>
            ))}
          </ol>
        ) : (
          <div className="flex min-h-28 items-center justify-center text-[13px] text-muted-foreground">{copy.noEntries}</div>
        )}
      </div> : null}
    </>;

  return (
    <section ref={calendarRef} tabIndex={-1} className="automation-calendar overflow-hidden bg-[hsl(var(--settings-surface))]">
      {loadingLabel ? <SkeletonStatus label={loadingLabel} className="">{content}</SkeletonStatus> : content}
      {selectedRun ? renderRunDialog({
        job: selectedRun.job, run: selectedRun.run, locale, open: runOpen,
        onOpenChange: setRunOpen,
        onCloseAutoFocus: (event) => {
          event.preventDefault();
          if (runOpen) return;
          setSelectedRun(null);
          const target = runTrigger.current?.isConnected ? runTrigger.current : calendarRef.current;
          target?.focus({ preventScroll: true });
        },
      }) : null}
    </section>
  );
}