mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-20 02:42:12 +08:00
perf(ui): memoize derived validation and list computations
The TUN dialog re-split its route-exclusion list, the rule provider dialog re-sorted its entries and the group-delays hook re-stringified its group key on every render; each now recomputes only when its input identity changes. The provider-button hunk re-indents its JSX block because the sorted entries moved out of the render expression.
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import dayjs from 'dayjs'
|
||||
import { useState } from 'react'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { updateRuleProvider } from 'tauri-plugin-mihomo-api'
|
||||
|
||||
@@ -42,6 +42,11 @@ export const ProviderButton = () => {
|
||||
const { t } = useTranslation()
|
||||
const [open, setOpen] = useState(false)
|
||||
const { ruleProviders } = useRulesData()
|
||||
|
||||
const sortedProviderEntries = useMemo(
|
||||
() => Object.entries(ruleProviders || {}).sort(),
|
||||
[ruleProviders],
|
||||
)
|
||||
const { refreshRules, refreshRuleProviders } = useAppRefreshers()
|
||||
const [updating, setUpdating] = useState<Record<string, boolean>>({})
|
||||
|
||||
@@ -166,121 +171,119 @@ export const ProviderButton = () => {
|
||||
|
||||
<DialogContent>
|
||||
<List sx={{ py: 0, minHeight: 250 }}>
|
||||
{Object.entries(ruleProviders || {})
|
||||
.sort()
|
||||
.map(([key, item]) => {
|
||||
const provider = item
|
||||
const time = dayjs(provider.updatedAt)
|
||||
const isUpdating = updating[key]
|
||||
{sortedProviderEntries.map(([key, item]) => {
|
||||
const provider = item
|
||||
const time = dayjs(provider.updatedAt)
|
||||
const isUpdating = updating[key]
|
||||
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: '8px',
|
||||
borderRadius: 2,
|
||||
overflow: 'hidden',
|
||||
transition: 'all 0.2s',
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor = mode === 'light' ? '#ffffff' : '#24252f'
|
||||
const hoverColor =
|
||||
mode === 'light'
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2)
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: '8px',
|
||||
borderRadius: 2,
|
||||
overflow: 'hidden',
|
||||
transition: 'all 0.2s',
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor = mode === 'light' ? '#ffffff' : '#24252f'
|
||||
const hoverColor =
|
||||
mode === 'light'
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2)
|
||||
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
'&:hover': {
|
||||
backgroundColor: hoverColor,
|
||||
borderColor: alpha(primary.main, 0.3),
|
||||
},
|
||||
}
|
||||
},
|
||||
]}
|
||||
>
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
<span style={{ marginRight: '8px' }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.ruleCount}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t('shared.labels.updateAt')}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
'&:hover': {
|
||||
backgroundColor: hoverColor,
|
||||
borderColor: alpha(primary.main, 0.3),
|
||||
},
|
||||
}
|
||||
secondary={
|
||||
<Box sx={{ display: 'flex' }}>
|
||||
<TypeBox component="span">
|
||||
{typeof provider.vehicleType === 'string'
|
||||
? provider.vehicleType
|
||||
: provider.vehicleType.Unknown}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">
|
||||
{typeof provider.behavior === 'string'
|
||||
? provider.behavior
|
||||
: provider.behavior.Unknown}
|
||||
</TypeBox>
|
||||
</Box>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
width: 40,
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
}}
|
||||
>
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => updateProvider(key)}
|
||||
disabled={isUpdating}
|
||||
aria-label={t('rules.page.provider.actions.update')}
|
||||
},
|
||||
]}
|
||||
>
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? 'spin 1s linear infinite'
|
||||
: 'none',
|
||||
'@keyframes spin': {
|
||||
'0%': { transform: 'rotate(0deg)' },
|
||||
'100%': { transform: 'rotate(360deg)' },
|
||||
},
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
}}
|
||||
title={t('rules.page.provider.actions.update')}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
)
|
||||
})}
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: 'flex', alignItems: 'center' }}
|
||||
>
|
||||
<span style={{ marginRight: '8px' }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.ruleCount}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t('shared.labels.updateAt')}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
}
|
||||
secondary={
|
||||
<Box sx={{ display: 'flex' }}>
|
||||
<TypeBox component="span">
|
||||
{typeof provider.vehicleType === 'string'
|
||||
? provider.vehicleType
|
||||
: provider.vehicleType.Unknown}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">
|
||||
{typeof provider.behavior === 'string'
|
||||
? provider.behavior
|
||||
: provider.behavior.Unknown}
|
||||
</TypeBox>
|
||||
</Box>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
width: 40,
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
}}
|
||||
>
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => updateProvider(key)}
|
||||
disabled={isUpdating}
|
||||
aria-label={t('rules.page.provider.actions.update')}
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? 'spin 1s linear infinite'
|
||||
: 'none',
|
||||
'@keyframes spin': {
|
||||
'0%': { transform: 'rotate(0deg)' },
|
||||
'100%': { transform: 'rotate(360deg)' },
|
||||
},
|
||||
}}
|
||||
title={t('rules.page.provider.actions.update')}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
)
|
||||
})}
|
||||
</List>
|
||||
</DialogContent>
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import type { Ref } from 'react'
|
||||
import { useState } from 'react'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import {
|
||||
@@ -58,8 +58,9 @@ export function TunViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
dnsMode: '',
|
||||
})
|
||||
|
||||
const routeExcludeAddressItems = splitRouteExcludeAddress(
|
||||
values.routeExcludeAddress,
|
||||
const routeExcludeAddressItems = useMemo(
|
||||
() => splitRouteExcludeAddress(values.routeExcludeAddress),
|
||||
[values.routeExcludeAddress],
|
||||
)
|
||||
const routeExcludeAddressError =
|
||||
values.autoRoute &&
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useSyncExternalStore } from 'react'
|
||||
import { useCallback, useMemo, useSyncExternalStore } from 'react'
|
||||
|
||||
import delayManager, { type DelaySnapshot } from '@/services/delay'
|
||||
import type { ResolvedProxyMember } from '@/types/proxy-view'
|
||||
@@ -32,7 +32,7 @@ export const useGroupsDelays = (
|
||||
groups: readonly string[],
|
||||
): ReadonlyMap<string, DelaySnapshot> => {
|
||||
// Group membership must remain unambiguous when names contain spaces.
|
||||
const groupKey = JSON.stringify(groups)
|
||||
const groupKey = useMemo(() => JSON.stringify(groups), [groups])
|
||||
|
||||
const subscribe = useCallback(
|
||||
(onSettle: () => void) => {
|
||||
|
||||
Reference in New Issue
Block a user