diff options
| author | Zephyruso <[email protected]> | 2023-06-28 12:55:58 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-06-28 12:55:58 +0800 |
| commit | d497b15bedae37abb105d750ef1dfe16f6a7e05d (patch) | |
| tree | a3567f2bf7bf9f228fea42ff996154198d38b311 /src/components/ConnectionTable.tsx | |
| parent | f189d5b14f8c37c8d48a5c54fa52a0125f4d5306 (diff) | |
feat: close connection single or with filter (#64)
Diffstat (limited to 'src/components/ConnectionTable.tsx')
| -rw-r--r-- | src/components/ConnectionTable.tsx | 103 |
1 files changed, 72 insertions, 31 deletions
diff --git a/src/components/ConnectionTable.tsx b/src/components/ConnectionTable.tsx index 4c20598..d5c50a7 100644 --- a/src/components/ConnectionTable.tsx +++ b/src/components/ConnectionTable.tsx @@ -1,32 +1,28 @@ +import './ConnectionTable.scss'; + import cx from 'clsx'; import { formatDistance, Locale } from 'date-fns'; import { enUS, zhCN, zhTW } from 'date-fns/locale'; -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; import { ChevronDown } from 'react-feather'; +import { XCircle } from 'react-feather'; import { useTranslation } from 'react-i18next'; import { useSortBy, useTable } from 'react-table'; +import { State } from '~/store/types'; + +import * as connAPI from '../api/connections'; import prettyBytes from '../misc/pretty-bytes'; +import { getClashAPIConfig } from '../store/app'; import s from './ConnectionTable.module.scss'; - -function renderCell(cell: { column: { id: string }; value: number }, locale: Locale) { - switch (cell.column.id) { - case 'start': - return formatDistance(cell.value, 0, { locale: locale }); - case 'download': - case 'upload': - return prettyBytes(cell.value); - case 'downloadSpeedCurr': - case 'uploadSpeedCurr': - return prettyBytes(cell.value) + '/s'; - default: - return cell.value; - } -} +import MOdalCloseConnection from './ModalCloseAllConnections'; +import { connect } from './StateProvider'; const sortById = { id: 'id', desc: true }; -function Table({ data, columns, hiddenColumns }) { +function Table({ data, columns, hiddenColumns, apiConfig }) { + const [operationId, setOperationId] = useState(''); + const [showModalDisconnect, setShowModalDisconnect] = useState(false); const tableState = { sortBy: [ // maintain a more stable order @@ -61,9 +57,44 @@ function Table({ data, columns, hiddenColumns }) { locale = enUS; } + const disconnectOperation = () => { + connAPI.closeConnById(apiConfig, operationId); + setShowModalDisconnect(false); + }; + + const handlerDisconnect = (id) => { + setOperationId(id); + setShowModalDisconnect(true); + }; + + const renderCell = ( + cell: { column: { id: string }; row: { original: { id: string } }; value: number }, + locale: Locale + ) => { + switch (cell.column.id) { + case 'ctrl': + return ( + <XCircle + style={{ cursor: 'pointer' }} + onClick={() => handlerDisconnect(cell.row.original.id)} + ></XCircle> + ); + case 'start': + return formatDistance(cell.value, 0, { locale: locale }); + case 'download': + case 'upload': + return prettyBytes(cell.value); + case 'downloadSpeedCurr': + case 'uploadSpeedCurr': + return prettyBytes(cell.value) + '/s'; + default: + return cell.value; + } + }; + return ( <div style={{ marginTop: '5px' }}> - <table {...getTableProps()} className={s.table}> + <table {...getTableProps()} className={cx(s.table, 'connections-table')}> <thead> {headerGroups.map((headerGroup, trindex) => { return ( @@ -71,11 +102,16 @@ function Table({ data, columns, hiddenColumns }) { {headerGroup.headers.map((column) => ( <th {...column.getHeaderProps(column.getSortByToggleProps())} className={s.th}> <span>{t(column.render('Header'))}</span> - <span className={s.sortIconContainer}> - {column.isSorted ? ( - <ChevronDown size={16} className={column.isSortedDesc ? '' : s.rotate180} /> - ) : null} - </span> + {column.id !== 'ctrl' ? ( + <span className={s.sortIconContainer}> + {column.isSorted ? ( + <ChevronDown + size={16} + className={column.isSortedDesc ? '' : s.rotate180} + /> + ) : null} + </span> + ) : null} </th> ))} </tr> @@ -87,16 +123,11 @@ function Table({ data, columns, hiddenColumns }) { prepareRow(row); return ( <tr className={s.tr} key={i}> - {row.cells.map((cell, j) => { + {row.cells.map((cell) => { return ( <td {...cell.getCellProps()} - className={cx( - s.td, - i % 2 === 0 ? s.odd : false, - j == 0 || (j >= 5 && j < 10) ? s.center : true - // j ==1 ? s.break : true - )} + className={cx(s.td, i % 2 === 0 ? s.odd : false, cell.column.id)} > {renderCell(cell, locale)} </td> @@ -107,8 +138,18 @@ function Table({ data, columns, hiddenColumns }) { })} </tbody> </table> + <MOdalCloseConnection + confirm={'disconnect'} + isOpen={showModalDisconnect} + onRequestClose={() => setShowModalDisconnect(false)} + primaryButtonOnTap={disconnectOperation} + ></MOdalCloseConnection> </div> ); } -export default Table; +const mapState = (s: State) => ({ + apiConfig: getClashAPIConfig(s), +}); + +export default connect(mapState)(Table); |
