Skip to content

[Performance] Migrate useDeferNonEssentials onto NavigationDeferredMount - #105496

Merged
mountiny merged 6 commits into
Expensify:mainfrom
software-mansion-labs:korytko/migrate-defer-non-essentials
Nov 2, 2026
Merged

mountiny merged 6 commits into
Expensify:mainfrom
software-mansion-labs:korytko/migrate-defer-non-essentials

Conversation

@JakubKorytko

@JakubKorytko JakubKorytko commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Explanation of Change

Moves ReportScreen's inline useDeferNonEssentials hook onto NavigationDeferredMount. The new ReportNonEssentialsDeferredMount reads the dismiss-and-open-report condition once at mount and wraps the composer, receipt panel and invisible handlers. TransitionTracker's own timeouts replace the hook's safety timeout. Removes the open-migration note from DEFER_AFTER_PAINT.md.

Fixed Issues

$ #88596
PROPOSAL: N/A

Tests

  1. Open the app on a wide screen and go to Inbox
  2. Open any chat from the list
  3. Click the green action button at the bottom left, then Create expense
  4. Pick the Manual tab, enter an amount and a merchant
  5. Tap the Report field and choose a different existing report than the default one
  6. Click Create expense
  7. Verify that the selected report opens with the new expense, and that the composer (Write something...) appears and accepts text
  8. Resize the window to a narrow (mobile) width, go to Inbox (stay on the chat list) and repeat steps 3-7

Offline tests

N/A

QA Steps

Same as tests

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified any copy / text that was added to the app is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS.mp4
iOS: mWeb Safari
MacOS: Chrome / Safari
Wide.mp4
Narrow.mp4

@JakubKorytko

Copy link
Copy Markdown
Member Author

@codex review

@JakubKorytko

Copy link
Copy Markdown
Member Author

@MelvinBot review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T13:18:03.017768Z 0eb5d1f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@MelvinBot

Copy link
Copy Markdown
Contributor

Review

The PR looks good, and I found no blocking issues. The migration keeps the old behavior: all four wrappers mount in the same commit as ReportScreen. That happens before the focus effect clears the pending action, so they agree on whether to defer. The worst-case wait drops from 3s to about 2s (MAX_TRANSITION_START_WAIT_MS plus MAX_TRANSITION_DURATION_MS). The tests cover the important cases. I didn't run them, and the CI test job is still pending.

I have two optional notes:

  1. Each wrapper reads the condition on its own. The old hook kept one flag in ReportScreen. Now each wrapper reads the condition when it mounts. If a subtree remounts between mount and focus, that wrapper starts a new wait of up to about 2s. For example, ReportNotFoundGuard remounts its children when it switches to ReportNotFoundInnerGuard. The window between mount and focus is tiny, so this is unlikely. Consider noting it in the component comment.
  2. The handlers now mount as a non-urgent update. NavigationDeferredMount wraps the swap in startTransition. So if the user interacts, React can push back mounting ReportFetchHandler and ReportNavigateAwayHandler, not just the composer. The old hook used a plain state update after one animation frame. This is probably fine, but please check that the report still loads promptly when you type right after it opens.
What I checked
  • The pending action is set in onBeforeNavigate before ReportScreen mounts. useSubmitToDestinationVisible clears it on focus, which runs after all four wrappers read it.
  • ReportNotFoundGuard and LinkedActionNotFoundGuard render their children in the same commit, so no wrapper mounts late.
  • The old useFocusEffect paused the wait on blur. The new useEffect doesn't. If the user leaves during the transition, the handlers mount while the screen is unfocused. That matches the non-deferred path, where they always mount.
  • The tests use the shared createTransitionTrackerHarness, and they cover the deferral path, the read-once-at-mount behavior, cancel on unmount, and the non-matching cases.

view run

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
...c/pages/inbox/ReportNonEssentialsDeferredMount.tsx 100.00% <100.00%> (ø)
src/pages/inbox/ReportScreen.tsx 96.49% <100.00%> (+0.06%) ⬆️
... and 118 files with indirect coverage changes

@JakubKorytko JakubKorytko changed the title [Performance] Migrate useDeferNonEssentials onto NavigationDeferredMount [Performance] Migrate useDeferNonEssentials onto NavigationDeferredMount Oct 30, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c8d6f7ef73

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@JakubKorytko

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5f0eaef0d2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@JakubKorytko

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 5f0eaef0d2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@JakubKorytko

Copy link
Copy Markdown
Member Author

@MelvinBot

  1. Leaving it as is. Hoisting the flag into ReportScreen would make a wrapper that remounts after the lift wait again for up to 1s, which seems worse than this rare case.
  2. Checked and the composer is still a placeholder in that window, so there is nothing to type into yet, and ReportActions already defers the same way in the Inbox flow. A side-by-side run against main on web showed no difference in submit-to-visible or time to composer.

Also, since your review the wait starts on report focus (Codex caught that on narrow the report mounts under the RHP before the dismiss starts) - please re-review.

@MelvinBot

Copy link
Copy Markdown
Contributor

Re-review

The focus change looks correct, and I found no blocking issues. Both of your answers make sense to me, and the focus gate also settles my first note.

Focus gate. ReportNonEssentialsDeferredMount.tsx:37-50 now shows the placeholder until the report first gains focus. Only then does it mount NavigationDeferredMount. On narrow, the report mounts under the RHP, so its wait now starts at the dismiss. This matches the old useFocusEffect timing. Either way, the wait covers the dismiss:

  • If the dismiss's transitionStart fires before the effect runs, activeNavigationCount > 0 and the callback waits for that transition to end.
  • If it fires after, the effect waits for that transition to start and then end.

On wide, the report is focused at mount, so the behavior is the same as before.

Note 1 (remounts) is resolved. On focus, useSubmitToDestinationVisible calls endSubmitFollowUpActionSpan, which clears the pending action. That gives two cases:

  • A wrapper that remounts before focus still reads the action. It then waits from focus, like its siblings.
  • A wrapper that remounts after focus reads nothing and mounts right away.

So no wrapper can start a second wait. That is better than hoisting the flag.

Note 2 (non-urgent mount). Agreed. The composer is still a placeholder in that window, so the side-by-side run settles it.

Blur after focus. I agree with your reply to Codex. Once the wait starts, a blur no longer cancels it, unlike the old hook. But the handlers then mount the same way they do on the non-deferred path, so it isn't a regression.

The new test covers the unfocused mount, the start of the wait on focus, and the latch on blur. All CI checks pass except Check independent approval. I didn't run the tests locally.


view run

@JakubKorytko
JakubKorytko marked this pull request as ready for review November 2, 2026 12:55
@JakubKorytko
JakubKorytko requested review from a team as code owners November 2, 2026 12:55
@melvin-bot
melvin-bot Bot requested review from mkhutornyi and removed request for a team November 2, 2026 12:55
@melvin-bot

melvin-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown

@mkhutornyi Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot requested review from trjExpensify and removed request for a team November 2, 2026 12:55
@mkhutornyi

mkhutornyi commented Nov 2, 2026 •

Copy link
Copy Markdown

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified any copy / text that was added to the app is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-11-02.at.2.14.07.PM.mov

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0eb5d1ffe0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@mkhutornyi mkhutornyi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB:

@melvin-bot
melvin-bot Bot requested review from mountiny and roryabraham November 2, 2026 14:18
@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 Web test steps verification results: ⚠️ partial

I found no regressions. Create expense opened the destination report with a working composer on wide and narrow layouts, and normal report navigation was unaffected. I couldn't test picking a different existing report, because this free test account hits the Collect-plan paywall. I also couldn't read the browser console.

Normal navigation with composer

Step Status
Wide: create expense into a different existing report, which opens with a working composerBlocked, not a PR failure. Both "Create report" and the expense's Report field opened the Collect-plan upgrade screen, so the test account had no second report to pick.
Report field blocked by Upgrade paywall
❌
Wide: create expense into the default report, which opens with the expense and a composerI submitted a $42.50 manual expense. The self-DM opened with the expense preview, and the "Write something..." composer accepted text.
Default report with expense and composer
✅
Wide: type into the composer right after the report opens, then sendThe text landed, and the message posted. The composer then reset.
✅
Wide: open the new expense previewThe expense details loaded with every field filled in, plus the receipt area. Nothing was blank or stuck.
Expense details view
✅
Narrow: create expense from a chat opened in Inbox, which opens with the expense and a working composerAt 390×844, I submitted a $15.75 expense. The report opened with the preview, and I sent a message from the composer.
Narrow report with expense and posted message
✅
Normal navigation: open an existing chat, where the composer and messages show at onceThe composer and message history appeared immediately, and typing worked.
Normal navigation with composer
✅
Browser console has no new errorsNot checked. The web test tool can't read the console. No error banners or stuck screens appeared.
❌
Code review: no regressions, with two minor differences from the old hook
  • No hard 3s cap. The old hook stopped waiting after 3s no matter what. NavigationDeferredMount.tsx:41-47 relies only on TransitionTracker. That means up to 1s for a transition to start, then a wait until no transition is active (TransitionTracker.ts:173-213). The normal worst case is about 2s. Only back-to-back transitions with no gap could hold the placeholder longer than the old cap.
  • Wrappers can lift separately. If ReportNotFoundGuard switches branches after focus, it remounts the lifecycle, receipt and footer wrappers. They re-read the pending action, which focus has already cleared (ReportNonEssentialsDeferredMount.tsx:53), so they mount before ReportFetchHandler. ReportLifecycleHandler doesn't fetch, so this only skips the deferral. No data goes missing.

These cases behave the same as before: focus never arriving, a route param changing on a reused screen, LinkedActionNotFoundGuard, setting state during render, and leftover references to useDeferNonEssentials.

@roryabraham

Copy link
Copy Markdown
Contributor

@MelvinBot are the Reassure test failures related to or caused by this PR?

@MelvinBot

Copy link
Copy Markdown
Contributor

No. The Reassure failure looks like a flaky test, not something this PR caused. Re-running validate-perf-tests should clear it.

Evidence
  • What failed: only one measurement, [ReportActionsList] should re-render when an unrelated transaction is written. Its render count went from 18.10 to 18.80, and the deviation allowed is 0 (run). A fractional baseline like 18.10 means the render count already varies between runs on main.
  • This PR's code is not in the test. tests/perf-test/ReportActionsListRowWrites.perf-test.tsx renders ReportActionsList directly. It never mounts ReportScreen, ReportNonEssentialsDeferredMount, or NavigationDeferredMount. No perf test references them. This PR only changes ReportScreen and the new wrapper.
  • The wrapper is a no-op outside a submit flow. It returns children unchanged unless a pending DISMISS_MODAL_AND_OPEN_REPORT action matches the report.
  • Other PRs fail the same tests today with similar fractional deviations (0.6 to 0.8):
  • This PR's earlier run (41456903995) failed on a different ReportActionsList measurement (report name changes). That also points to random noise, not a stable regression.

view run

@trjExpensify

Copy link
Copy Markdown
Contributor

PR doesn’t need product input as a performance refactor PR. Unassigning and unsubscribing myself.

@trjExpensify
trjExpensify removed their request for review November 2, 2026 18:24

@mountiny mountiny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me, thank you!

@mountiny
mountiny merged commit 7dc78ef into Expensify:main Nov 2, 2026
37 of 39 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@OSBotify OSBotify mentioned this pull request Nov 3, 2026
99 tasks done
@OSBotify

OSBotify commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.31-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 failure ❌
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update needed — this performance refactor changes internal rendering timing without adding a feature, changing user steps, or making existing article text incorrect.

@OSBotify

OSBotify commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.31-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@OSBotify

OSBotify commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/blimpich in version: 9.5.31-4 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants