Skip to content

Refactor: Split IOURequestStepScan/index.native.tsx into hooks and components#88344

Merged
mountiny merged 13 commits intomainfrom
claude-refactorIOURequestStepScanNative
May 7, 2026
Merged

Refactor: Split IOURequestStepScan/index.native.tsx into hooks and components#88344
mountiny merged 13 commits intomainfrom
claude-refactorIOURequestStepScanNative

Conversation

@MelvinBot
Copy link
Copy Markdown
Contributor

@MelvinBot MelvinBot commented Apr 20, 2026

Explanation of Change

IOURequestStepScan/index.native.tsx had grown to ~626 lines, mixing camera UI with business logic. This PR splits it into focused hooks and components:

  • useCameraInitTelemetry — owns the camera init telemetry span refs and the three useEffect blocks that manage CAMERA_INIT / OPEN_CREATE_EXPENSE spans, plus the handleCameraInitialized callback
  • useCapturePhoto — encapsulates the capturePhoto function: permission guard, telemetry spans, photo capture, multi-scan vs. single-scan branching, editing vs. new-receipt branching, optimistic transaction creation, and Onyx receipt merges
  • CameraPermissionPrompt — the permission-denied view with hand icon + text + continue button
  • CameraViewportGestureDetector + NavigationAwareCamera + animated focus indicator + blink overlay + flash button
  • ScannerControlsBarAttachmentPicker + shutter button + multi-scan/flash toggle

The main component is now a thin shell (~350 lines, down from 626) that composes these pieces.

Fixed Issues

$ #88197

Tests

  1. Go to Scan request
  2. Try to make a scan request by using the camera in the app
  3. Verify that the flow is working well

Offline tests

// TODO: The human co-author must fill out the offline tests

QA Steps

  1. Go to Scan request
  2. Try to make a scan request by using the camera in the app
  3. Verify that the flow is working well
  • 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 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.
  • I verified that similar component doesn't exist in the codebase
  • I verified that all props are defined accurately and each prop has a /** comment above it */
  • I verified that each file is named correctly
  • I verified that each component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
  • I verified that the only data being stored in component state is data necessary for rendering and nothing else
  • In component if we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
  • 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)
  • I verified that component 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)
  • I verified that all JSX used for rendering exists in the render method
  • I verified that each 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

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

…omponents

Extract camera init telemetry into useCameraInitTelemetry hook,
photo capture orchestration into useCapturePhoto hook, and split JSX
into CameraPermissionPrompt, CameraViewport, and ScannerControlsBar
components. Shrinks the main component from 626 to 350 lines.

Co-authored-by: truph01 <truph01@users.noreply.github.com>
Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Fixed the failing ESLint check: replaced deprecated MutableRefObject with RefObject in CameraViewport.tsx and useCapturePhoto.ts.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

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

Files with missing lines Coverage Δ
...u/request/step/IOURequestStepScan/index.native.tsx 87.71% <100.00%> (+21.81%) ⬆️
...URequestStepScan/components/ScannerControlsBar.tsx 88.46% <88.46%> (ø)
...IOURequestStepScan/hooks/useCameraInitTelemetry.ts 75.00% <75.00%> (ø)
...uestStepScan/components/CameraPermissionPrompt.tsx 0.00% <0.00%> (ø)
...p/IOURequestStepScan/components/CameraViewport.tsx 0.00% <0.00%> (ø)
...t/step/IOURequestStepScan/hooks/useCapturePhoto.ts 25.00% <25.00%> (ø)
... and 7 files with indirect coverage changes

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 20, 2026

@MelvinBot Mark this PR as ready for review

@MelvinBot MelvinBot marked this pull request as ready for review April 20, 2026 18:33
@MelvinBot MelvinBot requested a review from a team as a code owner April 20, 2026 18:33
@MelvinBot
Copy link
Copy Markdown
Contributor Author

✅ This PR has been marked as ready for review.

@melvin-bot melvin-bot Bot requested review from joekaufmanexpensify and truph01 and removed request for a team April 20, 2026 18:33
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented Apr 20, 2026

@truph01 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 request for a team and joekaufmanexpensify April 20, 2026 18:33
@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 20, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

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

cancelSpan(CONST.TELEMETRY.SPAN_SHUTTER_TO_CONFIRMATION);
}, [isMultiScanEnabled]);

const capturePhoto = useCallback(() => {
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.

@MelvinBot, can we remove useCallback here?

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.

Yes! Removed both useCallback wrappers (capturePhoto and maybeCancelShutterSpan). Neither needs a stable reference — ScannerControlsBar isn't memoized and neither function appears in any other hook's dependency array.

Neither capturePhoto nor maybeCancelShutterSpan need stable references
since ScannerControlsBar is not memoized and neither function is used
in any dependency array of other hooks.

Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot MelvinBot requested a review from a team as a code owner April 20, 2026 20:37
@melvin-bot melvin-bot Bot requested a review from truph01 April 20, 2026 20:37
@melvin-bot
Copy link
Copy Markdown

melvin-bot Bot commented Apr 20, 2026

@truph01 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 April 20, 2026 20:37
ScannerControlsBar is only rendered when cameraPermissionStatus is
GRANTED, so the permission check in the disabled prop is redundant.
Simplified to just !hasFlash and removed the unused
cameraPermissionStatus prop.

Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

The only failing checks are the PR Author Checklist and PR Reviewer Checklist — these fail because not all checklist items in the PR body are checked off yet. This is expected for a PR still in progress.

All substantive CI checks (ESLint, TypeScript, tests, builds, prettier, storybook, perf-tests, etc.) are passing. No code fix is needed.

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 20, 2026

Co-authored-by: truph01 <170341821+truph01@users.noreply.github.com>
Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Reverted index.native.tsx back to its original state from main.

@mountiny mountiny marked this pull request as draft April 27, 2026 12:14
@mountiny mountiny marked this pull request as ready for review April 27, 2026 12:14
@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 27, 2026

@MelvinBot Please check these 4 comments: #88344 (comment), #88344 (comment), #88344 (comment) and #88344 (comment).

CameraViewport and ScannerControlsBar now call
useMemoizedLazyExpensifyIcons and useMemoizedLazyIllustrations
internally instead of receiving icons as props from the parent.
This removes 6 prop-drilling entries (boltIcon, galleryIcon,
receiptMultipleIcon, boltSlashIcon, shutterIllustration from
ScannerControlsBar; boltIcon from CameraViewport) and lets each
component own its asset loading.

Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Addressed all 4 review comments in 623abcd:

CLEAN-REACT-PATTERNS-2 (own behavior):

  • ScannerControlsBar now loads its own icons/illustrations internally via useMemoizedLazyIllustrations and useMemoizedLazyExpensifyIcons — removed 5 icon props
  • CameraViewport now loads its own bolt icon internally — removed 1 icon prop

CLEAN-REACT-PATTERNS-1 (composition): The icon prop-drilling was the main contributor to the high prop count. The remaining behavioral props (flash, setFlash, hasFlash, etc.) are shared state coordinated by the parent, so further decomposition would require lifting state or introducing context. Happy to pursue that in a follow-up if desired.

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 27, 2026

@mountiny We’ve addressed the four latest AI comments. Here is the summary.

I also agree with Melvin that we shouldn’t split ScannerControlsBar and CameraViewport into smaller components, as it would likely introduce unnecessary complexity through additional context or prop drilling.

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented Apr 29, 2026

@MelvinBot Could you merge main?

@MelvinBot
Copy link
Copy Markdown
Contributor Author

Merged main into this branch. ✅

@MelvinBot
Copy link
Copy Markdown
Contributor Author

⏱️ Ran out of time before I could finish. This usually means the task was too complex or I got stuck exploring. Please try again with a more specific request or post in #melvinbot-ai.

Run details

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented May 5, 2026

@MelvinBot Fix conflicts

Resolve conflict in index.native.tsx by keeping the hook call and
updating useCameraInitTelemetry to include the new entry-to-scan
telemetry spans added on main.

Co-authored-by: truph01 <truph01@users.noreply.github.com>
@MelvinBot
Copy link
Copy Markdown
Contributor Author

Conflicts resolved. Merged main and updated the useCameraInitTelemetry hook to include the new entry-to-scan telemetry spans (SPAN_ENTRY_TO_SCAN_NAVIGATION, SPAN_ENTRY_TO_SCAN_READY, SPAN_ENTRY_TO_SCAN) that were added on main.

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented May 6, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

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

@truph01
Copy link
Copy Markdown
Contributor

truph01 commented May 7, 2026

@mountiny All yours!

@mountiny mountiny merged commit 4ce9da3 into main May 7, 2026
40 of 42 checks passed
@mountiny mountiny deleted the claude-refactorIOURequestStepScanNative branch May 7, 2026 12:56
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

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

@OSBotify
Copy link
Copy Markdown
Contributor

OSBotify commented May 7, 2026

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

@OSBotify
Copy link
Copy Markdown
Contributor

OSBotify commented May 8, 2026

🚀 Deployed to staging by https://github.com/mountiny in version: 9.3.69-0 🚀

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

Bundle Size Analysis (Sentry):

@MelvinBot
Copy link
Copy Markdown
Contributor Author

No help site changes are required for this PR.

This is a code refactoring that splits IOURequestStepScan/index.native.tsx into smaller hooks and components (useCameraInitTelemetry, useCapturePhoto, CameraPermissionPrompt, CameraViewport, ScannerControlsBar). There are no user-facing changes — no new features, no UI modifications, and no behavior changes that would affect documentation.

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