Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.2 KB
Raw Blame
import { useTranslation } from "react-i18next";
import { Icon } from "@editor/ui/Icon";
import { column, DataTable, type DataTableColumn } from "@editor/ui";
import { type Extraction, type ReviewDocument } from "@processor/api/documents";
import {
  confidencePct,
  confidenceTone,
} from "@processor/components/documents/format";

interface DocumentExtractionsProps {
  doc: ReviewDocument;
  /**
   * Whether sensitive content may be shown. A sensitive doc renders its fields
   * only once a timed elevation is active; the gate UI itself lives in the
   * drawer so it can sit above all sub-tabs.
   */
  unlocked: boolean;
}

/** Per-field extraction table, gated behind elevation for sensitive docs. */
export function DocumentExtractions({
  doc,
  unlocked,
}: DocumentExtractionsProps) {
  const { t } = useTranslation();

  const cols: DataTableColumn<Extraction>[] = [
    column.text({
      key: "field",
      header: t("portal.documents.extractions.columns.field"),
      sortable: true,
      get: (e) => e.field,
    }),
    column.mono({
      key: "value",
      header: t("portal.documents.extractions.columns.value"),
      privateContent: true,
      get: (e) => e.value,
    }),
    column.badge({
      key: "confidence",
      header: t("portal.documents.extractions.columns.confidence"),
      sortable: true,
      // Sort on the whole-percent integer, not the "92%" label (keeps decimals
      // out of the natural-sort comparator).
      sortBy: (e) => Math.round(e.confidence * 100),
      get: (e) => ({
        tone: confidenceTone(e.confidence),
        label: confidencePct(e.confidence),
      }),
    }),
  ];

  if (doc.sensitive && !unlocked) {
    return (
      <div className="portal-documents__masked">
        <span className="portal-documents__masked-icon" aria-hidden>
          <Icon name="lock" size={"1.5rem"} />
        </span>
        <p className="portal-documents__masked-text">
          {t("portal.documents.extractions.masked")}
        </p>
      </div>
    );
  }

  return (
    <DataTable<Extraction>
      columns={cols}
      rows={doc.extractions}
      rowKey={(e) => e.field}
      empty={t("portal.documents.extractions.empty")}
    />
  );
}