mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-30 01:20:07 +08:00
perf(connections): sort by column keys without building row snapshots
This commit is contained in:
@@ -218,11 +218,51 @@ const getConnectionCellValue = (
|
||||
}
|
||||
}
|
||||
|
||||
const compareConnectionCellValue = (
|
||||
field: ColumnField,
|
||||
type TrafficSortField = 'download' | 'upload' | 'dlSpeed' | 'ulSpeed'
|
||||
type DisplaySortField = Exclude<ColumnField, TrafficSortField>
|
||||
|
||||
const connectionSortKey = (field: DisplaySortField, row: IConnectionsItem) => {
|
||||
switch (field) {
|
||||
case 'host':
|
||||
return getConnectionHost(row)
|
||||
case 'process':
|
||||
return getConnectionProcess(row)
|
||||
case 'source':
|
||||
return getConnectionSource(row)
|
||||
case 'remoteDestination':
|
||||
return getConnectionDestination(row)
|
||||
case 'chains':
|
||||
return formatConnectionChains(row.chains)
|
||||
case 'rule':
|
||||
return getConnectionRule(row)
|
||||
case 'type':
|
||||
return getConnectionTypeLabel(row)
|
||||
case 'time':
|
||||
return getConnectionStartTime(row)
|
||||
}
|
||||
}
|
||||
|
||||
const sortByConnectionKey = (
|
||||
connections: IConnectionsItem[],
|
||||
field: DisplaySortField,
|
||||
direction: number,
|
||||
) => {
|
||||
return connections
|
||||
.map((row) => ({ row, key: connectionSortKey(field, row) }))
|
||||
.sort((a, b) => {
|
||||
const order =
|
||||
typeof a.key === 'number'
|
||||
? (Number(a.key) || 0) - (Number(b.key) || 0)
|
||||
: String(a.key).localeCompare(String(b.key))
|
||||
return order * direction
|
||||
})
|
||||
.map(({ row }) => row)
|
||||
}
|
||||
|
||||
const compareConnectionTraffic = (
|
||||
field: TrafficSortField,
|
||||
left: IConnectionsItem,
|
||||
right: IConnectionsItem,
|
||||
getSnapshot: (row: IConnectionsItem) => TableRowSnapshot,
|
||||
) => {
|
||||
switch (field) {
|
||||
case 'download':
|
||||
@@ -234,14 +274,6 @@ const compareConnectionCellValue = (
|
||||
case 'ulSpeed':
|
||||
return (left.curUpload ?? 0) - (right.curUpload ?? 0)
|
||||
}
|
||||
const leftValue = getConnectionCellValue(field, getSnapshot(left))
|
||||
const rightValue = getConnectionCellValue(field, getSnapshot(right))
|
||||
|
||||
if (typeof leftValue === 'number' || typeof rightValue === 'number') {
|
||||
return (Number(leftValue) || 0) - (Number(rightValue) || 0)
|
||||
}
|
||||
|
||||
return String(leftValue ?? '').localeCompare(String(rightValue ?? ''))
|
||||
}
|
||||
|
||||
const renderCell = (
|
||||
@@ -597,13 +629,21 @@ export const ConnectionTable = (props: Props) => {
|
||||
const sortedConnections = useMemo(() => {
|
||||
if (!sorting) return connections
|
||||
|
||||
const direction = sorting.desc ? -1 : 1
|
||||
return [...connections].sort(
|
||||
(left, right) =>
|
||||
compareConnectionCellValue(sorting.id, left, right, getRowSnapshot) *
|
||||
direction,
|
||||
)
|
||||
}, [connections, sorting, getRowSnapshot])
|
||||
const { id, desc } = sorting
|
||||
const direction = desc ? -1 : 1
|
||||
if (
|
||||
id === 'download' ||
|
||||
id === 'upload' ||
|
||||
id === 'dlSpeed' ||
|
||||
id === 'ulSpeed'
|
||||
) {
|
||||
return [...connections].sort(
|
||||
(left, right) => compareConnectionTraffic(id, left, right) * direction,
|
||||
)
|
||||
}
|
||||
|
||||
return sortByConnectionKey(connections, id, direction)
|
||||
}, [connections, sorting])
|
||||
|
||||
const tableWidth = useMemo(
|
||||
() => visibleColumns.reduce((total, column) => total + column.size, 0),
|
||||
|
||||
Reference in New Issue
Block a user