summaryrefslogtreecommitdiff
path: root/src/components/ConnectionTable.js
blob: 3fc0219e0ad730b2b4bb1d6302175755204d184c (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React from 'react';
import { ChevronDown } from 'react-feather';
import prettyBytes from '../misc/pretty-bytes';
import { formatDistance } from 'date-fns';
import cx from 'classnames';
import { useTable, useSortBy } from 'react-table';

import s from './ConnectionTable.module.css';

const columns = [
  // { accessor: 'id', show: false },
  { Header: 'Host', accessor: 'host' },
  { Header: 'Download', accessor: 'download' },
  { Header: 'Upload', accessor: 'upload' },
  { Header: 'Network', accessor: 'network' },
  { Header: 'Type', accessor: 'type' },
  { Header: 'Chains', accessor: 'chains' },
  { Header: 'Rule', accessor: 'rule' },
  { Header: 'Time', accessor: 'start' },
  { Header: 'Source IP', accessor: 'sourceIP' },
  { Header: 'Source Port', accessor: 'sourcePort' },
  { Header: 'Destination IP', accessor: 'destinationIP' }
];

function renderCell(cell, now) {
  switch (cell.column.id) {
    case 'start':
      return formatDistance(-cell.value, now);
    case 'download':
    case 'upload':
      return prettyBytes(cell.value);
    default:
      return cell.value;
  }
}

// const sortById = { id: 'id', desc: true };
// const sortByStart = { id: 'start', desc: true };
// const tableState = {
//   sortBy: [
//     // maintain a more stable order
//     sortById
//   ],
//   hiddenColumns: ['id']
// };

// function tableReducer(newState, action, _prevState) {
//   const { type } = action;
//   if (type === 'toggleSortBy') {
//     const { sortBy = [] } = newState;
//     if (sortBy.length === 0) {
//       return {
//         ...newState,
//         sortBy: [sortById]
//       };
//     }
//   }
//   return newState;
// }

function Table({ data }) {
  const now = new Date();
  const { getTableProps, headerGroups, rows, prepareRow } = useTable(
    {
      columns,
      data
      // initialState: tableState,
      // reducer: tableReducer
    },
    useSortBy
  );
  return (
    <div {...getTableProps()}>
      <div className={s.thead}>
        {headerGroups.map(headerGroup => (
          <div {...headerGroup.getHeaderGroupProps()} className={s.tr}>
            {headerGroup.headers.map(column => (
              <div
                {...column.getHeaderProps(column.getSortByToggleProps())}
                className={s.th}
              >
                <span>{column.render('Header')}</span>
                <span className={s.sortIconContainer}>
                  {column.isSorted ? (
                    <span className={column.isSortedDesc ? '' : s.rotate180}>
                      <ChevronDown size={16} />
                    </span>
                  ) : null}
                </span>
              </div>
            ))}

            {rows.map((row, i) => {
              prepareRow(row);
              return row.cells.map((cell, j) => {
                return (
                  <div
                    {...cell.getCellProps()}
                    className={cx(
                      s.td,
                      i % 2 === 0 ? s.odd : false,
                      j === 1 || j === 2 ? s.du : false
                    )}
                  >
                    {renderCell(cell, now)}
                  </div>
                );
              });
            })}
          </div>
        ))}
      </div>
    </div>
  );
}

export default Table;