From 5cee5a2047fcddefe5082dfb4475ce5e681b89d1 Mon Sep 17 00:00:00 2001 From: LucasDLTG Date: Wed, 22 Jul 2026 18:23:09 +0200 Subject: [PATCH] feat: avoid removing notification when mouse hover --- src/components/Notifications.tsx | 9 ++++++++- src/components/app/App.tsx | 4 ++-- src/hooks/useNotifications.tsx | 27 +++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/components/Notifications.tsx b/src/components/Notifications.tsx index ce62478..c82ee4f 100644 --- a/src/components/Notifications.tsx +++ b/src/components/Notifications.tsx @@ -3,15 +3,22 @@ import type { Notification } from '../types/types'; type Props = { notifications: Notification[]; onRemove: (id: number) => void; + onHover: (id: number, hovering: boolean) => void; }; -export default function Notifications({ notifications, onRemove }: Props) { +export default function Notifications({ + notifications, + onRemove, + onHover, +}: Props) { return (
{notifications.map((n) => (
onHover(n.id, true)} + onMouseLeave={() => onHover(n.id, false)} > {n.message} diff --git a/src/components/app/App.tsx b/src/components/app/App.tsx index ff6d2ac..e9ff995 100644 --- a/src/components/app/App.tsx +++ b/src/components/app/App.tsx @@ -16,7 +16,7 @@ function App() { const [dsps, setDsps] = useState([]); const [selected, setSelected] = useState(null); const [page, setPage] = useState('home'); - const { notifications, notify, removeNotification } = useNotifications(); + const { notifications, notify, removeNotification, hoverNotification } = useNotifications(); async function addDSP(dsp: Omit) { try { @@ -126,7 +126,7 @@ function App() { return (
- + ([]); + const hovered = useRef>(new Set()); const notify = useCallback( (message: string, type: NotificationType = 'error', dspName?: string) => { @@ -19,24 +20,42 @@ export function useNotifications() { }, ]); - setTimeout(() => { + const removeLater = () => { + if (hovered.current.has(id)) { + setTimeout(removeLater, 1000); + return; + } + setNotifications((prev) => prev.filter((n) => n.id !== id)); - }, 5000); + }; + + setTimeout(removeLater, 5000); }, [] ); const removeNotification = useCallback((id: number) => { - setNotifications((prev) => prev.map((n) => (n.id === id ? { ...n, removing: true } : n))); + setNotifications((prev) => + prev.map((n) => (n.id === id ? { ...n, removing: true } : n)) + ); setTimeout(() => { setNotifications((prev) => prev.filter((n) => n.id !== id)); }, 250); }, []); + const hoverNotification = useCallback((id: number, hovering: boolean) => { + if (hovering) { + hovered.current.add(id); + } else { + hovered.current.delete(id); + } + }, []); + return { notifications, notify, removeNotification, + hoverNotification, }; }