From 29799adf0cb350ded7459c6d2f0733e87fc2241f Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Tue, 21 Jul 2026 15:16:13 +0200 Subject: [PATCH 1/7] add OnyxSubscribedBoundary --- package-lock.json | 8 +++--- package.json | 2 +- src/ONYXKEYS.ts | 4 +++ .../OnyxSubscribedDefaultContext.ts | 9 +++++++ .../OnyxSubscribedBoundary/index.tsx | 20 +++++++++++++++ .../useOnyxSubscribedDefault.ts | 22 ++++++++++++++++ src/components/TestToolMenu.tsx | 15 ++++++++++- src/hooks/useOnyx.ts | 9 ++++++- src/libs/actions/User.ts | 5 ++++ .../Search/SearchMoneyRequestReportPage.tsx | 25 +++++++++++++------ 10 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts create mode 100644 src/components/OnyxSubscribedBoundary/index.tsx create mode 100644 src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts diff --git a/package-lock.json b/package-lock.json index 08dc99e4f023..d24d726cff04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -122,7 +122,7 @@ "react-native-localize": "^3.5.4", "react-native-nitro-modules": "0.35.0", "react-native-nitro-sqlite": "9.6.0", - "react-native-onyx": "3.0.88", + "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", "react-native-pager-view": "8.0.0", "react-native-pdf": "7.0.2", "react-native-permissions": "^5.4.0", @@ -36017,9 +36017,9 @@ } }, "node_modules/react-native-onyx": { - "version": "3.0.88", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-3.0.88.tgz", - "integrity": "sha512-wCNHe+Kc6DX3yYVZxrITZdunu74P2x0XE7t8FGkpjEQdCRnlJhCTxn4NvxWZkURFxn5IRvIlVCGU8OdZhFNoOg==", + "version": "3.0.91", + "resolved": "git+ssh://git@github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", + "integrity": "sha512-WODJsYII28bU/mLYr0RkkwRzhPJ7skbzr/wxs+/3vv+h7sYVL7rHQbZoQbu1R/QwQJswrwokprmy1v0uQcHXDQ==", "license": "MIT", "dependencies": { "ascii-table": "0.0.9", diff --git a/package.json b/package.json index c9ccc5d8ee9a..2b8e52508476 100644 --- a/package.json +++ b/package.json @@ -196,7 +196,7 @@ "react-native-localize": "^3.5.4", "react-native-nitro-modules": "0.35.0", "react-native-nitro-sqlite": "9.6.0", - "react-native-onyx": "3.0.88", + "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", "react-native-pager-view": "8.0.0", "react-native-pdf": "7.0.2", "react-native-permissions": "^5.4.0", diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 539e63cfda91..fbe337ec9098 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -583,6 +583,9 @@ const ONYXKEYS = { /** List of span operations to highlight in Sentry debug logs */ SENTRY_DEBUG_HIGHLIGHTED_SPAN_OPS: 'sentryDebugHighlightedSpanOps', + /** Dev toggle: off (default) forces useOnyx `subscribed: true` everywhere; on lets it follow the enclosing OnyxSubscribedBoundary's focus */ + SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS: 'shouldOnyxSubscribedFollowFocus', + /** Stores new group chat draft */ NEW_GROUP_CHAT_DRAFT: 'newGroupChatDraft', @@ -1664,6 +1667,7 @@ type OnyxValuesMapping = { [ONYXKEYS.IS_SENTRY_DEBUG_ENABLED]: boolean; [ONYXKEYS.IS_SENTRY_SEND_ENABLED]: boolean; [ONYXKEYS.SENTRY_DEBUG_HIGHLIGHTED_SPAN_OPS]: string[]; + [ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS]: boolean; [ONYXKEYS.CACHED_PDF_PATHS]: Record; [ONYXKEYS.POLICY_OWNERSHIP_CHANGE_CHECKS]: Record; [ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE]: OnyxTypes.QuickAction; diff --git a/src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts b/src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts new file mode 100644 index 000000000000..17a199e8749c --- /dev/null +++ b/src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts @@ -0,0 +1,9 @@ +import {createContext} from 'react'; + +/** + * Subtree default for `useOnyx`'s `subscribed`, provided by `OnyxSubscribedBoundary`. `undefined` = no + * boundary above. Wrapper resolution: explicit option > this context > `true`. + */ +const OnyxSubscribedDefaultContext = createContext(undefined); + +export default OnyxSubscribedDefaultContext; diff --git a/src/components/OnyxSubscribedBoundary/index.tsx b/src/components/OnyxSubscribedBoundary/index.tsx new file mode 100644 index 000000000000..bb0b5a769bf0 --- /dev/null +++ b/src/components/OnyxSubscribedBoundary/index.tsx @@ -0,0 +1,20 @@ +import type ChildrenProps from '@src/types/utils/ChildrenProps'; + +import {useIsFocused} from '@react-navigation/core'; +import React from 'react'; + +import OnyxSubscribedDefaultContext from './OnyxSubscribedDefaultContext'; + +/** + * Makes navigation focus the default `subscribed` for every `useOnyx` in the subtree: while the screen + * is blurred, subscriptions stay cache-warm but stop re-rendering; the first render after refocus reads + * the latest value. An explicit `subscribed` option still wins — use `subscribed: true` for keys that + * drive effects while blurred. Must be inside a navigator screen (`useIsFocused` throws otherwise) and + * only covers descendants — wrap the whole screen component, not its children. + */ +function OnyxSubscribedBoundary({children}: ChildrenProps) { + const isFocused = useIsFocused(); + return {children}; +} + +export default OnyxSubscribedBoundary; diff --git a/src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts b/src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts new file mode 100644 index 000000000000..0ea6a89534e6 --- /dev/null +++ b/src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts @@ -0,0 +1,22 @@ +import ONYXKEYS from '@src/ONYXKEYS'; + +import {use} from 'react'; +// eslint-disable-next-line no-restricted-imports +import {useOnyx as originalUseOnyx} from 'react-native-onyx'; + +import OnyxSubscribedDefaultContext from './OnyxSubscribedDefaultContext'; + +/** + * The `subscribed` default from the nearest `OnyxSubscribedBoundary` (`true` when none, or while the + * Test Tools toggle is off — same gate as the `useOnyx` wrapper). For call sites that import `useOnyx` + * directly from `react-native-onyx` and bypass the wrapper: + * + * const [value] = originalUseOnyx(key, {subscribed: useOnyxSubscribedDefault()}); + */ +function useOnyxSubscribedDefault(): boolean { + const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); + const contextDefault = use(OnyxSubscribedDefaultContext); + return shouldSubscribedFollowFocus ? (contextDefault ?? true) : true; +} + +export default useOnyxSubscribedDefault; diff --git a/src/components/TestToolMenu.tsx b/src/components/TestToolMenu.tsx index 75751785105c..8ffae0f4ed53 100644 --- a/src/components/TestToolMenu.tsx +++ b/src/components/TestToolMenu.tsx @@ -9,7 +9,7 @@ import {useIsAgentAccount} from '@libs/SessionUtils'; import {setShouldFailAllRequests, setShouldForceOffline, setShouldSimulatePoorConnection} from '@userActions/Network'; import {expireSessionWithDelay, invalidateAuthToken, invalidateCredentials} from '@userActions/Session'; -import {setIsDebugModeEnabled, setShouldShowBranchNameInTitle, setShouldUseStagingServer} from '@userActions/User'; +import {setIsDebugModeEnabled, setShouldOnyxSubscribedFollowFocus, setShouldShowBranchNameInTitle, setShouldUseStagingServer} from '@userActions/User'; import CONFIG from '@src/CONFIG'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -31,6 +31,7 @@ function TestToolMenu() { const [shouldUseStagingServer = isUsingStagingApi()] = useOnyx(ONYXKEYS.SHOULD_USE_STAGING_SERVER); const [isDebugModeEnabled = false] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED); const [shouldShowBranchNameInTitle = false] = useOnyx(ONYXKEYS.SHOULD_SHOW_BRANCH_NAME_IN_TITLE); + const [shouldOnyxSubscribedFollowFocus = false] = useOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); const styles = useThemeStyles(); const {translate} = useLocalize(); const {clearLHNCache} = useSidebarOrderedReportsActions(); @@ -49,6 +50,18 @@ function TestToolMenu() { > {translate('initialSettingsPage.troubleshoot.testingPreferences')} + + {/* Off (default): useOnyx always live. On: `subscribed` follows OnyxSubscribedBoundary focus */} + + setShouldOnyxSubscribedFollowFocus(!shouldOnyxSubscribedFollowFocus)} + /> + {isAuthenticated && ( <> {/* When toggled the app will be put into debug mode. */} diff --git a/src/hooks/useOnyx.ts b/src/hooks/useOnyx.ts index ea04ec1a7d38..0ff765c62818 100644 --- a/src/hooks/useOnyx.ts +++ b/src/hooks/useOnyx.ts @@ -1,3 +1,4 @@ +import OnyxSubscribedDefaultContext from '@components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext'; import {SearchQueryContext, SearchResultsContext} from '@components/Search/SearchContext'; import {useIsOnSearch} from '@components/Search/SearchScopeProvider'; @@ -77,6 +78,11 @@ const useOnyx: OriginalUseOnyx = key.startsWith(snapshotKey)); const isOnSearch = useIsOnSearch(); + // Test Tools toggle. Off (default): `subscribed` forced `true` everywhere. On: explicit caller option > + // enclosing OnyxSubscribedBoundary default > `true`. Read via raw useOnyx to avoid recursion. + const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); + const subscribedContextDefault = use(OnyxSubscribedDefaultContext); + let currentSearchHash: number | undefined; let shouldUseLiveData = false; if (isOnSearch && isSnapshotCompatibleKey) { @@ -95,7 +101,8 @@ const useOnyx: OriginalUseOnyx = | undefined) => selectorProp(getKeyData(data as SearchResults, key)); - const onyxOptions: UseOnyxOptions> = {...optionsWithoutSelector, selector}; + const subscribed = shouldSubscribedFollowFocus ? (useOnyxOptions?.subscribed ?? subscribedContextDefault ?? true) : true; + const onyxOptions: UseOnyxOptions> = {...optionsWithoutSelector, selector, subscribed}; const snapshotKey = shouldUseSnapshot ? (`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchHash}` as OnyxKey) : key; const originalResult = originalUseOnyx(snapshotKey, onyxOptions, dependencies); diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 900ff4e9b835..29e819421474 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -1418,6 +1418,10 @@ function setShouldShowBranchNameInTitle(value: boolean) { Onyx.set(ONYXKEYS.SHOULD_SHOW_BRANCH_NAME_IN_TITLE, value); } +function setShouldOnyxSubscribedFollowFocus(value: boolean) { + Onyx.set(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS, value); +} + function lockAccount(currentUserAccountID: number, accountID: number | undefined, domainAccountID: number | undefined, domainName: string | undefined) { let domainOptimisticData: DomainOnyxUpdate[] = []; let domainFailureData: DomainOnyxUpdate[] = []; @@ -2040,6 +2044,7 @@ export { clearValidateCodeActionError, setIsDebugModeEnabled, setShouldShowBranchNameInTitle, + setShouldOnyxSubscribedFollowFocus, lockAccount, requestUnlockAccount, respondToProactiveAppReview, diff --git a/src/pages/Search/SearchMoneyRequestReportPage.tsx b/src/pages/Search/SearchMoneyRequestReportPage.tsx index 1a3b116a4f77..741b73d25f4d 100644 --- a/src/pages/Search/SearchMoneyRequestReportPage.tsx +++ b/src/pages/Search/SearchMoneyRequestReportPage.tsx @@ -1,6 +1,7 @@ import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; import DragAndDropProvider from '@components/DragAndDrop/Provider'; import MoneyRequestReportView from '@components/MoneyRequestReportView/MoneyRequestReportView'; +import OnyxSubscribedBoundary from '@components/OnyxSubscribedBoundary'; import ScreenWrapper from '@components/ScreenWrapper'; import {useSearchResultsContext} from '@components/Search/SearchContext'; import useRHPWidth from '@components/WideRHPContextProvider/useRHPWidth'; @@ -67,6 +68,11 @@ const defaultReportLoadingState = { hasOnceLoadedReportActions: false, }; +// Module-scoped for a stable array identity: an inline array would churn useSubmitToDestinationVisible's `tryEnd` +// useCallback and its `onLayout`, re-rendering the memoized report list on every parent render. +const SUBMIT_TO_DESTINATION_FOLLOW_UP_ACTIONS = [CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_AND_OPEN_REPORT, CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_ONLY] as const; +const SUBMIT_TO_DESTINATION_TRIGGERS = [CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.LAYOUT, CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.FOCUS] as const; + function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { const {shouldUseNarrowLayout} = useResponsiveLayout(); const styles = useThemeStyles(); @@ -76,15 +82,12 @@ function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportIDFromRoute}`); const [hasReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportIDFromRoute}`, {selector: Boolean}); - const [deleteTransactionNavigateBackUrl] = useOnyx(ONYXKEYS.NVP_DELETE_TRANSACTION_NAVIGATE_BACK_URL); + // Must stay live while blurred: the clearing effect below only acts while unfocused, so focus-gating would starve it. + const [deleteTransactionNavigateBackUrl] = useOnyx(ONYXKEYS.NVP_DELETE_TRANSACTION_NAVIGATE_BACK_URL, {subscribed: true}); const parentReportAction = useParentReportAction(report); - const handleSubmitToDestinationVisibleLayout = useSubmitToDestinationVisible( - [CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_AND_OPEN_REPORT, CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_ONLY], - reportIDFromRoute, - [CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.LAYOUT, CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.FOCUS], - ); + const handleSubmitToDestinationVisibleLayout = useSubmitToDestinationVisible(SUBMIT_TO_DESTINATION_FOLLOW_UP_ACTIONS, reportIDFromRoute, SUBMIT_TO_DESTINATION_TRIGGERS); const [parentReportLoadingState] = useOnyx(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${report?.parentReportID}`); const {email: currentUserEmail, accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); @@ -421,4 +424,12 @@ function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { ); } -export default SearchMoneyRequestReportPage; +function SearchMoneyRequestReportPageWithOnyxBoundary(props: SearchMoneyRequestPageProps) { + return ( + + + + ); +} + +export default SearchMoneyRequestReportPageWithOnyxBoundary; From 17add2f76e70b95835b6e3e2741afe1dcca4ddb0 Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Mon, 20 Jul 2026 15:02:18 +0200 Subject: [PATCH 2/7] rename to OnyxFocusBoundary --- src/ONYXKEYS.ts | 2 +- .../OnyxFocusDefaultContext.ts} | 6 +++--- .../index.tsx | 8 ++++---- .../useOnyxFocusDefault.ts} | 12 ++++++------ src/components/TestToolMenu.tsx | 2 +- src/hooks/useOnyx.ts | 6 +++--- src/pages/Search/SearchMoneyRequestReportPage.tsx | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) rename src/components/{OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts => OnyxFocusBoundary/OnyxFocusDefaultContext.ts} (53%) rename src/components/{OnyxSubscribedBoundary => OnyxFocusBoundary}/index.tsx (70%) rename src/components/{OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts => OnyxFocusBoundary/useOnyxFocusDefault.ts} (63%) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index fbe337ec9098..a5f3ffd3fad9 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -583,7 +583,7 @@ const ONYXKEYS = { /** List of span operations to highlight in Sentry debug logs */ SENTRY_DEBUG_HIGHLIGHTED_SPAN_OPS: 'sentryDebugHighlightedSpanOps', - /** Dev toggle: off (default) forces useOnyx `subscribed: true` everywhere; on lets it follow the enclosing OnyxSubscribedBoundary's focus */ + /** Dev toggle: off (default) forces useOnyx `subscribed: true` everywhere; on lets it follow the enclosing OnyxFocusBoundary's focus */ SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS: 'shouldOnyxSubscribedFollowFocus', /** Stores new group chat draft */ diff --git a/src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts b/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts similarity index 53% rename from src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts rename to src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts index 17a199e8749c..8a39a5992638 100644 --- a/src/components/OnyxSubscribedBoundary/OnyxSubscribedDefaultContext.ts +++ b/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts @@ -1,9 +1,9 @@ import {createContext} from 'react'; /** - * Subtree default for `useOnyx`'s `subscribed`, provided by `OnyxSubscribedBoundary`. `undefined` = no + * Subtree default for `useOnyx`'s `subscribed`, provided by `OnyxFocusBoundary`. `undefined` = no * boundary above. Wrapper resolution: explicit option > this context > `true`. */ -const OnyxSubscribedDefaultContext = createContext(undefined); +const OnyxFocusDefaultContext = createContext(undefined); -export default OnyxSubscribedDefaultContext; +export default OnyxFocusDefaultContext; diff --git a/src/components/OnyxSubscribedBoundary/index.tsx b/src/components/OnyxFocusBoundary/index.tsx similarity index 70% rename from src/components/OnyxSubscribedBoundary/index.tsx rename to src/components/OnyxFocusBoundary/index.tsx index bb0b5a769bf0..6d73a31f99ad 100644 --- a/src/components/OnyxSubscribedBoundary/index.tsx +++ b/src/components/OnyxFocusBoundary/index.tsx @@ -3,7 +3,7 @@ import type ChildrenProps from '@src/types/utils/ChildrenProps'; import {useIsFocused} from '@react-navigation/core'; import React from 'react'; -import OnyxSubscribedDefaultContext from './OnyxSubscribedDefaultContext'; +import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; /** * Makes navigation focus the default `subscribed` for every `useOnyx` in the subtree: while the screen @@ -12,9 +12,9 @@ import OnyxSubscribedDefaultContext from './OnyxSubscribedDefaultContext'; * drive effects while blurred. Must be inside a navigator screen (`useIsFocused` throws otherwise) and * only covers descendants — wrap the whole screen component, not its children. */ -function OnyxSubscribedBoundary({children}: ChildrenProps) { +function OnyxFocusBoundary({children}: ChildrenProps) { const isFocused = useIsFocused(); - return {children}; + return {children}; } -export default OnyxSubscribedBoundary; +export default OnyxFocusBoundary; diff --git a/src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts b/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts similarity index 63% rename from src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts rename to src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts index 0ea6a89534e6..01e7f669c019 100644 --- a/src/components/OnyxSubscribedBoundary/useOnyxSubscribedDefault.ts +++ b/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts @@ -4,19 +4,19 @@ import {use} from 'react'; // eslint-disable-next-line no-restricted-imports import {useOnyx as originalUseOnyx} from 'react-native-onyx'; -import OnyxSubscribedDefaultContext from './OnyxSubscribedDefaultContext'; +import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; /** - * The `subscribed` default from the nearest `OnyxSubscribedBoundary` (`true` when none, or while the + * The `subscribed` default from the nearest `OnyxFocusBoundary` (`true` when none, or while the * Test Tools toggle is off — same gate as the `useOnyx` wrapper). For call sites that import `useOnyx` * directly from `react-native-onyx` and bypass the wrapper: * - * const [value] = originalUseOnyx(key, {subscribed: useOnyxSubscribedDefault()}); + * const [value] = originalUseOnyx(key, {subscribed: useOnyxFocusDefault()}); */ -function useOnyxSubscribedDefault(): boolean { +function useOnyxFocusDefault(): boolean { const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); - const contextDefault = use(OnyxSubscribedDefaultContext); + const contextDefault = use(OnyxFocusDefaultContext); return shouldSubscribedFollowFocus ? (contextDefault ?? true) : true; } -export default useOnyxSubscribedDefault; +export default useOnyxFocusDefault; diff --git a/src/components/TestToolMenu.tsx b/src/components/TestToolMenu.tsx index 8ffae0f4ed53..0fba9bbd4bd8 100644 --- a/src/components/TestToolMenu.tsx +++ b/src/components/TestToolMenu.tsx @@ -51,7 +51,7 @@ function TestToolMenu() { {translate('initialSettingsPage.troubleshoot.testingPreferences')} - {/* Off (default): useOnyx always live. On: `subscribed` follows OnyxSubscribedBoundary focus */} + {/* Off (default): useOnyx always live. On: `subscribed` follows OnyxFocusBoundary focus */} - // enclosing OnyxSubscribedBoundary default > `true`. Read via raw useOnyx to avoid recursion. + // enclosing OnyxFocusBoundary default > `true`. Read via raw useOnyx to avoid recursion. const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); - const subscribedContextDefault = use(OnyxSubscribedDefaultContext); + const subscribedContextDefault = use(OnyxFocusDefaultContext); let currentSearchHash: number | undefined; let shouldUseLiveData = false; diff --git a/src/pages/Search/SearchMoneyRequestReportPage.tsx b/src/pages/Search/SearchMoneyRequestReportPage.tsx index 741b73d25f4d..ba2a14120dfa 100644 --- a/src/pages/Search/SearchMoneyRequestReportPage.tsx +++ b/src/pages/Search/SearchMoneyRequestReportPage.tsx @@ -1,7 +1,7 @@ import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; import DragAndDropProvider from '@components/DragAndDrop/Provider'; import MoneyRequestReportView from '@components/MoneyRequestReportView/MoneyRequestReportView'; -import OnyxSubscribedBoundary from '@components/OnyxSubscribedBoundary'; +import OnyxFocusBoundary from '@components/OnyxFocusBoundary'; import ScreenWrapper from '@components/ScreenWrapper'; import {useSearchResultsContext} from '@components/Search/SearchContext'; import useRHPWidth from '@components/WideRHPContextProvider/useRHPWidth'; @@ -426,9 +426,9 @@ function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { function SearchMoneyRequestReportPageWithOnyxBoundary(props: SearchMoneyRequestPageProps) { return ( - + - + ); } From b4b1c49fb498c28351f04ac9fb66c918d543d766 Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Tue, 21 Jul 2026 12:03:27 +0200 Subject: [PATCH 3/7] use useDeferredValue in OnyxFocusBoundary --- .../OnyxFocusDefaultContext.ts | 2 +- src/components/OnyxFocusBoundary/index.tsx | 25 +++++++++++++------ .../OnyxFocusBoundary/useOnyxFocusDefault.ts | 13 +++------- src/hooks/useOnyx.ts | 5 +--- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts b/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts index 8a39a5992638..f2d9b9ff665c 100644 --- a/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts +++ b/src/components/OnyxFocusBoundary/OnyxFocusDefaultContext.ts @@ -2,7 +2,7 @@ import {createContext} from 'react'; /** * Subtree default for `useOnyx`'s `subscribed`, provided by `OnyxFocusBoundary`. `undefined` = no - * boundary above. Wrapper resolution: explicit option > this context > `true`. + * boundary above. */ const OnyxFocusDefaultContext = createContext(undefined); diff --git a/src/components/OnyxFocusBoundary/index.tsx b/src/components/OnyxFocusBoundary/index.tsx index 6d73a31f99ad..90d5d5009a22 100644 --- a/src/components/OnyxFocusBoundary/index.tsx +++ b/src/components/OnyxFocusBoundary/index.tsx @@ -1,20 +1,31 @@ +import ONYXKEYS from '@src/ONYXKEYS'; import type ChildrenProps from '@src/types/utils/ChildrenProps'; import {useIsFocused} from '@react-navigation/core'; -import React from 'react'; +import React, {useDeferredValue} from 'react'; +// eslint-disable-next-line no-restricted-imports +import {useOnyx as originalUseOnyx} from 'react-native-onyx'; import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; /** - * Makes navigation focus the default `subscribed` for every `useOnyx` in the subtree: while the screen - * is blurred, subscriptions stay cache-warm but stop re-rendering; the first render after refocus reads - * the latest value. An explicit `subscribed` option still wins — use `subscribed: true` for keys that - * drive effects while blurred. Must be inside a navigator screen (`useIsFocused` throws otherwise) and - * only covers descendants — wrap the whole screen component, not its children. + * Makes navigation focus the default `subscribed` for every `useOnyx` in the subtree: blurred screens + * stay cache-warm but stop re-rendering. An explicit `subscribed: true` opts a key out (e.g. for + * effects that must run while blurred). Wrap the whole screen component — must be inside a navigator + * screen, and only descendants are covered. + * + * `isFocused && deferredIsFocused`: blur closes the gate urgently, refocus opens it in the deferred + * lane so the catch-up re-render yields to the navigation animation. */ function OnyxFocusBoundary({children}: ChildrenProps) { const isFocused = useIsFocused(); - return {children}; + const deferredIsFocused = useDeferredValue(isFocused); + // Temporary Test Tools toggle, read once per boundary; off = `undefined` = "no boundary". + const [shouldFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); + + const subscribedDefault = shouldFollowFocus ? isFocused && deferredIsFocused : undefined; + + return {children}; } export default OnyxFocusBoundary; diff --git a/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts b/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts index 01e7f669c019..21956395872a 100644 --- a/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts +++ b/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts @@ -1,22 +1,15 @@ -import ONYXKEYS from '@src/ONYXKEYS'; - import {use} from 'react'; -// eslint-disable-next-line no-restricted-imports -import {useOnyx as originalUseOnyx} from 'react-native-onyx'; import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; /** - * The `subscribed` default from the nearest `OnyxFocusBoundary` (`true` when none, or while the - * Test Tools toggle is off — same gate as the `useOnyx` wrapper). For call sites that import `useOnyx` - * directly from `react-native-onyx` and bypass the wrapper: + * The `subscribed` default from the nearest `OnyxFocusBoundary`. For call sites that import + * `useOnyx` directly from `react-native-onyx` and bypass the wrapper: * * const [value] = originalUseOnyx(key, {subscribed: useOnyxFocusDefault()}); */ function useOnyxFocusDefault(): boolean { - const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); - const contextDefault = use(OnyxFocusDefaultContext); - return shouldSubscribedFollowFocus ? (contextDefault ?? true) : true; + return use(OnyxFocusDefaultContext) ?? true; } export default useOnyxFocusDefault; diff --git a/src/hooks/useOnyx.ts b/src/hooks/useOnyx.ts index ad24a437868e..7a9f80caaf09 100644 --- a/src/hooks/useOnyx.ts +++ b/src/hooks/useOnyx.ts @@ -78,9 +78,6 @@ const useOnyx: OriginalUseOnyx = key.startsWith(snapshotKey)); const isOnSearch = useIsOnSearch(); - // Test Tools toggle. Off (default): `subscribed` forced `true` everywhere. On: explicit caller option > - // enclosing OnyxFocusBoundary default > `true`. Read via raw useOnyx to avoid recursion. - const [shouldSubscribedFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); const subscribedContextDefault = use(OnyxFocusDefaultContext); let currentSearchHash: number | undefined; @@ -101,7 +98,7 @@ const useOnyx: OriginalUseOnyx = | undefined) => selectorProp(getKeyData(data as SearchResults, key)); - const subscribed = shouldSubscribedFollowFocus ? (useOnyxOptions?.subscribed ?? subscribedContextDefault ?? true) : true; + const subscribed = useOnyxOptions?.subscribed ?? subscribedContextDefault ?? true; const onyxOptions: UseOnyxOptions> = {...optionsWithoutSelector, selector, subscribed}; const snapshotKey = shouldUseSnapshot ? (`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchHash}` as OnyxKey) : key; From ee97c6f6a9731c261b562e548d4c64f3a35ac2eb Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Tue, 21 Jul 2026 15:25:54 +0200 Subject: [PATCH 4/7] update linked onyx commit --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index d24d726cff04..a50a3a30981b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -122,7 +122,7 @@ "react-native-localize": "^3.5.4", "react-native-nitro-modules": "0.35.0", "react-native-nitro-sqlite": "9.6.0", - "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", + "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#a5f78e58fb830925e7c2f1b52ce4ce15b2872ccf", "react-native-pager-view": "8.0.0", "react-native-pdf": "7.0.2", "react-native-permissions": "^5.4.0", @@ -36018,8 +36018,8 @@ }, "node_modules/react-native-onyx": { "version": "3.0.91", - "resolved": "git+ssh://git@github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", - "integrity": "sha512-WODJsYII28bU/mLYr0RkkwRzhPJ7skbzr/wxs+/3vv+h7sYVL7rHQbZoQbu1R/QwQJswrwokprmy1v0uQcHXDQ==", + "resolved": "git+ssh://git@github.com/callstack-internal/react-native-onyx.git#a5f78e58fb830925e7c2f1b52ce4ce15b2872ccf", + "integrity": "sha512-ch7rgOEgtE+oIB12ZZHAsEsrk3lmCSQlaa81DawvfcsFKR9QRQrXLkSAxzxuXLH7Tta7ufMnHZs8QZ2FLte0rQ==", "license": "MIT", "dependencies": { "ascii-table": "0.0.9", diff --git a/package.json b/package.json index 2b8e52508476..6a438568b110 100644 --- a/package.json +++ b/package.json @@ -196,7 +196,7 @@ "react-native-localize": "^3.5.4", "react-native-nitro-modules": "0.35.0", "react-native-nitro-sqlite": "9.6.0", - "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#578edade372c71a256fc6505070027d8effa73bb", + "react-native-onyx": "git+https://github.com/callstack-internal/react-native-onyx.git#a5f78e58fb830925e7c2f1b52ce4ce15b2872ccf", "react-native-pager-view": "8.0.0", "react-native-pdf": "7.0.2", "react-native-permissions": "^5.4.0", From a3078fbd29c4237cb40a4afd16cc1e648d878e06 Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Tue, 21 Jul 2026 18:08:08 +0200 Subject: [PATCH 5/7] split OnyxFocusBoundary per platform --- .../OnyxFocusBoundary/index.native.tsx | 27 +++++++++++++++++++ src/components/OnyxFocusBoundary/index.tsx | 5 ++-- .../OnyxFocusBoundary/useOnyxFocusDefault.ts | 15 ----------- 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 src/components/OnyxFocusBoundary/index.native.tsx delete mode 100644 src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts diff --git a/src/components/OnyxFocusBoundary/index.native.tsx b/src/components/OnyxFocusBoundary/index.native.tsx new file mode 100644 index 000000000000..37114cd85588 --- /dev/null +++ b/src/components/OnyxFocusBoundary/index.native.tsx @@ -0,0 +1,27 @@ +import ONYXKEYS from '@src/ONYXKEYS'; +import type ChildrenProps from '@src/types/utils/ChildrenProps'; + +import {useIsFocused} from '@react-navigation/core'; +import React, {useDeferredValue} from 'react'; +// eslint-disable-next-line no-restricted-imports +import {useOnyx as originalUseOnyx} from 'react-native-onyx'; + +import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; + +/** + * Native variant — see index.tsx for the full contract. Blur closes in the deferred lane too: + * at press time the JS thread is busy mounting the next screen and background writes land after + * the transition, so an urgent close costs render time without muting anything. + */ +function OnyxFocusBoundary({children}: ChildrenProps) { + const isFocused = useIsFocused(); + const deferredIsFocused = useDeferredValue(isFocused); + // Temporary Test Tools toggle, read once per boundary; off = `undefined` = "no boundary". + const [shouldFollowFocus] = originalUseOnyx(ONYXKEYS.SHOULD_ONYX_SUBSCRIBED_FOLLOW_FOCUS); + + const subscribedDefault = shouldFollowFocus ? deferredIsFocused : undefined; + + return {children}; +} + +export default OnyxFocusBoundary; diff --git a/src/components/OnyxFocusBoundary/index.tsx b/src/components/OnyxFocusBoundary/index.tsx index 90d5d5009a22..eb2fd9743fdc 100644 --- a/src/components/OnyxFocusBoundary/index.tsx +++ b/src/components/OnyxFocusBoundary/index.tsx @@ -14,8 +14,9 @@ import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; * effects that must run while blurred). Wrap the whole screen component — must be inside a navigator * screen, and only descendants are covered. * - * `isFocused && deferredIsFocused`: blur closes the gate urgently, refocus opens it in the deferred - * lane so the catch-up re-render yields to the navigation animation. + * Web variant: blur closes the gate urgently (`isFocused &&`) because Onyx writes land inside the + * navigation span and must be muted right away; refocus opens it in the deferred lane so the + * catch-up re-render yields to the navigation animation. Native (index.native.tsx) defers both. */ function OnyxFocusBoundary({children}: ChildrenProps) { const isFocused = useIsFocused(); diff --git a/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts b/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts deleted file mode 100644 index 21956395872a..000000000000 --- a/src/components/OnyxFocusBoundary/useOnyxFocusDefault.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {use} from 'react'; - -import OnyxFocusDefaultContext from './OnyxFocusDefaultContext'; - -/** - * The `subscribed` default from the nearest `OnyxFocusBoundary`. For call sites that import - * `useOnyx` directly from `react-native-onyx` and bypass the wrapper: - * - * const [value] = originalUseOnyx(key, {subscribed: useOnyxFocusDefault()}); - */ -function useOnyxFocusDefault(): boolean { - return use(OnyxFocusDefaultContext) ?? true; -} - -export default useOnyxFocusDefault; From 666da8f6c8c5ea15066667f0b25c83e37ea66369 Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Wed, 22 Jul 2026 10:22:54 +0200 Subject: [PATCH 6/7] remove unrelated changes to SearchMoneyRequestReportPage --- src/pages/Search/SearchMoneyRequestReportPage.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/pages/Search/SearchMoneyRequestReportPage.tsx b/src/pages/Search/SearchMoneyRequestReportPage.tsx index ba2a14120dfa..822888f89248 100644 --- a/src/pages/Search/SearchMoneyRequestReportPage.tsx +++ b/src/pages/Search/SearchMoneyRequestReportPage.tsx @@ -68,11 +68,6 @@ const defaultReportLoadingState = { hasOnceLoadedReportActions: false, }; -// Module-scoped for a stable array identity: an inline array would churn useSubmitToDestinationVisible's `tryEnd` -// useCallback and its `onLayout`, re-rendering the memoized report list on every parent render. -const SUBMIT_TO_DESTINATION_FOLLOW_UP_ACTIONS = [CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_AND_OPEN_REPORT, CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_ONLY] as const; -const SUBMIT_TO_DESTINATION_TRIGGERS = [CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.LAYOUT, CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.FOCUS] as const; - function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { const {shouldUseNarrowLayout} = useResponsiveLayout(); const styles = useThemeStyles(); @@ -87,7 +82,11 @@ function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) { const parentReportAction = useParentReportAction(report); - const handleSubmitToDestinationVisibleLayout = useSubmitToDestinationVisible(SUBMIT_TO_DESTINATION_FOLLOW_UP_ACTIONS, reportIDFromRoute, SUBMIT_TO_DESTINATION_TRIGGERS); + const handleSubmitToDestinationVisibleLayout = useSubmitToDestinationVisible( + [CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_AND_OPEN_REPORT, CONST.TELEMETRY.SUBMIT_FOLLOW_UP_ACTION.DISMISS_MODAL_ONLY], + reportIDFromRoute, + [CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.LAYOUT, CONST.TELEMETRY.SUBMIT_TO_DESTINATION_VISIBLE_TRIGGER.FOCUS], + ); const [parentReportLoadingState] = useOnyx(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${report?.parentReportID}`); const {email: currentUserEmail, accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); From fcd24bec6ed876850a01949cfcf06522a57a0090 Mon Sep 17 00:00:00 2001 From: Lukasz Modzelewski Date: Wed, 22 Jul 2026 12:38:56 +0200 Subject: [PATCH 7/7] fix potential read/unread issues with reportActionsList wrapped with OnyxFocusBoundary --- .../MoneyRequestReportActionsList.tsx | 20 +++++++++++-- src/pages/inbox/report/useFocusCatchUpTime.ts | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/pages/inbox/report/useFocusCatchUpTime.ts diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx index 1c9778815139..3aa253e09b53 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx @@ -55,6 +55,7 @@ import getInitialNumToRender from '@pages/inbox/report/getInitialNumReportAction import ReportActionIndexContext from '@pages/inbox/report/ReportActionIndexContext'; import ReportActionsListItemRenderer from '@pages/inbox/report/ReportActionsListItemRenderer'; import {getUnreadMarkerReportAction} from '@pages/inbox/report/shouldDisplayNewMarkerOnReportAction'; +import useFocusCatchUpTime from '@pages/inbox/report/useFocusCatchUpTime'; import useReportUnreadMessageScrollTracking from '@pages/inbox/report/useReportUnreadMessageScrollTracking'; import variables from '@styles/variables'; @@ -217,6 +218,10 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) const lastVisibleActionCreated = getReportLastVisibleActionCreated(report, transactionThreadReport); const hasNewestReportAction = lastAction?.created === lastVisibleActionCreated; const userActiveSince = useRef(DateUtils.getDBTime()); + // Actions created at or before this time arrived while the screen was blurred (focus-gated + // catch-up), so the unread-marker computation and mark-as-read effect must treat them as a + // refocus (baseline's focus-keyed path), not as watched-live arrivals + const focusedSince = useFocusCatchUpTime(); const reportActionIDs = useMemo(() => { return reportActions?.map((action) => action.reportActionID) ?? []; @@ -360,6 +365,12 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) return; } + // Catch-up delivery: these actions arrived while blurred, so this run replicates what the + // focus-keyed mark-as-read effect below does at refocus on a live subscription — read only + // when watching at the bottom, and never arm `readActionSkipped` (a live subscription's + // blurred run returns before arming it, so scrolling down later must not auto-read). + const isCatchUp = !!focusedSince && !!lastAction && lastAction.created <= focusedSince; + if (isUnread(report, transactionThreadReport, isReportArchived) || (lastAction && isCurrentActionUnread(report, lastAction, visibleReportActions))) { // On desktop, when the notification center is displayed, isVisible will return false. // Currently, there's no programmatic way to dismiss the notification center panel. @@ -373,7 +384,7 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) if (isFromNotification) { Navigation.setParams({referrer: undefined}); } - } else { + } else if (!isCatchUp) { readActionSkipped.current = true; } } @@ -426,6 +437,11 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) return lastIndex > -1 ? lastIndex : undefined; }, [getLocalDateFromDatetime, isOffline, lastOfflineAt, lastOnlineAt, reportActions, currentUserAccountID]); + // Catch-up deliveries must not count as "received live with window focus" — otherwise the + // isNewMessage heuristic suppresses the unread marker and the push-down effect below advances + // unreadMarkerTime past them, swallowing the marker permanently. + const isCatchUpAfterRefocus = !!focusedSince && !!lastAction && lastAction.created <= focusedSince; + /** * The reportActionID the unread marker should display above */ @@ -438,7 +454,7 @@ function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) isScrolledOverThreshold: scrollingVerticalBottomOffset.current >= CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD, isOffline, isReversed: true, - hasWindowFocus: Visibility.hasFocus(), + hasWindowFocus: Visibility.hasFocus() && !isCatchUpAfterRefocus, }); const {isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible, trackVerticalScrolling, onViewableItemsChanged} = useReportUnreadMessageScrollTracking({ diff --git a/src/pages/inbox/report/useFocusCatchUpTime.ts b/src/pages/inbox/report/useFocusCatchUpTime.ts new file mode 100644 index 000000000000..cb63a999b958 --- /dev/null +++ b/src/pages/inbox/report/useFocusCatchUpTime.ts @@ -0,0 +1,29 @@ +import OnyxFocusDefaultContext from '@components/OnyxFocusBoundary/OnyxFocusDefaultContext'; + +import DateUtils from '@libs/DateUtils'; + +import {useIsFocused} from '@react-navigation/native'; +import {use, useState} from 'react'; + +/** + * DB timestamp of this screen's last blur -> focus transition. On focus-gated screens + * (`OnyxFocusBoundary`), blurred-period Onyx changes are delivered in one catch-up render after + * refocus, so actions with `created` at or before this time arrived while the user was not watching. + * Returns `null` when the screen is not focus-gated or has not been blurred yet, so guards built on + * it stay inert. Never stamps on mount. + */ +function useFocusCatchUpTime(): string | null { + const isFocusGated = use(OnyxFocusDefaultContext) !== undefined; + const isFocused = useIsFocused(); + + // State adjusted during render (not a ref) so callers can use the value in the render phase. + // Skipped entirely when ungated, so those screens pay no render-phase setState re-run. + const [focusState, setFocusState] = useState<{isFocused: boolean; focusedSince: string | null}>({isFocused, focusedSince: null}); + if (isFocusGated && focusState.isFocused !== isFocused) { + setFocusState({isFocused, focusedSince: isFocused ? DateUtils.getDBTime() : focusState.focusedSince}); + } + + return isFocusGated ? focusState.focusedSince : null; +} + +export default useFocusCatchUpTime;