Files
clash-verge-rev/src/pages/rules.tsx
T
Tunglies 0c2de711bc perf(connections): reduce snapshot and table allocation churn (#8613)
* perf(connections): reduce snapshot and table allocation churn

Reuse unchanged connection lists, sort traffic columns without display snapshots, and suspend the relative-time clock while hidden. Keep rule rows indexed and release resize listeners on unmount.

* test(connections): limit coverage to changed behavior

Keep snapshot sharing, speed decay, bounded history, numeric sorting, and visibility regression guards. Remove unrelated clear-history and pending-delivery assertions.

* test(connections): focus clock regression on timer cost

Replace behavioral clock assertions with one resource-count check for shared, hidden, and released intervals.

* test(connections): retain only allocation and formatting guards

Reduce snapshot and table tests to reference reuse, notification suppression, and visible-row formatting cost.

* remove useless test

* no need changelog
2026-11-17 02:24:27 +00:00

110 lines
3.0 KiB
TypeScript

import { Box } from '@mui/material'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
BaseEmpty,
BasePage,
BaseSearchBox,
VirtualList,
type VirtualListHandle,
} from '@/components/base'
import { ScrollTopButton } from '@/components/layout/scroll-top-button'
import { ProviderButton } from '@/components/rule/provider-button'
import RuleItem from '@/components/rule/rule-item'
import { usePageSearch } from '@/hooks/use-page-search'
import { useVisibility } from '@/hooks/use-visibility'
import { useAppRefreshers, useRulesData } from '@/providers/app-data-context'
const RulesPage = () => {
const { t } = useTranslation()
const { rules = [] } = useRulesData()
const { refreshRules, refreshRuleProviders } = useAppRefreshers()
const { searchState, match, onSearch } = usePageSearch('rules')
const virtuosoRef = useRef<VirtualListHandle>(null)
const [showScrollTop, setShowScrollTop] = useState(false)
const pageVisible = useVisibility()
// 页面可见时刷新规则数据:挂载时可见或从隐藏恢复可见各触发一次
useEffect(() => {
if (pageVisible) {
refreshRules()
refreshRuleProviders()
}
}, [refreshRules, refreshRuleProviders, pageVisible])
const filteredRules = useMemo(() => {
const indices: number[] = []
for (let i = 0; i < rules.length; i++) {
if (match(rules[i].payload ?? '')) indices.push(i)
}
return indices
}, [rules, match])
const handleScroll = useCallback((e: Event) => {
setShowScrollTop((e.target as HTMLElement).scrollTop > 100)
}, [])
const scrollToTop = () => {
virtuosoRef.current?.scrollTo({ top: 0, behavior: 'smooth' })
}
return (
<BasePage
full
title={t('rules.page.title')}
contentStyle={{
height: '100%',
display: 'flex',
flexDirection: 'column',
overflow: 'auto',
}}
header={
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<ProviderButton />
</Box>
}
>
<Box
sx={{
pt: 1,
mb: 0.5,
mx: '10px',
height: '36px',
display: 'flex',
alignItems: 'center',
}}
>
<BaseSearchBox
defaultValue={searchState.text}
{...searchState}
onSearch={onSearch}
/>
</Box>
{filteredRules && filteredRules.length > 0 ? (
<>
<VirtualList
ref={virtuosoRef}
count={filteredRules.length}
estimateSize={40}
renderItem={(i) => (
<RuleItem
value={rules[filteredRules[i]]}
lineNo={filteredRules[i] + 1}
/>
)}
style={{ flex: 1 }}
onScroll={handleScroll}
/>
<ScrollTopButton onClick={scrollToTop} show={showScrollTop} />
</>
) : (
<BaseEmpty />
)}
</BasePage>
)
}
export default RulesPage