summaryrefslogtreecommitdiff
path: root/docs/src/components/codes/Table.jsx
blob: c7dd20c8c62b57add0dde1e129163fe3093dae43 (plain)
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
/*
 * Copyright (c) 2020 The ZMK Contributors
 *
 * SPDX-License-Identifier: CC-BY-NC-SA-4.0
 */

import React from "react";
import PropTypes from "prop-types";
import TableRow from "./TableRow";
import Footnotes from "./Footnotes";
import LinkIcon from "./LinkIcon";
import operatingSystems from "@site/src/data/operating-systems";
import { getCodes } from "@site/src/hid";
import { getGroup } from "@site/src/groups";
import { getFootnote } from "@site/src/footnotes";

function extractFootnoteIds(codes) {
  return Array.from(
    new Set(
      codes
        .flatMap(({ footnotes }) => Object.values(footnotes))
        .map((refs) => (Array.isArray(refs) ? refs.flat() : refs))
    )
  );
}

export default function Table({ group }) {
  const codes = getCodes(getGroup(group));

  const footnotesAnchor = group + "-" + "footnotes";

  const tableFootnotes = extractFootnoteIds(codes).map((id, i) => {
    const Component = getFootnote(id);
    return {
      id,
      anchor: footnotesAnchor,
      symbol: i + 1,
      value: Component ? <Component /> : undefined,
    };
  });

  return (
    <div className="codes">
      <table>
        <thead>
          <tr>
            <th className="names">Names</th>
            <th className="description">Description</th>
            <th className="documentation" title="Documentation">
              <LinkIcon />
            </th>
            {operatingSystems.map(({ key, className, heading, title }) => (
              <th key={key} className={`os ${className}`} title={title}>
                {heading}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {Array.isArray(codes)
            ? codes.map((code) => (
                <TableRow
                  key={code.names[0]}
                  {...code}
                  tableFootnotes={tableFootnotes}
                />
              ))
            : undefined}
        </tbody>
      </table>
      {tableFootnotes.length > 0 ? (
        <Footnotes id={footnotesAnchor} footnotes={tableFootnotes} />
      ) : undefined}
    </div>
  );
}

Table.propTypes = {
  group: PropTypes.string.isRequired,
};