fix: Net Worth defaults to 5y, Dashboard->Transactions keeps month/year

Net Worth opened on a 30-day window by default, which frequently shows
a dip (short windows don't have enough recorded income relative to
spending) even though the long-term trend is a steady climb —
demotivating for no good reason. Defaults to 5y instead.

Dashboard's "All transactions" link always sent users to the current
month regardless of which month they had selected. It now carries
?year=&month= and TransactionsPage seeds its state from those params
(clamped/validated, falls back to the current month if absent).
This commit is contained in:
ZProger
2026-08-16 22:31:10 +03:00
parent 5b42e3d972
commit 98c61d50d8
4 changed files with 28 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import { useState } from "react";
import { useSearchParams } from "react-router-dom";
import { Plus } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
@@ -16,11 +17,27 @@ import type { Transaction, TransactionType } from "@/types";
const PAGE_SIZE = 20;
/** Clamp a query-param month to 1-12, falling back to `fallback` for
* anything missing or out of range (e.g. a hand-edited URL). */
function parseMonthParam(value: string | null, fallback: number): number {
const parsed = Number(value);
return Number.isInteger(parsed) ? Math.min(12, Math.max(1, parsed)) : fallback;
}
function parseYearParam(value: string | null, fallback: number): number {
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}
export function TransactionsPage() {
const { t } = useTranslation();
const now = new Date();
const [year, setYear] = useState(now.getFullYear());
const [month, setMonth] = useState(now.getMonth() + 1);
// Deep-linked from the Dashboard's "All transactions" link, which carries
// the month/year the user was already looking at (?year=&month=) so this
// page doesn't reset back to the current month.
const [searchParams] = useSearchParams();
const [year, setYear] = useState(() => parseYearParam(searchParams.get("year"), now.getFullYear()));
const [month, setMonth] = useState(() => parseMonthParam(searchParams.get("month"), now.getMonth() + 1));
const [type, setType] = useState<TransactionType | "">("");
const [categoryId, setCategoryId] = useState<string>("");
const [sort, setSort] = useState<TransactionSort>("date_desc");