From 25bc4db8e9b918f0bbc47c91ebdf3a3549e236ab Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:16:46 +0800 Subject: [PATCH] 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. --- src/components/base/base-switch.tsx | 7 ++++ src/components/base/index.ts | 2 +- src/components/profile/profile-more.tsx | 7 ++-- src/components/profile/profile-viewer.tsx | 35 +++++++++++-------- src/components/setting/mods/layout-viewer.tsx | 29 ++++++++------- src/components/setting/mods/update-viewer.tsx | 5 +-- src/components/setting/setting-clash.tsx | 15 +++++--- src/components/setting/setting-system.tsx | 11 ++---- 8 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/components/base/base-switch.tsx b/src/components/base/base-switch.tsx index b6513e9f7..9437e4703 100644 --- a/src/components/base/base-switch.tsx +++ b/src/components/base/base-switch.tsx @@ -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) => ( { 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 ( <> diff --git a/src/components/profile/profile-viewer.tsx b/src/components/profile/profile-viewer.tsx index 582437fbe..4a430abd3 100644 --- a/src/components/profile/profile-viewer.tsx +++ b/src/components/profile/profile-viewer.tsx @@ -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) => { diff --git a/src/components/setting/mods/layout-viewer.tsx b/src/components/setting/mods/layout-viewer.tsx index ed2799899..d815f3525 100644 --- a/src/components/setting/mods/layout-viewer.tsx +++ b/src/components/setting/mods/layout-viewer.tsx @@ -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((_, 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((_, ref) => { value={decorated} valueProps="checked" onCatch={onError} - onFormat={onSwitchFormat} + onFormat={switchChangeValue} onChange={async () => { await toggleDecorations() }} @@ -151,7 +156,7 @@ export const LayoutViewer = forwardRef((_, 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((_, 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((_, 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((_, 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((_, 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((_, 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((_, 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((_, 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((_, 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 })} > diff --git a/src/components/setting/mods/update-viewer.tsx b/src/components/setting/mods/update-viewer.tsx index dc6a2659f..8fdb59adb 100644 --- a/src/components/setting/mods/update-viewer.tsx +++ b/src/components/setting/mods/update-viewer.tsx @@ -189,10 +189,7 @@ export function UpdateViewer({ ref }: { ref?: Ref }) { 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: () => { diff --git a/src/components/setting/setting-clash.tsx b/src/components/setting/setting-clash.tsx index 39b35839c..8d4b2d9f8 100644 --- a/src/components/setting/setting-clash.tsx +++ b/src/components/setting/setting-clash.tsx @@ -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(null) const tunnelRef = useRef(null) - const onSwitchFormat = (_e: any, value: boolean) => value const onChangeData = (patch: Partial) => { 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 })} > diff --git a/src/components/setting/setting-system.tsx b/src/components/setting/setting-system.tsx index f9992b907..3bf356d28 100644 --- a/src/components/setting/setting-system.tsx +++ b/src/components/setting/setting-system.tsx @@ -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, - value: boolean, - ) => value const onChangeData = (patch: Partial) => { 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 })} >