Skip to content

Add local CSV downloads to Insights charts - #104272

Merged
mountiny merged 3 commits into
mainfrom
cmartins-insights-chart-csv
Oct 20, 2026
Merged

mountiny merged 3 commits into
mainfrom
cmartins-insights-chart-csv

Conversation

@luacmartins

@luacmartins luacmartins commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Add Download CSV below View on Spend in each Insights chart menu. Generate the file locally from the loaded groups instead of exporting individual expenses or queuing a server export.

Export current-period totals, expense counts, and currency. Rankings include the displayed top five groups. Time-series exports identify buckets by their start date and include zero-spend periods across the selected date range. Comparison columns are excluded.

Fixed Issues

$ #104209
PROPOSAL:

Tests

  1. Open Insights > Spend and select a Date range, Workspace, and Group currency; confirm the charts show the selected data.
  2. Open each chart's three-dot menu; confirm Download CSV appears below View on Spend with the table icon.
  3. Select Download CSV and open the file; confirm it contains the group label, Expenses, Total, and Currency, with no individual expense rows or comparison columns.
  4. Export each ranking chart; confirm its rows and order match the groups displayed in the chart, including the top-five limit.
  5. Export Spend over time using each available Day, Week, Month, Quarter, and Year grouping; confirm period-start dates are chronological and periods without spend have zero expenses and a zero total.
  6. Change Compare and export again; confirm only the current period's values appear.
  7. Change Date, Workspace, or Group currency and export after loading; confirm the file matches the updated chart.
  8. Check the menus while charts are loading, empty, or showing Refresh needed; confirm Download CSV is hidden.
  9. Check the JS console throughout the flow; confirm no errors appear.

Offline tests

  1. Load an Insights dashboard with data, disconnect the network, and download a chart's CSV; confirm the cached chart exports without a network request.
  2. Open a chart with no cached data while offline; confirm Download CSV is unavailable.

QA Steps

  1. Repeat the Tests and Offline tests on desktop web, mobile web, iOS, and Android; confirm menu visibility and CSV contents match across platforms.
  2. Export charts with accented names, commas, quotes, and line breaks in group labels; confirm names remain readable and each label occupies one spreadsheet cell.
  3. Export a chart with a negative total or a group name beginning with a formula character; confirm the total remains numeric and the name is treated as text.

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 any copy / text that was added to the app 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 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 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

@luacmartins
luacmartins requested a review from a team October 16, 2026 18:37
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

This comment has been minimized.

@codecov

codecov Bot commented Oct 16, 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 Δ
...yCardsTable/WorkspaceCompanyCardsTableControls.tsx 72.41% <ø> (+27.10%) ⬆️
src/libs/actions/Card.ts 22.59% <ø> (+6.95%) ⬆️
src/libs/escapeCsvField.ts 100.00% <100.00%> (ø)
src/pages/Insights/charts/InsightsChartWidget.tsx 93.33% <85.71%> (-2.50%) ⬇️
src/pages/Insights/buildInsightsChartCSV.ts 93.75% <93.75%> (ø)
... and 14 files with indirect coverage changes

@JmillsExpensify

Copy link
Copy Markdown
Contributor

Looks solid to me. Thanks for the quick work on this one!

@luacmartins
luacmartins marked this pull request as ready for review October 16, 2026 20:33
@luacmartins
luacmartins requested a review from a team as a code owner October 16, 2026 20:33
@melvin-bot
melvin-bot Bot requested review from joekaufmanexpensify and removed request for a team October 16, 2026 20:33
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T20:38:06.586763Z f219c20 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@melvin-bot
melvin-bot Bot requested a review from neil-marcellini October 16, 2026 20:33
@melvin-bot

melvin-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

@neil-marcellini 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 October 16, 2026 20:33
Comment thread src/pages/Insights/buildInsightsChartCSV.ts Outdated
@JmillsExpensify
JmillsExpensify requested review from JmillsExpensify and removed request for joekaufmanexpensify October 16, 2026 22:04
@JmillsExpensify

Copy link
Copy Markdown
Contributor

Only additional thought from me is: Should we should skip the save dialogue? I kind of think we should and just save it to the default download location of the browser.

@luacmartins
luacmartins requested a review from a team as a code owner October 16, 2026 22:45
@melvin-bot
melvin-bot Bot requested review from huult and removed request for a team October 16, 2026 22:45
@melvin-bot

melvin-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown

@huult 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]

@luacmartins

Copy link
Copy Markdown
Contributor Author

@JmillsExpensify this already uses the browser's default download flow. If "Ask where to save each file" is enabled in the browser settings, it will still show a save dialog. We can't override that from the app.

@luacmartins

Copy link
Copy Markdown
Contributor Author

Updated

@huult

huult commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

I will check it soon

@huult

huult commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified any copy / text that was added to the app 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 verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The 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
  • 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).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this 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 have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Screen.Recording.2026-10-20.at.10.14.17.mov
Android: mWeb Chrome
Screen.Recording.2026-10-20.at.10.15.45.mov
iOS: HybridApp
Screen.Recording.2026-10-20.at.10.19.08.mov
iOS: mWeb Safari
Screen.Recording.2026-10-20.at.10.20.20.mov
MacOS: Chrome / Safari
Screen.Recording.2026-10-20.at.10.04.07.mov

Comment on lines +61 to +67
if (getDateRange && dateRange.start && dateRange.end) {
const rowsByDate = new Map(rows.map((row) => [row.label, row]));
rows = getPeriodStarts(groupBy, {start: parseISO(dateRange.start), end: parseISO(dateRange.end)}).map((date) => {
const label = format(date, CONST.DATE.FNS_FORMAT_STRING);
return rowsByDate.get(label) ?? {label, count: 0, total: 0, currency};
});
}

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.

Suggested change
if (getDateRange && dateRange.start && dateRange.end) {
const rowsByDate = new Map(rows.map((row) => [row.label, row]));
rows = getPeriodStarts(groupBy, {start: parseISO(dateRange.start), end: parseISO(dateRange.end)}).map((date) => {
const label = format(date, CONST.DATE.FNS_FORMAT_STRING);
return rowsByDate.get(label) ?? {label, count: 0, total: 0, currency};
});
}
if (getDateRange && dateRange.start && dateRange.end) {
const start = parseISO(dateRange.start);
// Don't pad periods that haven't happened yet: a future period has no data yet, so a 0 row would read as "no spend".
const parsedEnd = parseISO(dateRange.end);
const end = parsedEnd > new Date() ? new Date() : parsedEnd;
// Skip zero-fill for an invalid or inverted range (start > end, which also covers a range that begins in the future).
if (isValid(start) && isValid(end) && start <= end) {
const rowsByDate = new Map(rows.map((row) => [row.label, row]));
rows = getPeriodStarts(groupBy, {start, end}).map((date) => {
const label = format(date, CONST.DATE.FNS_FORMAT_STRING);
return rowsByDate.get(label) ?? {label, count: 0, total: 0, currency};
});
}
}
Screen.Recording.2026-10-17.at.16.00.50.mov

@luacmartins I found an interesting case. Nov and Dec are periods that haven't happened yet, but they show up in the CSV as "periods with zero spend", when in reality there is no data for them yet.

Steps to reproduce:

  1. Set the Date filter to a custom range: Oct 1, 2026 to Dec 31, 2026.
  2. Download the CSV from the "Spend over time" chart and open it.

Actual result: the CSV contains rows for Nov and Dec with 0 expenses and 0 total:

Expected result: I think future periods shouldn't appear. They haven't happened yet, so a 0 row reads as "no spend" instead of "no data yet".

@luacmartins luacmartins Oct 19, 2026 •

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.

Expected result: I think future periods shouldn't appear.

I'm not sure. It's possible for users to create expenses in future dates. @JmillsExpensify what's your take here?

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.

Future periods should appear, if they have future dated expenses. I'm not sure it's a bug for this PR really. It's more of a question of whether we should be showing an empty state in that case, versus plotting $0. Isn't that what this PR is doing though? #104337

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.

Yea, that makes sense. So NAB for this PR

@huult

huult commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Check the menus while charts are loading, empty, or showing Refresh needed; confirm Download CSV is hidden.

Screenshot 2026-10-17 at 16 26 29

@luacmartins For a date range with no spend (screenshot), Spend over time still shows "Download CSV" in its menu. The chart renders a flat $0 line, while Top spenders and Top merchants correctly show "Chart unavailable" with no menu. The exported CSV would only contain zero rows. Should Download CSV be hidden here too, or is this chart intentionally not treated as empty?

return;
}

const content = buildInsightsChartCSV({data, groupBy, dateRange: getDateFilterRange(queryJSON), currency: filters.groupCurrency, getCurrencyDecimals, translate});

@huult huult Oct 17, 2026 •

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.

Screen.Recording.2026-10-17.at.16.59.51.mov

the same way View on Spend builds its query:

@luacmartins Issue says the ranking export should follow View on Spend (no limit), but we export data, which is capped at 5 — is top 5 intended, or should it include every group with spend?

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.

Yea, I think top 5 is intended here since that's what the chart shows

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.

Agreed, top 5 is intended.

}

const content = buildInsightsChartCSV({data, groupBy, dateRange: getDateFilterRange(queryJSON), currency: filters.groupCurrency, getCurrencyDecimals, translate});
localFileDownload(`Expensify-${chart.graphKey}.csv`, content, translate);

@huult huult Oct 17, 2026 •

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.

The download reuses our existing export flow; no new component. Small files download immediately, and larger files are delivered by Concierge, as with other exports.

@luacmartins The issue says the download should reuse the existing export flow (Concierge for large files), but this generates the file locally with localFileDownload — is the local approach intended, e.g. to support offline export?

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.

Yea, we changed the approach to local only since that allows for offline export of the data that's already available. cc @JmillsExpensify

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.

Correct. We changed it and I never updated the OP, sorry.

@JmillsExpensify

Copy link
Copy Markdown
Contributor

@JmillsExpensify this already uses the browser's default download flow. If "Ask where to save each file" is enabled in the browser settings, it will still show a save dialog. We can't override that from the app.

Makes sense, thanks.

@huult

huult commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

@luacmartins could you sync with main?

@mhawryluk

Copy link
Copy Markdown
Member

Check the menus while charts are loading, empty, or showing Refresh needed; confirm Download CSV is hidden.

Screenshot 2026-10-17 at 16 26 29 @luacmartins For a date range with no spend (screenshot), Spend over time still shows "Download CSV" in its menu. The chart renders a flat $0 line, while Top spenders and Top merchants correctly show "Chart unavailable" with no menu. The exported CSV would only contain zero rows. Should Download CSV be hidden here too, or is this chart intentionally not treated as empty?

I have a fix PR up that makes the Spend over time correctly recognized as empty in this case: #104337

@neil-marcellini neil-marcellini 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 good thanks

@luacmartins

Copy link
Copy Markdown
Contributor Author

@luacmartins could you sync with main?

Done

@luacmartins

luacmartins commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@huult @neil-marcellini this is ready for you again

@huult huult 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.

@mountiny mountiny 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.

Chat it with Jason, approving this one since Neil already approved, just so we can move it ahead.

@mountiny
mountiny merged commit 630af56 into main Oct 20, 2026
43 of 53 checks passed
@mountiny
mountiny deleted the cmartins-insights-chart-csv branch October 20, 2026 07:11
@github-actions

Copy link
Copy Markdown
Contributor

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

@OSBotify

Copy link
Copy Markdown
Contributor

✋ 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

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.15-1 🚀

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

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update is needed: the new Download CSV chart option lives only on the Insights dashboard, which is still gated behind the INSIGHTS_PAGE beta (InsightsPage.tsx:27).

Details
  • InsightsChartWidget is only rendered by InsightsDashboard, so most users can't see the new menu item yet. Document it when the beta is removed.
  • The existing How to Use Insights article covers the Search-based Insights reports, not this beta dashboard. Its sentence "Insights cannot export grouped totals directly" stays accurate for that feature.
  • The company card and Expensify Card CSV changes only move shared escaping into escapeCsvField and guard against spreadsheet formulas. There is no visible change to how users export.

view run

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.

8 participants