Skip to content

[NoQA] [Insights] Show already-loaded charts on Insights and Home/Spend when fetched by different actions - #102311

Merged
luacmartins merged 18 commits into
Expensify:mainfrom
software-mansion-labs:insights/improve-chart-loading-state
Sep 30, 2026
Merged

luacmartins merged 18 commits into
Expensify:mainfrom
software-mansion-labs:insights/improve-chart-loading-state

Conversation

@mhawryluk

@mhawryluk mhawryluk commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Explanation of Change

The Insights page and the Home insights widget can load the same chart with different commands (GetInsights and Search), and both write it to the same snapshot key. Each could only partly use what the other loaded: Insights only once its own GetInsights response had named the snapshot, and Home not at all, since GetInsights snapshots had no state stamp and reported the wrong search metadata.

getInsights now stamps each graph's snapshot with state: loaded in successData, the same way search does in finallyData. This lets Search readers like the Home widget treat those snapshots as loaded. Insights charts read their snapshot under their own query's hash instead of going through the dashboard record. A chart counts as loaded once the snapshot is loaded, not matter what command sent it.

The page now decides its state from every visible chart instead of only the headline. It shows the full-page offline state only when no chart has anything to show, and otherwise renders the charts, each with its own offline, loading or empty state. It shows the full-page empty state only when every chart is loaded without rows. Nothing is requested or read until the saved filters resolve, and each chart shows its loading state until then.

Fixed Issues

$ #102515
PROPOSAL: N/A

Tests

Precondition: the insightsPage beta is enabled and the account is an admin of a workspace with expenses this year.

  1. Open Insights and wait for all charts. Open Home and verify the insights widget shows its chart right away, without the loading state. (on web there still might be a short loading spinner shown when switching between charts of different types, like between bar chart and a line chart, because of how Skia files are lazy loaded on web)
  2. Clear cache and restart. Open Home and wait for Spend over time to load, then open Insights. Verify Spend over time shows right away while the other charts load.
  3. On Insights, change the Spend over time group-by. Verify the other charts keep showing their data and no chart flashes the loading state when the new data arrives.
  4. Change the group-by to Quarter for example, then reload the page. Verify each chart shows its loading state and then quarterly data (on headline), with no monthly data shown in between.
  5. Set the date filter to a range with no expenses. Verify the full-page empty state is shown.

Offline tests

  1. Clear cache and restart. Open Home and wait for the insight chart to load.
  2. Go offline and open Insights.
  3. Verify that particular chart shows and the other charts show the offline state.
  4. Clear cache and restart, go offline and open Insights. Verify the full-page offline state is shown.
  5. Open Insights online and wait for all charts, then go offline. Verify every chart stays on screen.

QA Steps

No tests.

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 followed proper code patterns (see Reviewing the code)
    • 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 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)
  • 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)
  • 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 the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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
Nagranie.z.ekranu.2026-09-29.o.11.11.33.mov

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/libs/SearchUIUtils.ts 72.25% <ø> (ø)
src/libs/resolveInsightsChartData.ts 100.00% <100.00%> (ø)
src/pages/Insights/dashboardSpecs.ts 25.00% <ø> (ø)
src/pages/Insights/insightsQueries.ts 92.85% <100.00%> (+0.54%) ⬆️
src/pages/Insights/resolveDashboardState.ts 100.00% <100.00%> (ø)
src/components/Search/SearchChartView.tsx 0.00% <0.00%> (ø)
src/pages/home/InsightsSection/useInsightData.ts 0.00% <0.00%> (-36.37%) ⬇️
src/libs/actions/Insights.ts 11.76% <0.00%> (-0.74%) ⬇️
src/pages/Insights/charts/InsightsChartWidget.tsx 0.00% <0.00%> (ø)
...es/home/InsightsSection/InsightsSectionContent.tsx 0.00% <0.00%> (ø)
... and 2 more
... and 11 files with indirect coverage changes

@mhawryluk

Copy link
Copy Markdown
Member Author

@MelvinBot review

@MelvinBot

Copy link
Copy Markdown
Contributor

Review

I found one blocking issue. The rest of the change looks sound.

🔴 A failed GetInsights response now shows the "no results" state instead of the error state, on both Insights and Home.

finallyData runs for failed responses too. The new stamp marks every chart snapshot state: loaded at src/libs/actions/Insights.ts:43-49. But failureData only writes errors to the dashboard record, not to the snapshots. So after a non-200 response, each snapshot has no data, no errors, and state: loaded. isSearchDataLoaded then treats it as settled with no results.

What the user sees with a fresh cache (your test step 7):

  • Insights: every chart counts as loaded, so hasChartData is true and the full-page error is skipped at resolveDashboardState.ts:47. No chart has rows, so the page shows EMPTY ("filters matched nothing").
  • Home widget: getInsightState finds no snapshot errors, so it shows EMPTY instead of ERROR.

search() avoids this because its failureData writes errors to the snapshot (Search.ts:844-859).

Suggested fix: Move the stamp from finallyData to successData, so only a successful response marks snapshots loaded. If you would rather match search() and add snapshot errors to failureData, also exclude errored snapshots from hasChartData. Otherwise step 7 shows per-chart errors instead of the full-page error. Either way, please add a getDashboardState test for a failed request that went through this path.

Other things I checked
  • groupBy = chart.groupBy ?? filters.groupBy matches what applyInsightsFilters puts into the query, so removing queryJSON?.groupBy is safe.
  • SearchChartView only reads queryJSON in handleItemPress, which the new guard covers.
  • Group-by change (step 3): supporting charts keep their query hash, and the snapshots now carry the stamped hash and state. isSearchDataLoaded accepts them while the new dashboard record is still empty, so they don't flash the loading state.
  • Keying useMultipleSnapshots output by String(hash) and reading it with a numeric hash works, because JS coerces object keys to strings.

view run

@OlGierd03 OlGierd03 left a comment

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.

Looks great overall! 🚀

Comment thread src/libs/actions/Insights.ts Resolved
@mhawryluk
mhawryluk marked this pull request as ready for review September 29, 2026 09:19
@mhawryluk
mhawryluk requested review from a team as code owners September 29, 2026 09:19
@melvin-bot

melvin-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown

@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 September 29, 2026 09:19
@cristipaval
cristipaval requested review from JmillsExpensify and removed request for garrettmknight September 29, 2026 09:21

@JmillsExpensify JmillsExpensify left a comment

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.

Looks great!

@mhawryluk

Copy link
Copy Markdown
Member Author

@truph01 I've addressed the comments

return INSIGHTS_DASHBOARD_STATE.NO_EXPENSES;
}
if (headlineSnapshot?.data && !Object.keys(headlineSnapshot.data).some(isGroupEntry)) {
if (chartStates.every(({isLoaded}) => isLoaded)) {

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.

Could we exclude snapshots with errors here, so a failed Home Search doesn’t count as an empty result when opening Insights offline?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good call. I changed isLoaded to isSearchDataLoaded(snapshot, queryJSON) && isEmptyObject(snapshot?.errors)

luacmartins
luacmartins previously approved these changes Sep 30, 2026

@luacmartins luacmartins left a comment

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.

LGTM. @cristipaval @aldo-expensify all yours

cristipaval
cristipaval previously approved these changes Sep 30, 2026
@luacmartins

Copy link
Copy Markdown
Contributor

@mhawryluk conflicts

@mhawryluk
mhawryluk dismissed stale reviews from cristipaval and luacmartins via fa00934 September 30, 2026 15:02
cristipaval
cristipaval previously approved these changes Sep 30, 2026
luacmartins
luacmartins previously approved these changes Sep 30, 2026
@truph01

truph01 commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@mhawryluk We have conflicts again

@mhawryluk
mhawryluk dismissed stale reviews from luacmartins and cristipaval via 8976c7e September 30, 2026 17:11
@luacmartins
luacmartins merged commit c7f37e8 into Expensify:main Sep 30, 2026
49 of 71 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

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

@OSBotify

OSBotify commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/luacmartins in version: 9.5.0-0 🚀

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

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update needed. This PR changes how Insights charts load and show offline or empty states, and the Insights page and Home insights widget are still gated behind the insightsPage beta (CONST.BETAS.INSIGHTS_PAGE), which this PR doesn't remove.


view run

@OSBotify

OSBotify commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/Julesssss in version: 9.5.0-5 🚀

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

Bundle Size Analysis (Sentry):

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.

9 participants