File size: 3,492 Bytes
816fa8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from "react";
import { ArrowClockwise } from "@phosphor-icons/react";
import { intelligenceProblem } from "./intelligenceApi";
import type { IntelligenceApi, IntelligenceChangeCount, IntelligenceInsights } from "./intelligenceApi";

/** Sum the server's (person, section) rows into one row per person, in first-seen order. */
function byPerson(rows: IntelligenceChangeCount[]): Array<{ person: string; hand: number; agent: number }> {
  const people = new Map<string, { person: string; hand: number; agent: number }>();
  for (const row of rows) {
    const entry = people.get(row.person) || { person: row.person, hand: 0, agent: 0 };
    entry.hand += row.hand; entry.agent += row.agent;
    people.set(row.person, entry);
  }
  return [...people.values()];
}

export default function Insights({ api }: { api: IntelligenceApi }) {
  const [data, setData] = useState<IntelligenceInsights | null>(null);
  const [problem, setProblem] = useState("");
  const [loading, setLoading] = useState(true);
  const [retry, setRetry] = useState(0);
  useEffect(() => {
    const abort = new AbortController();
    setData(null); setProblem(""); setLoading(true);
    void api.insights(abort.signal).then((value) => {
      if (!abort.signal.aborted) setData(value);
    }).catch((error: unknown) => {
      if (!abort.signal.aborted) setProblem(intelligenceProblem(error, "Insights could not be read. Try again."));
    }).finally(() => { if (!abort.signal.aborted) setLoading(false); });
    return () => abort.abort();
  }, [api, retry]);
  return <section className="intelligence-insights" data-testid="intelligence-insights" aria-label="Insights" aria-busy={loading}>
    <header className="intelligence-history-heading"><p className="intelligence-meta">Last 30 days</p>
      <button type="button" className="intelligence-button intelligence-icon-button" aria-label="Refresh insights" disabled={loading}
        onClick={() => setRetry((value) => value + 1)}><ArrowClockwise size={18} aria-hidden="true" /></button></header>
    {problem ? <div className="intelligence-feedback" role="alert"><span>{problem}</span>
      <button type="button" className="intelligence-button" onClick={() => setRetry((value) => value + 1)}>Retry</button></div> : null}
    {loading ? <div className="intelligence-skeleton" role="status" aria-label="Loading insights"><span /><span /></div> : null}
    {data ? <>
      <dl className="intelligence-counts">
        <div><dt>By hand</dt><dd data-testid="intelligence-hand-count">{data.totals.hand.toLocaleString()}</dd></div>
        <div><dt>By agent</dt><dd data-testid="intelligence-agent-count">{data.totals.agent.toLocaleString()}</dd></div>
      </dl>
      {data.activity !== "included" ? <p className="intelligence-meta">{data.activity === "absent" ? "File changes only." : "Other activity unavailable."}</p> : null}
      {data.rows.length ? <div className="intelligence-insights-table" tabIndex={0} role="region" aria-label="Changes by person">
        <table><thead><tr><th scope="col">Person</th><th scope="col">By hand</th><th scope="col">By agent</th></tr></thead>
          <tbody>{byPerson(data.rows).map((row) => <tr key={row.person}>
            <th scope="row">{row.person}</th>
            <td>{row.hand.toLocaleString()}</td><td>{row.agent.toLocaleString()}</td>
          </tr>)}</tbody></table>
      </div> : <p className="intelligence-empty">No changes in the last 30 days.</p>}
    </> : null}
  </section>;
}