mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-30 01:20:07 +08:00
refactor(connections): align perf replay with visible row derivation
This commit is contained in:
@@ -6,8 +6,7 @@ import { createRoot } from 'react-dom/client'
|
||||
import { SWRConfig } from 'swr'
|
||||
|
||||
import { VirtualList } from '../../src/components/base/virtual-list'
|
||||
import { ConnectionRowItem } from '../../src/components/connection/connection-row-item'
|
||||
import { useConnectionRowViews } from '../../src/components/connection/connection-row-view'
|
||||
import { VisibleConnectionRowItem } from '../../src/components/connection/connection-row-item'
|
||||
import LogItem from '../../src/components/log/log-item'
|
||||
import { initializeLanguage } from '../../src/services/i18n'
|
||||
import { setPreloadConfig } from '../../src/services/preload'
|
||||
@@ -35,10 +34,9 @@ export function Components() {
|
||||
}),
|
||||
[],
|
||||
)
|
||||
const rows = useConnectionRowViews(connections)
|
||||
return (
|
||||
<VirtualList
|
||||
count={scenario === 'logs' ? logs.length : rows.length}
|
||||
count={scenario === 'logs' ? logs.length : connections.length}
|
||||
estimateSize={scenario === 'logs' ? 50 : 56}
|
||||
style={{ height: 640, margin: 24 }}
|
||||
renderItem={(index) =>
|
||||
@@ -48,8 +46,8 @@ export function Components() {
|
||||
searchState={searchState}
|
||||
/>
|
||||
) : (
|
||||
<ConnectionRowItem
|
||||
row={rows[index]}
|
||||
<VisibleConnectionRowItem
|
||||
connection={connections[index]}
|
||||
closed={false}
|
||||
onShowDetail={() => {}}
|
||||
/>
|
||||
|
||||
@@ -73,7 +73,7 @@ const actionStyle = {
|
||||
transform: 'translateY(-50%)',
|
||||
} as const
|
||||
|
||||
export const ConnectionRowItem = memo(
|
||||
const ConnectionRowItem = memo(
|
||||
function ConnectionRowItem({ row, closed, onShowDetail }: Props) {
|
||||
const { t } = useTranslation()
|
||||
const onDelete = useLockFn(async () => closeConnection(row.id))
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { useMemo, useRef } from 'react'
|
||||
|
||||
const TRAFFIC_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||
|
||||
export interface ConnectionRowView {
|
||||
@@ -114,66 +112,3 @@ export const createConnectionRowView = (connection: IConnectionsItem) => {
|
||||
searchableProcess: connection.metadata.process || '',
|
||||
} satisfies ConnectionRowView
|
||||
}
|
||||
|
||||
const sameConnectionRowView = (
|
||||
left: ConnectionRowView,
|
||||
right: ConnectionRowView,
|
||||
) =>
|
||||
left.host === right.host &&
|
||||
left.process === right.process &&
|
||||
left.network === right.network &&
|
||||
left.type === right.type &&
|
||||
left.chains === right.chains &&
|
||||
left.rule === right.rule &&
|
||||
left.time === right.time &&
|
||||
left.source === right.source &&
|
||||
left.destination === right.destination &&
|
||||
left.uploadText === right.uploadText &&
|
||||
left.downloadText === right.downloadText &&
|
||||
left.uploadSpeedText === right.uploadSpeedText &&
|
||||
left.downloadSpeedText === right.downloadSpeedText &&
|
||||
left.upload === right.upload &&
|
||||
left.download === right.download &&
|
||||
left.uploadSpeed === right.uploadSpeed &&
|
||||
left.downloadSpeed === right.downloadSpeed &&
|
||||
left.searchableHost === right.searchableHost &&
|
||||
left.searchableDestinationIP === right.searchableDestinationIP &&
|
||||
left.searchableProcess === right.searchableProcess
|
||||
|
||||
export const useConnectionRowViews = (connections: IConnectionsItem[]) => {
|
||||
const previousRowsRef = useRef(new Map<string, ConnectionRowView>())
|
||||
const previousConnectionsRef = useRef(new Map<string, IConnectionsItem>())
|
||||
|
||||
return useMemo(() => {
|
||||
const previousRows = previousRowsRef.current
|
||||
const previousConnections = previousConnectionsRef.current
|
||||
const nextRows = new Map<string, ConnectionRowView>()
|
||||
const nextConnections = new Map<string, IConnectionsItem>()
|
||||
const rows: ConnectionRowView[] = []
|
||||
|
||||
connections.forEach((connection) => {
|
||||
nextConnections.set(connection.id, connection)
|
||||
|
||||
const previousRow = previousRows.get(connection.id)
|
||||
const previousConnection = previousConnections.get(connection.id)
|
||||
|
||||
let row: ConnectionRowView
|
||||
if (previousRow && previousConnection === connection) {
|
||||
row = previousRow
|
||||
} else {
|
||||
const nextRow = createConnectionRowView(connection)
|
||||
row =
|
||||
previousRow && sameConnectionRowView(previousRow, nextRow)
|
||||
? previousRow
|
||||
: nextRow
|
||||
}
|
||||
|
||||
nextRows.set(connection.id, row)
|
||||
rows.push(row)
|
||||
})
|
||||
|
||||
previousRowsRef.current = nextRows
|
||||
previousConnectionsRef.current = nextConnections
|
||||
return rows
|
||||
}, [connections])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user