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 = [ { 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; } } const tableState = { sortBy: [ // maintain a more stable order { id: 'start', desc: true } ] }; function Table({ data }) { const now = new Date(); const { getTableProps, // getTableBodyProps, headerGroups, rows, prepareRow } = useTable( { columns, data, initialState: tableState }, useSortBy ); return (
{headerGroups.map(headerGroup => (
{headerGroup.headers.map(column => (
{column.render('Header')} {column.isSorted ? ( ) : null}
))} {rows.map((row, i) => { prepareRow(row); return row.cells.map((cell, j) => { return (
{renderCell(cell, now)}
); }); })}
))}
); } export default Table;