Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

8.9 KB
Raw Blame
import {
  Ban,
  Bot,
  History,
  Lock,
  MessageSquareText,
  MoreHorizontal,
  Pause,
  Pencil,
  Play,
  RotateCw,
  Trash2,
  Zap,
} from 'lucide-react';
import type { AgentSchedule } from '@/lib/api/endpoints/agentSchedules';
import { Button } from '@/components/ui/button';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { TableCell, TableRow } from '@/components/ui/table';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { cn } from '@/lib/utils';
import { PRO_URL } from '@/utils/app';
import SettingsIconButton from '../SettingsIconButton';
import { useSettingsCan } from '../../context/settingsPermission';
import { formatUtc } from '../../utils/cronSchedule';
import { SettingsScheduleWhen } from './SettingsScheduleWhen';
import { useTranslations } from 'next-intl';

export function SettingsScheduleRow({
  schedule,
  columnName,
  running,
  onToggle,
  onRun,
  onCancelPending,
  onHistory,
  onEdit,
  onDelete,
}: {
  schedule: AgentSchedule;
  columnName: string | null;
  running: boolean;
  onToggle: () => void;
  onRun: () => void;
  onCancelPending: () => void;
  onHistory: () => void;
  onEdit: () => void;
  onDelete: () => void;
}) {
  const t = useTranslations('settings.schedules');
  const can = useSettingsCan();
  // An unsupported schedule shows what it was set up to do and can be deleted, nothing more.
  const canEdit = can('edit') && schedule.supported;
  return (
    <TableRow className="group/item cursor-pointer" onClick={onHistory}>
      <TableCell className="px-3 py-4 align-top whitespace-normal">
        <div className="flex min-w-0 items-start gap-2.5">
          <span
            className={cn(
              'mt-[7px] size-2 shrink-0 rounded-full',
              statusDotClass(schedule.supported ? schedule.status : 'paused'),
            )}
            title={t(statusTitleKey(schedule))}
          />
          {schedule.prompt && (
            <Popover>
              <Tooltip>
                <TooltipTrigger asChild>
                  <PopoverTrigger asChild>
                    <Button
                      type="button"
                      variant="ghost"
                      size="icon"
                      className="size-7 shrink-0 text-muted-foreground hover:text-foreground"
                      onClick={(e) => e.stopPropagation()}
                    >
                      <MessageSquareText className="size-4" />
                    </Button>
                  </PopoverTrigger>
                </TooltipTrigger>
                <TooltipContent>{t('viewTask')}</TooltipContent>
              </Tooltip>
              <PopoverContent align="start" className="w-80">
                <p className="text-xs font-medium text-muted-foreground">{t('task')}</p>
                <p className="mt-1.5 text-sm whitespace-pre-wrap">{schedule.prompt}</p>
              </PopoverContent>
            </Popover>
          )}
          <div className="flex min-w-0 flex-col gap-0.5 pt-1">
            <span className="truncate text-sm font-medium">{schedule.name}</span>
            <span className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
              <Bot className="size-3.5 shrink-0" />
              <span className="truncate">{schedule.agentName}</span>
            </span>
          </div>
        </div>
      </TableCell>
      <TableCell className="px-3 py-4 align-top whitespace-normal">
        <SettingsScheduleWhen schedule={schedule} columnName={columnName} />
        {!schedule.supported && (
          <a
            href={PRO_URL}
            target="_blank"
            rel="noreferrer"
            title={t('unavailableHint')}
            className="mt-1.5 inline-flex items-center gap-1 text-xs font-medium text-amber-600 hover:underline dark:text-amber-500"
            onClick={(e) => e.stopPropagation()}
          >
            <Lock className="size-3.5" />
            {t('unavailable')}
          </a>
        )}
      </TableCell>
      <TableCell className="px-3 py-4 align-top text-sm whitespace-nowrap tabular-nums">
        <NextRun schedule={schedule} />
      </TableCell>
      <TableCell className="px-3 py-4 align-top whitespace-normal">
        {schedule.lastRunStatus ? (
          <div className="space-y-1.5">
            <div className="flex items-center gap-2">
              <span
                className={cn('size-2 shrink-0 rounded-full', runDotClass(schedule.lastRunStatus))}
              />
              <span className="text-sm">{t(`runStatus.${schedule.lastRunStatus}`)}</span>
            </div>
            {schedule.lastRunAt && (
              <p className="text-xs whitespace-nowrap text-muted-foreground tabular-nums">
                {formatUtc(schedule.lastRunAt)}
              </p>
            )}
          </div>
        ) : (
          <span className="text-xs text-muted-foreground">{t('noRunsShort')}</span>
        )}
      </TableCell>
      <TableCell className="px-3 py-3 align-top">
        <div className="flex items-center justify-end gap-1" onClick={(e) => e.stopPropagation()}>
          {canEdit && (
            <SettingsIconButton
              title={schedule.status === 'active' ? t('pause') : t('resume')}
              onClick={onToggle}
            >
              {schedule.status === 'active' ? (
                <Pause className="size-4" />
              ) : (
                <Play className="size-4" />
              )}
            </SettingsIconButton>
          )}
          <SettingsIconButton title={t('runHistory')} onClick={onHistory}>
            <History className="size-4" />
          </SettingsIconButton>
          {(can('edit') || can('delete')) && (
            <DropdownMenu>
              <Tooltip>
                <TooltipTrigger asChild>
                  <DropdownMenuTrigger asChild>
                    <Button
                      variant="ghost"
                      size="icon"
                      className="size-11 text-muted-foreground hover:text-foreground sm:size-8"
                    >
                      <MoreHorizontal className="size-4" />
                    </Button>
                  </DropdownMenuTrigger>
                </TooltipTrigger>
                <TooltipContent>{t('moreActions')}</TooltipContent>
              </Tooltip>
              <DropdownMenuContent align="end">
                {canEdit && schedule.type === 'cron' && (
                  <DropdownMenuItem
                    className="min-h-11 sm:min-h-8"
                    disabled={running}
                    onSelect={onRun}
                  >
                    {running ? <RotateCw className="animate-spin" /> : <Zap />}
                    {t('runNow')}
                  </DropdownMenuItem>
                )}
                {can('edit') && schedule.canTrigger && schedule.pendingRuns > 0 && (
                  <DropdownMenuItem className="min-h-11 sm:min-h-8" onSelect={onCancelPending}>
                    <Ban />
                    {t('cancelPending', { count: schedule.pendingRuns })}
                  </DropdownMenuItem>
                )}
                {canEdit && (
                  <DropdownMenuItem className="min-h-11 sm:min-h-8" onSelect={onEdit}>
                    <Pencil />
                    {t('edit')}
                  </DropdownMenuItem>
                )}
                {can('delete') && canEdit && <DropdownMenuSeparator />}
                {can('delete') && (
                  <DropdownMenuItem
                    className="min-h-11 sm:min-h-8"
                    variant="destructive"
                    onSelect={onDelete}
                  >
                    <Trash2 />
                    {t('delete')}
                  </DropdownMenuItem>
                )}
              </DropdownMenuContent>
            </DropdownMenu>
          )}
        </div>
      </TableCell>
    </TableRow>
  );
}

function NextRun({ schedule }: { schedule: AgentSchedule }) {
  const t = useTranslations('settings.schedules');
  if (!schedule.supported) return <span className="text-xs text-muted-foreground">—</span>;
  if (schedule.nextRunAt) return formatUtc(schedule.nextRunAt);
  return (
    <span className="text-xs whitespace-normal text-muted-foreground">
      {schedule.type === 'status' ? t('nextRunOnStatus') : t('nextRunOnTrigger')}
    </span>
  );
}

function statusTitleKey(schedule: AgentSchedule) {
  if (!schedule.supported) return 'unavailableHint';
  return schedule.status === 'active' ? 'statusActive' : 'statusPaused';
}

function statusDotClass(status: AgentSchedule['status']): string {
  return status === 'active' ? 'bg-emerald-500' : 'bg-muted-foreground/40';
}

function runDotClass(status: NonNullable<AgentSchedule['lastRunStatus']>): string {
  if (status === 'success') return 'bg-emerald-500';
  if (status === 'failed') return 'bg-red-500';
  if (status === 'canceled') return 'bg-muted-foreground/40';
  return 'bg-amber-500';
}