Skip to content

Remove Onyx.connect() usage for ONYXKEYS.COLLECTION.POLICY_TAGS in createDistanceRequest function from src/libs/actions/IOU/Split.ts#90353

Open
Guccio163 wants to merge 1 commit into
Expensify:mainfrom
software-mansion-labs:Guccio163/onyx-connect/policy_tags/createDistanceRequest
Open

Remove Onyx.connect() usage for ONYXKEYS.COLLECTION.POLICY_TAGS in createDistanceRequest function from src/libs/actions/IOU/Split.ts#90353
Guccio163 wants to merge 1 commit into
Expensify:mainfrom
software-mansion-labs:Guccio163/onyx-connect/policy_tags/createDistanceRequest

Conversation

@Guccio163
Copy link
Copy Markdown
Contributor

@Guccio163 Guccio163 commented May 12, 2026

Explanation of Change

This PR is part of a refactor to remove Onyx.connect for the keys: ONYXKEYS.COLLECTION.POLICY_TAGS from the src/libs/actions/IOU/Split.ts file and replace it with useOnyx.

It isolates the createDistanceRequest function from the ONYXKEYS.COLLECTION.POLICY_TAGS Onyx.connect key .

Fixed Issues

$ #72721
PROPOSAL:

Tests

Preconditions (all scenarios)

  • Two workspaces configured on the test account:
    • Workspace A — has a tag list (e.g., Tags) with values
      Tag 1, Tag 2, Tag 3.
    • Workspace B — has a tag list with different values:
      Tag 2.1, Tag 2.2, Tag 2.3.
  • Categories enabled on both workspaces (any value).
  • Distance rates enabled on both workspaces.
  • The user is a member of both workspaces.

Scenario 1 — Create distance expense in a Workspace A expense chat

  1. Open the LHN and tap the Workspace A chat (the policy expense chat).
  2. Tap the green + (Create) button and choose Track expenseDistance.
  3. Add two waypoints (Start + Stop) so the distance is greater than 0.
  4. On the confirmation screen, tap Tag in the field list.

Expected result:

  • The tag list shown contains Workspace A values (Tag 1, Tag 2,
    Tag 3).
  • Workspace B tags (Tag 2.1, Tag 2.2, Tag 2.3) are not shown.
  1. Pick Tag 1 → tap Save → back on confirmation tap Submit.

Expected result:

  • The expense is created in the Workspace A expense report.
  • Opening the newly created expense shows Tag 1 applied.

Scenario 2 — Add a distance expense to an existing expense report (different chat)

  1. In Workspace A chat, create any non-distance expense first so an
    open expense report exists with Workspace A policy. Note the
    report.
  2. Open that expense report (not the chat) and tap the green + in the
    report header → Add expenseDistance.
  3. Add waypoints and proceed to the confirmation screen.
  4. Tap Tag.

Expected result:

  • Tag list shows Workspace A values (Tag 1, Tag 2, Tag 3),
    matching the existing expense report's policy.
  1. Pick Tag 2SaveSubmit.

Expected result:

  • The new distance expense is added to the same expense report with
    Tag 2 applied.
  • No tag from any other workspace appears on the transaction.

Scenario 3 — Duplicate a distance expense (Duplicate.ts path)

Setup: In Workspace A, create two distance expenses with the same
amount and date so the app treats them as duplicates. Both expenses
must have a tag from Workspace A applied
(e.g., Tag 1) — this is
required because Duplicate.ts passes the resolved policyTagList
(the policy's available tag set) to the new distance request, so
without tags on the originals there is nothing observable to verify.
Open the report preview that shows the Duplicates detected banner.

  1. Open the expense report containing the flagged duplicate distance
    expense.
  2. Tap one of the duplicated distance expenses.
  3. Tap Review duplicates (or the duplicates banner on the expense
    detail).
  4. Step through the review wizard and tap Keep this one to commit
    the duplicate decision (this triggers Duplicate.ts
    createExpenseByType with transactionType: DISTANCE).
  5. After the flow completes, open the resulting distance expense.

Expected result:

  • The kept distance expense carries the same Tag 1 from the
    original expense (i.e., the policy tag list resolves to Workspace
    A — same as the existing IOU report — so the applied tag survives
    validation).
  • Tapping Tag on the kept expense shows the full Workspace A tag
    list (Tag 1, Tag 2, Tag 3).
  • No tag from any other workspace leaks in.

  • Verify that no errors appear in the JS console

Offline tests

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 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

Scenario 1
https://github.com/user-attachments/assets/ea3472ab-4bf4-421a-b0c9-ea3e3efd801f

Scenario 2
https://github.com/user-attachments/assets/463f3fd5-a0ad-4230-ad3b-e93b285a7b33

Scenario 3
https://github.com/user-attachments/assets/a53cc1a7-4c15-4f79-9e36-5b4f38d7b68d

@Guccio163 Guccio163 changed the title extract policTagsList one level higher Remove Onyx.connect() usage for ONYXKEYS.COLLECTION.POLICY_TAGS in createDistanceRequest function from src/libs/actions/IOU/Split.ts May 12, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

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

Files with missing lines Coverage Δ
src/libs/actions/IOU/Duplicate.ts 91.71% <100.00%> (+0.12%) ⬆️
src/libs/actions/IOU/MoneyRequest.ts 77.77% <100.00%> (+0.40%) ⬆️
src/libs/actions/IOU/Split.ts 72.26% <100.00%> (ø)
.../request/step/confirmation/useExpenseSubmission.ts 54.26% <88.88%> (+1.70%) ⬆️
... and 50 files with indirect coverage changes

@Guccio163 Guccio163 marked this pull request as ready for review May 12, 2026 18:12
@Guccio163 Guccio163 requested review from a team as code owners May 12, 2026 18:12
@melvin-bot melvin-bot Bot requested review from joekaufmanexpensify and parasharrajat and removed request for a team May 12, 2026 18:12
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented May 12, 2026

@parasharrajat 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]

@@ -176,6 +183,7 @@ function useExpenseSubmission(params: UseExpenseSubmissionParams) {
// Policy-scoped Onyx data
const policyID = policy?.id;
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.

❌ PERF-11 (docs)

useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS) subscribes to the entire policy tags collection, causing re-renders whenever any policy's tags change. However, only a single entry is accessed (allPolicyTags?.[key]). Use a targeted useOnyx call with the specific policy key instead, e.g., useOnyx(\${ONYXKEYS.COLLECTION.POLICY_TAGS}${iouReportPolicyID}`). If iouReportPolicyID` is not yet known at hook call time, consider restructuring so the lookup can use a specific key.


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

const reportTransactions = useReportTransactions(report?.reportID);
const isMoneyRequestReport = isMoneyRequestReportReportUtils(report);
const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : '';
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.

❌ PERF-11 (docs)

useOnyx(ONYXKEYS.COLLECTION.REPORT) subscribes to the entire reports collection without a selector. The reports collection can be very large, and this will re-render the component on every report change. The only usage is allReports?.[key]?.policyID to get a single report's policyID. Use a targeted useOnyx with the specific report key, or if the key depends on runtime values, use a selector that extracts only the needed policyID.


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

const isMoneyRequestReport = isMoneyRequestReportReportUtils(params.report);
const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(params.report?.chatReportID) : params.report;
const moneyRequestReportID = isMoneyRequestReport ? params.report?.reportID : '';
// eslint-disable-next-line @typescript-eslint/no-deprecated
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-5 (docs)

// eslint-disable-next-line @typescript-eslint/no-deprecated lacks a justification comment explaining why the deprecated API is being used. Add a comment explaining why getMoneyRequestPolicyTags is used despite being deprecated (e.g., // Using deprecated API because no replacement is available yet for this use case).


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

const isMoneyRequestReport = isMoneyRequestReportReportUtils(report);
const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : '';
// eslint-disable-next-line @typescript-eslint/no-deprecated
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-5 (docs)

// eslint-disable-next-line @typescript-eslint/no-deprecated lacks a justification comment explaining why the deprecated API is being used. Add a comment explaining why getMoneyRequestPolicyTags is used despite being deprecated.


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

@joekaufmanexpensify
Copy link
Copy Markdown
Contributor

No product review needed

@joekaufmanexpensify joekaufmanexpensify removed their request for review May 12, 2026 20:04
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.

2 participants