Skip to content

[Due for payment 2026-08-27] Remove OnyxUtils.get() from GPSTripStateChecker (use useOnyx) #98609

Closed

Description

@tgolen

Problem

OnyxUtils.get() (imported from the internal deep path react-native-onyx/dist/OnyxUtils) is not a sanctioned way to read Onyx data. The only two supported read APIs are:

  1. useOnyx() — for anything in a component's render path.
  2. Onyx.connectWithoutView() — for non-render logic that genuinely can't use useOnyx.

Reaching into react-native-onyx/dist/* bypasses the public API and couples us to Onyx internals.

This issue covers src/components/GPSTripStateChecker/index.native.tsx, which contains one OnyxUtils.get(ONYXKEYS.GPS_DRAFT_DETAILS) call (as of writing, line 42, inside the mount useEffect).

Why useOnyx here

This is a React component — and it already subscribes to the same key via useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS) (as of writing, line 29). Components that can use useOnyx should use it; the mount-effect read should be served from that subscription rather than a raw OnyxUtils.get().

The effect runs once on mount to answer "is there a persisted in-progress trip on app restart?" Because useOnyx resolves asynchronously on first render, the implementation should read the value once it has loaded — e.g. gate the mount logic on the useOnyx loading status (the second tuple element / status), or mirror the value into a ref for the effect to read. Avoid reintroducing a separate one-shot read.

Proposed change

  • Remove the import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'.
  • Serve the handleGpsTripInProgressOnAppRestart read from the existing useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS) subscription (gating on loaded status / ref as appropriate) instead of OnyxUtils.get(...).

Acceptance criteria

  • No references to OnyxUtils / react-native-onyx/dist/OnyxUtils remain in this file.
  • The restart check reads GPS_DRAFT_DETAILS via the existing useOnyx subscription.
  • On app restart, an in-progress trip still prompts the "continue trip" modal, and an orphaned background task is still stopped when there's no active trip.

Notes

This is one of several issues removing OnyxUtils.get() across the app. A separate issue will add an ESLint rule to prevent it from being reintroduced.

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @mallenexpensify

Activity

  1. added
    ExternalAdded to denote the issue can be worked on by a contributor
    ImprovementItem broken or needs improvement.
    on Aug 13, 2026
  2. melvin-bot commented on Aug 13, 2026

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @mkhutornyi (External)

  3. melvin-bot commented on Aug 13, 2026

    @melvin-bot
  4. mkhutornyi commented on Aug 13, 2026

    @mkhutornyi
    Contributor

    @MelvinBot submit proposal

  5. MelvinBot commented on Aug 13, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    GPSTripStateChecker/index.native.tsx reads GPS_DRAFT_DETAILS in its mount effect via OnyxUtils.get(...), imported from the internal deep path react-native-onyx/dist/OnyxUtils. That is not a sanctioned Onyx read API and couples the component to Onyx internals. This is a React component that already subscribes to the same key via useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS), so the raw read is redundant. It only exists because the mount effect has [] deps and useOnyx hydrates asynchronously — on first render the subscription value may not be loaded yet, so the effect can't simply trust gpsDraftDetails.

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

    Serve the restart check from the existing useOnyx subscription, gated on its loaded status:

    1. Remove the import OnyxUtils from 'react-native-onyx/dist/OnyxUtils' line and the OnyxUtils.get(...) call.
    2. Capture the subscription's metadata: const [gpsDraftDetails, gpsDraftDetailsMetadata] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS);
    3. Gate the restart effect so it runs once, only after Onyx reports 'loaded', and reads gpsDraftDetails (the value that OnyxUtils.get used to fetch). Use a useRef flag so it fires a single time even as the value changes later in the session:
      • Early-return while gpsDraftDetailsMetadata.status !== 'loaded' or the ref flag is already set; otherwise set the flag and run handleGpsTripInProgressOnAppRestart(), reading gpsDraftDetails?.isTracking instead of gpsTrip?.isTracking.
      • Add gpsDraftDetails and gpsDraftDetailsMetadata.status to the dependency array.
    4. Keep the unmount cleanup unchanged — the current return cleanup that stops orphaned location tracking must still run only on unmount, so it stays in its own []-dependency useEffect rather than moving into the newly gated effect.

    This satisfies the acceptance criteria: no OnyxUtils references remain, the restart check reads GPS_DRAFT_DETAILS through the existing subscription, and behavior is preserved — an in-progress trip still prompts the "continue trip" modal, and an orphaned background task is still stopped when there's no active trip. Gating on status === 'loaded' (rather than reading the value early) is important so we don't misread the not-yet-hydrated state as "no trip" and wrongly stop tracking.

    What alternative solutions did you explore? (Optional)

    The repo already has useInitialOnyxValue (useInitialOnyxValue(ONYXKEYS.GPS_DRAFT_DETAILS)), which returns the value snapshotted at the first 'loaded' and never updates — a natural fit for a mount-time snapshot. I lean away from it here because it returns undefined both before load and when there's genuinely no trip, so the effect still needs the status to safely distinguish "not loaded yet" from "no active trip" before stopping the background task. The status-gated useOnyx approach above makes that distinction explicit, so I'd recommend it.


    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/98609) to create a draft PR.
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.

  6. mkhutornyi commented on Aug 13, 2026

    @mkhutornyi
    Contributor

    @MelvinBot's proposal looks good.
    🎀 👀 🎀 C+ reviewed

  7. melvin-bot commented on Aug 13, 2026

    @melvin-bot

    Triggered auto assignment to @amyevans, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

  8. self-assigned this
    on Aug 13, 2026
  9. MelvinBot commented on Aug 13, 2026

    @MelvinBot
    Contributor

    🎉 Thanks for the review! Glad the proposal looks good.

    Whenever you're ready for me to open the draft PR, reply with @MelvinBot implement this and I'll implement the status-gated useOnyx change (remove the OnyxUtils.get() deep-import read, serve the restart check from the existing subscription once status === 'loaded', and keep the unmount cleanup on its own [] effect).

  10. amyevans commented on Aug 14, 2026

    @amyevans
    Contributor

    👍 , feel free to proceed with Melvin on the PR @mkhutornyi!

  11. mkhutornyi commented on Aug 15, 2026

    @mkhutornyi
    Contributor

    @MelvinBot implement this

  12. 9 remaining items

  13. mkhutornyi commented on Aug 21, 2026

    @mkhutornyi
    Contributor

    BugZero Checklist:

    • [Contributor] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake.

      Link to comment:

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner.

      Link to discussion:

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to issue:

    Regression Test Proposal

    Refactor PR. No need regression test

  14. added
    Awaiting PaymentAuto-added when associated PR is deployed to production
    and removed on Aug 21, 2026
  15. melvin-bot commented on Aug 26, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  16. melvin-bot commented on Aug 26, 2026

    @melvin-bot

    Payment Summary

    Resolving PRs:

    BugZero Checklist (@mallenexpensify)

    • I have confirmed assignees, roles, and Upwork contracts look correct
    • I have paid out Upwork contracts / manual NewDot requests
    • [BugZero Assignee] I have created a GH issue for creating/updating the regression test once above steps have been agreed upon
  17. mallenexpensify commented on Aug 27, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @mkhutornyi due $250 via NewDot

  18. trjExpensify commented on Sep 17, 2026

    @trjExpensify
    Contributor

    approved $250 for @mkhutornyi

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 productionDailyKSv2ExternalAdded to denote the issue can be worked on by a contributorImprovementItem broken or needs improvement.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions