From b1ae07ced54c4d7aa1a67355cf7390da754fc03f Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 12 Oct 2026 20:49:35 +0300 Subject: [PATCH] APP-3354: Peek at Notifications v2 on a quick return to the app, refresh on a significant one (#11944) Co-authored-by: Claude Opus 5.5 --- src/lib/appState.test.ts | 113 ++++++++++++++++++++++++++++ src/lib/appState.ts | 68 ++++++++++++++++- src/screens/Notifications/index.tsx | 34 +++++++-- 3 files changed, 204 insertions(+), 11 deletions(-) diff --git a/src/lib/appState.test.ts b/src/lib/appState.test.ts index 9d6694806..8f21920af 100644 --- a/src/lib/appState.test.ts +++ b/src/lib/appState.test.ts @@ -5,6 +5,7 @@ import type * as TestingLibrary from '@testing-library/react-native/pure' import type * as AppStateModule from '#/lib/appState' type AppReturn = AppStateModule.AppReturn +type AnyAppReturn = AppStateModule.AnyAppReturn type AppStateListener = (state: AppStateStatus) => void const mockListeners = new Set() @@ -193,6 +194,88 @@ describe('onAppReturnedFromBackground', () => { }) }) +describe('onAnyAppReturn', () => { + it('fires on every return from background, saying whether it was significant', () => { + const {onAnyAppReturn, RETURN_MIN_TIME_AWAY} = load() + const cb = jest.fn() + onAnyAppReturn(cb) + + emit('background', RETURN_MIN_TIME_AWAY - 1, 'active') + emit('background', RETURN_MIN_TIME_AWAY, 'active') + expect(cb.mock.calls).toEqual([ + [ + { + id: 1, + timestamp: RETURN_MIN_TIME_AWAY - 1, + timeAway: RETURN_MIN_TIME_AWAY - 1, + isSignificant: false, + }, + ], + [ + { + id: 2, + timestamp: 2 * RETURN_MIN_TIME_AWAY - 1, + timeAway: RETURN_MIN_TIME_AWAY, + isSignificant: true, + }, + ], + ]) + }) + + it('ignores a foreground launch and inactive-only interruptions', () => { + mockAppState.currentState = 'inactive' + const {onAnyAppReturn} = load() + const cb = jest.fn() + onAnyAppReturn(cb) + + emit(AWAY, 'active', 'inactive', AWAY, 'active') + expect(cb).not.toHaveBeenCalled() + }) + + it('measures from background even when inactive follows', () => { + const {onAnyAppReturn} = load() + const cb = jest.fn() + onAnyAppReturn(cb) + + emit('inactive', 'background', 1_000, 'inactive', 1_000, 'active') + expect(cb).toHaveBeenCalledWith({ + id: 1, + timestamp: 2_000, + timeAway: 2_000, + isSignificant: false, + }) + }) + + it('leaves the numbering of significant returns as it was', () => { + const {onAnyAppReturn, onAppReturnedFromBackground} = load() + const any: AnyAppReturn[] = [] + const significant: AppReturn[] = [] + onAnyAppReturn(r => any.push(r)) + onAppReturnedFromBackground(r => significant.push(r)) + expect(mockListeners.size).toBe(1) + + emit('background', AWAY, 'active') + emit('background', 1_000, 'active') + emit('background', AWAY, 'active') + expect(any.map(r => [r.id, r.isSignificant])).toEqual([ + [1, true], + [2, false], + [3, true], + ]) + expect(significant.map(r => r.id)).toEqual([1, 2]) + }) + + it('skips a listener removed during the same return', () => { + const {onAnyAppReturn} = load() + const second = jest.fn() + onAnyAppReturn(() => secondSub.remove()) + const secondSub = onAnyAppReturn(second) + + emit('background', 'active') + expect(second).not.toHaveBeenCalled() + }) +}) + describe('useOnAppReturnedFromBackground', () => { it('uses the latest callback and stops on unmount', () => { const {renderHook, useOnAppReturnedFromBackground} = load() @@ -237,3 +320,33 @@ describe('useOnAppReturnedFromBackground', () => { expect(second).toHaveBeenCalledTimes(1) }) }) + +describe('useOnAnyAppReturn', () => { + it('uses the latest callback in a memo() component, and stops on unmount', () => { + // React 19.2 never updates a useEffectEvent there (react/react#35187). + const {React, render, useOnAnyAppReturn} = load() + const Listener = React.memo(function Listener({ + cb, + }: { + cb: (appReturn: AnyAppReturn) => void + }) { + useOnAnyAppReturn(cb) + return null + }) + const first = jest.fn() + const second = jest.fn() + const {rerender, unmount} = render( + React.createElement(Listener, {cb: first}), + ) + + rerender(React.createElement(Listener, {cb: second})) + emit('background', 1_000, 'active') + + expect(first).not.toHaveBeenCalled() + expect(second).toHaveBeenCalledTimes(1) + + unmount() + emit('background', 1_000, 'active') + expect(second).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/lib/appState.ts b/src/lib/appState.ts index fa0f22ea6..06bf4b6b9 100644 --- a/src/lib/appState.ts +++ b/src/lib/appState.ts @@ -36,6 +36,23 @@ export type AppReturn = { readonly timestamp: number } +/** + * Any return from `background`, significant or not, shared by every listener. + */ +export type AnyAppReturn = { + /** Increases for each return from `background` during this JS runtime. */ + readonly id: number + /** `Date.now()` when the app became active. */ + readonly timestamp: number + /** How long the app was away, measured from the first `background`. */ + readonly timeAway: number + /** + * Whether it was away at least {@link RETURN_MIN_TIME_AWAY}, which makes it + * a significant return (see {@link onAppReturnedFromBackground}). + */ + readonly isSignificant: boolean +} + /** * Android backgrounds the app for its own activities (pickers, share sheets, * etc.), so it needs a longer cutoff than iOS to filter those trips. @@ -43,8 +60,10 @@ export type AppReturn = { export const RETURN_MIN_TIME_AWAY = IS_IOS ? 30 * 1000 : 60 * 1000 const returnListeners = new Set<(appReturn: AppReturn) => void>() +const anyReturnListeners = new Set<(appReturn: AnyAppReturn) => void>() let isTrackingReturns = false let lastReturnId = 0 +let lastAnyReturnId = 0 /** * Keep one tracker alive across subscriptions so a new listener cannot lose @@ -63,7 +82,18 @@ function trackReturns() { const now = Date.now() const timeAway = now - backgroundedAt backgroundedAt = undefined - if (timeAway < RETURN_MIN_TIME_AWAY) return + const isSignificant = timeAway >= RETURN_MIN_TIME_AWAY + const anyReturn: AnyAppReturn = { + id: ++lastAnyReturnId, + timestamp: now, + timeAway, + isSignificant, + } + for (const listener of [...anyReturnListeners]) { + // an earlier listener may have removed this one + if (anyReturnListeners.has(listener)) listener(anyReturn) + } + if (!isSignificant) return const appReturn: AppReturn = {id: ++lastReturnId, timestamp: now} for (const listener of [...returnListeners]) { // an earlier listener may have removed this one @@ -74,8 +104,9 @@ function trackReturns() { } /** - * Calls `cb` on the first `active` after at least {@link RETURN_MIN_TIME_AWAY} - * in `background`. An `inactive` interruption alone does not count. + * Calls `cb` on a significant return: the first `active` after at least + * {@link RETURN_MIN_TIME_AWAY} in `background`. An `inactive` interruption + * alone does not count. */ export function onAppReturnedFromBackground( cb: (appReturn: AppReturn) => void, @@ -105,3 +136,34 @@ export function useOnAppReturnedFromBackground( return () => sub.remove() }, [onReturn]) } + +/** + * Calls `cb` on every `active` after `background`, however long the app was + * away, saying whether it was a significant return (see + * {@link onAppReturnedFromBackground}). An `inactive` interruption alone does + * not count. + */ +export function onAnyAppReturn(cb: (appReturn: AnyAppReturn) => void) { + trackReturns() + // Give each subscription its own identity, even for the same callback. + const listener = (appReturn: AnyAppReturn) => cb(appReturn) + anyReturnListeners.add(listener) + return { + remove: () => { + anyReturnListeners.delete(listener) + }, + } +} + +/** + * Hook form of {@link onAnyAppReturn}, using the latest `cb`. Not with + * `useEffectEvent`, which React 19.2 never updates in `memo()` and + * `forwardRef()` components (react/react#35187). + */ +export function useOnAnyAppReturn(cb: (appReturn: AnyAppReturn) => void) { + const onReturn = useNonReactiveCallback(cb) + useEffect(() => { + const sub = onAnyAppReturn(appReturn => onReturn(appReturn)) + return () => sub.remove() + }, [onReturn]) +} diff --git a/src/screens/Notifications/index.tsx b/src/screens/Notifications/index.tsx index 2bdae3785..a54b1ec68 100644 --- a/src/screens/Notifications/index.tsx +++ b/src/screens/Notifications/index.tsx @@ -17,7 +17,7 @@ import { } from '@react-navigation/native' import {useQueryClient} from '@tanstack/react-query' -import {onAppReturnedFromBackground} from '#/lib/appState' +import {type AnyAppReturn, onAnyAppReturn} from '#/lib/appState' import { type NativeStackScreenProps, type NotificationsTabNavigatorParams, @@ -119,7 +119,11 @@ function NewNotificationsScreenInner() { void refresh(feed, mode) } } - useReturnToScreen({activeFeed, checkLatest}) + useReturnToScreen({ + activeFeed, + checkLatest, + peekLatest: () => void unreadApi.checkUnread(), + }) /* * With no notifications at all, every tab is empty, so the screen drops * the tabs and just shows the "All" tab's empty state. @@ -306,9 +310,11 @@ function useSessionSeenAt() { /** * Coming back to the screen: focusing it again, mounting it (web remounts - * the screen on every visit), or, while it's open, coming back to the app - * after a while in the background or opening a push. Each one checks the tab - * in view for anything new, with `checkLatest`. + * the screen on every visit), or, while it's open, a significant return to + * the app or opening a push. Each one checks the tab in view for anything + * new, with `checkLatest`. A quick hop out of the app and back, e.g. to a + * share sheet, only peeks with `peekLatest`, so the list stays as it is and + * the "Refresh" pill offers anything new. * * - From a screen pushed within the Notifications tab (e.g. a post), the * unread tint is kept as it was, and anything new is tinted too. @@ -318,9 +324,15 @@ function useSessionSeenAt() { function useReturnToScreen({ activeFeed, checkLatest, + peekLatest, }: { activeFeed: React.RefObject checkLatest: (feed: GroupedNotificationsFeed, mode: SeenAtMode) => void + /** + * Asks the server for anything new without loading it, which offers the + * "Refresh" pill if there is. + */ + peekLatest: () => void }) { const navigation = useNavigation() const isFocused = useIsFocused() @@ -358,16 +370,22 @@ function useReturnToScreen({ onReturn() } }) + const onAppReturn = useEffectEvent((appReturn: AnyAppReturn) => { + if (appReturn.isSignificant) { + onLeftApp() + } else if (isFocused) { + peekLatest() + } + }) useEffect(() => { - // Short trips away, e.g. to a share sheet or photo picker, don't count - const appReturn = onAppReturnedFromBackground(() => onLeftApp()) + const returns = onAnyAppReturn(appReturn => onAppReturn(appReturn)) /* * Opening a push leaves the app too, even when it never went to the * background, e.g. from Notification Center on iOS. */ const unlistenPush = listenPushNotificationOpened(() => onLeftApp()) return () => { - appReturn.remove() + returns.remove() unlistenPush() } }, []) -- knotmirror 0.0.1