diff --git a/frontend/src/hooks/useBudget.ts b/frontend/src/hooks/useBudget.ts index 673b563..927c020 100644 --- a/frontend/src/hooks/useBudget.ts +++ b/frontend/src/hooks/useBudget.ts @@ -16,11 +16,11 @@ import { /** All budget data hangs off the ['budget', ...] key family; every mutation * invalidates the family so projection, month detail and items stay in sync. */ -export function useBudget(initialYear: number) { +export function useBudget(initialYear: number, initialMonth?: number) { const queryClient = useQueryClient(); const [year, setYear] = useState(initialYear); const [selectedMonth, setSelectedMonth] = useState( - new Date().getMonth() + 1, + initialMonth ?? new Date().getMonth() + 1, ); const projectionQ = useQuery({ diff --git a/frontend/src/pages/Budget.tsx b/frontend/src/pages/Budget.tsx index cd6ce15..b12e0ea 100644 --- a/frontend/src/pages/Budget.tsx +++ b/frontend/src/pages/Budget.tsx @@ -22,6 +22,17 @@ const MAX_YEAR = 2030; export default function Budget() { const currentYear = Math.max(MIN_YEAR, new Date().getFullYear()); + const location = useLocation(); + const navigate = useNavigate(); + // Proyecciones row clicks land here with the clicked period in the state. + const navState = location.state as + | { from?: string; year?: number; month?: number } + | null; + const cameFromProyecciones = navState?.from === 'proyecciones'; + const initialYear = + navState?.year && navState.year >= MIN_YEAR && navState.year <= MAX_YEAR + ? navState.year + : currentYear; const { year, setYear, @@ -35,12 +46,8 @@ export default function Budget() { updateItem, deleteItem, refresh, - } = useBudget(currentYear); + } = useBudget(initialYear, navState?.month); const queryClient = useQueryClient(); - const location = useLocation(); - const navigate = useNavigate(); - const cameFromProyecciones = - (location.state as { from?: string } | null)?.from === 'proyecciones'; const [subTab, setSubTab] = useState<'detail' | 'transactions'>('detail'); const [txSearch, setTxSearch] = useState(''); diff --git a/frontend/src/pages/Proyecciones.tsx b/frontend/src/pages/Proyecciones.tsx index b133e83..0f2b692 100644 --- a/frontend/src/pages/Proyecciones.tsx +++ b/frontend/src/pages/Proyecciones.tsx @@ -99,7 +99,9 @@ export default function Proyecciones() { year={year} onSelectMonth={(m) => { setSelectedMonth(m); - navigate('/budget', { state: { from: 'proyecciones' } }); + navigate('/budget', { + state: { from: 'proyecciones', year, month: m }, + }); }} onSaveOverride={async (month, value) => { await saveBalanceOverride(year, month, value);