All files / src/pages RobotsPage.tsx

100% Statements 46/46
100% Branches 13/13
100% Functions 1/1
100% Lines 46/46

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 571x 1x 1x 1x 1x 1x 1x 1x 1x   1x 8x 8x 8x 8x 8x   8x 8x 8x 1x   7x 7x 7x 7x 7x 7x 7x     7x 7x   7x 7x 5x   2x 2x 3x 3x 3x 3x 3x 3x 3x 2x 2x   7x 7x   8x   8x  
import { useMemo } from "react";
import { useApi } from "../hooks/useApi";
import { useResource } from "../hooks/useResource";
import { ErrorBanner } from "../components/ErrorBanner";
import { Panel } from "../components/KpiCards";
import { RobotGrid } from "../components/RobotGrid";
import { robotAvailability, staleRobots } from "../lib/health";
import { formatPercent } from "../lib/format";
import { robotStatusLabel } from "../lib/status";
 
export function RobotsPage() {
  const api = useApi();
  const robots = useResource(() => api.getRobots(), [], 5000);
  const items = useMemo(() => robots.data ?? [], [robots.data]);
  const fleet = useMemo(() => robotAvailability(items), [items]);
  const stale = useMemo(() => staleRobots(items), [items]);
 
  return (
    <div className="stack">
      {robots.error ? (
        <ErrorBanner message={robots.error} onRetry={robots.reload} />
      ) : (
        <>
          <Panel
            title="Парк роботов"
            actions={
              <span className="muted">
                {fleet.online} из {fleet.total} на связи · {formatPercent(fleet.online, Math.max(1, fleet.total))}
              </span>
            }
          >
            <RobotGrid robots={items} />
          </Panel>
 
          <Panel title="Требуют внимания" actions={<span className="muted">{stale.length}</span>}>
            {stale.length === 0 ? (
              <p className="muted">Все роботы выходили на связь недавно</p>
            ) : (
              <ul className="list">
                {stale.map((robot) => (
                  <li key={robot.id} className="list__row">
                    <strong>{robot.name}</strong>
                    <span className="muted">
                      {robotStatusLabel(robot.status)} · последний heartbeat{" "}
                      {robot.last_seen_at ? robot.last_seen_at : "нет"}
                    </span>
                  </li>
                ))}
              </ul>
            )}
          </Panel>
        </>
      )}
    </div>
  );
}