File size: 3,709 Bytes
89af3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { ReactNode } from "react";
import { safeWorklistLink, worklistText } from "./worklistApi";
import type { WorklistCell, WorklistColumn, WorklistPage } from "./worklistApi";

export function worklistFigure(value: unknown, money = false): string {
  return typeof value === "number" && Number.isFinite(value)
    ? value.toLocaleString(undefined, money ? { minimumFractionDigits: 2, maximumFractionDigits: 2 } : { maximumFractionDigits: 20 })
    : worklistText(value);
}

function Cell({ column, value }: { column: WorklistColumn; value: WorklistCell }) {
  if (value == null || value === "") return null;
  if (column.type === "link") {
    const href = safeWorklistLink(value);
    return href ? <a href={href} target="_blank" rel="noopener noreferrer">Open {worklistText(column.label)}</a> : <>Link unavailable.</>;
  }
  return <>{column.type === "money" || column.type === "number" ? worklistFigure(value, column.type === "money") : worklistText(value)}</>;
}

export default function WorklistTable({ page, details, loading, problem, onShowMore, onRetry }: {
  page: WorklistPage | null; details?: ReactNode; loading: boolean; problem: string; onShowMore: () => void; onRetry: () => void;
}) {
  return <section className="ar-worklist" aria-label="Worklist" aria-busy={loading}>
    {details || page?.validation.length ? <details className="ar-details">
      <summary>Details</summary><div className="ar-details-content">{details}
      {page?.validation.length ? <ul className="ar-validation" aria-label="Odoo validation">
        {page.validation.map((check, index) => <li className={check.ok ? "ar-validation-pass" : "ar-validation-fail"} key={index}>
          <span className="ar-validation-check">{worklistText(check.check)}</span>
          <span className="ar-validation-figures">Worklist: {worklistFigure(check.a)} · Odoo: {worklistFigure(check.b)}</span>
          <span className="ar-validation-status">{check.ok ? "Ties to Odoo" : "Does not tie to Odoo"}</span>
        </li>)}
      </ul> : null}
      </div>
    </details> : null}
    {page ? <>
      {page.rows.length ? <>
        <p className="ar-worklist-count" role="status">{page.rows.length.toLocaleString()} of {page.total.toLocaleString()} rows</p>
        <div className="ar-worklist-scroll" tabIndex={0} role="region" aria-label="Worklist table">
          <table className="ar-worklist-table" aria-label={worklistText(page.title)}>
            <thead><tr>{page.columns.map((column) => <th key={column.key} scope="col"
              className={column.type === "number" || column.type === "money" ? "ar-numeric" : undefined}>{worklistText(column.label)}</th>)}</tr></thead>
            <tbody>{page.rows.map((row, rowIndex) => <tr key={rowIndex}>{page.columns.map((column, index) => <td key={column.key}
              className={column.type === "number" || column.type === "money" ? "ar-numeric" : undefined}>
              <Cell column={column} value={Array.isArray(row) ? row[index] : row[column.key]} />
            </td>)}</tr>)}</tbody>
          </table>
        </div>
      </> : <p className="ar-worklist-empty" role="status">Nothing found.</p>}
    </> : null}
    {loading ? <div className="ar-worklist-loading" role="status" aria-label="Loading worklist"><span /><span /></div> : null}
    {problem ? <p className="ar-worklist-problem" role="status"><span>{worklistText(problem)}</span>{" "}
      <button type="button" className="ar-retry" disabled={loading} onClick={onRetry}>Try again</button></p> : null}
    {page && page.rows.length < page.total ? <button className="ar-button ar-worklist-more" type="button"
      disabled={loading} onClick={onShowMore}>Show more</button> : null}
  </section>;
}