refactor(webui): extract useFlashMessage hook for settings panels

三个 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 回归。
This commit is contained in:
DrSmoothl
2026-07-01 20:03:58 +08:00
parent a8f24b8f13
commit c822c5dab6
4 changed files with 43 additions and 23 deletions
@@ -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<GlobalSettings | null>(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<number | null>(null);
const msgTimer = useRef<number | null>(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. */
@@ -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<DialogState>({ open: false, index: null, seed: blankChannel([]) });
const [testing, setTesting] = useState<string | null>(null);
const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null);
const { msg, flash, setMsg } = useFlashMessage();
const saveTimer = useRef<number | null>(null);
const msgTimer = useRef<number | null>(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) => {
@@ -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<SystemSettingsResponse | null>(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);
@@ -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<FlashMessage | null>(null);
const timer = useRef<number | null>(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 };
}