Skip to content

A local-first personal finance app

Add sorting option to custom reports (#4141)

Matt Fiddamanbec841932d1f
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-1-chromium-linux.png
index f95e2069d7..f35536a052 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-2-chromium-linux.png
index 515682792f..8895034178 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-3-chromium-linux.png
index f88ba16bd1..b25b2a374f 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Data-Table-and-checks-the-visuals-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png
index de3ebd81cd..e723e78cae 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png
index 163556249a..bb49a78e21 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png
index 75342c0ebc..5a3da2c6e1 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Switches-to-Donut-Graph-and-checks-the-visuals-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-1-chromium-linux.png
index d26d5a37af..a64192fb93 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-2-chromium-linux.png
index 55fe92047a..3b34a76a79 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-3-chromium-linux.png
index d2ebbf9d9e..b13fa54483 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-labels-button-shows-the-labels-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-1-chromium-linux.png
index d80f8f5fec..e35ae86c41 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-2-chromium-linux.png
index 7600ed988a..49a0c2af9d 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-3-chromium-linux.png
index a36d9fda9e..f7654637a5 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-legend-button-shows-the-legend-side-bar-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-1-chromium-linux.png
index 0bc78b0705..de1e45ad17 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-2-chromium-linux.png
index 095c2db3de..dafdb37202 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-3-chromium-linux.png
index 2196282f42..5f103071d2 100644
Binary files a/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.js-snapshots/Reports-custom-reports-Validates-that-show-summary-button-shows-the-summary-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/reports/ReportOptions.ts b/packages/desktop-client/src/components/reports/ReportOptions.ts
index 953771f1f6..1d2a2a8684 100644
--- a/packages/desktop-client/src/components/reports/ReportOptions.ts
+++ b/packages/desktop-client/src/components/reports/ReportOptions.ts
@@ -23,6 +23,7 @@ export const defaultReport: CustomReportEntity = {
groupBy: 'Category',
interval: 'Monthly',
balanceType: 'Payment',
+ sortBy: 'Descending',
showEmpty: false,
showOffBudget: false,
showHiddenCategories: false,
@@ -49,6 +50,13 @@ const groupByOptions = [
{ description: 'Interval' },
];
+const sortByOptions = [
+ { description: t('Ascending'), format: 'asc' as const },
+ { description: t('Descending'), format: 'desc' as const },
+ { description: t('Name'), format: 'name' as const },
+ { description: t('Budget'), format: 'budget' as const },
+];
+
export type dateRangeProps = {
description: string;
name: number | string;
@@ -198,6 +206,10 @@ export const ReportOptions = {
balanceTypeMap: new Map(
balanceTypeOptions.map(item => [item.description, item.format]),
),
+ sortBy: sortByOptions,
+ sortByMap: new Map(
+ sortByOptions.map(item => [item.description, item.format]),
+ ),
dateRange: dateRangeOptions,
dateRangeMap: new Map(
dateRangeOptions.map(item => [item.description, item.name]),
diff --git a/packages/desktop-client/src/components/reports/ReportSidebar.tsx b/packages/desktop-client/src/components/reports/ReportSidebar.tsx
index 85f81d4007..e4bc7ccf52 100644
--- a/packages/desktop-client/src/components/reports/ReportSidebar.tsx
+++ b/packages/desktop-client/src/components/reports/ReportSidebar.tsx
@@ -39,6 +39,7 @@ type ReportSidebarProps = {
setGroupBy: (value: string) => void;
setInterval: (value: string) => void;
setBalanceType: (value: string) => void;
+ setSortBy: (value: string) => void;
setMode: (value: string) => void;
setIsDateStatic: (value: boolean) => void;
setShowEmpty: (value: boolean) => void;
@@ -72,6 +73,7 @@ export function ReportSidebar({
setGroupBy,
setInterval,
setBalanceType,
+ setSortBy,
setMode,
setIsDateStatic,
setShowEmpty,
@@ -141,6 +143,12 @@ export function ReportSidebar({
setBalanceType(cond);
};
+ const onChangeSortBy = (cond: string) => {
+ setSessionReport('sortBy', cond);
+ onReportChange({ type: 'modify' });
+ setSortBy(cond);
+ };
+
const rangeOptions = useMemo(() => {
const options: SelectOption[] = ReportOptions.dateRange
.filter(f => f[customReportItems.interval as keyof dateRangeProps])
@@ -153,6 +161,15 @@ export function ReportSidebar({
return options;
}, [customReportItems, dateRangeLine]);
+ const disableSort =
+ customReportItems.graphType !== 'TableGraph' &&
+ (customReportItems.groupBy === 'Interval' ||
+ (disabledList?.mode
+ ?.find(m => m.description === customReportItems.mode)
+ ?.graphs.find(g => g.description === customReportItems.graphType)
+ ?.disableSort ??
+ false));
+
return (
<View
style={{
@@ -266,6 +283,30 @@ export function ReportSidebar({
disabledKeys={[]}
/>
</View>
+
+ {!disableSort && (
+ <View
+ style={{
+ flexDirection: 'row',
+ padding: 5,
+ alignItems: 'center',
+ }}
+ >
+ <Text style={{ width: 50, textAlign: 'right', marginRight: 5 }}>
+ {t('Sort:')}
+ </Text>
+ <Select
+ value={customReportItems.sortBy}
+ onChange={e => onChangeSortBy(e)}
+ options={ReportOptions.sortBy.map(option => [
+ option.description,
+ option.description,
+ ])}
+ disabledKeys={disabledItems('sort')}
+ />
+ </View>
+ )}
+
<View
style={{
flexDirection: 'row',
diff --git a/packages/desktop-client/src/components/reports/disabledList.ts b/packages/desktop-client/src/components/reports/disabledList.ts
index 99ebe0f79c..214d682c9e 100644
--- a/packages/desktop-client/src/components/reports/disabledList.ts
+++ b/packages/desktop-client/src/components/reports/disabledList.ts
@@ -48,8 +48,10 @@ type graphOptions = {
defaultSplit: string;
disabledType: string[];
defaultType: string;
+ defaultSort: string;
disableLegend?: boolean;
disableLabel?: boolean;
+ disableSort?: boolean;
};
const totalGraphOptions: graphOptions[] = [
{
@@ -60,6 +62,7 @@ const totalGraphOptions: graphOptions[] = [
defaultType: 'Payment',
disableLegend: true,
disableLabel: true,
+ defaultSort: 'Budget',
},
{
description: 'BarGraph',
@@ -67,6 +70,7 @@ const totalGraphOptions: graphOptions[] = [
defaultSplit: 'Category',
disabledType: [],
defaultType: 'Payment',
+ defaultSort: 'Descending',
},
{
description: 'AreaGraph',
@@ -75,6 +79,8 @@ const totalGraphOptions: graphOptions[] = [
disabledType: [],
defaultType: 'Payment',
disableLegend: true,
+ disableSort: true,
+ defaultSort: 'Descending',
},
{
description: 'DonutGraph',
@@ -82,6 +88,7 @@ const totalGraphOptions: graphOptions[] = [
defaultSplit: 'Category',
disabledType: ['Net'],
defaultType: 'Payment',
+ defaultSort: 'Descending',
},
];
@@ -94,6 +101,8 @@ const timeGraphOptions: graphOptions[] = [
defaultType: 'Payment',
disableLegend: true,
disableLabel: true,
+ disableSort: true,
+ defaultSort: 'Descending',
},
{
description: 'StackedBarGraph',
@@ -101,6 +110,8 @@ const timeGraphOptions: graphOptions[] = [
defaultSplit: 'Category',
disabledType: [],
defaultType: 'Payment',
+ disableSort: true,
+ defaultSort: 'Descending',
},
{
description: 'LineGraph',
@@ -110,6 +121,8 @@ const timeGraphOptions: graphOptions[] = [
defaultType: 'Payment',
disableLegend: false,
disableLabel: true,
+ disableSort: true,
+ defaultSort: 'Descending',
},
];
@@ -169,7 +182,7 @@ export function disabledLegendLabel(
export function defaultsGraphList(
item: string,
newGraph: string,
- type: 'defaultSplit' | 'defaultType',
+ type: 'defaultSplit' | 'defaultType' | 'defaultSort',
) {
const graphList = modeOptions.find(d => d.description === item);
if (!graphList) {
diff --git a/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx b/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
index ebc4a6af65..e32a841441 100644
--- a/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
@@ -1,5 +1,5 @@
// @ts-strict-ignore
-import React, { useMemo, useState, type CSSProperties } from 'react';
+import React, { useState, type CSSProperties } from 'react';
import { useTranslation } from 'react-i18next';
import { css } from '@emotion/css';
@@ -203,12 +203,6 @@ export function BarGraph({
const leftMargin = Math.abs(largestValue) > 1000000 ? 20 : 0;
- // Sort the data in the bar chart
- const unsortedData = data[splitData];
- const sortedData = useMemo(() => {
- return unsortedData.sort((a, b) => a[balanceTypeOp] - b[balanceTypeOp]);
- }, [unsortedData, balanceTypeOp]);
-
return (
<Container
style={{
@@ -225,7 +219,7 @@ export function BarGraph({
width={width}
height={height}
stackOffset="sign"
- data={sortedData}
+ data={data[splitData]}
style={{ cursor: pointer }}
margin={{
top: labelsMargin,
diff --git a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
index 4dae3d6d88..40a88443f0 100644
--- a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
@@ -241,17 +241,13 @@ export function DonutGraph({
const [activeIndex, setActiveIndex] = useState(0);
- // Sort the data in the pie chart
- const unsortedData = data[splitData];
- const sortedData = unsortedData.slice().sort((a, b) => getVal(b) - getVal(a));
-
return (
<Container style={style}>
{(width, height) => {
const compact = height <= 300 || width <= 300;
return (
- sortedData && (
+ data[splitData] && (
<ResponsiveContainer>
<div>
{!compact && <div style={{ marginTop: '15px' }} />}
@@ -272,7 +268,7 @@ export function DonutGraph({
dataKey={val => getVal(val)}
nameKey={yAxis}
isAnimationActive={false}
- data={sortedData}
+ data={data[splitData]}
innerRadius={Math.min(width, height) * 0.2}
fill="#8884d8"
labelLine={false}
diff --git a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
index 170e11bfaf..c88d0abad4 100644
--- a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
+++ b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
@@ -12,6 +12,7 @@ import { amountToCurrency } from 'loot-core/src/shared/util';
import { type CategoryEntity } from 'loot-core/types/models/category';
import {
type balanceTypeOpType,
+ type sortByOpType,
type CustomReportEntity,
type DataEntity,
} from 'loot-core/types/models/reports';
@@ -230,6 +231,8 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
const [groupBy, setGroupBy] = useState(loadReport.groupBy);
const [interval, setInterval] = useState(loadReport.interval);
const [balanceType, setBalanceType] = useState(loadReport.balanceType);
+ const [sortBy, setSortBy] = useState(loadReport.sortBy);
+
const [showEmpty, setShowEmpty] = useState(loadReport.showEmpty);
const [showOffBudget, setShowOffBudget] = useState(loadReport.showOffBudget);
const [includeCurrentInterval, setIncludeCurrentInterval] = useState(
@@ -359,6 +362,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
const balanceTypeOp: balanceTypeOpType =
ReportOptions.balanceTypeMap.get(balanceType) || 'totalDebts';
+ const sortByOp: sortByOpType = ReportOptions.sortByMap.get(sortBy) || 'desc';
const payees = usePayees();
const accounts = useAccounts();
@@ -381,6 +385,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
showHiddenCategories,
showUncategorized,
balanceTypeOp,
+ sortByOp,
firstDayOfWeekIdx,
});
}, [
@@ -395,6 +400,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
showOffBudget,
showHiddenCategories,
showUncategorized,
+ sortByOp,
firstDayOfWeekIdx,
]);
@@ -414,6 +420,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
showUncategorized,
groupBy,
balanceTypeOp,
+ sortByOp,
payees,
accounts,
graphType,
@@ -435,6 +442,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
showOffBudget,
showHiddenCategories,
showUncategorized,
+ sortByOp,
graphType,
firstDayOfWeekIdx,
]);
@@ -454,6 +462,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
groupBy,
interval,
balanceType,
+ sortBy,
showEmpty,
showOffBudget,
showHiddenCategories,
@@ -533,6 +542,12 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
setSessionReport('balanceType', cond);
setBalanceType(cond);
}
+
+ const defaultSort = defaultsGraphList(mode, chooseGraph, 'defaultSort');
+ if (defaultSort) {
+ setSessionReport('sortBy', defaultSort);
+ setSortBy(defaultSort);
+ }
};
const isItemDisabled = (type: string) => {
@@ -592,6 +607,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
setGroupBy(input.groupBy);
setInterval(input.interval);
setBalanceType(input.balanceType);
+ setSortBy(input.sortBy);
setShowEmpty(input.showEmpty);
setShowOffBudget(input.showOffBudget);
setShowHiddenCategories(input.showHiddenCategories);
@@ -722,6 +738,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
setGroupBy={setGroupBy}
setInterval={setInterval}
setBalanceType={setBalanceType}
+ setSortBy={setSortBy}
setMode={setMode}
setIsDateStatic={setIsDateStatic}
setShowEmpty={setShowEmpty}
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/custom-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/custom-spreadsheet.ts
index bbfec73387..dfd11e58fa 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/custom-spreadsheet.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/custom-spreadsheet.ts
@@ -14,6 +14,7 @@ import {
} from 'loot-core/src/types/models';
import {
type balanceTypeOpType,
+ type sortByOpType,
type DataEntity,
type GroupedEntity,
type IntervalEntity,
@@ -33,6 +34,7 @@ import { filterEmptyRows } from './filterEmptyRows';
import { filterHiddenItems } from './filterHiddenItems';
import { makeQuery } from './makeQuery';
import { recalculate } from './recalculate';
+import { sortData } from './sortData';
export type createCustomSpreadsheetProps = {
startDate: string;
@@ -47,6 +49,7 @@ export type createCustomSpreadsheetProps = {
showUncategorized: boolean;
groupBy?: string;
balanceTypeOp?: balanceTypeOpType;
+ sortByOp?: sortByOpType;
payees?: PayeeEntity[];
accounts?: AccountEntity[];
graphType?: string;
@@ -67,6 +70,7 @@ export function createCustomSpreadsheet({
showUncategorized,
groupBy = '',
balanceTypeOp = 'totalDebts',
+ sortByOp = 'desc',
payees = [],
accounts = [],
graphType,
@@ -281,8 +285,12 @@ export function createCustomSpreadsheet({
balanceTypeOp,
);
+ const sortedCalcDataFiltered = [...calcDataFiltered].sort(
+ sortData({ balanceTypeOp, sortByOp }),
+ );
+
setData({
- data: calcDataFiltered,
+ data: sortedCalcDataFiltered,
intervalData,
legend,
startDate,
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/grouped-spreadsheet.ts b/packages/desktop-client/src/components/reports/spreadsheets/grouped-spreadsheet.ts
index 799f0589ba..a62af48fd6 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/grouped-spreadsheet.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/grouped-spreadsheet.ts
@@ -14,6 +14,7 @@ import { type createCustomSpreadsheetProps } from './custom-spreadsheet';
import { filterEmptyRows } from './filterEmptyRows';
import { makeQuery } from './makeQuery';
import { recalculate } from './recalculate';
+import { sortData } from './sortData';
export function createGroupedSpreadsheet({
startDate,
@@ -27,6 +28,7 @@ export function createGroupedSpreadsheet({
showHiddenCategories,
showUncategorized,
balanceTypeOp,
+ sortByOp,
firstDayOfWeekIdx,
}: createCustomSpreadsheetProps) {
const [categoryList, categoryGroup] = categoryLists(categories);
@@ -135,10 +137,20 @@ export function createGroupedSpreadsheet({
},
[startDate, endDate],
);
- setData(
- groupedData.filter(i =>
- filterEmptyRows({ showEmpty, data: i, balanceTypeOp }),
- ),
+
+ const groupedDataFiltered = groupedData.filter(i =>
+ filterEmptyRows({ showEmpty, data: i, balanceTypeOp }),
);
+
+ const sortedGroupedDataFiltered = [...groupedDataFiltered]
+ .sort(sortData({ balanceTypeOp, sortByOp }))
+ .map(g => {
+ g.categories = [...(g.categories ?? [])].sort(
+ sortData({ balanceTypeOp, sortByOp }),
+ );
+ return g;
+ });
+
+ setData(sortedGroupedDataFiltered);
};
}
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/sortData.ts b/packages/desktop-client/src/components/reports/spreadsheets/sortData.ts
new file mode 100644
index 0000000000..f22c922fe7
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/spreadsheets/sortData.ts
@@ -0,0 +1,45 @@
+import {
+ type balanceTypeOpType,
+ type sortByOpType,
+ type GroupedEntity,
+} from 'loot-core/src/types/models/reports';
+
+const reverseSort: Partial<Record<sortByOpType, sortByOpType>> = {
+ asc: 'desc',
+ desc: 'asc',
+};
+
+const balanceTypesToReverse = ['totalDebts', 'netDebts'];
+
+const shouldReverse = (balanceTypeOp: balanceTypeOpType) =>
+ balanceTypesToReverse.includes(balanceTypeOp);
+
+export function sortData({
+ balanceTypeOp,
+ sortByOp,
+}: {
+ balanceTypeOp?: balanceTypeOpType;
+ sortByOp?: sortByOpType;
+}): (a: GroupedEntity, b: GroupedEntity) => number {
+ if (!balanceTypeOp || !sortByOp) return () => 0;
+
+ if (shouldReverse(balanceTypeOp)) {
+ sortByOp = reverseSort[sortByOp] ?? sortByOp;
+ }
+
+ // Return a comparator function
+ return (a, b) => {
+ let comparison = 0;
+ if (sortByOp === 'asc') {
+ comparison = a[balanceTypeOp] - b[balanceTypeOp];
+ } else if (sortByOp === 'desc') {
+ comparison = b[balanceTypeOp] - a[balanceTypeOp];
+ } else if (sortByOp === 'name') {
+ comparison = (a.name ?? '').localeCompare(b.name ?? '');
+ } else if (sortByOp === 'budget') {
+ comparison = 0;
+ }
+
+ return comparison;
+ };
+}
diff --git a/packages/loot-core/migrations/1736640000000__custom_report_sorting.sql b/packages/loot-core/migrations/1736640000000__custom_report_sorting.sql
new file mode 100644
index 0000000000..63489fe046
--- /dev/null
+++ b/packages/loot-core/migrations/1736640000000__custom_report_sorting.sql
@@ -0,0 +1,7 @@
+BEGIN TRANSACTION;
+
+ALTER TABLE custom_reports ADD COLUMN sort_by TEXT DEFAULT 'Descending';
+UPDATE custom_reports SET sort_by = 'Descending';
+UPDATE custom_reports SET sort_by = 'Budget' where graph_type = 'TableGraph';
+
+COMMIT;
diff --git a/packages/loot-core/src/client/data-hooks/reports.ts b/packages/loot-core/src/client/data-hooks/reports.ts
index 01f6fdf2df..d10acfe659 100644
--- a/packages/loot-core/src/client/data-hooks/reports.ts
+++ b/packages/loot-core/src/client/data-hooks/reports.ts
@@ -18,6 +18,7 @@ function toJS(rows: CustomReportData[]) {
dateRange: row.date_range,
mode: row.mode,
groupBy: row.group_by,
+ sortBy: row.sort_by,
interval: row.interval,
balanceType: row.balance_type,
showEmpty: row.show_empty === 1,
diff --git a/packages/loot-core/src/server/aql/schema/index.ts b/packages/loot-core/src/server/aql/schema/index.ts
index ddcac48056..f5e6dc47aa 100644
--- a/packages/loot-core/src/server/aql/schema/index.ts
+++ b/packages/loot-core/src/server/aql/schema/index.ts
@@ -142,6 +142,7 @@ export const schema = {
date_range: f('string'),
mode: f('string', { default: 'total' }),
group_by: f('string', { default: 'Category' }),
+ sort_by: f('string', { default: 'desc' }),
balance_type: f('string', { default: 'Expense' }),
show_empty: f('integer', { default: 0 }),
show_offbudget: f('integer', { default: 0 }),
diff --git a/packages/loot-core/src/server/reports/app.ts b/packages/loot-core/src/server/reports/app.ts
index 889432c145..e1356696ca 100644
--- a/packages/loot-core/src/server/reports/app.ts
+++ b/packages/loot-core/src/server/reports/app.ts
@@ -41,6 +41,7 @@ export const reportModel = {
dateRange: row.date_range,
mode: row.mode,
groupBy: row.group_by,
+ sortBy: row.sort_by,
interval: row.interval,
balanceType: row.balance_type,
showEmpty: row.show_empty === 1,
@@ -64,6 +65,7 @@ export const reportModel = {
date_range: report.dateRange,
mode: report.mode,
group_by: report.groupBy,
+ sort_by: report.sortBy,
interval: report.interval,
balance_type: report.balanceType,
show_empty: report.showEmpty ? 1 : 0,
@@ -96,7 +98,7 @@ async function reportNameExists(
//for update/rename
if (!newItem) {
/*
- -if the found item is the same as the existing item
+ -if the found item is the same as the existing item
then no name change was made.
-if they are not the same then there is another
item with that name already.
diff --git a/packages/loot-core/src/types/models/reports.d.ts b/packages/loot-core/src/types/models/reports.d.ts
index e59e655241..d109a1af02 100644
--- a/packages/loot-core/src/types/models/reports.d.ts
+++ b/packages/loot-core/src/types/models/reports.d.ts
@@ -11,6 +11,7 @@ export interface CustomReportEntity {
groupBy: string;
interval: string;
balanceType: string;
+ sortBy: string;
showEmpty: boolean;
showOffBudget: boolean;
showHiddenCategories: boolean;
@@ -30,6 +31,8 @@ export type balanceTypeOpType =
| 'netAssets'
| 'netDebts';
+export type sortByOpType = 'asc' | 'desc' | 'name' | 'budget';
+
export type SpendingMonthEntity = Record<
string | number,
{
@@ -122,6 +125,7 @@ export interface CustomReportData {
date_range: string;
mode: string;
group_by: string;
+ sort_by: sortByOpType;
balance_type: string;
show_empty: number;
show_offbudget: number;
diff --git a/upcoming-release-notes/4141.md b/upcoming-release-notes/4141.md
new file mode 100644
index 0000000000..f726a60b78
--- /dev/null
+++ b/upcoming-release-notes/4141.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [matt-fidd]
+---
+
+Add sorting options to custom reports