All files / src/components TaskTable.tsx

100% Statements 72/72
100% Branches 25/25
100% Functions 3/3
100% Lines 72/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x     1x 1x 1x 1x                 1x 1x 1x 1x 1x   1x 67x   67x 14x 14x   53x 159x 33x 33x 159x 159x 159x 159x 159x 159x 159x 159x   159x 159x 159x 159x   159x   53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 261x 53x 53x 53x 53x   53x      
import { Link } from "react-router-dom";
import type { Task } from "../api/types";
import type { SortDirection, SortKey } from "../lib/tasks";
import { formatDateTime, formatDuration, formatRelative } from "../lib/format";
import { taskStatusLabel, taskStatusTone } from "../lib/status";
import { StatusBadge } from "./StatusBadge";
import { EmptyState } from "./EmptyState";
 
export interface TaskTableProps {
  tasks: Task[];
  now?: Date;
  sort?: { key: SortKey; dir: SortDirection };
  onSort?: (key: SortKey) => void;
}
 
const COLUMNS: Array<{ key: SortKey; label: string }> = [
  { key: "priority", label: "Приоритет" },
  { key: "created", label: "Создана" },
  { key: "available", label: "Доступна" },
];
 
export function TaskTable({ tasks, now, sort, onSort }: TaskTableProps) {
  const reference = now ?? new Date();
 
  if (tasks.length === 0) {
    return <EmptyState title="Задач нет" hint="Измените фильтры или создайте задачу через API." />;
  }
 
  function header(label: string, key?: SortKey) {
    if (!key || !onSort) {
      return <th scope="col">{label}</th>;
    }
    const active = sort?.key === key;
    const direction = active ? (sort?.dir === "asc" ? "ascending" : "descending") : "none";
    return (
      <th scope="col" aria-sort={direction}>
        <button
          type="button"
          className={active ? "th-sort th-sort--active" : "th-sort"}
          onClick={() => onSort(key)}
        >
          {label}
          <span aria-hidden="true">{active ? (sort?.dir === "asc" ? " ↑" : " ↓") : ""}</span>
        </button>
      </th>
    );
  }
 
  return (
    <div className="table-wrap">
      <table className="table">
        <thead>
          <tr>
            <th scope="col">Тип</th>
            <th scope="col">Статус</th>
            {header("Приоритет", "priority")}
            <th scope="col">Попытки</th>
            <th scope="col">Робот</th>
            {header("Создана", "created")}
            {header("Лимит", "available")}
          </tr>
        </thead>
        <tbody>
          {tasks.map((task) => (
            <tr key={task.id} data-testid="task-row">
              <td>
                <Link to={`/tasks/${task.id}`} className="link">
                  {task.type}
                </Link>
                <small className="mono">{task.id.slice(0, 8)}</small>
              </td>
              <td>
                <StatusBadge tone={taskStatusTone(task.status)}>{taskStatusLabel(task.status)}</StatusBadge>
                {task.last_error ? <small className="error-text">{task.last_error}</small> : null}
              </td>
              <td>{task.priority}</td>
              <td>
                {task.attempts} / {task.max_attempts}
              </td>
              <td className="mono">{task.robot_id ? task.robot_id.slice(0, 8) : "—"}</td>
              <td title={formatDateTime(task.created_at)}>{formatRelative(task.created_at, reference)}</td>
              <td>{formatDuration(task.timeout_seconds)}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}
 
export { COLUMNS };