mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-03 12:20:05 +08:00
perf(ui): hoist static identities and stabilize remaining hot-path allocations (#8696)
* perf(ui): hoist static style, menu and OS identities Dialog controls rebuilt static option arrays, style objects and label maps on every render; the sysproxy viewer also probed the OS and split the default bypass string per render. These are now module constants or single memos keyed on their real inputs (translation function, active stage). * 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. * perf(ui): hoist hot regexes and stabilize clash mutators The clash log parser and the URI validators recompiled their patterns per call; the patterns are module constants now. mutateClash and invalidateClashConfig are stable callbacks so consumers can keep them in dependency arrays; mutateClash revalidates through the module-level query helper instead of the per-render refetch closure (no caller reads its resolved value). * perf(ui): batch notice removal and hoist drag sensors hideNoticesByCode removed matching notices one by one, notifying subscribers per item; it now filters in one pass and notifies once. The chain list, test card and grouped editor list allocated their drag sensor arrays per render; they are module constants now. * refactor(ui): share the switch adapter and drop dead memos Three settings files each defined the same boolean switch-format adapter; it is exported once from the Switch module. The update dialog memoized trivial arithmetic, the profile-more reset callback keyed on a per-render document object instead of its stable setter, and the profile viewer's imperative handle had no dependency array.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Alert, Box, Typography } from '@mui/material'
|
||||
import { useMemo } from 'react'
|
||||
import { Trans, useTranslation } from 'react-i18next'
|
||||
|
||||
const stageHints = {
|
||||
@@ -14,26 +15,31 @@ interface Props {
|
||||
|
||||
export const EnhanceHint = ({ stage }: Props) => {
|
||||
const { t } = useTranslation()
|
||||
const orderComponents = Object.fromEntries(
|
||||
Object.keys(stageHints).map((key) => [
|
||||
key,
|
||||
<Box
|
||||
key={key}
|
||||
component={key === stage ? 'strong' : 'span'}
|
||||
sx={
|
||||
key === stage
|
||||
? {
|
||||
fontWeight: 'bold',
|
||||
color: 'primary.contrastText',
|
||||
bgcolor: 'primary.main',
|
||||
borderRadius: 0.5,
|
||||
px: 0.5,
|
||||
boxDecorationBreak: 'clone',
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
/>,
|
||||
]),
|
||||
// The component map identity changes only when the active stage changes.
|
||||
const orderComponents = useMemo(
|
||||
() =>
|
||||
Object.fromEntries(
|
||||
Object.keys(stageHints).map((key) => [
|
||||
key,
|
||||
<Box
|
||||
key={key}
|
||||
component={key === stage ? 'strong' : 'span'}
|
||||
sx={
|
||||
key === stage
|
||||
? {
|
||||
fontWeight: 'bold',
|
||||
color: 'primary.contrastText',
|
||||
bgcolor: 'primary.main',
|
||||
borderRadius: 0.5,
|
||||
px: 0.5,
|
||||
boxDecorationBreak: 'clone',
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
/>,
|
||||
]),
|
||||
),
|
||||
[stage],
|
||||
)
|
||||
|
||||
return (
|
||||
|
||||
@@ -56,6 +56,8 @@ interface GroupedVirtualListProps<T> {
|
||||
style?: CSSProperties
|
||||
}
|
||||
|
||||
const GROUPED_LIST_SENSORS = [PointerSensor, KeyboardSensor]
|
||||
|
||||
/**
|
||||
* 用于编辑规则/节点/代理组功能的 prepend/original/append 分组虚拟化列表
|
||||
*/
|
||||
@@ -141,7 +143,7 @@ export function GroupedVirtualList<T>(props: GroupedVirtualListProps<T>) {
|
||||
|
||||
return (
|
||||
<DragDropProvider
|
||||
sensors={[PointerSensor, KeyboardSensor]}
|
||||
sensors={GROUPED_LIST_SENSORS}
|
||||
onDragStart={onDragStart}
|
||||
onDragOver={onDragOver}
|
||||
onDragEnd={onDragEnd}
|
||||
|
||||
@@ -112,9 +112,12 @@ export const ProfileMore = (props: Props) => {
|
||||
document.markSaved(currentValue)
|
||||
})
|
||||
|
||||
// document gets a fresh identity every render; setValue is the stable
|
||||
// useState setter, so the callback keys on it instead.
|
||||
const { setValue } = document
|
||||
const handleResetToDefault = useCallback(() => {
|
||||
document.setValue(DEFAULT_SCRIPT)
|
||||
}, [document])
|
||||
setValue(DEFAULT_SCRIPT)
|
||||
}, [setValue])
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -61,21 +61,26 @@ export function ProfileViewer({ onChange, ref }: ProfileViewerProps) {
|
||||
},
|
||||
})
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
create: () => {
|
||||
setOpenType('new')
|
||||
setOpen(true)
|
||||
},
|
||||
edit: (item: IProfileItem) => {
|
||||
if (item) {
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
setValue(key as any, value)
|
||||
})
|
||||
}
|
||||
setOpenType('edit')
|
||||
setOpen(true)
|
||||
},
|
||||
}))
|
||||
// The handle only closes over stable form/state setters.
|
||||
useImperativeHandle(
|
||||
ref,
|
||||
() => ({
|
||||
create: () => {
|
||||
setOpenType('new')
|
||||
setOpen(true)
|
||||
},
|
||||
edit: (item: IProfileItem) => {
|
||||
if (item) {
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
setValue(key as any, value)
|
||||
})
|
||||
}
|
||||
setOpenType('edit')
|
||||
setOpen(true)
|
||||
},
|
||||
}),
|
||||
[setValue],
|
||||
)
|
||||
|
||||
const handleOk = useLockFn(
|
||||
handleSubmit(async (form) => {
|
||||
|
||||
Reference in New Issue
Block a user