Skip to content

Commit

Permalink
automagically reverse sort direction, add options for alphabetical an…
Browse files Browse the repository at this point in the history
…d budget sort order
  • Loading branch information
matt-fidd committed Jan 13, 2025
1 parent 04d7609 commit dc8cd63
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const defaultReport: CustomReportEntity = {
groupBy: 'Category',
interval: 'Monthly',
balanceType: 'Payment',
sortBy: 'Ascending',
sortBy: 'Descending',
showEmpty: false,
showOffBudget: false,
showHiddenCategories: false,
Expand Down Expand Up @@ -53,6 +53,8 @@ const groupByOptions = [
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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {

const balanceTypeOp: balanceTypeOpType =
ReportOptions.balanceTypeMap.get(balanceType) || 'totalDebts';
const sortByOp: sortByOpType = ReportOptions.sortByMap.get(sortBy) || 'asc';
const sortByOp: sortByOpType = ReportOptions.sortByMap.get(sortBy) || 'desc';
const payees = usePayees();
const accounts = useAccounts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createCustomSpreadsheet({
showUncategorized,
groupBy = '',
balanceTypeOp = 'totalDebts',
sortByOp = 'asc',
sortByOp = 'desc',
payees = [],
accounts = [],
graphType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@ import {
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) => {
if (!balanceTypeOp) return 0;

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BEGIN TRANSACTION;

ALTER TABLE custom_reports ADD COLUMN sort_by TEXT DEFAULT 'asc';
UPDATE custom_reports SET sort_by = 'asc';
ALTER TABLE custom_reports ADD COLUMN sort_by TEXT DEFAULT 'desc';
UPDATE custom_reports SET sort_by = 'desc';

COMMIT;
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/aql/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +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: 'asc' }),
sort_by: f('string', { default: 'desc' }),
balance_type: f('string', { default: 'Expense' }),
show_empty: f('integer', { default: 0 }),
show_offbudget: f('integer', { default: 0 }),
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/types/models/reports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type balanceTypeOpType =
| 'netAssets'
| 'netDebts';

export type sortByOpType = 'asc' | 'desc';
export type sortByOpType = 'asc' | 'desc' | 'name' | 'budget';

export type SpendingMonthEntity = Record<
string | number,
Expand Down

0 comments on commit dc8cd63

Please sign in to comment.