Open-source personal finance manager. Self-hosted, privacy-first.
feat: sync URL with search filters for transactions page (#144)
Rodrigo Tavaresb4dbb3d2f4ac
diff --git a/frontend/src/pages/transactions.tsx b/frontend/src/pages/transactions.tsxindex 02ab80aa..78ef9bca 100644--- a/frontend/src/pages/transactions.tsx+++ b/frontend/src/pages/transactions.tsx@@ -93,13 +93,51 @@ export default function TransactionsPage() {const nextQ = searchParams.get('q') ?? ''setSearchInput(nextQ)setSearchQuery(nextQ)+ const tags = searchParams.get('tags');+ setTagFilters(tags ? tags.split(',') : []);setFilterPayee(searchParams.get('payee_id') ?? '')- const nextCategory = searchParams.get('category_id')- setFilterCategoryIds(nextCategory ? [nextCategory] : [])- setFilterUncategorized(false)+ const categories = searchParams.get('category_id');+ setFilterCategoryIds(categories ? categories.split(',') : []);+ setFilterUncategorized(searchParams.get('uncategorized') === '1');+ const accounts = searchParams.get('account_id');+ setFilterAccountIds(accounts ? accounts.split(',') : []);+ setFilterFrom(searchParams.get('from') ?? '');+ setFilterTo(searchParams.get('to') ?? '');setPage(1)}, [searchParams])+ // Keep the URL in sync with the current filters, so that the current page can be+ // refreshed, bookmarked or shared.+ useEffect(() => {+ const params = new URLSearchParams(+ [+ ['q', searchQuery],+ ['tags', tagFilters.join(',')],+ ['payee_id', filterPayee],+ ['category_id', filterCategoryIds.join(',')],+ ['uncategorized', filterUncategorized ? '1' : ''],+ ['account_id', filterAccountIds.join(',')],+ ['from', filterFrom],+ ['to', filterTo],+ ].filter(([, v]) => v.length),+ );++ window.history.replaceState(+ null,+ '',+ params.size ? `?${params}` : window.location.pathname,+ );+ }, [+ searchQuery,+ tagFilters,+ filterPayee,+ filterCategoryIds,+ filterUncategorized,+ filterAccountIds,+ filterFrom,+ filterTo,+ ]);+useEffect(() => {if (debounceRef.current) clearTimeout(debounceRef.current)debounceRef.current = setTimeout(() => {