Skip to content

A local-first personal finance app

Move redux state to react-query - category states [Part 2 - expose react-query states e.g. isPending, isSuccess, etc] (#6882)

Joel Jeremy Marqueze3e4b13d2b8c
diff --git a/packages/desktop-client/src/components/ManageRules.tsx b/packages/desktop-client/src/components/ManageRules.tsx
index d4092adb74..6d9aa2d785 100644
--- a/packages/desktop-client/src/components/ManageRules.tsx
+++ b/packages/desktop-client/src/components/ManageRules.tsx
@@ -134,7 +134,7 @@ export function ManageRules({
const { schedules = [] } = useSchedules({
query: useMemo(() => q('schedules').select('*'), []),
});
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const payees = usePayees();
const accounts = useAccounts();
const filterData = useMemo(
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx
index a3b0a6da83..6f72882621 100644
--- a/packages/desktop-client/src/components/accounts/Account.tsx
+++ b/packages/desktop-client/src/components/accounts/Account.tsx
@@ -29,6 +29,7 @@ import { applyChanges } from 'loot-core/shared/util';
import type { IntegerAmount } from 'loot-core/shared/util';
import type {
AccountEntity,
+ CategoryGroupEntity,
NewRuleEntity,
RuleActionEntity,
RuleConditionEntity,
@@ -239,7 +240,7 @@ type AccountInternalProps = {
failedAccounts: ReturnType<typeof useFailedAccounts>;
dateFormat: ReturnType<typeof useDateFormat>;
payees: ReturnType<typeof usePayees>;
- categoryGroups: ReturnType<typeof useCategories>['grouped'];
+ categoryGroups: CategoryGroupEntity[];
hideFraction: boolean;
accountsSyncing: string[];
dispatch: AppDispatch;
@@ -1957,7 +1958,8 @@ export function Account() {
const params = useParams();
const location = useLocation();
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const newTransactions = useSelector(
state => state.transactions.newTransactions,
);
diff --git a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
index 39a71c8c8d..19d3f40437 100644
--- a/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/CategoryAutocomplete.tsx
@@ -234,7 +234,8 @@ export function CategoryAutocomplete({
showHiddenCategories,
...props
}: CategoryAutocompleteProps) {
- const { grouped: defaultCategoryGroups } = useCategories();
+ const { data: { grouped: defaultCategoryGroups } = { grouped: [] } } =
+ useCategories();
const categorySuggestions: CategoryAutocompleteItem[] = useMemo(() => {
const allSuggestions = (categoryGroups || defaultCategoryGroups).reduce(
(list, group) =>
diff --git a/packages/desktop-client/src/components/budget/BudgetTable.tsx b/packages/desktop-client/src/components/budget/BudgetTable.tsx
index 2263f826b4..ab660bdd28 100644
--- a/packages/desktop-client/src/components/budget/BudgetTable.tsx
+++ b/packages/desktop-client/src/components/budget/BudgetTable.tsx
@@ -73,7 +73,8 @@ export function BudgetTable(props: BudgetTableProps) {
onBudgetAction,
} = props;
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const [collapsedGroupIds = [], setCollapsedGroupIdsPref] =
useLocalPref('budget.collapsed');
const [showHiddenCategories, setShowHiddenCategoriesPef] = useLocalPref(
diff --git a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
index 796b7f6dc2..dc0c134480 100644
--- a/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/CoverMenu.tsx
@@ -37,7 +37,8 @@ export function CoverMenu({
}: CoverMenuProps) {
const { t } = useTranslation();
- const { grouped: originalCategoryGroups } = useCategories();
+ const { data: { grouped: originalCategoryGroups } = { grouped: [] } } =
+ useCategories();
const [fromCategoryId, setFromCategoryId] = useState<string | null>(null);
diff --git a/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx b/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
index 2c90b068f0..f7272e1904 100644
--- a/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
+++ b/packages/desktop-client/src/components/budget/envelope/TransferMenu.tsx
@@ -32,7 +32,8 @@ export function TransferMenu({
onSubmit,
onClose,
}: TransferMenuProps) {
- const { grouped: originalCategoryGroups } = useCategories();
+ const { data: { grouped: originalCategoryGroups } = { grouped: [] } } =
+ useCategories();
const filteredCategoryGroups = useMemo(() => {
const expenseCategoryGroups = originalCategoryGroups.filter(
g => !g.is_income,
diff --git a/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts b/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
index ee403d5e80..1b59513974 100644
--- a/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
+++ b/packages/desktop-client/src/components/budget/goals/useBudgetAutomationCategories.ts
@@ -5,7 +5,7 @@ import { useCategories } from '@desktop-client/hooks/useCategories';
export function useBudgetAutomationCategories() {
const { t } = useTranslation();
- const { grouped } = useCategories();
+ const { data: { grouped } = { grouped: [] } } = useCategories();
const categories = useMemo(() => {
const incomeGroup = grouped.filter(group => group.name === 'Income')[0];
return [
diff --git a/packages/desktop-client/src/components/budget/index.tsx b/packages/desktop-client/src/components/budget/index.tsx
index ce94c0400a..34c50ae170 100644
--- a/packages/desktop-client/src/components/budget/index.tsx
+++ b/packages/desktop-client/src/components/budget/index.tsx
@@ -53,7 +53,8 @@ export function Budget() {
const [maxMonthsPref] = useGlobalPref('maxMonths');
const maxMonths = maxMonthsPref || 1;
const [initialized, setInitialized] = useState(false);
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const init = useEffectEvent(() => {
async function run() {
diff --git a/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx b/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
index ac522b46cb..a241077d6b 100644
--- a/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/BudgetPage.tsx
@@ -73,7 +73,12 @@ function isBudgetType(input?: string): input is 'envelope' | 'tracking' {
export function BudgetPage() {
const { t } = useTranslation();
const locale = useLocale();
- const { list: categories, grouped: categoryGroups } = useCategories();
+ const {
+ data: { list: categories, grouped: categoryGroups } = {
+ list: [],
+ grouped: [],
+ },
+ } = useCategories();
const [budgetTypePref] = useSyncedPref('budgetType');
const budgetType = isBudgetType(budgetTypePref) ? budgetTypePref : 'envelope';
const spreadsheet = useSpreadsheet();
@@ -725,7 +730,7 @@ function OverbudgetedBanner({ month, onBudgetAction, ...props }) {
>(envelopeBudget.toBudget);
const dispatch = useDispatch();
const { showUndoNotification } = useUndo();
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const categoriesById = useMemo(() => groupById(categories), [categories]);
const openCoverOverbudgetedModal = useCallback(() => {
@@ -805,7 +810,12 @@ function OverbudgetedBanner({ month, onBudgetAction, ...props }) {
function OverspendingBanner({ month, onBudgetAction, budgetType, ...props }) {
const { t } = useTranslation();
- const { list: categories, grouped: categoryGroups } = useCategories();
+ const {
+ data: { list: categories, grouped: categoryGroups } = {
+ list: [],
+ grouped: [],
+ },
+ } = useCategories();
const categoriesById = useMemo(() => groupById(categories), [categories]);
const dispatch = useDispatch();
diff --git a/packages/desktop-client/src/components/mobile/budget/CategoryPage.tsx b/packages/desktop-client/src/components/mobile/budget/CategoryPage.tsx
index 9b4fd4c248..ee099386ad 100644
--- a/packages/desktop-client/src/components/mobile/budget/CategoryPage.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/CategoryPage.tsx
@@ -13,7 +13,7 @@ import { UncategorizedTransactions } from './UncategorizedTransactions';
import { MobileBackButton } from '@desktop-client/components/mobile/MobileBackButton';
import { AddTransactionButton } from '@desktop-client/components/mobile/transactions/AddTransactionButton';
import { MobilePageHeader, Page } from '@desktop-client/components/Page';
-import { useCategories } from '@desktop-client/hooks/useCategories';
+import { useCategory } from '@desktop-client/hooks/useCategory';
import { useLocale } from '@desktop-client/hooks/useLocale';
import { useSyncedPref } from '@desktop-client/hooks/useSyncedPref';
@@ -26,8 +26,7 @@ export function CategoryPage() {
const { id: categoryIdParam } = useParams();
const [searchParams] = useSearchParams();
const month = searchParams.get('month') || monthUtils.currentMonth();
- const { list: categories } = useCategories();
- const category = categories.find(c => c.id === categoryIdParam);
+ const { data: category } = useCategory(categoryIdParam);
return (
<Page
diff --git a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
index d77c2667e9..6d2b925e42 100644
--- a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryListItem.tsx
@@ -13,7 +13,6 @@ import { View } from '@actual-app/components/view';
import type { BudgetType } from 'loot-core/server/prefs';
import * as monthUtils from 'loot-core/shared/months';
-import { groupById } from 'loot-core/shared/util';
import type { CategoryEntity } from 'loot-core/types/models';
import { BalanceCell } from './BalanceCell';
@@ -21,7 +20,7 @@ import { BudgetCell } from './BudgetCell';
import { getColumnWidth, ROW_HEIGHT } from './BudgetTable';
import { SpentCell } from './SpentCell';
-import { useCategories } from '@desktop-client/hooks/useCategories';
+import { useCategoriesById } from '@desktop-client/hooks/useCategories';
import { useFormat } from '@desktop-client/hooks/useFormat';
import { useNavigate } from '@desktop-client/hooks/useNavigate';
import { useSheetValue } from '@desktop-client/hooks/useSheetValue';
@@ -238,8 +237,11 @@ export function ExpenseCategoryListItem({
`${budgetType as BudgetType}-balance-menu` as const;
const dispatch = useDispatch();
const { showUndoNotification } = useUndo();
- const { list: categories } = useCategories();
- const categoriesById = groupById(categories);
+ const {
+ data: { list: categoriesById } = {
+ list: {} as Record<string, CategoryEntity>,
+ },
+ } = useCategoriesById();
const onCarryover = useCallback(
(carryover: boolean) => {
diff --git a/packages/desktop-client/src/components/mobile/rules/MobileRulesPage.tsx b/packages/desktop-client/src/components/mobile/rules/MobileRulesPage.tsx
index 3b017ffbe8..a24cd7eb26 100644
--- a/packages/desktop-client/src/components/mobile/rules/MobileRulesPage.tsx
+++ b/packages/desktop-client/src/components/mobile/rules/MobileRulesPage.tsx
@@ -40,7 +40,7 @@ export function MobileRulesPage() {
const { schedules = [] } = useSchedules({
query: useMemo(() => q('schedules').select('*'), []),
});
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const payees = usePayees();
const accounts = useAccounts();
const filterData = useMemo(
diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx
index 584d96b023..c93daa7a18 100644
--- a/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx
+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx
@@ -584,7 +584,8 @@ const TransactionEditInner = memo<TransactionEditInnerProps>(
) || [],
[unserializedTransactions, dateFormat],
);
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
useEffect(() => {
if (window.history.length === 1) {
@@ -1679,7 +1680,7 @@ type TransactionEditProps = Omit<
>;
export const TransactionEdit = (props: TransactionEditProps) => {
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const payees = usePayees();
const lastTransaction = useSelector(
state => state.transactions.lastTransaction,
diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx
index e6816aaa68..7ffdceb0b4 100644
--- a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx
+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx
@@ -34,13 +34,13 @@ import { isPreviewId } from 'loot-core/shared/transactions';
import { validForTransfer } from 'loot-core/shared/transfer';
import { groupById, integerToCurrency } from 'loot-core/shared/util';
import type { IntegerAmount } from 'loot-core/shared/util';
-import type { TransactionEntity } from 'loot-core/types/models';
+import type { CategoryEntity, TransactionEntity } from 'loot-core/types/models';
import { ROW_HEIGHT, TransactionListItem } from './TransactionListItem';
import { FloatingActionBar } from '@desktop-client/components/mobile/FloatingActionBar';
import { useAccounts } from '@desktop-client/hooks/useAccounts';
-import { useCategories } from '@desktop-client/hooks/useCategories';
+import { useCategoriesById } from '@desktop-client/hooks/useCategories';
import { useLocale } from '@desktop-client/hooks/useLocale';
import { useNavigate } from '@desktop-client/hooks/useNavigate';
import { usePayees } from '@desktop-client/hooks/usePayees';
@@ -340,8 +340,11 @@ function SelectedTransactionsFloatingActionBar({
const payees = usePayees();
const payeesById = useMemo(() => groupById(payees), [payees]);
- const { list: categories } = useCategories();
- const categoriesById = useMemo(() => groupById(categories), [categories]);
+ const {
+ data: { list: categoriesById } = {
+ list: {} as Record<string, CategoryEntity>,
+ },
+ } = useCategoriesById();
const dispatch = useDispatch();
useEffect(() => {
diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionListItem.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionListItem.tsx
index 1db2854a71..5663531dd1 100644
--- a/packages/desktop-client/src/components/mobile/transactions/TransactionListItem.tsx
+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionListItem.tsx
@@ -87,7 +87,7 @@ export function TransactionListItem({
...itemProps
}: TransactionListItemProps) {
const { t } = useTranslation();
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const payee = usePayee(transaction?.payee || '');
const displayPayee = useDisplayPayee({ transaction });
diff --git a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
index a9095d1574..7137854d74 100644
--- a/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
+++ b/packages/desktop-client/src/components/modals/BudgetAutomationsModal.tsx
@@ -195,7 +195,7 @@ export function BudgetAutomationsModal({ categoryId }: { categoryId: string }) {
});
const categories = useBudgetAutomationCategories();
- const currentCategory = useCategory(categoryId);
+ const { data: currentCategory } = useCategory(categoryId);
const needsMigration = currentCategory?.template_settings?.source !== 'ui';
diff --git a/packages/desktop-client/src/components/modals/CategoryGroupMenuModal.tsx b/packages/desktop-client/src/components/modals/CategoryGroupMenuModal.tsx
index 9ca56eb037..6c6d99480a 100644
--- a/packages/desktop-client/src/components/modals/CategoryGroupMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/CategoryGroupMenuModal.tsx
@@ -54,7 +54,8 @@ export function CategoryGroupMenuModal({
onApplyBudgetTemplatesInGroup,
}: CategoryGroupMenuModalProps) {
const [showMore, setShowMore] = useState(false);
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const group = categoryGroups.find(g => g.id === groupId);
const notes = useNotes(group.id);
const { showUndoNotification } = useUndo();
diff --git a/packages/desktop-client/src/components/modals/CategoryMenuModal.tsx b/packages/desktop-client/src/components/modals/CategoryMenuModal.tsx
index 4cfa2df51d..eb3f06b24c 100644
--- a/packages/desktop-client/src/components/modals/CategoryMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/CategoryMenuModal.tsx
@@ -45,8 +45,8 @@ export function CategoryMenuModal({
onClose,
}: CategoryMenuModalProps) {
const { t } = useTranslation();
- const category = useCategory(categoryId);
- const categoryGroup = useCategoryGroup(category?.group);
+ const { data: category } = useCategory(categoryId);
+ const { data: categoryGroup } = useCategoryGroup(category?.group);
const originalNotes = useNotes(category.id);
const onRename = newName => {
diff --git a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
index b07f89cc46..44f3dd28ff 100644
--- a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
+++ b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
@@ -57,7 +57,12 @@ export function CloseAccountModal({
}: CloseAccountModalProps) {
const { t } = useTranslation(); // Initialize translation hook
const accounts = useAccounts().filter(a => a.closed === 0);
- const { grouped: categoryGroups, list: categories } = useCategories();
+ const {
+ data: { grouped: categoryGroups, list: categories } = {
+ grouped: [],
+ list: [],
+ },
+ } = useCategories();
const [loading, setLoading] = useState(false);
const [transferAccountId, setTransferAccountId] = useState('');
const transferAccount = accounts.find(a => a.id === transferAccountId);
diff --git a/packages/desktop-client/src/components/modals/ConfirmCategoryDeleteModal.tsx b/packages/desktop-client/src/components/modals/ConfirmCategoryDeleteModal.tsx
index c356b3bc40..b67d261c7a 100644
--- a/packages/desktop-client/src/components/modals/ConfirmCategoryDeleteModal.tsx
+++ b/packages/desktop-client/src/components/modals/ConfirmCategoryDeleteModal.tsx
@@ -32,7 +32,12 @@ export function ConfirmCategoryDeleteModal({
const { t } = useTranslation(); // Initialize translation hook
const [transferCategory, setTransferCategory] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
- const { grouped: categoryGroups, list: categories } = useCategories();
+ const {
+ data: { grouped: categoryGroups, list: categories } = {
+ grouped: [],
+ list: [],
+ },
+ } = useCategories();
const group = categoryGroups.find(g => g.id === groupId);
const category = categories.find(c => c.id === categoryId);
diff --git a/packages/desktop-client/src/components/modals/CoverModal.tsx b/packages/desktop-client/src/components/modals/CoverModal.tsx
index 1f8c413bdf..dc1a552f8b 100644
--- a/packages/desktop-client/src/components/modals/CoverModal.tsx
+++ b/packages/desktop-client/src/components/modals/CoverModal.tsx
@@ -40,7 +40,8 @@ export function CoverModal({
}: CoverModalProps) {
const { t } = useTranslation();
- const { grouped: originalCategoryGroups } = useCategories();
+ const { data: { grouped: originalCategoryGroups } = { grouped: [] } } =
+ useCategories();
const [categoryGroups, categories] = useMemo(() => {
const expenseGroups = originalCategoryGroups.filter(g => !g.is_income);
const categoryGroups = showToBeBudgeted
diff --git a/packages/desktop-client/src/components/modals/EnvelopeBalanceMenuModal.tsx b/packages/desktop-client/src/components/modals/EnvelopeBalanceMenuModal.tsx
index aeae5e0160..ad9da5c895 100644
--- a/packages/desktop-client/src/components/modals/EnvelopeBalanceMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/EnvelopeBalanceMenuModal.tsx
@@ -41,7 +41,7 @@ export function EnvelopeBalanceMenuModal({
borderTop: `1px solid ${theme.pillBorder}`,
};
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
if (!category) {
return null;
diff --git a/packages/desktop-client/src/components/modals/EnvelopeBudgetMenuModal.tsx b/packages/desktop-client/src/components/modals/EnvelopeBudgetMenuModal.tsx
index 9496c39998..05841e6e01 100644
--- a/packages/desktop-client/src/components/modals/EnvelopeBudgetMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/EnvelopeBudgetMenuModal.tsx
@@ -45,7 +45,7 @@ export function EnvelopeBudgetMenuModal({
const budgeted = useEnvelopeSheetValue(
envelopeBudget.catBudgeted(categoryId),
);
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
const [amountFocused, setAmountFocused] = useState(false);
const _onUpdateBudget = (amount: number) => {
diff --git a/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx b/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
index 122cc69a3e..404bf55da6 100644
--- a/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
+++ b/packages/desktop-client/src/components/modals/EnvelopeBudgetSummaryModal.tsx
@@ -8,7 +8,7 @@ import {
prevMonth,
sheetForMonth,
} from 'loot-core/shared/months';
-import { groupById } from 'loot-core/shared/util';
+import type { CategoryEntity } from 'loot-core/types/models/category';
import { ToBudgetAmount } from '@desktop-client/components/budget/envelope/budgetsummary/ToBudgetAmount';
import { TotalsList } from '@desktop-client/components/budget/envelope/budgetsummary/TotalsList';
@@ -18,7 +18,7 @@ import {
ModalCloseButton,
ModalHeader,
} from '@desktop-client/components/common/Modal';
-import { useCategories } from '@desktop-client/hooks/useCategories';
+import { useCategoriesById } from '@desktop-client/hooks/useCategories';
import { useFormat } from '@desktop-client/hooks/useFormat';
import { useLocale } from '@desktop-client/hooks/useLocale';
import { SheetNameProvider } from '@desktop-client/hooks/useSheetName';
@@ -50,8 +50,11 @@ export function EnvelopeBudgetSummaryModal({
}) ?? 0;
const { showUndoNotification } = useUndo();
- const { list: categories } = useCategories();
- const categoriesById = groupById(categories);
+ const {
+ data: { list: categoriesById } = {
+ list: {} as Record<string, CategoryEntity>,
+ },
+ } = useCategoriesById();
const openTransferAvailableModal = () => {
dispatch(
diff --git a/packages/desktop-client/src/components/modals/EnvelopeIncomeBalanceMenuModal.tsx b/packages/desktop-client/src/components/modals/EnvelopeIncomeBalanceMenuModal.tsx
index f538c388f6..7dc2c70e49 100644
--- a/packages/desktop-client/src/components/modals/EnvelopeIncomeBalanceMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/EnvelopeIncomeBalanceMenuModal.tsx
@@ -42,7 +42,7 @@ export function EnvelopeIncomeBalanceMenuModal({
};
const { t } = useTranslation();
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
const carryover = useEnvelopeSheetValue(
envelopeBudget.catCarryover(categoryId),
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
index 30d2da822e..8b20cbacba 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
@@ -162,7 +162,7 @@ export function ImportTransactionsModal({
const dateFormat = useDateFormat() || ('MM/dd/yyyy' as const);
const [prefs, savePrefs] = useSyncedPrefs();
const dispatch = useDispatch();
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const [multiplierAmount, setMultiplierAmount] = useState('');
const [loadingState, setLoadingState] = useState<
diff --git a/packages/desktop-client/src/components/modals/TrackingBalanceMenuModal.tsx b/packages/desktop-client/src/components/modals/TrackingBalanceMenuModal.tsx
index c2f108a728..2d5b6c6dd8 100644
--- a/packages/desktop-client/src/components/modals/TrackingBalanceMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/TrackingBalanceMenuModal.tsx
@@ -39,7 +39,7 @@ export function TrackingBalanceMenuModal({
borderTop: `1px solid ${theme.pillBorder}`,
};
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
if (!category) {
return null;
diff --git a/packages/desktop-client/src/components/modals/TrackingBudgetMenuModal.tsx b/packages/desktop-client/src/components/modals/TrackingBudgetMenuModal.tsx
index 971e1b8da8..d11043548d 100644
--- a/packages/desktop-client/src/components/modals/TrackingBudgetMenuModal.tsx
+++ b/packages/desktop-client/src/components/modals/TrackingBudgetMenuModal.tsx
@@ -45,7 +45,7 @@ export function TrackingBudgetMenuModal({
const budgeted = useTrackingSheetValue(
trackingBudget.catBudgeted(categoryId),
);
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
const [amountFocused, setAmountFocused] = useState(false);
const _onUpdateBudget = (amount: number) => {
diff --git a/packages/desktop-client/src/components/modals/TransferModal.tsx b/packages/desktop-client/src/components/modals/TransferModal.tsx
index 9dcd6f35a9..f8aec19663 100644
--- a/packages/desktop-client/src/components/modals/TransferModal.tsx
+++ b/packages/desktop-client/src/components/modals/TransferModal.tsx
@@ -39,7 +39,8 @@ export function TransferModal({
}: TransferModalProps) {
const { t } = useTranslation();
- const { grouped: originalCategoryGroups } = useCategories();
+ const { data: { grouped: originalCategoryGroups } = { grouped: [] } } =
+ useCategories();
const [categoryGroups, categories] = useMemo(() => {
const expenseGroups = originalCategoryGroups.filter(g => !g.is_income);
const categoryGroups = showToBeBudgeted
diff --git a/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx b/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx
index 3dc286b0b1..f180486749 100644
--- a/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx
+++ b/packages/desktop-client/src/components/modals/UnmigrateBudgetAutomationsModal.tsx
@@ -27,7 +27,7 @@ export function UnmigrateBudgetAutomationsModal({
templates: Template[];
}) {
const { t } = useTranslation();
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
const existingNotes = useNotes(categoryId) || '';
const [editedNotes, setEditedNotes] = useState<string>('');
diff --git a/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx b/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
index d3e00f1b0b..1b14117f1a 100644
--- a/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/BarGraph.tsx
@@ -195,7 +195,7 @@ export function BarGraph({
}: BarGraphProps) {
const animationProps = useRechartsAnimation();
const navigate = useNavigate();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const accounts = useAccounts();
const privacyMode = usePrivacyMode();
const format = useFormat();
diff --git a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
index 24447d4ec0..cb34bb7c85 100644
--- a/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/DonutGraph.tsx
@@ -251,7 +251,7 @@ export function DonutGraph({
const splitData = groupBy === 'Interval' ? 'intervalData' : 'data';
const navigate = useNavigate();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const accounts = useAccounts();
const [pointer, setPointer] = useState('');
diff --git a/packages/desktop-client/src/components/reports/graphs/LineGraph.tsx b/packages/desktop-client/src/components/reports/graphs/LineGraph.tsx
index b861b7cf4f..a84d230aa6 100644
--- a/packages/desktop-client/src/components/reports/graphs/LineGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/LineGraph.tsx
@@ -168,7 +168,7 @@ export function LineGraph({
}: LineGraphProps) {
const animationProps = useRechartsAnimation();
const navigate = useNavigate();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const accounts = useAccounts();
const privacyMode = usePrivacyMode();
const format = useFormat();
diff --git a/packages/desktop-client/src/components/reports/graphs/StackedBarGraph.tsx b/packages/desktop-client/src/components/reports/graphs/StackedBarGraph.tsx
index 4e6fdbc8e2..b19fc9642d 100644
--- a/packages/desktop-client/src/components/reports/graphs/StackedBarGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/StackedBarGraph.tsx
@@ -199,7 +199,7 @@ export function StackedBarGraph({
}: StackedBarGraphProps) {
const animationProps = useRechartsAnimation();
const navigate = useNavigate();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const accounts = useAccounts();
const privacyMode = usePrivacyMode();
const format = useFormat();
diff --git a/packages/desktop-client/src/components/reports/graphs/tableGraph/ReportTableRow.tsx b/packages/desktop-client/src/components/reports/graphs/tableGraph/ReportTableRow.tsx
index 3dc538e6db..e6894cff67 100644
--- a/packages/desktop-client/src/components/reports/graphs/tableGraph/ReportTableRow.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/tableGraph/ReportTableRow.tsx
@@ -74,7 +74,7 @@ export const ReportTableRow = memo(
const navigate = useNavigate();
const { isNarrowWidth } = useResponsive();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const accounts = useAccounts();
const pointer =
diff --git a/packages/desktop-client/src/components/reports/reports/Calendar.tsx b/packages/desktop-client/src/components/reports/reports/Calendar.tsx
index 86be29e138..d9a9444a01 100644
--- a/packages/desktop-client/src/components/reports/reports/Calendar.tsx
+++ b/packages/desktop-client/src/components/reports/reports/Calendar.tsx
@@ -120,7 +120,8 @@ function CalendarInner({ widget, parameters }: CalendarInnerProps) {
const accounts = useAccounts();
const payees = usePayees();
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const [_firstDayOfWeekIdx] = useSyncedPref('firstDayOfWeekIdx');
const firstDayOfWeekIdx = _firstDayOfWeekIdx || '0';
diff --git a/packages/desktop-client/src/components/reports/reports/Crossover.tsx b/packages/desktop-client/src/components/reports/reports/Crossover.tsx
index 42102e56bf..3df62dfe25 100644
--- a/packages/desktop-client/src/components/reports/reports/Crossover.tsx
+++ b/packages/desktop-client/src/components/reports/reports/Crossover.tsx
@@ -80,7 +80,10 @@ function CrossoverInner({ widget }: CrossoverInnerProps) {
const { t } = useTranslation();
const dispatch = useDispatch();
const accounts = useAccounts();
- const categories = useCategories();
+ const {
+ data: categories = { grouped: [], list: [] },
+ isPending: isCategoriesLoading,
+ } = useCategories();
const format = useFormat();
const expenseCategoryGroups = categories.grouped.filter(
@@ -126,11 +129,7 @@ function CrossoverInner({ widget }: CrossoverInnerProps) {
// initialize once when data is available
useEffect(() => {
- if (
- selectionsInitialized ||
- accounts.length === 0 ||
- categories.list.length === 0
- ) {
+ if (selectionsInitialized || accounts.length === 0 || isCategoriesLoading) {
return;
}
@@ -162,7 +161,13 @@ function CrossoverInner({ widget }: CrossoverInnerProps) {
setShowHiddenCategories(widget?.meta?.showHiddenCategories ?? false);
setSelectionsInitialized(true);
- }, [selectionsInitialized, accounts, categories.list, widget?.meta]);
+ }, [
+ selectionsInitialized,
+ accounts,
+ categories.list,
+ isCategoriesLoading,
+ widget?.meta,
+ ]);
useEffect(() => {
async function run() {
@@ -400,7 +405,7 @@ function CrossoverInner({ widget }: CrossoverInnerProps) {
!data ||
!start ||
!end ||
- categories.list.length === 0 ||
+ isCategoriesLoading ||
accounts.length === 0
) {
return <LoadingIndicator />;
diff --git a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
index 54091cebdf..553de9f7c5 100644
--- a/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
+++ b/packages/desktop-client/src/components/reports/reports/CustomReport.tsx
@@ -139,7 +139,7 @@ function CustomReportInner({ report: initialReport }: CustomReportInnerProps) {
const { t } = useTranslation();
const format = useFormat();
- const categories = useCategories();
+ const { data: categories = { grouped: [], list: [] } } = useCategories();
const { isNarrowWidth } = useResponsive();
const [_firstDayOfWeekIdx] = useSyncedPref('firstDayOfWeekIdx');
const firstDayOfWeekIdx = _firstDayOfWeekIdx || '0';
diff --git a/packages/desktop-client/src/components/reports/reports/CustomReportListCards.tsx b/packages/desktop-client/src/components/reports/reports/CustomReportListCards.tsx
index 62ab6b2e40..d094d75547 100644
--- a/packages/desktop-client/src/components/reports/reports/CustomReportListCards.tsx
+++ b/packages/desktop-client/src/components/reports/reports/CustomReportListCards.tsx
@@ -81,7 +81,7 @@ function CustomReportListCardsInner({
const payees = usePayees();
const accounts = useAccounts();
- const categories = useCategories();
+ const { data: categories = { list: [], grouped: [] } } = useCategories();
const hasWarning = calculateHasWarning(report.conditions ?? [], {
categories: categories.list,
diff --git a/packages/desktop-client/src/components/rules/Value.tsx b/packages/desktop-client/src/components/rules/Value.tsx
index b4baf7ec05..c8503cbf06 100644
--- a/packages/desktop-client/src/components/rules/Value.tsx
+++ b/packages/desktop-client/src/components/rules/Value.tsx
@@ -43,7 +43,7 @@ export function Value<T>({
const format = useFormat();
const dateFormat = useDateFormat() || 'MM/dd/yyyy';
const payees = usePayees();
- const { list: categories } = useCategories();
+ const { data: { list: categories } = { list: [] } } = useCategories();
const accounts = useAccounts();
const valueStyle = {
color: theme.pageTextPositive,
diff --git a/packages/desktop-client/src/components/transactions/SimpleTransactionsTable.tsx b/packages/desktop-client/src/components/transactions/SimpleTransactionsTable.tsx
index 3e3ecab357..c0895abd88 100644
--- a/packages/desktop-client/src/components/transactions/SimpleTransactionsTable.tsx
+++ b/packages/desktop-client/src/components/transactions/SimpleTransactionsTable.tsx
@@ -63,7 +63,7 @@ const TransactionRow = memo(function TransactionRow({
}: TransactionRowProps) {
const { t } = useTranslation();
- const category = useCategory(transaction.category || '');
+ const { data: category } = useCategory(transaction.category);
const account = useAccount(transaction.account);
const dispatchSelected = useSelectedDispatch();
diff --git a/packages/desktop-client/src/components/util/GenericInput.tsx b/packages/desktop-client/src/components/util/GenericInput.tsx
index f6bd7550e7..28b98e9c79 100644
--- a/packages/desktop-client/src/components/util/GenericInput.tsx
+++ b/packages/desktop-client/src/components/util/GenericInput.tsx
@@ -123,7 +123,8 @@ export const GenericInput = ({
const dispatch = useDispatch();
const { isNarrowWidth } = useResponsive();
const { t } = useTranslation();
- const { grouped: categoryGroups } = useCategories();
+ const { data: { grouped: categoryGroups } = { grouped: [] } } =
+ useCategories();
const dateFormat = useDateFormat() || 'MM/dd/yyyy';
let content: JSX.Element | null = null;
diff --git a/packages/desktop-client/src/hooks/useCategories.ts b/packages/desktop-client/src/hooks/useCategories.ts
index c52bb4dfcd..340446e547 100644
--- a/packages/desktop-client/src/hooks/useCategories.ts
+++ b/packages/desktop-client/src/hooks/useCategories.ts
@@ -1,8 +1,21 @@
-import { useCategoriesQuery } from './useCategoriesQuery';
+import { useQuery } from '@tanstack/react-query';
+
+import { groupById } from 'loot-core/shared/util';
+
+import { categoryQueries } from '@desktop-client/budget';
export function useCategories() {
- const query = useCategoriesQuery();
- // TODO: Update to return query states (e.g. isFetching, isError, etc)
- // so clients can handle loading and error states appropriately.
- return query.data ?? { list: [], grouped: [] };
+ return useQuery(categoryQueries.list());
+}
+
+export function useCategoriesById() {
+ return useQuery({
+ ...categoryQueries.list(),
+ select: data => {
+ return {
+ list: groupById(data.list),
+ grouped: groupById(data.grouped),
+ };
+ },
+ });
}
diff --git a/packages/desktop-client/src/hooks/useCategoriesQuery.ts b/packages/desktop-client/src/hooks/useCategoriesQuery.ts
deleted file mode 100644
index 816e8826dc..0000000000
--- a/packages/desktop-client/src/hooks/useCategoriesQuery.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { useQuery } from '@tanstack/react-query';
-
-import { categoryQueries } from '@desktop-client/budget';
-
-export function useCategoriesQuery() {
- return useQuery(categoryQueries.list());
-}
diff --git a/packages/desktop-client/src/hooks/useCategory.ts b/packages/desktop-client/src/hooks/useCategory.ts
index e2c35f64c2..10e7dc3caa 100644
--- a/packages/desktop-client/src/hooks/useCategory.ts
+++ b/packages/desktop-client/src/hooks/useCategory.ts
@@ -2,10 +2,10 @@ import { useQuery } from '@tanstack/react-query';
import { categoryQueries } from '@desktop-client/budget';
-export function useCategory(id: string) {
- const query = useQuery({
+export function useCategory(id?: string | null) {
+ return useQuery({
...categoryQueries.list(),
select: data => data.list.find(c => c.id === id),
+ enabled: !!id,
});
- return query.data;
}
diff --git a/packages/desktop-client/src/hooks/useCategoryGroup.ts b/packages/desktop-client/src/hooks/useCategoryGroup.ts
index 043367d941..6ea6cc7bea 100644
--- a/packages/desktop-client/src/hooks/useCategoryGroup.ts
+++ b/packages/desktop-client/src/hooks/useCategoryGroup.ts
@@ -2,10 +2,10 @@ import { useQuery } from '@tanstack/react-query';
import { categoryQueries } from '@desktop-client/budget';
-export function useCategoryGroup(id: string) {
- const query = useQuery({
+export function useCategoryGroup(id?: string | null) {
+ return useQuery({
...categoryQueries.list(),
select: data => data.grouped.find(g => g.id === id),
+ enabled: !!id,
});
- return query.data;
}
diff --git a/packages/desktop-client/src/hooks/useCategoryPreviewTransactions.ts b/packages/desktop-client/src/hooks/useCategoryPreviewTransactions.ts
index e5f5cef150..5b41e541c7 100644
--- a/packages/desktop-client/src/hooks/useCategoryPreviewTransactions.ts
+++ b/packages/desktop-client/src/hooks/useCategoryPreviewTransactions.ts
@@ -23,7 +23,7 @@ export function useCategoryPreviewTransactions({
categoryId,
month,
}: UseCategoryPreviewTransactionsProps): UseCategoryPreviewTransactionsResult {
- const category = useCategory(categoryId);
+ const { data: category } = useCategory(categoryId);
const { schedules } = useCategoryScheduleGoalTemplates({
category,
});
diff --git a/packages/desktop-client/src/hooks/useOverspentCategories.ts b/packages/desktop-client/src/hooks/useOverspentCategories.ts
index 19bc28395d..f5f6e3c7f6 100644
--- a/packages/desktop-client/src/hooks/useOverspentCategories.ts
+++ b/packages/desktop-client/src/hooks/useOverspentCategories.ts
@@ -30,7 +30,12 @@ export function useOverspentCategories({
const spreadsheet = useSpreadsheet();
const [budgetType = 'envelope'] = useSyncedPref('budgetType');
- const { list: categories, grouped: categoryGroups } = useCategories();
+ const {
+ data: { list: categories, grouped: categoryGroups } = {
+ list: [],
+ grouped: [],
+ },
+ } = useCategories();
const categoryGroupsById = useMemo(
() => groupById(categoryGroups),
[categoryGroups],
diff --git a/upcoming-release-notes/6882.md b/upcoming-release-notes/6882.md
new file mode 100644
index 0000000000..81f960bee3
--- /dev/null
+++ b/upcoming-release-notes/6882.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Migrate category state management from Redux to React Query, updating hooks and components accordingly.