Skip to content

Fix commuter distance flicker after rate changes - #98371

Open
fedirjh wants to merge 9 commits into
Expensify:mainfrom
fedirjh:fix/98231-commuter-rate-flicker
Open

Fix commuter distance flicker after rate changes#98371
fedirjh wants to merge 9 commits into
Expensify:mainfrom
fedirjh:fix/98231-commuter-rate-flicker

Conversation

@fedirjh

@fedirjh fedirjh commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

When a workspace distance unit changed, the optimistic rate update converted the transaction's already rounded quantity into the new unit. For example, an exact 10 km route stored as 6.21 miles became 9.99 km optimistically, then changed to 10 km when the server response arrived. This caused the Original and Removed commuter distances to flicker between different values.

This change derives the converted quantity from the transaction's exact routeDistanceMeters value when available. Older transactions without that field retain the existing quantity-conversion fallback. The commuter exclusion, amount, and merchant are therefore calculated from the same exact route distance as the server.

Fixed Issues

$ #98231
PROPOSAL:

Tests

  1. Enable distance rates in a workspace and create a second distance rate.
  2. Enable "Exclude a fixed distance per claim" in the workspace distance settings.
  3. Create a map distance expense.
  4. Change the workspace distance unit.
  5. Open the expense and select another distance rate.
  6. Verify the Original and Removed commuter distances immediately show the correct converted values without flickering.
  7. Verify the final values remain unchanged after the server response arrives.

Automated validation:

  • bun run test -- --runInBand --watchman=false tests/unit/TransactionUtilsTest.ts

  • bun x eslint src/libs/TransactionUtils/index.ts tests/unit/TransactionUtilsTest.ts

  • bun run typecheck-tsgo reports only the two existing unrelated baseline errors.

  • Verify that no errors appear in the JS console

Offline tests

  1. Complete steps 1–4 above, then go offline.
  2. Open the expense and select another distance rate.
  3. Verify the Original and Removed commuter distances immediately show the correct converted values without flickering.
  4. Reconnect and verify the values remain unchanged after synchronization.

QA Steps

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 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 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)
  • 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)
  • 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.
  • 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

CleanShot.2026-08-11.at.22.29.32.mp4
Android: Native

Not tested yet.

Android: mWeb Chrome

Not tested yet.

iOS: Native

Not tested yet.

iOS: mWeb Safari

Not tested yet.

MacOS: Chrome / Safari

Not tested yet.

@fedirjh
fedirjh marked this pull request as ready for review August 11, 2026 21:31
@fedirjh
fedirjh requested review from a team as code owners August 11, 2026 21:31
@melvin-bot
melvin-bot Bot requested review from garrettmknight and marufsharifi and removed request for a team and garrettmknight August 11, 2026 21:31
@melvin-bot

melvin-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

@marufsharifi 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 a team and Gonals August 11, 2026 21:31
@melvin-bot

melvin-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

@Gonals 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 August 11, 2026 21:31
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Coverage Δ
src/libs/TransactionUtils/index.ts 91.85% <100.00%> (+0.70%) ⬆️
... and 10 files with indirect coverage changes

@marufsharifi

marufsharifi commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

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 proper code patterns were followed (see Reviewing the code)
    • 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 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)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • 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 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 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.
  • 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-08-12.at.2.35.13.AM.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: b65e71d19f

ℹ️ 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".

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/libs/TransactionUtils/index.ts Outdated
@marufsharifi

Copy link
Copy Markdown
Contributor

@fedirjh, could you please check the failed actions. thanks.

@fedirjh

fedirjh commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@marufsharifi This is ready.

Comment thread src/libs/TransactionUtils/index.ts
Comment thread src/libs/TransactionUtils/index.ts Outdated
Comment thread tests/unit/TransactionUtilsTest.ts
@marufsharifi

Copy link
Copy Markdown
Contributor

@MelvinBot Review

Co-authored-by: Maruf Sharifi <maruf.sharifi.work@gmail.com>
@MelvinBot

Copy link
Copy Markdown
Contributor

Review

The logic change is sound — deriving the converted quantity from the exact routeDistanceMeters (with a rounding-tolerance guard for manual overrides) correctly eliminates the double-rounding flicker. But there's one blocking bug that's currently failing CI.

🔴 Dangling duplicate expression breaks typecheck (and ESLint/Oxfmt)

src/libs/TransactionUtils/index.ts:762-763

            Math.abs(quantity - DistanceRequestUtils.convertDistanceUnit(routeDistanceMeters, existingDistanceUnit)) > ROUNDING_TOLERANCE;
            Math.abs(quantity - DistanceRequestUtils.convertDistanceUnit(routeDistanceMeters, existingDistanceUnit)) > 0.01;   // ← leftover

The last commit (Apply suggestions from code review) extracted 0.01 into the ROUNDING_TOLERANCE constant but left the original line behind. Line 763 is now a standalone no-op expression statement (its result is discarded), and it lives outside the typeof narrowing that guards line 762 — so quantity/routeDistanceMeters are number | undefined there.

That single line is the cause of all three failing checks:

  • typecheck ❌ — TS18049: 'quantity' is possibly 'null' or 'undefined' and TS2345 at index.ts(763,...)
  • ESLint (pending) — will fail @typescript-eslint/no-unused-expressions
  • Oxfmt ❌ — diff on this file

Fix: delete line 763, then run npm run fmt:

        const hasManualDistanceOverride =
            typeof routeDistanceMeters === 'number' &&
            typeof quantity === 'number' &&
            !!existingDistanceUnit &&
            Math.abs(quantity - DistanceRequestUtils.convertDistanceUnit(routeDistanceMeters, existingDistanceUnit)) > ROUNDING_TOLERANCE;

Everything else looks good — Bun tests pass, the new manual-override and fallback-policy test cases are solid coverage, and the convertDistanceUnit(meters, unit) calls are used correctly.

Note on verification

I didn't run the UI flicker flow — the fix needs to compile clean first. Once line 763 is removed and CI is green, please re-verify the Original/Removed commuter distances no longer flicker per the test steps.


Next Steps: Reply with @MelvinBot implement this to have me push the one-line fix, or @MelvinBot <feedback> to refine.

@fedirjh
fedirjh requested a review from marufsharifi August 11, 2026 23:35

@marufsharifi marufsharifi 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.

LGTM

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.

4 participants