mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-20 19:12:09 +08:00
384 lines
11 KiB
TypeScript
384 lines
11 KiB
TypeScript
import {
|
|
DeleteForeverRounded,
|
|
TableChartRounded,
|
|
TableRowsRounded,
|
|
ViewColumnRounded,
|
|
} from '@mui/icons-material'
|
|
import {
|
|
Box,
|
|
Button,
|
|
ButtonGroup,
|
|
Fab,
|
|
IconButton,
|
|
MenuItem,
|
|
Tooltip,
|
|
Zoom,
|
|
} from '@mui/material'
|
|
import { useLockFn } from 'ahooks'
|
|
import { useCallback, useMemo, useRef, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { closeAllConnections } from 'tauri-plugin-mihomo-api'
|
|
|
|
import {
|
|
BaseEmpty,
|
|
BasePage,
|
|
BaseSearchBox,
|
|
BaseStyledSelect,
|
|
VirtualList,
|
|
} from '@/components/base'
|
|
import {
|
|
ConnectionDetail,
|
|
ConnectionDetailRef,
|
|
} from '@/components/connection/connection-detail'
|
|
import { ConnectionRowItem } from '@/components/connection/connection-row-item'
|
|
import {
|
|
getConnectionStartTime,
|
|
useConnectionRowViews,
|
|
} from '@/components/connection/connection-row-view'
|
|
import type { ConnectionRuleKind } from '@/components/connection/connection-rule'
|
|
import { ConnectionTable } from '@/components/connection/connection-table'
|
|
import { useConnectionData } from '@/hooks/use-connection-data'
|
|
import { useConnectionSetting } from '@/hooks/use-connection-setting'
|
|
import { usePageSearch } from '@/hooks/use-page-search'
|
|
import { useTrafficData } from '@/hooks/use-traffic-data'
|
|
import { useVisibility } from '@/hooks/use-visibility'
|
|
import { mutate } from '@/services/mutate'
|
|
import parseTraffic from '@/utils/parse-traffic'
|
|
|
|
type OrderFunc = (list: IConnectionsItem[]) => IConnectionsItem[]
|
|
|
|
const ORDER_OPTIONS = [
|
|
{
|
|
id: 'default',
|
|
labelKey: 'connections.components.order.default',
|
|
fn: (list: IConnectionsItem[]) =>
|
|
list.sort(
|
|
(a, b) => getConnectionStartTime(b) - getConnectionStartTime(a),
|
|
),
|
|
},
|
|
{
|
|
id: 'uploadSpeed',
|
|
labelKey: 'connections.components.order.uploadSpeed',
|
|
fn: (list: IConnectionsItem[]) =>
|
|
list.sort((a, b) => (b.curUpload ?? 0) - (a.curUpload ?? 0)),
|
|
},
|
|
{
|
|
id: 'downloadSpeed',
|
|
labelKey: 'connections.components.order.downloadSpeed',
|
|
fn: (list: IConnectionsItem[]) =>
|
|
list.sort((a, b) => (b.curDownload ?? 0) - (a.curDownload ?? 0)),
|
|
},
|
|
] as const
|
|
|
|
type OrderKey = (typeof ORDER_OPTIONS)[number]['id']
|
|
|
|
type NetworkFilter = 'all' | 'tcp' | 'udp'
|
|
|
|
let savedNetworkFilter: NetworkFilter = 'all'
|
|
|
|
const orderFunctionMap = ORDER_OPTIONS.reduce<Record<OrderKey, OrderFunc>>(
|
|
(acc, option) => {
|
|
acc[option.id] = option.fn
|
|
return acc
|
|
},
|
|
{} as Record<OrderKey, OrderFunc>,
|
|
)
|
|
|
|
const EMPTY_CONNECTIONS: IConnectionsItem[] = []
|
|
const ConnectionsPage = () => {
|
|
const { t } = useTranslation()
|
|
const pageVisible = useVisibility()
|
|
const { searchState, match, onSearch } = usePageSearch('connections')
|
|
const hasSearch = searchState.text.length > 0
|
|
const [curOrderOpt, setCurOrderOpt] = useState<OrderKey>('default')
|
|
const [networkFilter, setNetworkFilter] =
|
|
useState<NetworkFilter>(savedNetworkFilter)
|
|
const hasFilter = networkFilter !== 'all'
|
|
const [connectionsType, setConnectionsType] = useState<'active' | 'closed'>(
|
|
'active',
|
|
)
|
|
|
|
const {
|
|
response: { data: connections },
|
|
clearClosedConnections,
|
|
} = useConnectionData({ enabled: pageVisible })
|
|
const {
|
|
response: { data: traffic },
|
|
} = useTrafficData({ enabled: pageVisible })
|
|
|
|
const [setting, setSetting] = useConnectionSetting()
|
|
|
|
const isTableLayout = setting.layout === 'table'
|
|
|
|
const [isColumnManagerOpen, setIsColumnManagerOpen] = useState(false)
|
|
|
|
const selectedConnections =
|
|
connectionsType === 'active'
|
|
? (connections?.activeConnections ?? EMPTY_CONNECTIONS)
|
|
: (connections?.closedConnections ?? EMPTY_CONNECTIONS)
|
|
|
|
const filterConn = useMemo(() => {
|
|
const orderFunc = orderFunctionMap[curOrderOpt]
|
|
|
|
if (isTableLayout && !hasSearch && !hasFilter) return selectedConnections
|
|
if (!hasSearch && !hasFilter) return orderFunc([...selectedConnections])
|
|
|
|
const matchConns = selectedConnections.filter((conn) => {
|
|
const { host, destinationIP, process, network } = conn.metadata
|
|
if (networkFilter !== 'all' && network !== networkFilter) return false
|
|
return (
|
|
!hasSearch ||
|
|
match(host || '') ||
|
|
match(destinationIP || '') ||
|
|
match(process || '')
|
|
)
|
|
})
|
|
|
|
return orderFunc ? orderFunc(matchConns) : matchConns
|
|
}, [
|
|
selectedConnections,
|
|
isTableLayout,
|
|
hasSearch,
|
|
hasFilter,
|
|
networkFilter,
|
|
match,
|
|
curOrderOpt,
|
|
])
|
|
|
|
const displayRows = useConnectionRowViews(
|
|
isTableLayout ? EMPTY_CONNECTIONS : filterConn,
|
|
)
|
|
|
|
const detailRef = useRef<ConnectionDetailRef>(null!)
|
|
|
|
const selectConnectionsType = useCallback(
|
|
(type: 'active' | 'closed') => {
|
|
if (type === connectionsType) return
|
|
detailRef.current?.close()
|
|
setIsColumnManagerOpen(false)
|
|
setConnectionsType(type)
|
|
},
|
|
[connectionsType],
|
|
)
|
|
|
|
const showDetailById = useCallback(
|
|
(id: string, preferredKind: ConnectionRuleKind = 'domain') => {
|
|
const connection = filterConn.find((item) => item.id === id)
|
|
if (connection) {
|
|
detailRef.current?.open(
|
|
connection,
|
|
connectionsType === 'closed',
|
|
preferredKind,
|
|
)
|
|
}
|
|
},
|
|
[connectionsType, filterConn],
|
|
)
|
|
|
|
const onCloseAll = useLockFn(() =>
|
|
mutate(() => closeAllConnections(), {
|
|
id: 'close-all-connections',
|
|
errorNotice: false,
|
|
}),
|
|
)
|
|
|
|
const hasTableData = filterConn.length > 0
|
|
|
|
return (
|
|
<BasePage
|
|
full
|
|
title={
|
|
<span style={{ whiteSpace: 'nowrap' }}>
|
|
{t('connections.page.title')}
|
|
</span>
|
|
}
|
|
contentStyle={{
|
|
height: '100%',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
overflow: 'hidden',
|
|
borderRadius: '8px',
|
|
minHeight: 0,
|
|
}}
|
|
header={
|
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
|
|
<Box sx={{ mx: 1 }}>
|
|
{t('shared.labels.downloaded')}:{' '}
|
|
{parseTraffic(traffic?.downTotal || 0)}
|
|
</Box>
|
|
<Box sx={{ mx: 1 }}>
|
|
{t('shared.labels.uploaded')}: {parseTraffic(traffic?.upTotal || 0)}
|
|
</Box>
|
|
<IconButton
|
|
color="inherit"
|
|
size="small"
|
|
onClick={() =>
|
|
setSetting((o) =>
|
|
o?.layout !== 'table'
|
|
? { ...o, layout: 'table' }
|
|
: { ...o, layout: 'list' },
|
|
)
|
|
}
|
|
>
|
|
{isTableLayout ? (
|
|
<TableRowsRounded titleAccess={t('shared.actions.listView')} />
|
|
) : (
|
|
<TableChartRounded titleAccess={t('shared.actions.tableView')} />
|
|
)}
|
|
</IconButton>
|
|
<Button size="small" variant="contained" onClick={onCloseAll}>
|
|
<span style={{ whiteSpace: 'nowrap' }}>
|
|
{t('shared.actions.closeAll')}
|
|
</span>
|
|
</Button>
|
|
</Box>
|
|
}
|
|
>
|
|
<Box
|
|
sx={{
|
|
pt: 1,
|
|
mb: 0.5,
|
|
mx: '10px',
|
|
minHeight: '36px',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
gap: 1,
|
|
userSelect: 'text',
|
|
position: 'sticky',
|
|
top: 0,
|
|
zIndex: 2,
|
|
}}
|
|
>
|
|
<ButtonGroup sx={{ mr: 1, flexBasis: 'content' }}>
|
|
<Button
|
|
size="small"
|
|
variant={connectionsType === 'active' ? 'contained' : 'outlined'}
|
|
onClick={() => selectConnectionsType('active')}
|
|
>
|
|
{t('connections.components.actions.active')}{' '}
|
|
{connections?.activeConnections.length}
|
|
</Button>
|
|
<Button
|
|
size="small"
|
|
variant={connectionsType === 'closed' ? 'contained' : 'outlined'}
|
|
onClick={() => selectConnectionsType('closed')}
|
|
>
|
|
{t('connections.components.actions.closed')}{' '}
|
|
{connections?.closedConnections.length}
|
|
</Button>
|
|
</ButtonGroup>
|
|
{!isTableLayout && (
|
|
<BaseStyledSelect
|
|
value={curOrderOpt}
|
|
onChange={(e) => setCurOrderOpt(e.target.value as OrderKey)}
|
|
>
|
|
{ORDER_OPTIONS.map((option) => (
|
|
<MenuItem key={option.id} value={option.id}>
|
|
<span style={{ fontSize: 14 }}>{t(option.labelKey)}</span>
|
|
</MenuItem>
|
|
))}
|
|
</BaseStyledSelect>
|
|
)}
|
|
<BaseStyledSelect
|
|
style={{ width: 80 }}
|
|
value={networkFilter}
|
|
onChange={(e) => {
|
|
savedNetworkFilter = e.target.value as NetworkFilter
|
|
setNetworkFilter(savedNetworkFilter)
|
|
}}
|
|
>
|
|
<MenuItem value="all">
|
|
<span style={{ fontSize: 14 }}>All</span>
|
|
</MenuItem>
|
|
<MenuItem value="tcp">
|
|
<span style={{ fontSize: 14 }}>TCP</span>
|
|
</MenuItem>
|
|
<MenuItem value="udp">
|
|
<span style={{ fontSize: 14 }}>UDP</span>
|
|
</MenuItem>
|
|
</BaseStyledSelect>
|
|
<Box
|
|
sx={{
|
|
flex: 1,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
'& > *': {
|
|
flex: 1,
|
|
},
|
|
}}
|
|
>
|
|
<BaseSearchBox
|
|
defaultValue={searchState.text}
|
|
{...searchState}
|
|
onSearch={onSearch}
|
|
/>
|
|
</Box>
|
|
{isTableLayout && hasTableData && (
|
|
<Tooltip title={t('connections.components.columnManager.title')}>
|
|
<IconButton
|
|
size="small"
|
|
aria-label={t('connections.components.columnManager.title')}
|
|
onClick={() => setIsColumnManagerOpen(true)}
|
|
sx={{ flex: '0 0 auto' }}
|
|
>
|
|
<ViewColumnRounded fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
)}
|
|
</Box>
|
|
|
|
{!hasTableData ? (
|
|
<BaseEmpty />
|
|
) : isTableLayout ? (
|
|
<ConnectionTable
|
|
connections={filterConn}
|
|
onShowDetail={showDetailById}
|
|
columnManagerOpen={isColumnManagerOpen}
|
|
onCloseColumnManager={() => setIsColumnManagerOpen(false)}
|
|
/>
|
|
) : (
|
|
<VirtualList
|
|
key={connectionsType}
|
|
count={displayRows.length}
|
|
estimateSize={56}
|
|
renderItem={(i) => (
|
|
<ConnectionRowItem
|
|
row={displayRows[i]}
|
|
closed={connectionsType === 'closed'}
|
|
onShowDetail={showDetailById}
|
|
/>
|
|
)}
|
|
style={{
|
|
flex: 1,
|
|
borderRadius: '8px',
|
|
WebkitOverflowScrolling: 'touch',
|
|
overscrollBehavior: 'contain',
|
|
}}
|
|
/>
|
|
)}
|
|
<ConnectionDetail ref={detailRef} />
|
|
<Zoom
|
|
in={connectionsType === 'closed' && filterConn.length > 0}
|
|
unmountOnExit
|
|
>
|
|
<Fab
|
|
size="medium"
|
|
variant="extended"
|
|
sx={{
|
|
position: 'absolute',
|
|
right: 16,
|
|
bottom: isTableLayout ? 70 : 16,
|
|
}}
|
|
color="primary"
|
|
onClick={() => clearClosedConnections()}
|
|
>
|
|
<DeleteForeverRounded sx={{ mr: 1 }} fontSize="small" />
|
|
{t('shared.actions.clear')}
|
|
</Fab>
|
|
</Zoom>
|
|
</BasePage>
|
|
)
|
|
}
|
|
|
|
export default ConnectionsPage
|