Skip to content

[HOLD] refactor: Decompose SettlementButton into variant components by report type#85367

Draft
TaduJR wants to merge 22 commits intoExpensify:mainfrom
TaduJR:test-Clean-up-SettlementButton
Draft

[HOLD] refactor: Decompose SettlementButton into variant components by report type#85367
TaduJR wants to merge 22 commits intoExpensify:mainfrom
TaduJR:test-Clean-up-SettlementButton

Conversation

@TaduJR
Copy link
Copy Markdown
Contributor

@TaduJR TaduJR commented Mar 16, 2026

Explanation of Change

Decomposes SettlementButton into variant components by report type (ExpenseSettlementButton, InvoiceSettlementButton, IOUSettlementButton) with a shared BaseSettlementButton rendering shell. A hook-free router selects the correct variant based on iouReport?.type, so each variant only mounts the hooks it needs invoice-only subscriptions no longer mount for expense/IOU reports. Shared logic extracted into useSettlementData, usePaymentGuard, useApproveAction, settlementUtils, and useInvoicePaymentOptions. Also migrates usePaymentOptions/useBulkPayOptions to share centralized data via useSettlementData.

Fixed Issues

$ #79387
PROPOSAL:

Tests

Prerequisites

  • Have a workspace with a verified business bank account connected
  • Have Expensify Wallet set up
  • Have a second test account to send/receive payments
  • Have an invoice room set up between two accounts

Test 1 — Pay from Report Preview in Chat

  1. Open a chat containing a submitted expense report preview card
  2. Verify the Pay button on the preview card displays with the correct amount (e.g. "Pay $50.00")
  3. Verify the subtitle below the button shows the correct payment method (e.g. bank last 4 digits or policy name)
  4. Press the Pay button to open the dropdown
  5. Verify the expected payment options appear (business bank account, "Mark as paid")
  6. Verify no "Wallet" option appears for expense reports
  7. Select a payment method and verify the payment processes
  8. Verify the paid animation (checkmark) plays after successful payment

Test 2 — Pay from Report Header

  1. Open the full expense report view (tap into the report from the chat)
  2. Verify the Pay button displays correctly in the header with the correct amount
  3. Press the Pay button dropdown and verify the payment options appear
  4. If an Approve option appears inside the dropdown, verify tapping it changes the report status
  5. Select "Mark as paid" and verify the payment completes
  6. Verify the paid animation (checkmark) plays after successful payment

Test 3 — Invoice Room

  1. Create or open an invoice room with an individual receiver
  2. Verify the app does NOT freeze or hang when the invoice room loads
  3. Verify the Pay button renders correctly on the invoice
  4. Press the Pay button and verify the dropdown shows two options:
    • "Pay $X.XX as an individual" (opens submenu with personal bank accounts, "Add bank account", and "Mark as paid")
    • "Pay $X.XX as a business" (opens submenu with business bank accounts, "Add bank account", and "Mark as paid")
  5. Verify no workspace is created automatically — check Settings > Workspaces for unexpected new workspaces
  6. Repeat with a business invoice room:
    • Verify payment options show directly as a flat list (business bank accounts, "Add bank account", "Mark as paid") without the individual/business split
    • Verify only fully verified bank accounts appear (partially setup ones should not)

Test 4 — Search Results

  1. Navigate to the Search tab
  2. Find unpaid expenses/IOUs in the results
  3. Verify the compact "Pay" button appears (just "Pay", no amount shown)
  4. Press the Pay button and verify the dropdown options appear
  5. Select a payment method and verify the payment processes

Test 5 — Pay Someone (Quick Action)

  1. Open a DM chat with another user
  2. Tap the "Pay someone" quick action
  3. Enter an amount on the amount screen
  4. Verify the Pay button appears directly on the amount screen (not a "Next" button)
  5. Verify payment method options are available in the dropdown (Wallet, personal bank account, "Mark as paid")
  6. Select a payment method and verify the payment completes

Test 6 — Bulk Pay from Search

  1. Open the Search tab
  2. Select multiple unpaid expense reports using checkboxes
  3. Verify the "Pay" option appears in the bulk action menu
  4. Tap "Pay" and verify the payment method submenu appears
  5. Select a payment method and verify the selected reports are paid
  • Verify that no errors appear in the JS console

Offline tests

Same as 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
    • MacOS: Desktop
  • 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 there are no new alerts related to the canBeMissing param for useOnyx
  • 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
Mac-Chrome.mp4

@TaduJR TaduJR requested review from a team as code owners March 16, 2026 11:17
@melvin-bot melvin-bot bot requested review from linhvovan29546 and trjExpensify and removed request for a team March 16, 2026 11:17
@melvin-bot
Copy link
Copy Markdown

melvin-bot bot commented Mar 16, 2026

@linhvovan29546 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 March 16, 2026 11:17
Comment thread src/components/SettlementButton/usePaymentGuard.ts Outdated
Comment thread src/components/SettlementButton/useApproveAction.ts Outdated
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 89.83607% with 31 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...nents/SettlementButton/InvoiceSettlementButton.tsx 81.81% 8 Missing ⚠️
...nents/SettlementButton/useInvoicePaymentOptions.ts 72.41% 8 Missing ⚠️
...nents/SettlementButton/ExpenseSettlementButton.tsx 93.33% 4 Missing ⚠️
src/hooks/usePaymentOptions.ts 33.33% 4 Missing ⚠️
...omponents/SettlementButton/IOUSettlementButton.tsx 95.74% 2 Missing ⚠️
...rc/components/SettlementButton/usePaymentGuard.tsx 83.33% 2 Missing ⚠️
src/components/SettlementButton/settlementUtils.ts 96.42% 1 Missing ⚠️
...rc/components/SettlementButton/useApproveAction.ts 90.90% 1 Missing ⚠️
src/hooks/useSettlementData.ts 96.87% 1 Missing ⚠️
Files with missing lines Coverage Δ
...mponents/SettlementButton/BaseSettlementButton.tsx 100.00% <100.00%> (ø)
src/components/SettlementButton/index.tsx 100.00% <100.00%> (+15.24%) ⬆️
src/components/SettlementButton/settlementUtils.ts 96.42% <96.42%> (ø)
...rc/components/SettlementButton/useApproveAction.ts 90.90% <90.90%> (ø)
src/hooks/useSettlementData.ts 96.87% <96.87%> (ø)
...omponents/SettlementButton/IOUSettlementButton.tsx 95.74% <95.74%> (ø)
...rc/components/SettlementButton/usePaymentGuard.tsx 83.33% <83.33%> (ø)
...nents/SettlementButton/ExpenseSettlementButton.tsx 93.33% <93.33%> (ø)
src/hooks/usePaymentOptions.ts 50.74% <33.33%> (-4.69%) ⬇️
...nents/SettlementButton/InvoiceSettlementButton.tsx 81.81% <81.81%> (ø)
... and 1 more

... and 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@linhvovan29546
Copy link
Copy Markdown
Contributor

@MelvinBot Could you please review this PR?

@MelvinBot
Copy link
Copy Markdown
Contributor

PR Review

CI is green (all checks pass except the PR Author Checklist, which is expected). I reviewed the full diff — here are my findings.

Overall Assessment

This is a well-structured decomposition. The original SettlementButton was a ~550-line monolith. Breaking it into useSettlementData, useSettlementButtonOptions, usePaymentGuard, useApproveAction, and useInvoicePaymentOptions is a clear improvement. The key benefit — useSettlementData being shared across SettlementButton, usePaymentOptions, and useBulkPayOptions — eliminates real duplication (bank account filtering, payment method formatting, report type flags).

Specific Observations

Architecture (positive)

  • useSettlementData centralizes Onyx subscriptions and derived flags. The getFilteredBankItems generic mapper pattern is clean and avoids the previous 3x copy of the bank-account-filtering logic.
  • usePaymentGuard is a clean extraction — self-contained, well-tested, no dependencies on the other new hooks.
  • The shouldUseFakePolicyFallback flag on useSettlementData preserves the behavioral difference between usePaymentOptions (which always fell back to ID_FAKE) and SettlementButton (which fell back to iouReport?.policyID). This is a thoughtful way to handle the divergence.

Items to address

  1. getFilteredBankItems referential instabilitygetFilteredBankItems is a function returned from useSettlementData. It captures formattedPaymentMethods in its closure, which is recomputed on every render (since formatPaymentMethods returns a new array each time). This means getFilteredBankItems itself is a new function reference every render. In useBulkPayOptions, it's used inside getPaymentSubItems which feeds into bulkPayButtonOptions — if anyone memoizes downstream, the unstable reference will break memoization. Consider wrapping getFilteredBankItems in useCallback (or memoizing formattedPaymentMethods with useMemo), or add a comment noting that React Compiler handles this.

  2. buildInvoiceOptions recreated every render — In useInvoicePaymentOptions, buildInvoiceOptions is a plain function recreated on each render. It's called in useSettlementButtonOptions during the render-phase option building, so it works, but callers can't memoize around it. Since this hook is only consumed in one place and the logic runs inline, this is acceptable — just calling it out in case the intent is for it to be composable elsewhere.

  3. Spread of optionProps — In SettlementButton/index.tsx, the component uses {...optionProps} to collect remaining props via rest syntax and passes them to useSettlementButtonOptions. This works but makes the prop interface less explicit. The UseSettlementButtonOptionsProps type (a Pick from SettlementButtonProps) ensures type safety, so this is fine in practice — but it means reading the component requires cross-referencing the Pick type to understand which props flow where.

  4. ownerBillingGraceEndPeriod removed from usePaymentGuard dependency coverage — In the original code, ownerBillingGraceEndPeriod was part of the useCallback dependency array for checkForNecessaryAction. It's still in the new usePaymentGuard hook's dependency array (line 66), so this is correct. Just confirming the billing restriction check (shouldRestrictUserBillableActions) still receives it properly — it does via the closure.

  5. userBillingGraceEndPeriods threaded through return valueusePaymentGuard returns {checkForNecessaryAction, userBillingGraceEndPeriods}. The userBillingGraceEndPeriods is only consumed by useApproveAction (for approveMoneyRequest). This creates a data flow where a "guard" hook is also responsible for providing data to an "action" hook. It works, but it's a slightly unusual coupling. An alternative would be for useApproveAction to subscribe to userBillingGraceEndPeriods directly. Not a blocker — just a design note.

  6. Review comments re: useCallback and transaction violations selector — The automated review flagged useCallback in usePaymentGuard and subscribing to full TRANSACTION_VIOLATIONS in useApproveAction. The PR author agreed to address both if easy. I'd echo both suggestions:

    • Remove useCallback from checkForNecessaryAction (React Compiler handles this).
    • Use a selector on the TRANSACTION_VIOLATIONS subscription in useApproveAction to compute the boolean directly, avoiding unnecessary re-renders.

Tests

Good test coverage:

  • usePaymentGuard.test.ts — covers all guard conditions (delegate, locked, unvalidated) and priority ordering.
  • useSettlementData.test.ts — covers report type detection, wallet eligibility, getFilteredBankItems filtering (including partial setup exclusion), policyIDKey fallback behavior, and currency handling.
  • SettlementButtonTest.tsx — updated existing tests and added new coverage for approve action, invoice room individual/business split, and currency support.
  • Shared test helpers in settlementTestHelpers.ts eliminate duplication between test files.

The IntlStore mock refactoring (from IntlStore.load(CONST.LOCALES.EN) to a full jest mock) and the additional emoji/EmojiTrie mocks are test infrastructure changes that seem necessary for the hooks to work in the test environment.

Verdict

This PR is a solid refactoring that improves maintainability without changing behavior. The items above are mostly minor design observations, not blockers. Items 1 and 6 (referential stability of getFilteredBankItems and the already-agreed automated review suggestions) are the most actionable.

…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
@trjExpensify
Copy link
Copy Markdown
Contributor

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

@trjExpensify trjExpensify removed their request for review March 17, 2026 16:26
Copy link
Copy Markdown
Contributor

@linhvovan29546 linhvovan29546 left a comment

Choose a reason for hiding this comment

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

Could you please keep the hook's return focused on what it actually computes?

Comment thread src/components/SettlementButton/index.tsx Outdated
Comment thread src/hooks/useSettlementData.ts
Comment thread src/components/SettlementButton/useSettlementButtonOptions.ts Outdated
Comment thread src/components/SettlementButton/useInvoicePaymentOptions.ts Outdated
TaduJR added 2 commits March 22, 2026 16:25
…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Mar 22, 2026

Could you please keep the hook's return focused on what it actually computes?

Sure, Done.

TaduJR added 2 commits March 23, 2026 14:33
…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
@linhvovan29546
Copy link
Copy Markdown
Contributor

linhvovan29546 commented Mar 25, 2026

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 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
    • 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 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)
  • 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
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • 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
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • 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 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 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.
  • 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
Screen.Recording.2026-03-25.at.20.49.57.mov
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-03-25.at.20.39.24.mov

@melvin-bot melvin-bot bot requested a review from roryabraham March 25, 2026 23:23
Copy link
Copy Markdown
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

I don’t think this actually accomplishes the goal of decomposition. Yes, it breaks logic up into separate hooks a bit, but:

  • all the hooks are still called in the SettlementButton unconditionally
  • we still have configuration props for different forms of the component

@linhvovan29546
Copy link
Copy Markdown
Contributor

I don’t think this actually accomplishes the goal of decomposition. Yes, it breaks logic up into separate hooks a bit, but:

  • all the hooks are still called in the SettlementButton unconditionally
  • we still have configuration props for different forms of the component

That’s exactly what I meant here #79387 (comment)

TaduJR added 2 commits March 30, 2026 09:46
…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
@TaduJR TaduJR requested a review from linhvovan29546 March 30, 2026 09:27
@roryabraham
Copy link
Copy Markdown
Contributor

Sorry for the delay I caused by forgetting that we discussed doing it in a follow-up. But at minimum, we should update the PR title and description accordingly, because the PR does not do what it says it does. Personally I'd focus on just doing the decomposition correctly rather than doing this PR which doesn't really have any effect beyond better organization.

…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
@TaduJR TaduJR changed the title refactor: Decompose SettlementButton into focused hooks via composition refactor: Decompose SettlementButton into variant components by report type Mar 31, 2026
@linhvovan29546
Copy link
Copy Markdown
Contributor

@codex review

@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Mar 31, 2026

@roryabraham

Sorry for the delay I caused by forgetting that we discussed doing it in a follow-up.

No worries, but I already implemented the follow-up here thinking you wanted to handle it here.

The variant component decomposition is already implemented in this PR the latest commit add ExpenseSettlementButton, InvoiceSettlementButton, IOUSettlementButton, BaseSettlementButton, and a hook-free router. Each variant only mounts the hooks it needs (invoice-only subscriptions don't mount for expense/IOU reports).

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

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

@linhvovan29546
Copy link
Copy Markdown
Contributor

@MelvinBot Please review this PR

@MelvinBot
Copy link
Copy Markdown
Contributor

PR Review (Updated)

This is an updated review since the PR has evolved significantly. The variant component decomposition (ExpenseSettlementButton, InvoiceSettlementButton, IOUSettlementButton with a hook-free router) now addresses roryabraham's core concern — each variant only mounts the hooks it needs, and the router in index.tsx selects the correct variant based on iouReport?.type without calling any hooks.

All CI checks pass. Here are my findings on the current state:

What works well

  • True decomposition achieved: The router in index.tsx is hook-free — it determines the variant purely from report type, then renders only that variant. Invoice-only subscriptions (useInvoicePaymentOptions, invoice receiver policy lookups, activePolicyID) no longer mount for expense/IOU reports.
  • useSettlementData is a clean centralization layer. The shouldUseFakePolicyFallback flag correctly preserves the behavioral difference between usePaymentOptions (which always fell back to ID_FAKE) and SettlementButton (which fell back to iouReport?.policyID).
  • settlementUtils.ts extracts buildHandlePaymentSelection, getCustomText, and getDefaultSelectedIndex as pure helper functions, eliminating 3x duplication across variants.
  • Test improvements: Good extraction of shared test helpers into settlementTestHelpers.ts, new coverage for approve action and invoice room individual/business split.

Items to consider

  1. Heavy prop pass-through to BaseSettlementButton: Each variant passes ~30+ props to BaseSettlementButton, of which many are just forwarded unchanged from the variant's own props. BaseSettlementButtonProps inherits all of SettlementButtonProps via Omit<SettlementButtonProps, 'onPress'>, but BaseSettlementButton only uses a subset for rendering (the rest are destructured but unused — e.g., currency, policyID, shouldHidePaymentOptions, shouldShowApproveButton, shouldDisableApproveButton, confirmApproval). Consider trimming BaseSettlementButtonProps to only the props that BaseSettlementButton actually uses in its JSX, rather than inheriting the full SettlementButtonProps shape. This would make the component's actual API explicit.

  2. Duplicated lastPaymentMethod / lastBankAccountID setup: ExpenseSettlementButton and InvoiceSettlementButton have identical blocks for computing lastPaymentMethod, lastBankAccountID, hasPreferredPaymentMethod, lastPaymentPolicy, and bankAccount (~10 lines each). IOUSettlementButton has the same minus lastBankAccountID/bankAccount. This could be extracted into useSettlementData or a small companion hook, since the inputs (policyIDKey, personalPolicyID, lastPaymentMethods, iouReport?.type) are all already available there.

  3. Duplicated shouldHidePaymentOptions && shouldShowApproveButton / onlyShowPayElsewhere blocks: All three variants have an identical preamble for the "only approve" and "only pay elsewhere" cases before diverging into variant-specific option building. This shared prefix could be handled in a small utility or moved into the option-building logic to reduce the ~15 lines of identical code in each variant.

  4. containerStyles subtle change: In the original code, containerStyles used {} for the falsy case (paymentButtonOptions.length > 5 ? styles.settlementButtonListContainer : {}). In BaseSettlementButton it uses undefined. Similarly, wrapperStyle was [wrapperStyle, shouldLimitWidth ? styles.settlementButtonShortFormWidth : {}] and is now shouldLimitWidth ? [wrapperStyle, styles.settlementButtonShortFormWidth] : wrapperStyle. These are likely equivalent in React Native's style handling, but worth confirming no edge case differs (e.g., [wrapperStyle, undefined] vs wrapperStyle when wrapperStyle is itself an array).

Verdict

The decomposition is now genuine — the router is hook-free, each variant is self-contained, and invoice-only subscriptions don't mount for other report types. The code is correct and all checks pass. Items 1–3 above are cleanup suggestions that could reduce boilerplate, not blockers. Item 4 is a minor behavioral equivalence check worth confirming.

Copy link
Copy Markdown
Contributor

@linhvovan29546 linhvovan29546 left a comment

Choose a reason for hiding this comment

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

NAB: I think we can split useSettlementData into narrower as well that would save 2-3 Onyx subscriptions per variant. That's meaningful but not huge. The bigger wins are:

  1. USER_WALLET removed from Expense and Invoice paths (wallet is IOU-only)
  2. BANK_ACCOUNT_LIST + FUND_LIST removed from IOU path (IOU uses wallet, not bank accounts directly)
  3. usePermissions() removed from Expense and IOU (beta flags are invoice-only)
  4. useActiveAdminPolicies() removed from Invoice (policy selection is IOU-only)

Combined with the duplicate subscriptions in usePaymentGuard/useApproveAction (BETAS, CONCIERGE_REPORT_ID, OWNER_BILLING), the total potential savings is ~5-6 subscriptions per variant — roughly a 30% reduction from the current state.

But this should be handled in a separate PR

Comment thread src/components/SettlementButton/BaseSettlementButton.tsx
Copy link
Copy Markdown
Contributor

@linhvovan29546 linhvovan29546 left a comment

Choose a reason for hiding this comment

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

This PR is mostly a code reorganization (despite adding variants), not a major performance improvement. I think we still need to handle performance work in follow-up PRs per variant

@melvin-bot melvin-bot bot requested a review from roryabraham March 31, 2026 10:22
Copy link
Copy Markdown
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

I think there's a lot of good here, but it's still not really accomplishing the goal of decomposition. Ultimately, I think it's not really practical to decompose SettlementButton without first decomposing ButtonWithDropdownMenu to use composition-via-JSX rather than composition-via-config.

current:

// ExpenseSettlementButton (trimmed)
function ExpenseSettlementButton({shouldHidePaymentOptions, shouldShowApproveButton, ...}) {
    // ... hooks, data fetching ...

    let paymentButtonOptions: Array<DropdownOption<string>>;

    if (shouldHidePaymentOptions && shouldShowApproveButton) {
        paymentButtonOptions = [approveButtonOption];
    } else {
        const buttonOptions = [];
        if (shouldShowBusinessBankAccountOptions) {
            for (const account of businessBankAccountOptionList) {
                buttonOptions.push({text: account.text, icon: ..., value: ..., onSelected: () => { ... }});
            }
        }
        if (shouldShowPayElsewhereOption) {
            buttonOptions.push({text: ..., icon: ..., value: ...});
        }
        if (shouldShowApproveButton) {
            buttonOptions.push(approveButtonOption);
        }
        paymentButtonOptions = buttonOptions;
    }

    return <BaseSettlementButton paymentButtonOptions={paymentButtonOptions} ... />;
}

Compositional (if ButtonWithDropdownMenu accepted children and was also compositional):

// ExpenseSettlementButton -- only knows about expense payment options
function ExpenseSettlementButton({onPress, children, ...props}) {
    // ... hooks, data fetching ...

    return (
        <KYCWall ...>
            {(triggerKYCFlow, buttonRef) => (
                <ButtonWithDropdownMenu onPress={...} ...>
                    {shouldShowBusinessBankAccountOptions && businessBankAccountOptionList.length === 0 && (
                        <ButtonWithDropdownMenu.Option
                            text={translate('iou.settleExpensify')}
                            icon={Bank}
                            value={CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT}
                        />
                    )}
                    {shouldShowBusinessBankAccountOptions && businessBankAccountOptionList.map((account) => (
                        <ButtonWithDropdownMenu.Option
                            key={account.methodID}
                            text={account.text}
                            icon={account.icon}
                            value={CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT}
                            onSelected={() => onPress({paymentType: CONST.IOU.PAYMENT_TYPE.VBBA, ...})}
                        />
                    ))}
                    {shouldShowPayElsewhereOption && (
                        <ButtonWithDropdownMenu.Option
                            text={translate('iou.payElsewhere')}
                            icon={Cash}
                            value={CONST.IOU.PAYMENT_TYPE.ELSEWHERE}
                        />
                    )}
                    {children}
                </ButtonWithDropdownMenu>
            )}
        </KYCWall>
    );
}

// Consumer (PayPrimaryAction) -- composes what it needs
{shouldShowPayButton ? (
    <AnimatedActionButton ...>
        <ExpenseSettlementButton onPress={confirmPayment} ...>
            {shouldShowApproveButton && (
                <ButtonWithDropdownMenu.Option
                    text={translate('iou.approve', {formattedAmount})}
                    icon={ThumbsUp}
                    value={CONST.IOU.REPORT_ACTION_TYPE.APPROVE}
                    disabled={shouldDisableApproveButton}
                    onSelected={confirmApproval ?? handleApprove}
                />
            )}
        </ExpenseSettlementButton>
    </AnimatedActionButton>
) : shouldShowApproveButton ? (
    <AnimatedActionButton ...>
        <ApproveButton onPress={confirmApproval ?? handleApprove} ... />
    </AnimatedActionButton>
) : null}

That would enable us to eliminate boolean flags such as shouldShowApproveButton, and overall implement this refactor correctly. I think the lesson is that decomposition needs to start at the lowest level and then work up.

I think our next step should be to HOLD or close this PR for now, create a separate PR to refactor ButtonWithDropdownMenu to support composition via children/JSX, eliminating config-array driven rendering. Then come back to this, using JSX to properly compose variants of the settlement button and nested options in the dropdown menu, without SettlementButton/index.tsx acting as a switch or without megalithic props.

@TaduJR TaduJR changed the title refactor: Decompose SettlementButton into variant components by report type [HOLD] refactor: Decompose SettlementButton into variant components by report type Apr 6, 2026
@roryabraham
Copy link
Copy Markdown
Contributor

Thanks for sticking with this @TaduJR and @linhvovan29546, you've been doing a great job. It's just a big, challenging refactor, and it's really valuable to get it right.

@TaduJR
Copy link
Copy Markdown
Contributor Author

TaduJR commented Apr 6, 2026

Thanks for sticking with this @TaduJR and @linhvovan29546, you've been doing a great job.

Oh don't mention it, Please.

It's just a big, challenging refactor, and it's really valuable to get it right.

Yea, but learning and researching a lot. That makes it super exciting for me.

Thanks so much for following up @roryabraham

…up-SettlementButton

# Conflicts:
#	src/components/SettlementButton/index.tsx
#	src/hooks/useBulkPayOptions.ts
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.

5 participants