diff --git a/packages/desktop-client/src/components/Titlebar.tsx b/packages/desktop-client/src/components/Titlebar.tsx index 816753b0c3b..d41155b3a35 100644 --- a/packages/desktop-client/src/components/Titlebar.tsx +++ b/packages/desktop-client/src/components/Titlebar.tsx @@ -12,6 +12,7 @@ import { Routes, Route, useLocation } from 'react-router-dom'; import * as Platform from 'loot-core/src/client/platform'; import * as queries from 'loot-core/src/client/queries'; import { listen } from 'loot-core/src/platform/client/fetch'; +import { type LocalPrefs } from 'loot-core/src/types/prefs'; import { useActions } from '../hooks/useActions'; import useFeatureFlag from '../hooks/useFeatureFlag'; @@ -41,9 +42,10 @@ import useSheetValue from './spreadsheet/useSheetValue'; import { ThemeSelector } from './ThemeSelector'; import { Tooltip } from './tooltips'; +type Listener = (msg: string, newBudgetType?: LocalPrefs['budgetType']) => void; export type TitlebarContextValue = { - sendEvent: (msg: string) => void; - subscribe: (listener) => () => void; + sendEvent: (msg: string, newBudgetType?: LocalPrefs['budgetType']) => void; + subscribe: (listener: Listener) => () => void; }; export let TitlebarContext = createContext(null); @@ -53,13 +55,13 @@ type TitlebarProviderProps = { }; export function TitlebarProvider({ children }: TitlebarProviderProps) { - let listeners = useRef([]); + let listeners = useRef([]); - function sendEvent(msg: string) { - listeners.current.forEach(func => func(msg)); + function sendEvent(msg: string, newBudgetType?: LocalPrefs['budgetType']) { + listeners.current.forEach(func => func(msg, newBudgetType)); } - function subscribe(listener) { + function subscribe(listener: Listener) { listeners.current.push(listener); return () => (listeners.current = listeners.current.filter(func => func !== listener)); @@ -268,7 +270,8 @@ function BudgetTitlebar() { function onSwitchType() { setLoading(true); if (!loading) { - sendEvent('budget/switch-type'); + const newBudgetType = budgetType === 'rollover' ? 'report' : 'rollover'; + sendEvent('budget/switch-type', newBudgetType); } } diff --git a/packages/desktop-client/src/components/budget/MobileBudget.js b/packages/desktop-client/src/components/budget/MobileBudget.js index f937f5f9a1b..f624771d643 100644 --- a/packages/desktop-client/src/components/budget/MobileBudget.js +++ b/packages/desktop-client/src/components/budget/MobileBudget.js @@ -328,8 +328,13 @@ class Budget extends Component { this.setState({ initialized: false }); - await switchBudgetType(budgetType, spreadsheet, bounds, currentMonth, () => - loadPrefs(), + const newBudgetType = budgetType === 'rollover' ? 'report' : 'rollover'; + await switchBudgetType( + newBudgetType, + spreadsheet, + bounds, + currentMonth, + () => loadPrefs(), ); this.setState({ initialized: true }); diff --git a/packages/desktop-client/src/components/budget/index.tsx b/packages/desktop-client/src/components/budget/index.tsx index 12c70c57065..4a071cfc20c 100644 --- a/packages/desktop-client/src/components/budget/index.tsx +++ b/packages/desktop-client/src/components/budget/index.tsx @@ -369,11 +369,14 @@ function Budget(props: BudgetProps) { props.savePrefs({ 'budget.summaryCollapsed': collapsed }); }; - const onTitlebarEvent = async msg => { + const onTitlebarEvent = async ( + msg: string, + newBudgetType: LocalPrefs['budgetType'], + ) => { switch (msg) { case 'budget/switch-type': { await switchBudgetType( - props.budgetType, + newBudgetType, props.spreadsheet, bounds, prewarmStartMonth, diff --git a/packages/desktop-client/src/components/budget/util.ts b/packages/desktop-client/src/components/budget/util.ts index ba590982b1b..6e9f7dd1427 100644 --- a/packages/desktop-client/src/components/budget/util.ts +++ b/packages/desktop-client/src/components/budget/util.ts @@ -165,18 +165,15 @@ export async function prewarmAllMonths( } export async function switchBudgetType( - budgetType: LocalPrefs['budgetType'], + newBudgetType: LocalPrefs['budgetType'], spreadsheet: ReturnType, bounds: { start: string; end: string }, startMonth: string, onSuccess: () => Promise | undefined, ) { - let newType: 'rollover' | 'report' = - budgetType === 'rollover' ? 'report' : 'rollover'; - spreadsheet.disableObservers(); - await send('budget-set-type', { type: newType }); - await prewarmAllMonths(newType, spreadsheet, bounds, startMonth); + await send('budget-set-type', { type: newBudgetType }); + await prewarmAllMonths(newBudgetType, spreadsheet, bounds, startMonth); spreadsheet.enableObservers(); await onSuccess?.(); } diff --git a/upcoming-release-notes/1933.md b/upcoming-release-notes/1933.md new file mode 100644 index 00000000000..12b52dec1d8 --- /dev/null +++ b/upcoming-release-notes/1933.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [paulsukow] +--- + +Fix switching budget from rollover budget to report budget and immediately back does not work