fix(profile): correct batch state updates in editors and stale reads in settings dialogs (#8695)

* fix(setting): keep the copy-success snackbar visible for its full duration

The clipboard handler cleared the success flag with a zero-delay
timeout, so the snackbar configured for 2000 ms was hidden on the next
macrotask. The clear now runs after 2000 ms, resetting the timer on
each copy so a second copy within the window keeps its full duration.

* fix(profile): update editor sequences through functional state

The rule, group and proxy editors mixed functional updaters with writes
computed from render-captured arrays, so two clicks in one React batch
(the second before re-render) read a pre-batch array: deletes dropped
an entry queued by the sibling updater, reorders resolved indices
against a stale list, and add buttons could duplicate past their
uniqueness checks. Every sequence mutation now derives from the live
previous state; the group add buttons keep their translated duplicate
notice and recheck inside the updater.

* fix(sysproxy): read the live verge config in the toggle drain loop

The drain loop captured the render-time verge snapshot, so a config
revalidation landing mid-loop left later iterations deciding connection
closing and rollback with stale values. The confirmed-state seed and
the in-loop auto-close check now read through a latest ref.

* perf(setting): defer layout dialog icon loading to first open

Both settings sections mount the layout dialog, so its eager mount
effect issued about eleven Tauri calls twice per settings-page visit
before the dialog was ever opened. The icon scan now runs once, on the
first open.

* fix(setting): clear the copy toast state when the dialog closes

Closing and reopening the controller dialog inside the two-second
window replayed the previous session's toast for the residual time;
every close path now clears the timer and the flag.
This commit is contained in:
Tunglies
2026-11-23 04:49:13 +00:00
committed by GitHub
parent 3eb6883e12
commit b0d0b9a944
7 changed files with 115 additions and 68 deletions
@@ -12,7 +12,7 @@ import {
Tooltip,
} from '@mui/material'
import { useLockFn } from 'ahooks'
import { useState, type Ref } from 'react'
import { useRef, useState, type Ref } from 'react'
import { useTranslation } from 'react-i18next'
import { BaseDialog, DialogRef, Switch } from '@/components/base'
@@ -26,6 +26,10 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
const { t } = useTranslation()
const [open, setOpen] = useState(false)
const [copySuccess, setCopySuccess] = useState<null | string>(null)
// A second copy within the window must not be hidden by the first timer.
const copySuccessTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(
undefined,
)
const [isSaving, setIsSaving] = useState(false)
const { clashInfo, patchInfo } = useClashInfo()
@@ -36,6 +40,14 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
verge?.enable_external_controller ?? false,
)
// A reopen within the window must not replay the previous session's toast.
const closeDialog = () => {
clearTimeout(copySuccessTimerRef.current)
copySuccessTimerRef.current = undefined
setCopySuccess(null)
setOpen(false)
}
// 对话框打开时初始化配置
useSettingDialog(ref, 'controller-viewer', open, () => ({
open: async () => {
@@ -44,7 +56,7 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
setSecret(clashInfo?.secret || '')
setEnableController(verge?.enable_external_controller ?? false)
},
close: () => setOpen(false),
close: closeDialog,
}))
// 保存配置
@@ -78,7 +90,7 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
}
showNotice.success('shared.feedback.notifications.common.saveSuccess')
setOpen(false)
closeDialog()
} catch (err) {
showNotice.error(
'shared.feedback.notifications.common.saveFailed',
@@ -96,7 +108,11 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
try {
await navigator.clipboard.writeText(text)
setCopySuccess(type)
setTimeout(() => setCopySuccess(null))
clearTimeout(copySuccessTimerRef.current)
copySuccessTimerRef.current = setTimeout(
() => setCopySuccess(null),
2000,
)
} catch (err) {
console.warn('[ControllerViewer] copy to clipboard failed:', err)
showNotice.error(
@@ -122,8 +138,8 @@ export function ControllerViewer({ ref }: { ref?: Ref<DialogRef> }) {
)
}
cancelBtn={t('shared.actions.cancel')}
onClose={() => setOpen(false)}
onCancel={() => setOpen(false)}
onClose={closeDialog}
onCancel={closeDialog}
onOk={onSave}
>
<span data-settings-dialog="controller-viewer" />
@@ -14,7 +14,7 @@ import { convertFileSrc } from '@tauri-apps/api/core'
import { join } from '@tauri-apps/api/path'
import { open as openDialog } from '@tauri-apps/plugin-dialog'
import { exists } from '@tauri-apps/plugin-fs'
import { forwardRef, useEffect, useState } from 'react'
import { forwardRef, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { BaseDialog, DialogRef, Switch, TooltipIcon } from '@/components/base'
@@ -62,9 +62,16 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
const { decorated, toggleDecorations } = useWindowDecorations()
// Icon paths are only reachable from the open dialog; both settings sections
// mount this viewer, so an eager read would double ~11 IPC calls per visit.
// Loaded once per mount, deferred to the first open.
const iconsLoadedRef = useRef(false)
useEffect(() => {
if (!open || iconsLoadedRef.current) return
iconsLoadedRef.current = true
initIconPath()
}, [])
}, [open])
async function initIconPath() {
const appDir = await getAppDir()