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:
Tunglies
2026-11-19 23:16:46 +08:00
parent 5043b8706d
commit 25bc4db8e9
8 changed files with 64 additions and 47 deletions
+7
View File
@@ -1,6 +1,13 @@
import { styled } from '@mui/material/styles'
import { default as MuiSwitch, SwitchProps } from '@mui/material/Switch'
/**
* MUI Switch delivers the boolean as the second onChange argument, so
* switch handlers use this adapter to stay boolean-only.
*/
export const switchChangeValue = (_event: unknown, value: boolean): boolean =>
value
export const Switch = styled((props: SwitchProps) => (
<MuiSwitch
focusVisibleClassName=".Mui-focusVisible"
+1 -1
View File
@@ -10,7 +10,7 @@ export { BaseSplitChipEditor } from './base-split-chip-editor'
export { BaseStyledSelect } from './base-styled-select'
export { BaseStyledTextField } from './base-styled-text-field'
export { MonacoEditor } from './monaco-editor'
export { Switch } from './base-switch'
export { Switch, switchChangeValue } from './base-switch'
export { TooltipIcon } from './base-tooltip-icon'
export { VirtualList, type VirtualListHandle } from './virtual-list'
export {
+5 -2
View File
@@ -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 (
<>
+20 -15
View File
@@ -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) => {
+17 -12
View File
@@ -17,7 +17,13 @@ import { exists } from '@tauri-apps/plugin-fs'
import { forwardRef, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { BaseDialog, DialogRef, Switch, TooltipIcon } from '@/components/base'
import {
BaseDialog,
DialogRef,
Switch,
TooltipIcon,
switchChangeValue,
} from '@/components/base'
import { DEFAULT_HOVER_DELAY } from '@/components/proxy/proxy-group-navigator'
import { useVerge } from '@/hooks/use-verge'
import { useWindowDecorations } from '@/hooks/use-window'
@@ -104,7 +110,6 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
close: () => setOpen(false),
}))
const onSwitchFormat = (_e: any, value: boolean) => value
const onError = (err: any) => {
showNotice.error(err)
}
@@ -134,7 +139,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={decorated}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={async () => {
await toggleDecorations()
}}
@@ -151,7 +156,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.traffic_graph ?? true}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ traffic_graph: e })}
onGuard={(e) => patchVerge({ traffic_graph: e })}
>
@@ -167,7 +172,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.enable_memory_usage ?? true}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_memory_usage: e })}
onGuard={(e) => patchVerge({ enable_memory_usage: e })}
>
@@ -185,7 +190,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.enable_group_icon ?? true}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_group_icon: e })}
onGuard={(e) => patchVerge({ enable_group_icon: e })}
>
@@ -203,7 +208,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.pause_render_traffic_stats_on_blur ?? true}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) =>
onChangeData({ pause_render_traffic_stats_on_blur: e })
}
@@ -271,7 +276,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.enable_hover_jump_navigator ?? true}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_hover_jump_navigator: e })}
onGuard={(e) => patchVerge({ enable_hover_jump_navigator: e })}
>
@@ -373,7 +378,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.collapse_navbar ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ collapse_navbar: e })}
onGuard={(e) => patchVerge({ collapse_navbar: e })}
>
@@ -420,7 +425,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.enable_tray_speed ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_tray_speed: e })}
onGuard={(e) => patchVerge({ enable_tray_speed: e })}
>
@@ -440,7 +445,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_tray_icon: e })}
onGuard={(e) => patchVerge({ enable_tray_icon: e })}
>
@@ -494,7 +499,7 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
value={verge?.tray_inline_outbound_modes ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ tray_inline_outbound_modes: e })}
onGuard={(e) => patchVerge({ tray_inline_outbound_modes: e })}
>
@@ -189,10 +189,7 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
void openExternalUrl(url).catch(showNotice.error)
}
const progress = useMemo(() => {
if (total <= 0) return 0
return Math.min((downloaded / total) * 100, 100)
}, [downloaded, total])
const progress = total <= 0 ? 0 : Math.min((downloaded / total) * 100, 100)
useImperativeHandle(ref, () => ({
open: () => {
+10 -5
View File
@@ -5,7 +5,13 @@ import { useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { updateGeo, type LogLevel } from 'tauri-plugin-mihomo-api'
import { BaseDialog, DialogRef, Switch, TooltipIcon } from '@/components/base'
import {
BaseDialog,
DialogRef,
Switch,
TooltipIcon,
switchChangeValue,
} from '@/components/base'
import { useClash } from '@/hooks/use-clash'
import { useClashLog } from '@/hooks/use-clash-log'
import { useDisplayedMixedPort } from '@/hooks/use-displayed-mixed-port'
@@ -69,7 +75,6 @@ const SettingClash = ({ onError }: Props) => {
const corsRef = useRef<DialogRef>(null)
const tunnelRef = useRef<DialogRef>(null)
const onSwitchFormat = (_e: any, value: boolean) => value
const onChangeData = (patch: Partial<IConfigData>) => {
mutateClash((old) => ({ ...old!, ...patch }), false)
}
@@ -173,7 +178,7 @@ const SettingClash = ({ onError }: Props) => {
value={allowLan ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ 'allow-lan': e })}
onGuard={(e) => patchClash({ 'allow-lan': e })}
>
@@ -213,7 +218,7 @@ const SettingClash = ({ onError }: Props) => {
value={ipv6 ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ ipv6: e })}
onGuard={(e) => patchClash({ ipv6: e })}
>
@@ -235,7 +240,7 @@ const SettingClash = ({ onError }: Props) => {
value={unifiedDelay ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ 'unified-delay': e })}
onGuard={(e) => patchClash({ 'unified-delay': e })}
>
+3 -8
View File
@@ -1,8 +1,7 @@
import { Box } from '@mui/material'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { Switch, TooltipIcon } from '@/components/base'
import { Switch, TooltipIcon, switchChangeValue } from '@/components/base'
import ProxyControlSwitches from '@/components/shared/proxy-control-switches'
import { useVerge } from '@/hooks/use-verge'
@@ -20,10 +19,6 @@ const SettingSystem = ({ onError }: Props) => {
const { enable_auto_launch, enable_silent_start } = verge ?? {}
const onSwitchFormat = (
_e: React.ChangeEvent<HTMLInputElement>,
value: boolean,
) => value
const onChangeData = (patch: Partial<IVergeConfig>) => {
mutateVerge({ ...verge, ...patch })
}
@@ -52,7 +47,7 @@ const SettingSystem = ({ onError }: Props) => {
value={enable_auto_launch ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => {
onChangeData({ enable_auto_launch: e })
}}
@@ -85,7 +80,7 @@ const SettingSystem = ({ onError }: Props) => {
value={enable_silent_start ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onFormat={switchChangeValue}
onChange={(e) => onChangeData({ enable_silent_start: e })}
onGuard={(e) => patchVerge({ enable_silent_start: e })}
>