Show a spinner the instant any heavy button is pressed
From https://expensify.slack.com/archives/C08CZDJFJ77/p1787675298864849
Background: In many places the UI is slow to respond to user actions. We measure this with INP (Interaction to Next Paint, the time from a user's tap to the next frame painted). The industry threshold for a "good" interaction is 200 ms; across our production environment over the last 14 days, the 75th-percentile INP is 399 ms.
An interaction's INP can be improved in two ways:
• make the work behind it cheaper by optimizing the code,. • paint visual feedback sooner: by deferring the work until after the next paint or by introducing a loading state before the work runs.. We have already started on both fronts.
Today, in most of the app, the only spinner mechanism is a plain isLoading prop on ButtonComposed, driven by Onyx: the press handler flips it with a write like Onyx.merge(formID, {isLoading: true}) alongside the rest of the API call's optimistic data. That write is asynchronous and batched, so useOnyx notifies subscribers only after the handler's synchronous work is done (validation, building optimistic data, etc.) and React then re-renders every component subscribed to the touched keys in the same pass as the button. The spinner therefore paints only after the work it was meant to cover has partially completed.
The app also has a newer mechanism, currently limited to form submit buttons: the usePressLoading hook introduced in PR #92498 and wired into FormAlertWithSubmitButton. It inverts the order of operations: on press it commits a local loading state, lets React paint the spinner and only then, one macrotask later, runs the real handler, handing over to the Onyx-driven isLoading (where one exists) with no flicker.
Problem: Buttons in the app freeze the UI with no response for 400+ ms, even in places where a spinner is already wired up via isLoading, leaving the user doubting whether the tap registered.
Solution: Give ButtonComposed its own immediate loading state, so any button that can leave the user waiting 200+ ms acknowledges the tap within a frame. The mechanism lives in ButtonComposed and follows the same paint-first-then-run order proven by usePressLoading. The difference is that the state and the deferral live inside button, so a consumer opts in instead of wiring a hook.
The plan would look like this:
- Build an inventory of buttons that can benefit from migration. Start from the labels in Sentry's INP attribution and fill gaps with a code audit of ButtonComposed usages.
- Build the mechanism in ButtonComposed. This is a single PR that everything else builds on. POC PR
- Migrate in order of user impact, ranked by current INP span counts per label. Mainly a flag flip. Handlers that validate in onPress receive the button's startWithLoading callback, so the spinner starts only when validation passes and real work begins, with no flicker.
- Verify each label the same way #92498 was verified: the Sentry query per data-sentry-label, INP span count plus the badness score.
- Optional: make the immediate loading state the default for all new buttons, opt-out through the same prop.
This solves the problem directly: the user's tap is acknowledged within a frame, so the press never feels sluggish and the spinner honestly communicates "working on it" for however long the unavoidable work takes. Optimizing the underlying Onyx writes and re-renders remains worthwhile, but it's a long, per-flow effort. This proposal makes every consequential button trustworthy now, with a proven, drop-in mechanism.
Benefits: Sentry INP spans count attributed to Form-SubmitButton (via data-sentry-label) dropped ~90% and the badness score flatlined to near zero, with hard break exactly at the 16 Jul usePressLoading deploy. Since Sentry usually records only a session's single worst interaction, each of those spans meant "submitting a form was the slowest thing this user did all session"; that stopped being true for thousands of sessions a week.
Issues https://github.com/Expensify/App/issues/96058 and https://github.com/Expensify/App/issues/96125 were raised after the immediate spinner on the Form-SubmitButton PR was merged and should be taken into account during the migration process
Not overdue, PR in progress
@OlGierd03 Whoops! This issue is 2 days overdue. Let's get this updated quick!
@OlGierd03 Whoops! This issue is 2 days overdue. Let's get this updated quick!
PR containing the core changes for the Instant Spinner is currently undergoing an internal review
@rlinoz — to hire @marufsharifi on Upwork, add the External label so a job is created and the offer is sent automatically.
Current assignee @marufsharifi is eligible for the External assigner, not assigning anyone new.
Unable to auto-create job on Upwork. The BZ team member should create it manually for this issue.
@marufsharifi Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
I got sidetracked from this issue a bit while working on the Insights project. The PR is waiting for my responses to the review. I'm going to address them shortly
@marufsharifi Eep! 4 days overdue now. Issues have feelings too...
We’re still waiting for @OlGierd03 to review the PR comments.
True, I plan to take care of it today
@marufsharifi Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
I started making changes after the review. Finishing up today
@marufsharifi Huh... This is 4 days overdue. Who can take care of this?
@marufsharifi Still overdue 6 days?! Let's take care of this!
@marufsharifi Still overdue 6 days?! Let's take care of this!
waiting for @OlGierd03 to resolve the conflicts.
@marufsharifi I've merged main and am already looking into your comment