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 }; +}