Skip to content

[Payment due @ShridharGoel] Decompose distance and report step into hooks#89599

Merged
Julesssss merged 11 commits into
Expensify:mainfrom
callstack-internal:decompose-distance-and-report-steps
May 11, 2026
Merged

[Payment due @ShridharGoel] Decompose distance and report step into hooks#89599
Julesssss merged 11 commits into
Expensify:mainfrom
callstack-internal:decompose-distance-and-report-steps

Conversation

@OlimpiaZurek
Copy link
Copy Markdown
Contributor

@OlimpiaZurek OlimpiaZurek commented May 5, 2026

Explanation of Change

IOURequestStepDistance, IOURequestStepDistanceMap, IOURequestStepDistanceOdometer and IOURequestStepReport had each grown to 600–740 lines, mixing rendering with backup lifecycle, form state, validation, navigation and policy/billing/violation subscriptions. This PR extracts that logic into focused hooks and unblocks React Compiler on the distance screen.

New hooks under IOURequestStepDistance/hooks/ (shared across Distance, DistanceMap and Odometer):

  • useDistanceTransactionBackup — mount/unmount backup-and-restore lifecycle
  • useWaypointValidation — derived validation flags + isWaypointEmpty helper
  • useDistanceRequestData — mileage/amount/split-share derivation that primes the transaction before navigation
  • useDistanceNavigation — owns the navigation step and absorbs every Onyx subscription that exists only to feed it (lastSelectedDistanceRates, quickAction, recently-used currencies, draft transaction IDs, onboarding tour, concierge report ID, amount owed, billing grace periods, transaction violations)
  • useOdometerNavigation — sibling of useDistanceNavigation for the odometer flow
  • useOdometerTransactionBackup — image-cleanup backup lifecycle for editing an existing odometer expense
  • useOdometerImageHandlers — capture/view callbacks for the start and end odometer images
  • useOdometerReadingsState — local form state for the odometer editor (state + refs + tab-reset, mount-init, re-sync effects)

New hooks under IOURequestStepReport/hooks/:

  • useReportSelectionActions — owns POLICY_CATEGORIES / POLICY_TAGS / TRANSACTION subs and the three selection callbacks
  • usePerDiemPolicyData — owns the POLICY collection sub and derives perDiemOriginalPolicy
  • useCreateReportRestrictionCheck — owns the billing/grace-period subs and returns the create-report restriction check

Fixed Issues

#81849

Tests

Distance — manual entry

  1. Open any chat, tap the green + (FAB) → Track distance.
  2. On the Manual tab, enter any distance using the on-screen keypad (e.g. 10).
  3. Tap Next.
  4. On the confirmation screen, check the amount, distance and currency match the policy's mileage rate × the distance you entered.
  5. Tap the green Create … expense button.
  6. Confirm the new expense shows up on the correct report (Spend tab) with the right amount and distance.

Distance — map editor

  1. Open an existing distance expense that was created through the Map flow → tap the Distance row to open the map editor.
  2. Drag the waypoints to reorder them — the new order should stick once the route refreshes.
  3. Trigger each error case and check the matching message appears under the form:
    • Two waypoints with the same address → "Please remove duplicate waypoints".
    • Only one waypoint filled in → "Please enter at least two different addresses".
  4. Tap Back without saving → reopen the editor and confirm the original waypoints are restored.
  5. Turn airplane mode on while still in the editor, then turn it off → the map should fill in using the backup route once you're back online.

Distance — odometer

  1. FAB → Track distanceOdometer tab.
  2. Tap Odometer start photo, take a picture, then do the same for the end photo.
  3. Enter a start reading (e.g. 12345) and an end reading (e.g. 12450) → check that Total distance updates live to 105 mi.
  4. Switch to the Manual tab and back to Odometer → both fields and the total should reset, and the input labels should be back in their default position.
  5. Re-enter the readings, tap Next, and verify the confirmation amount equals rate × computed distance.
  6. From the confirmation screen, tap the Distance row → the editor reopens with the previous values. Change one reading, tap Save, and confirm the new amount on the confirmation screen.
  7. Reopen the editor, change a reading again, then tap Back. The "Discard changes?" modal should appear; tap Discard changes → the confirmation screen should still show the previous (saved) values, including any captured images.

Report selection (Move expense / Submit on report)

  1. Open any existing expense and tap the Report row → the workspace + report picker opens.
  2. Pick a different report on the same workspace → the expense should optimistically move to the new report and stay there once the request finishes.
  3. From the global create flow with a per-diem expense, pick a report on a different workspace → confirm the Destination step opens (subrates are cleared in the background).
  4. From the Unreported view, tap Move to reportCreate new report → check a new report appears in the chosen workspace.
  5. Pick a workspace that has an overdue invoice and try to create a report from it → the Restricted action page should open instead.
  • Verify that no errors appear in the JS console

Offline tests

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."

Same as tests

  • Verify that no errors appear in the JS console

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 followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • 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 a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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: mWeb Safari
MacOS: Chrome / Safari

@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

@OlimpiaZurek OlimpiaZurek changed the title decompose distance and report step into hooks Decompose distance and report step into hooks May 5, 2026
@OlimpiaZurek
Copy link
Copy Markdown
Contributor Author

@codex

@OlimpiaZurek OlimpiaZurek marked this pull request as ready for review May 5, 2026 12:48
@OlimpiaZurek OlimpiaZurek requested review from a team as code owners May 5, 2026 12:48
@melvin-bot melvin-bot Bot requested review from JmillsExpensify and youssef-lr and removed request for a team May 5, 2026 12:48
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented May 5, 2026

@youssef-lr 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 removed the request for review from a team May 5, 2026 12:49
const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES);
const [quickAction] = useOnyx(ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE);
const [policyRecentlyUsedCurrencies] = useOnyx(ONYXKEYS.RECENTLY_USED_CURRENCIES);
const [draftTransactionIDs] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {selector: validTransactionDraftIDsSelector});
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.

❌ CONSISTENCY-3 (docs)

The Onyx subscriptions in this hook (lines 127-137) are identical to those in useDistanceNavigation.ts (lines 151-161). Both hooks subscribe to the same 10 Onyx keys (NVP_LAST_SELECTED_DISTANCE_RATES, NVP_QUICK_ACTION_GLOBAL_CREATE, RECENTLY_USED_CURRENCIES, COLLECTION.TRANSACTION_DRAFT, NVP_ONBOARDING, CONCIERGE_REPORT_ID, NVP_PRIVATE_AMOUNT_OWED, etc.) and both pass them to handleMoneyRequestStepDistanceNavigation.

Consider extracting the shared Onyx subscriptions into a common hook (e.g., useDistanceNavigationOnyxData) that both useDistanceNavigation and useOdometerNavigation consume.


Reviewed at: cf9774d | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The two hooks subscribe to the same 10 keys, but those keys aren't related to each other — they only happen to share one caller. Bundling them into a shared hook would violate [CLEAN-REACT-PATTERNS-5]. There's no logic in the duplicated lines, so nothing can drift.
The real fix is upstream: handleMoneyRequestStepDistanceNavigation shouldn't require its callers to pre-fetch this data. Moving the 10 reads inside the function (the way getReportOrDraftReport already does) would let every caller drop 10 useOnyx calls and stop re-rendering the editor on unrelated writes. Out of scope for this PR.

@ShridharGoel
Copy link
Copy Markdown
Contributor

Should I review this?

Copy link
Copy Markdown
Contributor

@Julesssss Julesssss left a comment

Choose a reason for hiding this comment

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

FYI we just updated the Odometer feature which may affect this PR. Could you please add the test cases from that PR to ensure the changes are compatible. thank you!

@github-actions

This comment has been minimized.

@OlimpiaZurek OlimpiaZurek requested review from a team and stephanieelliott as code owners May 8, 2026 17:59
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

⚠️ This PR is possibly changing native code and/or updating libraries, it may cause problems with HybridApp. Please check if any patch updates are required in the HybridApp repo and run an AdHoc build to verify that HybridApp will not break. Ask Contributor Plus for help if you are not sure how to handle this. ⚠️

@OlimpiaZurek OlimpiaZurek force-pushed the decompose-distance-and-report-steps branch from 4c8fde3 to 6046229 Compare May 8, 2026 18:01
@mountiny
Copy link
Copy Markdown
Contributor

@ShridharGoel how is the testing going?

@github-actions
Copy link
Copy Markdown
Contributor

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

@melvin-bot melvin-bot Bot changed the title Decompose distance and report step into hooks [Payment due @ShridharGoel] Decompose distance and report step into hooks May 11, 2026
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented May 11, 2026

🎯 @ShridharGoel, thanks for reviewing and testing this PR! 🎉

A payment issue will be created for your review once this PR is deployed to production.
E/E issue linked to the PR - no internal issue.

If payment is not needed (e.g., regression PR review fix etc), react with 👎 to this comment to prevent the payment issue from being created.

@ShridharGoel
Copy link
Copy Markdown
Contributor

Was working fine 👍🏻

@stephanieelliott stephanieelliott removed their request for review May 11, 2026 20:54
@Julesssss Julesssss merged commit 5fda328 into Expensify:main May 11, 2026
40 checks passed
@OSBotify
Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions
Copy link
Copy Markdown
Contributor

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

@OSBotify
Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/Julesssss in version: 9.3.70-0 🚀

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

Bundle Size Analysis (Sentry):

@MelvinBot
Copy link
Copy Markdown
Contributor

No help site changes are required for this PR.

This is a pure internal code refactoring that extracts rendering/lifecycle logic from large components (IOURequestStepDistance, IOURequestStepDistanceMap, IOURequestStepDistanceOdometer, IOURequestStepReport) into focused React hooks. There are no user-facing changes — no new features, no UI modifications, no renamed labels or settings, and no behavioral differences.

@OSBotify
Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/Julesssss in version: 9.3.72-0 🚀

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

Bundle Size Analysis (Sentry):

@MelvinBot
Copy link
Copy Markdown
Contributor

I reviewed the changes in this PR. This is a pure internal refactoring that extracts existing logic from large components (IOURequestStepDistance, IOURequestStepDistanceMap, IOURequestStepDistanceOdometer, IOURequestStepReport) into focused React hooks. There are no changes to user-facing behavior, UI text, feature names, settings labels, or workflows.

No help site documentation changes are required.

@OSBotify
Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/lakchote in version: 9.3.72-1 🚀

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

@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented May 13, 2026

🤖 Payment issue created: #90417

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