summaryrefslogtreecommitdiff
path: root/src/components/ConnectionTable.js
diff options
context:
space:
mode:
authorHaishan <[email protected]>2019-11-08 00:40:48 +0800
committerHaishan <[email protected]>2019-11-09 13:21:25 +0800
commit6754620d7abfb7273cff5f173349eb507fc0c8b7 (patch)
treed91136e4863b4c41ff521bf182dd8cc08bb355bc /src/components/ConnectionTable.js
parent130793446476e47b2e5f0457482dfbfdf1944b48 (diff)
feat: connections inspection
Diffstat (limited to 'src/components/ConnectionTable.js')
-rw-r--r--src/components/ConnectionTable.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/components/ConnectionTable.js b/src/components/ConnectionTable.js
new file mode 100644
index 0000000..547925c
--- /dev/null
+++ b/src/components/ConnectionTable.js
@@ -0,0 +1,99 @@
+import React from 'react';
+import { ArrowUp, ArrowDown } 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 = [
+ { 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: 'Designation IP', accessor: 'destinationIP' },
+ { Header: 'Designation Port', accessor: 'destinationPort' }
+];
+
+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;
+ }
+}
+
+function Table({ data }) {
+ const now = new Date();
+ const {
+ getTableProps,
+ // getTableBodyProps,
+ headerGroups,
+ rows,
+ prepareRow
+ } = useTable(
+ {
+ columns,
+ data
+ },
+ 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>
+ {column.isSorted ? (
+ column.isSortedDesc ? (
+ <ArrowDown size={16} />
+ ) : (
+ <ArrowUp size={16} />
+ )
+ ) : 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;