Skip to content

A local-first personal finance app

"has tags" filter (#3290)

lelemm7769d0303e1f
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png
index 4a682c1f0e..a099f6a117 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png
index f98e9d0f63..1b27a5bcb6 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-3-chromium-linux.png b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-3-chromium-linux.png
index 6e51c3f9fa..8fc889f31b 100644
Binary files a/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-3-chromium-linux.png and b/packages/desktop-client/e2e/transactions.test.js-snapshots/Transactions-filters-transactions-by-category-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/filters/FilterExpression.tsx b/packages/desktop-client/src/components/filters/FilterExpression.tsx
index d649fd09c3..34df7694a7 100644
--- a/packages/desktop-client/src/components/filters/FilterExpression.tsx
+++ b/packages/desktop-client/src/components/filters/FilterExpression.tsx
@@ -79,7 +79,8 @@ export function FilterExpression<T extends RuleConditionEntity>({
valueIsRaw={
op === 'contains' ||
op === 'matches' ||
- op === 'doesNotContain'
+ op === 'doesNotContain' ||
+ op === 'hasTags'
}
/>
</>
diff --git a/packages/desktop-client/src/components/filters/FiltersMenu.jsx b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
index 61720dff6d..8baacffe99 100644
--- a/packages/desktop-client/src/components/filters/FiltersMenu.jsx
+++ b/packages/desktop-client/src/components/filters/FiltersMenu.jsx
@@ -201,7 +201,10 @@ function ConfigureField({
subfield={subfield}
type={
type === 'id' &&
- (op === 'contains' || op === 'matches' || op === 'doesNotContain')
+ (op === 'contains' ||
+ op === 'matches' ||
+ op === 'doesNotContain' ||
+ op === 'hasTags')
? 'string'
: type
}
diff --git a/packages/desktop-client/src/components/filters/updateFilterReducer.ts b/packages/desktop-client/src/components/filters/updateFilterReducer.ts
index 5cb3727367..4f6286f838 100644
--- a/packages/desktop-client/src/components/filters/updateFilterReducer.ts
+++ b/packages/desktop-client/src/components/filters/updateFilterReducer.ts
@@ -18,7 +18,8 @@ export function updateFilterReducer(
action.op === 'matches' ||
action.op === 'is' ||
action.op === 'doesNotContain' ||
- action.op === 'isNot')
+ action.op === 'isNot' ||
+ action.op === 'hasTags')
) {
// Clear out the value if switching between contains or
// is/oneof for the id or string type
diff --git a/packages/desktop-client/src/components/modals/EditRule.jsx b/packages/desktop-client/src/components/modals/EditRule.jsx
index b896846fcf..c36e331dc9 100644
--- a/packages/desktop-client/src/components/modals/EditRule.jsx
+++ b/packages/desktop-client/src/components/modals/EditRule.jsx
@@ -116,7 +116,7 @@ export function OpSelect({
// TODO: Add matches op support for payees, accounts, categories.
.filter(op =>
type === 'id'
- ? !['contains', 'matches', 'doesNotContain'].includes(op)
+ ? !['contains', 'matches', 'doesNotContain', 'hasTags'].includes(op)
: true,
)
.map(op => [op, formatOp(op, type)]);
diff --git a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
index c0ed863034..df09f6eed2 100644
--- a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
+++ b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
@@ -155,7 +155,7 @@ export function CustomReport() {
!!conditions.find(
({ field, op }) =>
field === 'category' &&
- ['contains', 'doesNotContain', 'matches'].includes(op),
+ ['contains', 'doesNotContain', 'matches', 'hasTags'].includes(op),
) || conditions.filter(({ field }) => field === 'category').length >= 2;
const setSelectedCategories = (newCategories: CategoryEntity[]) => {
diff --git a/packages/desktop-client/src/components/transactions/TransactionList.jsx b/packages/desktop-client/src/components/transactions/TransactionList.jsx
index 9b3eb7895f..14fee62c9a 100644
--- a/packages/desktop-client/src/components/transactions/TransactionList.jsx
+++ b/packages/desktop-client/src/components/transactions/TransactionList.jsx
@@ -1,8 +1,6 @@
import React, { useRef, useCallback, useLayoutEffect } from 'react';
import { useDispatch } from 'react-redux';
-import escapeRegExp from 'lodash/escapeRegExp';
-
import { pushModal } from 'loot-core/client/actions';
import { send } from 'loot-core/src/platform/client/fetch';
import {
@@ -191,8 +189,8 @@ export function TransactionList({
const onNotesTagClick = useCallback(tag => {
onApplyFilter({
field: 'notes',
- op: 'matches',
- value: `(^|\\s|\\w)${escapeRegExp(tag)}($|\\s|#)`,
+ op: 'hasTags',
+ value: tag,
type: 'string',
});
});
diff --git a/packages/loot-core/src/server/accounts/rules.ts b/packages/loot-core/src/server/accounts/rules.ts
index 467dc7bc80..e97fa8ef2f 100644
--- a/packages/loot-core/src/server/accounts/rules.ts
+++ b/packages/loot-core/src/server/accounts/rules.ts
@@ -147,6 +147,46 @@ const CONDITION_TYPES = {
},
},
string: {
+ ops: [
+ 'is',
+ 'contains',
+ 'matches',
+ 'oneOf',
+ 'isNot',
+ 'doesNotContain',
+ 'notOneOf',
+ 'hasTags',
+ ],
+ nullable: true,
+ parse(op, value, fieldName) {
+ if (op === 'oneOf' || op === 'notOneOf') {
+ assert(
+ Array.isArray(value),
+ 'no-empty-array',
+ `oneOf must have an array value (field: ${fieldName}): ${JSON.stringify(
+ value,
+ )}`,
+ );
+ return value.filter(Boolean).map(val => val.toLowerCase());
+ }
+
+ if (
+ op === 'contains' ||
+ op === 'matches' ||
+ op === 'doesNotContain' ||
+ op === 'hasTags'
+ ) {
+ assert(
+ typeof value === 'string' && value.length > 0,
+ 'no-empty-string',
+ `contains must have non-empty string (field: ${fieldName})`,
+ );
+ }
+
+ return value.toLowerCase();
+ },
+ },
+ imported_payee: {
ops: [
'is',
'contains',
@@ -379,6 +419,13 @@ export class Condition {
return false;
}
return this.value.indexOf(fieldValue) !== -1;
+
+ case 'hasTags':
+ if (fieldValue === null) {
+ return false;
+ }
+ return fieldValue.indexOf(this.value) !== -1;
+
case 'notOneOf':
if (fieldValue === null) {
return false;
@@ -863,6 +910,7 @@ const OP_SCORES: Record<RuleConditionEntity['op'], number> = {
contains: 0,
doesNotContain: 0,
matches: 0,
+ hasTags: 0,
};
function computeScore(rule) {
diff --git a/packages/loot-core/src/server/accounts/transaction-rules.ts b/packages/loot-core/src/server/accounts/transaction-rules.ts
index 66024acf8d..e22697ea40 100644
--- a/packages/loot-core/src/server/accounts/transaction-rules.ts
+++ b/packages/loot-core/src/server/accounts/transaction-rules.ts
@@ -472,6 +472,19 @@ export function conditionsToAQL(conditions, { recurDateBounds = 100 } = {}) {
return { id: null };
}
return { $or: values.map(v => apply(field, '$eq', v)) };
+
+ case 'hasTags':
+ const tagValues = value
+ .split(/(?<!#)(#[\w\d\p{Emoji}-]+)(?=\s|$)/gu)
+ .filter(tag => tag.startsWith('#'));
+
+ return {
+ $and: tagValues.map(v => {
+ const regex = new RegExp(`(^|\\s)${v}(\\s|$)`);
+ return apply(field, '$regexp', regex.source);
+ }),
+ };
+
case 'notOneOf':
const notValues = value;
if (notValues.length === 0) {
diff --git a/packages/loot-core/src/shared/rules.ts b/packages/loot-core/src/shared/rules.ts
index b920d1b0cb..76eaa29260 100644
--- a/packages/loot-core/src/shared/rules.ts
+++ b/packages/loot-core/src/shared/rules.ts
@@ -17,6 +17,7 @@ export const TYPE_INFO = {
'isNot',
'doesNotContain',
'notOneOf',
+ 'hasTags',
],
nullable: true,
},
@@ -25,6 +26,19 @@ export const TYPE_INFO = {
nullable: false,
},
string: {
+ ops: [
+ 'is',
+ 'contains',
+ 'matches',
+ 'oneOf',
+ 'isNot',
+ 'doesNotContain',
+ 'notOneOf',
+ 'hasTags',
+ ],
+ nullable: true,
+ },
+ imported_payee: {
ops: [
'is',
'contains',
@@ -48,7 +62,7 @@ export const TYPE_INFO = {
export const FIELD_TYPES = new Map(
Object.entries({
- imported_payee: 'string',
+ imported_payee: 'imported_payee',
payee: 'id',
date: 'date',
notes: 'string',
@@ -107,6 +121,8 @@ export function friendlyOp(op, type?) {
return 'is between';
case 'contains':
return 'contains';
+ case 'hasTags':
+ return 'has tag(s)';
case 'matches':
return 'matches';
case 'doesNotContain':
diff --git a/packages/loot-core/src/types/models/rule.d.ts b/packages/loot-core/src/types/models/rule.d.ts
index 3c61ebf70a..bacb94ca55 100644
--- a/packages/loot-core/src/types/models/rule.d.ts
+++ b/packages/loot-core/src/types/models/rule.d.ts
@@ -25,6 +25,7 @@ export type RuleConditionOp =
| 'lte'
| 'contains'
| 'doesNotContain'
+ | 'hasTags'
| 'matches';
type FieldValueTypes = {
@@ -96,6 +97,7 @@ export type RuleConditionEntity =
| 'contains'
| 'doesNotContain'
| 'matches'
+ | 'hasTags'
>
| BaseConditionEntity<
'payee',
diff --git a/upcoming-release-notes/3290.md b/upcoming-release-notes/3290.md
new file mode 100644
index 0000000000..5cbcce408c
--- /dev/null
+++ b/upcoming-release-notes/3290.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [lelemm]
+---
+
+Add new 'has tag(s)' filter to filter note tags.