Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

feat(rules): group rule extensions by category and make dropdown searchable (#388)

Danny Felizd2e069e2154a
diff --git a/frontend/src/components/transaction-dialog.tsx b/frontend/src/components/transaction-dialog.tsx
index 28d2ea08..c5f14fa5 100644
--- a/frontend/src/components/transaction-dialog.tsx
+++ b/frontend/src/components/transaction-dialog.tsx
@@ -7,11 +7,13 @@ import { useAuth } from '@/contexts/auth-context'
import { currencies as currenciesApi, transactions as transactionsApi, settings as settingsApi, payees as payeesApi, rules as rulesApi } from '@/lib/api'
import { invalidateFinancialQueries } from '@/lib/invalidate-queries'
import { normalizeRuleMatchValue } from '@/lib/rule-match-utils'
-import { cn } from '@/lib/utils'
+import { cn, normalizeText } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { DatePickerInput } from '@/components/ui/date-picker-input'
+import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
+import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from '@/components/ui/command'
import {
Dialog,
DialogContent,
@@ -20,7 +22,7 @@ import {
DialogTitle,
DialogFooter,
} from '@/components/ui/dialog'
-import { AlertTriangle, ChevronDown, ChevronLeft, Download, Eye, EyeClosed, Paperclip, Upload, X, FileText, Plus, Unlink, SlidersHorizontal, ListPlus } from 'lucide-react'
+import { AlertTriangle, ChevronDown, ChevronLeft, Download, Eye, EyeClosed, Paperclip, Upload, X, FileText, Plus, Unlink, SlidersHorizontal, ListPlus, Check } from 'lucide-react'
import {
DropdownMenu,
DropdownMenuContent,
@@ -1149,6 +1151,7 @@ function TransactionForm({
transactionDescription={transaction.description}
rules={extendableRules}
categories={categories}
+ categoryGroups={categoryGroups}
loadingRules={rulesLoading}
loading={extendRuleMutation.isPending}
onSubmit={({ rule, condition }) => extendRuleMutation.mutate({ rule, condition })}
@@ -1164,6 +1167,7 @@ function AddTransactionToRuleDialog({
transactionDescription,
rules,
categories,
+ categoryGroups,
loadingRules,
loading,
onSubmit,
@@ -1173,12 +1177,14 @@ function AddTransactionToRuleDialog({
transactionDescription: string
rules: Rule[]
categories: Category[]
+ categoryGroups: CategoryGroup[]
loadingRules: boolean
loading: boolean
onSubmit: (data: { rule: Rule; condition: RuleCondition }) => void
}) {
const { t } = useTranslation()
const [ruleId, setRuleId] = useState('')
+ const [openCombobox, setOpenCombobox] = useState(false)
const [matchOp, setMatchOp] = useState<'contains' | 'starts_with'>('contains')
const [matchText, setMatchText] = useState(transactionDescription)
@@ -1187,10 +1193,42 @@ function AddTransactionToRuleDialog({
: rules[0]?.id ?? ''
const selectedRule = rules.find(rule => rule.id === effectiveRuleId) ?? null
- function getCategoryName(rule: Rule): string {
- const categoryId = getRuleCategoryId(rule)
- return categories.find(category => category.id === categoryId)?.name ?? t('transactions.category')
- }
+ const groupedRules = useMemo(() => {
+ const groupsMap: { [categoryId: string]: { categoryName: string; rules: Rule[] } } = {}
+
+ for (const rule of rules) {
+ const categoryId = getRuleCategoryId(rule) ?? 'uncategorized'
+ let categoryName = t('transactions.uncategorized')
+
+ if (categoryId !== 'uncategorized') {
+ const category = categories.find(c => c.id === categoryId)
+ if (category) {
+ categoryName = category.name
+ if (category.group_id) {
+ const group = categoryGroups.find(g => g.id === category.group_id)
+ if (group) {
+ categoryName = `${group.name} > ${category.name}`
+ }
+ }
+ } else {
+ categoryName = t('transactions.category')
+ }
+ }
+
+ if (!groupsMap[categoryId]) {
+ groupsMap[categoryId] = {
+ categoryName,
+ rules: [],
+ }
+ }
+ groupsMap[categoryId].rules.push(rule)
+ }
+
+ return Object.entries(groupsMap).map(([categoryId, data]) => ({
+ categoryId,
+ ...data,
+ }))
+ }, [rules, categories, categoryGroups, t])
function handleSubmit(event: React.FormEvent) {
event.preventDefault()
@@ -1221,32 +1259,62 @@ function AddTransactionToRuleDialog({
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label>{t('transactions.existingRule')}</Label>
- <Select
- value={effectiveRuleId}
- onValueChange={setRuleId}
- disabled={loadingRules || loading || rules.length === 0}
- >
- <SelectTrigger className="w-full">
- <SelectValue
- className="min-w-0 flex-1 text-left [&_span]:items-start [&_span]:text-left"
- placeholder={loadingRules
- ? t('common.loading')
- : t('transactions.noExistingRules')}
- />
- </SelectTrigger>
- <SelectContent>
- {rules.map(rule => (
- <SelectItem key={rule.id} value={rule.id}>
- <span className="flex w-full min-w-0 flex-col items-start text-left">
- <span className="w-full truncate">{rule.name}</span>
- <span className="w-full text-xs text-muted-foreground truncate">
- {getCategoryName(rule)}
- </span>
- </span>
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
+ <Popover open={openCombobox} onOpenChange={setOpenCombobox}>
+ <PopoverTrigger asChild>
+ <button
+ type="button"
+ disabled={loadingRules || loading || rules.length === 0}
+ className="flex w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 text-sm text-left shadow-xs transition-[color,box-shadow] outline-hidden focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-input/30 dark:hover:bg-input/50 h-9 cursor-pointer"
+ >
+ <span className="flex-1 truncate text-left">
+ {loadingRules ? (
+ t('common.loading')
+ ) : selectedRule ? (
+ selectedRule.name
+ ) : (
+ t('transactions.noExistingRules')
+ )}
+ </span>
+ <ChevronDown className="size-4 shrink-0 opacity-50" />
+ </button>
+ </PopoverTrigger>
+ <PopoverContent
+ align="start"
+ className="w-[var(--radix-popover-trigger-width)] p-0 overflow-hidden"
+ >
+ <Command
+ filter={(itemValue, search) => {
+ return normalizeText(itemValue).includes(normalizeText(search)) ? 1 : 0
+ }}
+ >
+ <CommandInput placeholder={t('transactions.searchRule', 'Search rule...')} />
+ <CommandList className="max-h-[300px] overflow-y-auto">
+ <CommandEmpty>{t('transactions.noRulesFound', 'No rules found.')}</CommandEmpty>
+ {groupedRules.map((group) => (
+ <CommandGroup key={group.categoryId}>
+ <div className="px-2 py-1 text-[10.5px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/70">
+ {group.categoryName}
+ </div>
+ {group.rules.map((rule) => (
+ <CommandItem
+ key={rule.id}
+ value={`${group.categoryName} ${rule.name}`}
+ onSelect={() => {
+ setRuleId(rule.id)
+ setOpenCombobox(false)
+ }}
+ className="flex items-center justify-between gap-2 cursor-pointer"
+ >
+ <span className="flex-1 truncate">{rule.name}</span>
+ {effectiveRuleId === rule.id && <Check className="size-4 shrink-0" />}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ ))}
+ </CommandList>
+ </Command>
+ </PopoverContent>
+ </Popover>
{!loadingRules && rules.length === 0 && (
<p className="text-xs text-muted-foreground">{t('transactions.noExistingRules')}</p>
)}
diff --git a/frontend/src/locales/de.json b/frontend/src/locales/de.json
index d439246d..f6876d76 100644
--- a/frontend/src/locales/de.json
+++ b/frontend/src/locales/de.json
@@ -290,6 +290,8 @@
"noCategory": "Keine Kategorie",
"searchCategory": "Kategorie suchen...",
"noCategoryFound": "Keine Kategorie gefunden.",
+ "searchRule": "Regel suchen...",
+ "noRulesFound": "Keine Regeln gefunden.",
"addManual": "Transaktion hinzufügen",
"duplicate": "Duplizieren",
"createRule": "Regel erstellen",
diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.json
index 1cfe4d49..a2be1f68 100644
--- a/frontend/src/locales/en.json
+++ b/frontend/src/locales/en.json
@@ -290,6 +290,8 @@
"noCategory": "No category",
"searchCategory": "Search category...",
"noCategoryFound": "No category found.",
+ "searchRule": "Search rule...",
+ "noRulesFound": "No rules found.",
"addManual": "Add Transaction",
"duplicate": "Duplicate",
"createRule": "Create Rule",
diff --git a/frontend/src/locales/es.json b/frontend/src/locales/es.json
index 33af15fe..aac4cc58 100644
--- a/frontend/src/locales/es.json
+++ b/frontend/src/locales/es.json
@@ -290,6 +290,8 @@
"noCategory": "Sin categoría",
"searchCategory": "Buscar categoría...",
"noCategoryFound": "No se encontraron categorías.",
+ "searchRule": "Buscar regla...",
+ "noRulesFound": "No se encontraron reglas.",
"addManual": "Agregar transacción",
"duplicate": "Duplicar",
"createRule": "Crear regla",
diff --git a/frontend/src/locales/it.json b/frontend/src/locales/it.json
index db25b218..06df076e 100644
--- a/frontend/src/locales/it.json
+++ b/frontend/src/locales/it.json
@@ -290,6 +290,8 @@
"noCategory": "Nessuna categoria",
"searchCategory": "Cerca categoria...",
"noCategoryFound": "Nessuna categoria trovata.",
+ "searchRule": "Cerca regola...",
+ "noRulesFound": "Nessuna regola trovata.",
"addManual": "Aggiungi transazione",
"duplicate": "Duplica",
"createRule": "Crea regola",
diff --git a/frontend/src/locales/pl.json b/frontend/src/locales/pl.json
index 23cfc756..580de584 100644
--- a/frontend/src/locales/pl.json
+++ b/frontend/src/locales/pl.json
@@ -296,6 +296,8 @@
"noCategory": "Brak kategorii",
"searchCategory": "Szukaj kategorii...",
"noCategoryFound": "Nie znaleziono żadnej kategorii.",
+ "searchRule": "Szukaj reguły...",
+ "noRulesFound": "Nie znaleziono reguł.",
"addManual": "Dodaj transakcję",
"duplicate": "Duplikuj",
"createRule": "Utwórz regułę",
diff --git a/frontend/src/locales/pt-BR.json b/frontend/src/locales/pt-BR.json
index 97a6a0de..44642a10 100644
--- a/frontend/src/locales/pt-BR.json
+++ b/frontend/src/locales/pt-BR.json
@@ -290,6 +290,8 @@
"noCategory": "Sem categoria",
"searchCategory": "Buscar categoria...",
"noCategoryFound": "Nenhuma categoria encontrada.",
+ "searchRule": "Buscar regra...",
+ "noRulesFound": "Nenhuma regra encontrada.",
"addManual": "Adicionar Transação",
"duplicate": "Duplicar",
"createRule": "Criar Regra",
diff --git a/frontend/src/locales/ru.json b/frontend/src/locales/ru.json
index 63d1e06f..01f6dfd3 100644
--- a/frontend/src/locales/ru.json
+++ b/frontend/src/locales/ru.json
@@ -290,6 +290,8 @@
"noCategory": "Без категории",
"searchCategory": "Поиск категории...",
"noCategoryFound": "Категория не найдена.",
+ "searchRule": "Поиск правила...",
+ "noRulesFound": "Правила не найдены.",
"addManual": "Добавить транзакцию",
"duplicate": "Дублировать",
"createRule": "Создать правило",
diff --git a/frontend/src/locales/uk.json b/frontend/src/locales/uk.json
index 93d6b79c..44307360 100644
--- a/frontend/src/locales/uk.json
+++ b/frontend/src/locales/uk.json
@@ -290,6 +290,8 @@
"noCategory": "Без категорії",
"searchCategory": "Пошук категорії...",
"noCategoryFound": "Категорію не знайдено.",
+ "searchRule": "Пошук правила...",
+ "noRulesFound": "Правил не знайдено.",
"addManual": "Додати транзакцію",
"duplicate": "Дублювати",
"createRule": "Створити правило",