From b2d7b65ce9ac210c2e33827f91880e9ea2703176 Mon Sep 17 00:00:00 2001
From: Matiss Janis Aboltins <matiss@mja.lv>
Date: Tue, 10 Sep 2024 21:29:51 +0100
Subject: [PATCH] :recycle: (synced-prefs) moving budgetType pref back to
 MetadataPrefs (#3410)

* :recycle: (synced-prefs) moving budgetType pref back to MetadataPrefs (temporary)

* Release notes
---
 .../components/autocomplete/CategoryAutocomplete.tsx   |  4 ++--
 .../desktop-client/src/components/budget/index.tsx     |  4 ++--
 packages/desktop-client/src/components/budget/util.ts  |  8 ++++----
 .../src/components/mobile/budget/BudgetTable.jsx       |  6 +++---
 .../src/components/mobile/budget/index.tsx             |  3 ++-
 .../src/components/settings/BudgetTypeSettings.tsx     |  4 ++--
 .../src/components/settings/Experimental.tsx           |  3 ++-
 packages/loot-core/src/server/prefs.ts                 | 10 ++++------
 packages/loot-core/src/types/prefs.d.ts                |  3 ++-
 upcoming-release-notes/3410.md                         |  6 ++++++
 10 files changed, 29 insertions(+), 22 deletions(-)
 create mode 100644 upcoming-release-notes/3410.md

diff --git a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
index c5d0a2462..c8733e817 100644
--- a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
@@ -22,7 +22,7 @@ import {
 } from 'loot-core/src/types/models';
 
 import { useCategories } from '../../hooks/useCategories';
-import { useSyncedPref } from '../../hooks/useSyncedPref';
+import { useMetadataPref } from '../../hooks/useMetadataPref';
 import { SvgSplit } from '../../icons/v0';
 import { useResponsive } from '../../ResponsiveProvider';
 import { type CSSProperties, theme, styles } from '../../style';
@@ -379,7 +379,7 @@ function CategoryItem({
         borderTop: `1px solid ${theme.pillBorder}`,
       }
     : {};
-  const [budgetType = 'rollover'] = useSyncedPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
 
   const balanceBinding =
     budgetType === 'rollover'
diff --git a/packages/desktop-client/src/components/budget/index.tsx b/packages/desktop-client/src/components/budget/index.tsx
index 9fecac872..1f2e29995 100644
--- a/packages/desktop-client/src/components/budget/index.tsx
+++ b/packages/desktop-client/src/components/budget/index.tsx
@@ -23,8 +23,8 @@ import * as monthUtils from 'loot-core/src/shared/months';
 import { useCategories } from '../../hooks/useCategories';
 import { useGlobalPref } from '../../hooks/useGlobalPref';
 import { useLocalPref } from '../../hooks/useLocalPref';
+import { useMetadataPref } from '../../hooks/useMetadataPref';
 import { useNavigate } from '../../hooks/useNavigate';
-import { useSyncedPref } from '../../hooks/useSyncedPref';
 import { styles } from '../../style';
 import { View } from '../common/View';
 import { NamespaceContext } from '../spreadsheet/NamespaceContext';
@@ -76,7 +76,7 @@ function BudgetInner(props: BudgetInnerProps) {
     start: startMonth,
     end: startMonth,
   });
-  const [budgetType = 'rollover'] = useSyncedPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
   const [maxMonthsPref] = useGlobalPref('maxMonths');
   const maxMonths = maxMonthsPref || 1;
   const [initialized, setInitialized] = useState(false);
diff --git a/packages/desktop-client/src/components/budget/util.ts b/packages/desktop-client/src/components/budget/util.ts
index 44f5471d7..239a5ba15 100644
--- a/packages/desktop-client/src/components/budget/util.ts
+++ b/packages/desktop-client/src/components/budget/util.ts
@@ -4,7 +4,7 @@ import { send } from 'loot-core/src/platform/client/fetch';
 import * as monthUtils from 'loot-core/src/shared/months';
 import { type Handlers } from 'loot-core/src/types/handlers';
 import { type CategoryGroupEntity } from 'loot-core/src/types/models';
-import { type SyncedPrefs } from 'loot-core/src/types/prefs';
+import { type MetadataPrefs } from 'loot-core/src/types/prefs';
 
 import { type CSSProperties, styles, theme } from '../../style';
 import { type DropPosition } from '../sort';
@@ -141,7 +141,7 @@ export function getScrollbarWidth() {
 }
 
 export async function prewarmMonth(
-  budgetType: SyncedPrefs['budgetType'],
+  budgetType: MetadataPrefs['budgetType'],
   spreadsheet: ReturnType<typeof useSpreadsheet>,
   month: string,
 ) {
@@ -156,7 +156,7 @@ export async function prewarmMonth(
 }
 
 export async function prewarmAllMonths(
-  budgetType: SyncedPrefs['budgetType'],
+  budgetType: MetadataPrefs['budgetType'],
   spreadsheet: ReturnType<typeof useSpreadsheet>,
   bounds: { start: string; end: string },
   startMonth: string,
@@ -176,7 +176,7 @@ export async function prewarmAllMonths(
 }
 
 export async function switchBudgetType(
-  newBudgetType: SyncedPrefs['budgetType'],
+  newBudgetType: MetadataPrefs['budgetType'],
   spreadsheet: ReturnType<typeof useSpreadsheet>,
   bounds: { start: string; end: string },
   startMonth: string,
diff --git a/packages/desktop-client/src/components/mobile/budget/BudgetTable.jsx b/packages/desktop-client/src/components/mobile/budget/BudgetTable.jsx
index 2961520b0..bcef3cfa2 100644
--- a/packages/desktop-client/src/components/mobile/budget/BudgetTable.jsx
+++ b/packages/desktop-client/src/components/mobile/budget/BudgetTable.jsx
@@ -12,9 +12,9 @@ import * as monthUtils from 'loot-core/src/shared/months';
 import { useCategories } from '../../../hooks/useCategories';
 import { useFeatureFlag } from '../../../hooks/useFeatureFlag';
 import { useLocalPref } from '../../../hooks/useLocalPref';
+import { useMetadataPref } from '../../../hooks/useMetadataPref';
 import { useNavigate } from '../../../hooks/useNavigate';
 import { useNotes } from '../../../hooks/useNotes';
-import { useSyncedPref } from '../../../hooks/useSyncedPref';
 import { useUndo } from '../../../hooks/useUndo';
 import { SvgLogo } from '../../../icons/logo';
 import { SvgExpandArrow } from '../../../icons/v0';
@@ -225,7 +225,7 @@ function BudgetCell({
 }) {
   const dispatch = useDispatch();
   const { showUndoNotification } = useUndo();
-  const [budgetType = 'rollover'] = useLocalPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
 
   const categoryBudgetMenuModal = `${budgetType}-budget-menu`;
   const categoryNotes = useNotes(category.id);
@@ -358,7 +358,7 @@ const ExpenseCategory = memo(function ExpenseCategory({
   const goalTemp = useSheetValue(goal);
   const goalValue = isGoalTemplatesEnabled ? goalTemp : null;
 
-  const [budgetType = 'rollover'] = useSyncedPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
   const dispatch = useDispatch();
   const { showUndoNotification } = useUndo();
   const { list: categories } = useCategories();
diff --git a/packages/desktop-client/src/components/mobile/budget/index.tsx b/packages/desktop-client/src/components/mobile/budget/index.tsx
index cf1fc310b..65eb9543f 100644
--- a/packages/desktop-client/src/components/mobile/budget/index.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/index.tsx
@@ -27,6 +27,7 @@ import {
 
 import { useCategories } from '../../../hooks/useCategories';
 import { useLocalPref } from '../../../hooks/useLocalPref';
+import { useMetadataPref } from '../../../hooks/useMetadataPref';
 import { useSetThemeColor } from '../../../hooks/useSetThemeColor';
 import { useSyncedPref } from '../../../hooks/useSyncedPref';
 import { AnimatedLoading } from '../../../icons/AnimatedLoading';
@@ -466,7 +467,7 @@ function BudgetInner(props: BudgetInnerProps) {
 
 export function Budget() {
   const { list: categories, grouped: categoryGroups } = useCategories();
-  const [budgetType] = useSyncedPref('budgetType');
+  const [budgetType] = useMetadataPref('budgetType');
   const spreadsheet = useSpreadsheet();
   useSetThemeColor(theme.mobileViewTheme);
   return (
diff --git a/packages/desktop-client/src/components/settings/BudgetTypeSettings.tsx b/packages/desktop-client/src/components/settings/BudgetTypeSettings.tsx
index ad5c3e746..7bc50ec01 100644
--- a/packages/desktop-client/src/components/settings/BudgetTypeSettings.tsx
+++ b/packages/desktop-client/src/components/settings/BudgetTypeSettings.tsx
@@ -6,7 +6,7 @@ import { useSpreadsheet } from 'loot-core/src/client/SpreadsheetProvider';
 import * as monthUtils from 'loot-core/src/shared/months';
 
 import { useLocalPref } from '../../hooks/useLocalPref';
-import { useSyncedPref } from '../../hooks/useSyncedPref';
+import { useMetadataPref } from '../../hooks/useMetadataPref';
 import { switchBudgetType } from '../budget/util';
 import { ButtonWithLoading } from '../common/Button2';
 import { Link } from '../common/Link';
@@ -16,7 +16,7 @@ import { Setting } from './UI';
 
 export function BudgetTypeSettings() {
   const dispatch = useDispatch();
-  const [budgetType = 'rollover'] = useSyncedPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
   const [loading, setLoading] = useState(false);
 
   const currentMonth = monthUtils.currentMonth();
diff --git a/packages/desktop-client/src/components/settings/Experimental.tsx b/packages/desktop-client/src/components/settings/Experimental.tsx
index fa6da21cf..58ccc41d7 100644
--- a/packages/desktop-client/src/components/settings/Experimental.tsx
+++ b/packages/desktop-client/src/components/settings/Experimental.tsx
@@ -4,6 +4,7 @@ import { Trans, useTranslation } from 'react-i18next';
 import type { FeatureFlag } from 'loot-core/src/types/prefs';
 
 import { useFeatureFlag } from '../../hooks/useFeatureFlag';
+import { useMetadataPref } from '../../hooks/useMetadataPref';
 import { useSyncedPref } from '../../hooks/useSyncedPref';
 import { theme } from '../../style';
 import { Link } from '../common/Link';
@@ -69,7 +70,7 @@ function FeatureToggle({
 
 function ReportBudgetFeature() {
   const { t } = useTranslation();
-  const [budgetType = 'rollover'] = useSyncedPref('budgetType');
+  const [budgetType = 'rollover'] = useMetadataPref('budgetType');
   const enabled = useFeatureFlag('reportBudget');
   const blockToggleOff = budgetType === 'report' && enabled;
   return (
diff --git a/packages/loot-core/src/server/prefs.ts b/packages/loot-core/src/server/prefs.ts
index 1df9f0341..e18d107d3 100644
--- a/packages/loot-core/src/server/prefs.ts
+++ b/packages/loot-core/src/server/prefs.ts
@@ -2,18 +2,16 @@
 import { Timestamp } from '@actual-app/crdt';
 
 import * as fs from '../platform/server/fs';
-import type { MetadataPrefs, SyncedPrefs } from '../types/prefs';
+import type { MetadataPrefs } from '../types/prefs';
 
 import { Message, sendMessages } from './sync';
 
-type LocalPrefs = MetadataPrefs & Pick<SyncedPrefs, 'budgetType'>;
-
 export const BUDGET_TYPES = ['report', 'rollover'] as const;
 export type BudgetType = (typeof BUDGET_TYPES)[number];
 
 let prefs: MetadataPrefs = null;
 
-export async function loadPrefs(id?: string): Promise<LocalPrefs> {
+export async function loadPrefs(id?: string): Promise<MetadataPrefs> {
   if (process.env.NODE_ENV === 'test' && !id) {
     prefs = getDefaultPrefs('test', 'test_LocalPrefs');
     return prefs;
@@ -50,7 +48,7 @@ export async function loadPrefs(id?: string): Promise<LocalPrefs> {
 }
 
 export async function savePrefs(
-  prefsToSet: LocalPrefs,
+  prefsToSet: MetadataPrefs,
   { avoidSync = false } = {},
 ): Promise<void> {
   Object.assign(prefs, prefsToSet);
@@ -87,7 +85,7 @@ export function unloadPrefs(): void {
   prefs = null;
 }
 
-export function getPrefs(): LocalPrefs {
+export function getPrefs(): MetadataPrefs {
   return prefs;
 }
 
diff --git a/packages/loot-core/src/types/prefs.d.ts b/packages/loot-core/src/types/prefs.d.ts
index fe88076af..132fde3a9 100644
--- a/packages/loot-core/src/types/prefs.d.ts
+++ b/packages/loot-core/src/types/prefs.d.ts
@@ -29,7 +29,6 @@ export type SyncedPrefs = Partial<
     | `csv-has-header-${string}`
     | `ofx-fallback-missing-payee-${string}`
     | `flip-amount-${string}-${'csv' | 'qif'}`
-    | 'budgetType'
     | `flags.${FeatureFlag}`,
     string
   >
@@ -40,6 +39,8 @@ export type SyncedPrefs = Partial<
  * core database.
  */
 export type MetadataPrefs = Partial<{
+  // TODO: move budgetType to SyncedPrefs
+  budgetType: string;
   budgetName: string;
   id: string;
   lastUploaded: string;
diff --git a/upcoming-release-notes/3410.md b/upcoming-release-notes/3410.md
new file mode 100644
index 000000000..aff78c981
--- /dev/null
+++ b/upcoming-release-notes/3410.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+SyncedPrefs: move `budgetType` back to metadata prefs.
-- 
GitLab