From c822c5dab627f06dbf975666a5fb6eb8435e693f Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 1 Jul 2026 20:03:58 +0800 Subject: [PATCH] refactor(webui): extract useFlashMessage hook for settings panels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 三个 settings 面板各手写同一套 flash-message 状态机(msg state + setTimeout 自动 消失 + 卸载清 timer)。global-config / notifications 是完整版(逐字相同); system-panel 是退化版(无 timer ref、无 cleanup、rapid flash 累积 timer、卸载后可能 stale setMsg)。抽 useFlashMessage(durationMs=2400) → { msg, flash, setMsg }。 - global-config / notifications:迁移后行为不变(2400ms;flash 瞬时 + setMsg 持久 错误保留)。 - system-panel:保留 4000ms(useFlashMessage(4000)),顺带补上 prior-timer 取消 + 卸载 cleanup(修 timer 泄漏,现代 React 里无害的小改善)。 webui typecheck(tsc -b) + vite build 干净;对抗性 Agent 逐行核对确认 hook 等价、 两面板行为不变、无依赖/import 回归。 --- .../settings/global-config-panel.tsx | 11 +----- .../settings/notifications-panel.tsx | 11 +----- .../src/components/settings/system-panel.tsx | 7 +--- packages/webui/src/hooks/use-flash-message.ts | 37 +++++++++++++++++++ 4 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 packages/webui/src/hooks/use-flash-message.ts diff --git a/packages/webui/src/components/settings/global-config-panel.tsx b/packages/webui/src/components/settings/global-config-panel.tsx index 19ba66f0..eef75ae3 100644 --- a/packages/webui/src/components/settings/global-config-panel.tsx +++ b/packages/webui/src/components/settings/global-config-panel.tsx @@ -11,6 +11,7 @@ import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { useApi } from '@/lib/api'; +import { useFlashMessage } from '@/hooks/use-flash-message'; import { cn } from '@/lib/utils'; import type { GlobalSettings } from '@/types'; @@ -30,9 +31,8 @@ export function GlobalConfigPanel() { const [config, setConfig] = useState(null); const [loading, setLoading] = useState(true); const [loadFailed, setLoadFailed] = useState(false); - const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null); + const { msg, flash } = useFlashMessage(); const saveTimer = useRef(null); - const msgTimer = useRef(null); const saveGen = useRef(0); const load = useCallback(async () => { @@ -54,17 +54,10 @@ export function GlobalConfigPanel() { useEffect( () => () => { if (saveTimer.current) clearTimeout(saveTimer.current); - if (msgTimer.current) clearTimeout(msgTimer.current); }, [], ); - const flash = (kind: 'ok' | 'err', text: string) => { - setMsg({ kind, text }); - if (msgTimer.current) clearTimeout(msgTimer.current); - msgTimer.current = window.setTimeout(() => setMsg(null), 2400); - }; - /** Apply locally + debounced auto-save. Deliberately does NOT reconcile the * inputs from the server's normalized response (it would wipe a half-typed * URL); a generation guard just confirms the latest save. */ diff --git a/packages/webui/src/components/settings/notifications-panel.tsx b/packages/webui/src/components/settings/notifications-panel.tsx index 67b0dff3..8ac4e42c 100644 --- a/packages/webui/src/components/settings/notifications-panel.tsx +++ b/packages/webui/src/components/settings/notifications-panel.tsx @@ -11,6 +11,7 @@ import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { useApi } from '@/lib/api'; +import { useFlashMessage } from '@/hooks/use-flash-message'; import { useTheme } from '@/contexts/ThemeContext'; import { cn } from '@/lib/utils'; import type { NotificationChannel, NotificationsConfig } from '@/types'; @@ -41,9 +42,8 @@ export function NotificationsPanel() { const [loading, setLoading] = useState(true); const [dialog, setDialog] = useState({ open: false, index: null, seed: blankChannel([]) }); const [testing, setTesting] = useState(null); - const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null); + const { msg, flash, setMsg } = useFlashMessage(); const saveTimer = useRef(null); - const msgTimer = useRef(null); const saveGen = useRef(0); useEffect(() => { @@ -66,17 +66,10 @@ export function NotificationsPanel() { useEffect( () => () => { if (saveTimer.current) clearTimeout(saveTimer.current); - if (msgTimer.current) clearTimeout(msgTimer.current); }, [], ); - const flash = (kind: 'ok' | 'err', text: string) => { - setMsg({ kind, text }); - if (msgTimer.current) clearTimeout(msgTimer.current); - msgTimer.current = window.setTimeout(() => setMsg(null), 2400); - }; - /** Apply locally + debounced auto-save. A generation guard reconciles with * the server's normalized result only if no newer edit landed meanwhile. */ const commit = (next: NotificationsConfig) => { diff --git a/packages/webui/src/components/settings/system-panel.tsx b/packages/webui/src/components/settings/system-panel.tsx index df264164..87366ea3 100644 --- a/packages/webui/src/components/settings/system-panel.tsx +++ b/packages/webui/src/components/settings/system-panel.tsx @@ -16,6 +16,7 @@ import { Select } from '@/components/ui/select'; import { Textarea } from '@/components/ui/textarea'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { useApi } from '@/lib/api'; +import { useFlashMessage } from '@/hooks/use-flash-message'; import { cn } from '@/lib/utils'; import type { SystemSettingsResponse } from '@/types'; @@ -24,7 +25,7 @@ export function SystemPanel() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); - const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null); + const { msg, flash, setMsg } = useFlashMessage(4000); // editable form state const [port, setPort] = useState(''); @@ -60,10 +61,6 @@ export function SystemPanel() { useEffect(() => { void load(); }, []); // eslint-disable-line react-hooks/exhaustive-deps const overridden = (field: string) => data?.envOverrides.includes(field) ?? false; - const flash = (kind: 'ok' | 'err', text: string) => { - setMsg({ kind, text }); - window.setTimeout(() => setMsg(null), 4000); - }; const saveSettings = async () => { const portNum = Number(port); diff --git a/packages/webui/src/hooks/use-flash-message.ts b/packages/webui/src/hooks/use-flash-message.ts new file mode 100644 index 00000000..48699c1f --- /dev/null +++ b/packages/webui/src/hooks/use-flash-message.ts @@ -0,0 +1,37 @@ +import { useEffect, useRef, useState } from 'react'; + +export type FlashKind = 'ok' | 'err'; +export interface FlashMessage { + kind: FlashKind; + text: string; +} + +/** + * Transient status-banner state for a settings panel. + * + * - `flash(kind, text)` shows a message that auto-clears after `durationMs`, + * cancelling any prior auto-clear timer first. + * - `setMsg` sets/clears the message directly (a persistent one that stays + * until the next action — e.g. a load-failure banner). + * - The pending timer is cleared on unmount. + * + * Replaces the per-panel hand-rolled `msg` state + `msgTimer` ref + `flash` + + * unmount cleanup, which had drifted (one panel omitted the ref, the + * prior-timer cancel, and the unmount cleanup entirely — leaking a timer that + * could fire a stale `setMsg` after unmount). + */ +export function useFlashMessage(durationMs = 2400): { + msg: FlashMessage | null; + flash: (kind: FlashKind, text: string) => void; + setMsg: (msg: FlashMessage | null) => void; +} { + const [msg, setMsg] = useState(null); + const timer = useRef(null); + useEffect(() => () => { if (timer.current) clearTimeout(timer.current); }, []); + const flash = (kind: FlashKind, text: string) => { + setMsg({ kind, text }); + if (timer.current) clearTimeout(timer.current); + timer.current = window.setTimeout(() => setMsg(null), durationMs); + }; + return { msg, flash, setMsg }; +}