Skip to content

[Due for payment 2026-04-30] [Due for payment 2026-04-28] [$250] Refactor: extract MoneyReportHeader actions into hooks and components #87937

Closed

Description

@mountiny

Context

MoneyReportHeader had grown to ~2400+ lines, mixing UI with business logic (hold menus, primary actions, expense actions, selection mode). That layout makes the header hard to maintain and can encourage unnecessary re-renders.

Goal

Split responsibility into focused hooks and components so the header stays thin and action logic lives next to the right Onyx/data access:

  • useReportPrimaryAction — primary action derivation; getReportPrimaryAction returns a plain value (no animation coupling)
  • useExpenseActions — secondary expense actions (hold, unhold, etc.); isReportInSearch passed in instead of hardcoded
  • useTransactionThreadReport — loads the transaction thread report internally via useOnyx
  • ActionsBar — renders subcomponents directly (no ReactNode props that force parent re-renders)
  • MoneyReportHeaderHoldMenu — extracted with an early return so the heavy menu tree is skipped when unused
  • Remove dead code (e.g. unused selection/payment refs and duplicated callbacks)

Target: shrink MoneyReportHeader to a small shell (~300 lines) with behavior owned by the hooks above.

Implementation

Tracked in: #87361

Related issues (from PR)

QA / verification

  • Open an expense report (single transaction): More menu, each action (export sub-menu, Download PDF, etc.).
  • Search: multi-select expenses from the same report; selection dropdown shows correct actions.
  • No new JS console errors on these flows.
Issue OwnerCurrent Issue Owner: @mallenexpensify
Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~022044207170746363662
  • Upwork Job ID: 2044207170746363662
  • Last Price Increase: 2026-04-15

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 15, 2026
  2. MelvinBot commented on Apr 15, 2026

    @MelvinBot
    Contributor

    Issue Analysis

    Summary: This is a well-scoped refactoring issue with an active implementation PR (Expensify/App#87361) that currently has merge conflicts and needs resolution before it can proceed.

    Current State:

    PR #87361 extraction plan details

    The PR creates these new files:

    Hooks (in src/hooks/):

    • useReportPrimaryAction.ts — primary action derivation (59 lines)
    • useExpenseActions.ts — secondary expense action callbacks (556 lines)
    • useExportActions.ts — export-related actions (268 lines)
    • useHoldRejectActions.ts — hold/reject logic (120 lines)
    • useLifecycleActions.tsx — lifecycle management (385 lines)
    • useTransactionThreadReport.ts — fetches transaction thread via useOnyx (40 lines)

    Components (in src/components/MoneyReportHeaderActions/):

    • index.tsx — actions bar entry point (90 lines)
    • MoneyReportHeaderSecondaryActions.tsx — secondary actions UI (397 lines)
    • MoneyReportHeaderSelectionDropdown.tsx — selection mode dropdown (546 lines)

    Also adds: PaymentAnimationsContext.tsx to decouple animation state from getReportPrimaryAction

    Removes: ~2,003 lines from MoneyReportHeader.tsx, 14 lines from ReportPrimaryActionUtils.ts, dead code refs

    Possibly related open issues:

    Recommended Next Step: Resolve the merge conflicts on PR #87361 branch (perf/extract-money-report-header-actions) against main to unblock review.


    Next Steps: Reply with @MelvinBot implement this to create a draft PR, or @MelvinBot <your feedback> to refine this analysis.

  3. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Apr 15, 2026
  4. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Apr 15, 2026
  5. melvin-bot commented on Apr 15, 2026

    @melvin-bot

    Current assignee @abzokhattab is eligible for the External assigner, not assigning anyone new.

  6. changed the title [-]Refactor: extract MoneyReportHeader actions into hooks and components[/-] [+][$250] Refactor: extract MoneyReportHeader actions into hooks and components[/+] on Apr 15, 2026
  7. melvin-bot commented on Apr 15, 2026

    @melvin-bot
  8. yuvrajangadsingh commented on Apr 15, 2026

    @yuvrajangadsingh
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    MoneyReportHeader.tsx has grown to 2189 lines on main, mixing 30+ useOnyx subscriptions, animation state, business logic callbacks, and JSX rendering in one function body. Any state change anywhere in the component re-evaluates the entire secondaryActionsImplementation record and primaryActionsImplementation map on every render.

    What is the root cause of that problem?

    Three concrete architectural problems, each independently causing unnecessary work:

    1. getReportPrimaryAction is coupled to animation state (src/libs/ReportPrimaryActionUtils.ts:62-79, src/libs/ReportPrimaryActionUtils.ts:443-449). The GetReportPrimaryActionParams type includes isPaidAnimationRunning, isApprovedAnimationRunning, and isSubmittingAnimationRunning. A pure action-derivation utility has no business reading UI animation flags. The useMemo at src/components/MoneyReportHeader.tsx:955-991 re-runs on every animation tick, re-deriving the action from scratch each frame.

    2. Transaction thread report lookup is split across parent and child. The transactionThreadReportID prop is computed by the parent (MoneyRequestReportView.tsx:135, ReportScreen.tsx:327) via getOneTransactionThreadReportID, then the header fetches the report from Onyx itself (MoneyReportHeader.tsx:232). The issue wants useTransactionThreadReport to own both steps internally, eliminating the prop.

    3. Both primaryActionsImplementation and secondaryActionsImplementation are built as inline records (lines 1139-1294 and 1370-1772). They hold ~630 lines of JSX and callback definitions that rebuild on any render of the parent component, even renders unrelated to actions. ProcessMoneyReportHoldMenu at lines 2042-2062 is guarded by isHoldMenuVisible && requestType !== undefined in JSX, but JavaScript still evaluates all its prop expressions every render.

    What changes do you think we should make in order to solve the problem?

    src/libs/ReportPrimaryActionUtils.ts

    Remove isPaidAnimationRunning, isApprovedAnimationRunning, isSubmittingAnimationRunning from GetReportPrimaryActionParams (lines 76-78) and delete the animation override block (lines 443-449). getReportPrimaryAction becomes a pure function of report/transaction/policy data.

    src/hooks/useReportPrimaryAction.ts (new)

    function useReportPrimaryAction(params: Omit<GetReportPrimaryActionParams, AnimationKeys>) {
        const {isPaidAnimationRunning, isApprovedAnimationRunning, isSubmittingAnimationRunning} = usePaymentAnimations();
        return useMemo(() => {
            if (isPaidAnimationRunning || isApprovedAnimationRunning) return CONST.REPORT.PRIMARY_ACTIONS.PAY;
            if (isSubmittingAnimationRunning) return CONST.REPORT.PRIMARY_ACTIONS.SUBMIT;
            return getReportPrimaryAction(params);
        }, [isPaidAnimationRunning, isApprovedAnimationRunning, isSubmittingAnimationRunning, ...params deps]);
    }

    This mirrors the existing usePaymentAnimations hook at src/hooks/usePaymentAnimations.ts - animation state stays in the hook layer. MoneyReportHeader drops the primaryAction useMemo (lines 955-991) entirely and calls useReportPrimaryAction instead.

    src/hooks/useTransactionThreadReport.ts (new)

    function useTransactionThreadReport(report, chatReport, reportActions, isOffline, reportTransactionIDs) {
        const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActions, isOffline, reportTransactionIDs);
        const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);
        return {transactionThreadReportID, transactionThreadReport};
    }

    This is identical to the pattern already used in src/pages/ReportDetailsPage.tsx:185 + src/components/MoneyRequestReportView/MoneyRequestReportView.tsx:135,163 - those callers derive transactionThreadReportID then immediately useOnyx the result. The hook owns both steps. Remove transactionThreadReportID from MoneyReportHeaderProps (line 190) since the header now derives it internally.

    src/hooks/useExpenseActions.ts (new)

    Extract confirmPayment (lines 615-700), confirmApproval (lines 715-741), markAsCash (lines 743-753), duplicateExpenseTransaction (lines 758-813), and the hold menu state (isHoldMenuVisible, paymentType, requestType) into a focused hook. Accept isReportInSearch as a param (currently hardcoded at line 610 from route.name). Pattern to follow: useSelectedTransactionsActions at src/hooks/useSelectedTransactionsActions.ts - same shape (hook accepts report/policy/session, returns callbacks + state).

    src/components/MoneyReportHeaderActions/index.tsx (new - ActionsBar)

    Extract the actions bar at lines 1959-2017. Accepts primaryAction, applicableSecondaryActions, selectedTransactionsOptions, and callbacks - no ReactNode props. The primaryActionsImplementation map (lines 1139-1294) moves inside this component so it only rebuilds when action-relevant state changes, not on every parent render.

    src/components/MoneyReportHeaderHoldMenu.tsx (new)

    function MoneyReportHeaderHoldMenu({isVisible, requestType, ...props}) {
        if (!isVisible || requestType === undefined) return null;
        return <ProcessMoneyReportHoldMenu {...props} />;
    }

    The early return means React skips the component subtree entirely when unused, vs the current JSX guard (line 2042) which still evaluates all prop expressions every render.

    src/components/MoneyReportHeader.tsx

    After extractions: remove the 30+ variable declarations that move into hooks, the primaryActionsImplementation record, the secondaryActionsImplementation record, and the inline hold menu JSX. Target is ~300 lines of thin shell that wires hooks together and renders the layout.

    What alternative solutions did you explore?

    I looked at whether secondaryActionsImplementation could be memoized in place using a deep-equal selector pattern, but the record has 18 keys each with onSelected closures capturing different state slices - a useCallback per key approach still leaves the record construction in the component. Moving the whole block into useExpenseActions is cleaner and consistent with how useSelectedTransactionsActions already handles the bulk-edit equivalents.

  9. alexanderxfgl-bit commented on Apr 15, 2026

    @alexanderxfgl-bit

    I can help refactor the MoneyReportHeader component to improve maintainability and performance.

    Proposed Approach:

    1. Extract focused hooks for specific responsibilities:
      • useReportPrimaryAction for primary action derivation
      • useExpenseActions for secondary expense actions
      • useTransactionThreadReport for transaction thread loading
    2. Create specialized components for different action types
    3. Remove duplicate code and unused references
    4. Reduce component re-renders by eliminating unnecessary props

    Implementation Plan:

    • Split the 2400+ line component into focused, manageable pieces
    • Move business logic close to data access points
    • Extract reusable hooks and components
    • Optimize rendering by avoiding unnecessary parent re-renders
    • Remove dead code and duplicate callbacks

    The goal is to reduce the header to a small (~300 line) shell while maintaining all existing functionality and improving performance.

  10. 18 remaining items

  11. melvin-bot commented on Apr 28, 2026

    @melvin-bot

    @abzokhattab 6 days overdue. This is scarier than being forced to listen to Vogon poetry!

  12. rlinoz commented on Apr 29, 2026

    @rlinoz
    Contributor

    @TMisiukiewicz can you confirm whether we are done here?

  13. TMisiukiewicz commented on Apr 29, 2026

    @TMisiukiewicz
    Contributor

    @rlinoz yes we are done ✅

  14. melvin-bot commented on Apr 29, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  15. melvin-bot commented on Apr 29, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  16. melvin-bot commented on Apr 29, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  17. melvin-bot commented on Apr 30, 2026

    @melvin-bot

    Payment Summary

    Upwork Job

    BugZero Checklist (@mallenexpensify)

    • (if NewFeature) I have created a PR for any necessary HelpDot updates, or I confirmed no updates are necessary.
    • I have verified the correct assignees and roles are listed above and updated the necessary manual offers
    • I have verified that there are no duplicate or incorrect contracts on Upwork for this job (https://www.upwork.com/ab/applicants/2044207170746363662/hired)
    • I have verified the PR was not reverted
    • I have applied any discounts due to bugs/regressions introduced by this PR
    • I have paid out the Upwork contracts or cancelled the ones that are incorrect
    • I have verified the payment summary above is correct
  18. mallenexpensify commented on May 1, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @abzokhattab due $250 via NewDot

    I think that's correct and, with it being a refactor, don't think we need a BZ checklist. Comment/reopen, if needed

  19. quinthar commented on May 12, 2026

    @quinthar
    Contributor

    Approved $250 to @abzokhattab

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

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions