:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(notifications): auto-mark in-app notifications read after 3s in view (#67331)
Yasenf5baa6dd214f
diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationGroupRow.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationGroupRow.tsxindex d0716856ef84..21acb9cc0594 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationGroupRow.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationGroupRow.tsx@@ -7,6 +7,7 @@ import {NotificationRow,NotificationTitle,} from 'lib/components/NotificationsMenu/NotificationRow'+import { useAutoMarkRead } from 'lib/components/NotificationsMenu/useAutoMarkRead'import { dayjs } from 'lib/dayjs'import {@@ -54,6 +55,12 @@ export function NotificationGroupRow({const isExpanded = expandedGroupKeys.has(group.group_key)const isLoading = loadingGroupKeys.has(group.group_key)+ // Dwelling on a collapsed, unread group marks the whole group read. When expanded,+ // the individual child rows mark themselves read instead, so disarm here.+ const autoMarkRef = useAutoMarkRead(group.count > 1 && group.has_unread && !isExpanded, () =>+ toggleGroupRead(group)+ )+if (group.count === 1) {return <NotificationRow notification={group.representative} onNavigate={onNavigate} />}@@ -76,6 +83,7 @@ export function NotificationGroupRow({return (<div className="flex flex-col"><div+ ref={autoMarkRef}className={`flex items-start gap-2 p-2 rounded cursor-pointer transition-colors ${allRead ? 'hover:bg-fill-highlight-100' : 'bg-fill-highlight-50 hover:bg-fill-highlight-100'}`}diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsxindex 4f96016b1eb3..7d57e1a3b5f6 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx@@ -5,6 +5,7 @@ import { Tooltip } from '@posthog/lemon-ui'import { getNotificationDescriber } from 'lib/components/NotificationsMenu/notificationDescribers'import { getNotificationIcon } from 'lib/components/NotificationsMenu/notificationToasts'+import { useAutoMarkRead } from 'lib/components/NotificationsMenu/useAutoMarkRead'import { dayjs } from 'lib/dayjs'import { IconRadioButtonUnchecked } from 'lib/lemon-ui/icons'@@ -122,9 +123,11 @@ export function NotificationRow({notification: InAppNotificationonNavigate?: () => void}): JSX.Element {- const { navigateToNotification, toggleRead } = useActions(sidePanelNotificationsLogic)+ const { navigateToNotification, toggleRead, markAsRead } = useActions(sidePanelNotificationsLogic)const { projectNameForNotification, sourcePathForNotification } = useValues(sidePanelNotificationsLogic)+ const autoMarkRef = useAutoMarkRead(!notification.read, () => markAsRead(notification.id))+const otherProjectName = projectNameForNotification(notification)const describer = getNotificationDescriber(notification)const customBody = describer ? <describer.Component notification={notification} onNavigate={onNavigate} /> : null@@ -149,6 +152,7 @@ export function NotificationRow({return (<div+ ref={autoMarkRef}className={`flex items-start gap-2 p-2 rounded cursor-pointer transition-colors ${notification.read ? 'hover:bg-fill-highlight-100' : 'bg-fill-highlight-50 hover:bg-fill-highlight-100'}`}diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationsPanel.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationsPanel.tsxindex 30d14f98118a..6e2008e9fcb0 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationsPanel.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationsPanel.tsx@@ -1,6 +1,6 @@import { useActions, useValues } from 'kea'-import { IconNotification } from '@posthog/icons'+import { IconCheckCircle, IconNotification } from '@posthog/icons'import { LemonButton, LemonSkeleton } from '@posthog/lemon-ui'import { ScrollableShadows } from 'lib/components/ScrollableShadows/ScrollableShadows'@@ -51,16 +51,29 @@ export function NotificationsPanel(): JSX.Element {)}</button></div>- {loadedUnreadCount > 0 && (- <LemonButton size="xsmall" type="secondary" onClick={() => markAllAsRead()} className="ml-auto">- Mark all as read- </LemonButton>- )}</div>)+ // "Mark all as read" is a rare action, so tuck it into the panel's overflow (⋯) menu+ // rather than pinning a button in the header. Only surfaced when there's something to clear.+ const panelActions =+ loadedUnreadCount > 0+ ? [+ {+ 'data-attr': 'notifications-mark-all-read',+ onClick: () => markAllAsRead(),+ children: (+ <>+ <IconCheckCircle />+ Mark all as read+ </>+ ),+ },+ ]+ : undefined+return (- <PanelLayoutPanel searchField={header}>+ <PanelLayoutPanel searchField={header} panelActionsNewSceneLayout={panelActions}><ScrollableShadowsdirection="vertical"styledScrollbarsdiff --git a/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.test.ts b/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.test.tsnew file mode 100644index 000000000000..b4b191aee280--- /dev/null+++ b/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.test.ts@@ -0,0 +1,95 @@+import { act, renderHook } from '@testing-library/react'+import { useInView } from 'react-intersection-observer'++import { usePageVisibility } from 'lib/hooks/usePageVisibility'++import { AUTO_MARK_READ_DWELL_MS, useAutoMarkRead } from './useAutoMarkRead'++jest.mock('react-intersection-observer', () => ({ useInView: jest.fn() }))+jest.mock('lib/hooks/usePageVisibility', () => ({ usePageVisibility: jest.fn() }))++const mockUseInView = useInView as jest.Mock+const mockUsePageVisibility = usePageVisibility as jest.Mock++describe('useAutoMarkRead', () => {+ let inView: boolean+ let pageVisible: boolean++ beforeEach(() => {+ jest.useFakeTimers()+ inView = true+ pageVisible = true+ mockUseInView.mockImplementation(() => ({ ref: jest.fn(), inView }))+ mockUsePageVisibility.mockImplementation(() => ({ isVisible: pageVisible }))+ })++ afterEach(() => {+ jest.clearAllTimers()+ jest.useRealTimers()+ })++ it('fires once after an uninterrupted dwell', () => {+ const onDwell = jest.fn()+ renderHook(() => useAutoMarkRead(true, onDwell))++ expect(onDwell).not.toHaveBeenCalled()+ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS))+ expect(onDwell).toHaveBeenCalledTimes(1)++ // Staying visible past the dwell does not fire again.+ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS * 2))+ expect(onDwell).toHaveBeenCalledTimes(1)+ })++ it('does not fire if the item leaves the viewport before the dwell elapses', () => {+ const onDwell = jest.fn()+ const { rerender } = renderHook(() => useAutoMarkRead(true, onDwell))++ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS - 500))+ inView = false+ rerender()+ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS))++ expect(onDwell).not.toHaveBeenCalled()+ })++ it('restarts the dwell after the item re-enters the viewport', () => {+ const onDwell = jest.fn()+ const { rerender } = renderHook(() => useAutoMarkRead(true, onDwell))++ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS - 500))+ inView = false+ rerender()+ act(() => jest.advanceTimersByTime(1000))+ inView = true+ rerender()++ // Only a partial dwell so far — still nothing.+ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS - 500))+ expect(onDwell).not.toHaveBeenCalled()+ act(() => jest.advanceTimersByTime(500))+ expect(onDwell).toHaveBeenCalledTimes(1)+ })++ it('does not count time while the browser tab is hidden', () => {+ const onDwell = jest.fn()+ pageVisible = false+ const { rerender } = renderHook(() => useAutoMarkRead(true, onDwell))++ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS * 2))+ expect(onDwell).not.toHaveBeenCalled()++ pageVisible = true+ rerender()+ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS))+ expect(onDwell).toHaveBeenCalledTimes(1)+ })++ it('never fires for an inactive (already read) item', () => {+ const onDwell = jest.fn()+ renderHook(() => useAutoMarkRead(false, onDwell))++ act(() => jest.advanceTimersByTime(AUTO_MARK_READ_DWELL_MS * 2))+ expect(onDwell).not.toHaveBeenCalled()+ })+})diff --git a/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.ts b/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.tsnew file mode 100644index 000000000000..556f9ca982dc--- /dev/null+++ b/frontend/src/lib/components/NotificationsMenu/useAutoMarkRead.ts@@ -0,0 +1,53 @@+import { useEffect, useLayoutEffect, useRef } from 'react'+import { useInView } from 'react-intersection-observer'++import { usePageVisibility } from 'lib/hooks/usePageVisibility'++export const AUTO_MARK_READ_DWELL_MS = 3000++// The row must be at least half on-screen before the dwell timer starts, so items+// barely peeking past the scroll edges don't get marked read.+const VISIBILITY_THRESHOLD = 0.5++/**+ * Marks a notification (or collapsed group) read once it has stayed continuously+ * visible in the viewport for `dwellMs`. Scrolling it out of view — or the browser+ * tab becoming hidden — resets the timer, so the dwell must be uninterrupted.+ * `onDwell` fires at most once per mounted item.+ *+ * `active` gates the whole thing: pass the item's unread state so an already-read+ * item is never observed, and pass false to disarm (e.g. an expanded group whose+ * children mark themselves read individually).+ *+ * Returns the ref callback to attach to the element whose visibility should count.+ */+export function useAutoMarkRead(+ active: boolean,+ onDwell: () => void,+ dwellMs: number = AUTO_MARK_READ_DWELL_MS+): (node?: Element | null) => void {+ const { ref, inView } = useInView({ threshold: VISIBILITY_THRESHOLD, skip: !active })+ const { isVisible: pageVisible } = usePageVisibility()++ // Hold onDwell behind a ref so a non-memoized inline callback doesn't reset the+ // dwell timer on every parent re-render.+ const onDwellRef = useRef(onDwell)+ useLayoutEffect(() => {+ onDwellRef.current = onDwell+ })++ const firedRef = useRef(false)++ useEffect(() => {+ if (!active || firedRef.current || !inView || !pageVisible) {+ return+ }+ const timer = setTimeout(() => {+ firedRef.current = true+ onDwellRef.current()+ }, dwellMs)+ return () => clearTimeout(timer)+ }, [active, inView, pageVisible, dwellMs])++ return ref+}