Skip to content

A local-first personal finance app

67.4 KB
Raw Blame
import React, {
  createRef,
  PureComponent,
  startTransition,
  useEffect,
  useMemo,
} from 'react';
import type { ReactElement, RefObject } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { Trans } from 'react-i18next';
import { Navigate, useLocation, useParams } from 'react-router';

import { styles } from '@actual-app/components/styles';
import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
import { listen, send } from '@actual-app/core/platform/client/connection';
import * as undo from '@actual-app/core/platform/client/undo';
import type { UndoState } from '@actual-app/core/server/undo';
import { q } from '@actual-app/core/shared/query';
import type { Query } from '@actual-app/core/shared/query';
import {
  makeAsNonChildTransactions,
  makeChild,
  ungroupTransaction,
  ungroupTransactions,
} from '@actual-app/core/shared/transactions';
import type { IntegerAmount } from '@actual-app/core/shared/util';
import type {
  AccountEntity,
  CategoryGroupEntity,
  NewRuleEntity,
  PayeeEntity,
  RuleActionEntity,
  RuleConditionEntity,
  TransactionEntity,
  TransactionFilterEntity,
} from '@actual-app/core/types/models';
import { debounce, isEqual } from 'es-toolkit/compat';
import { t } from 'i18next';
import { v4 as uuidv4 } from 'uuid';

import {
  useReopenAccountMutation,
  useSyncAndDownloadMutation,
  useUnlinkAccountMutation,
  useUpdateAccountMutation,
} from '#accounts';
import { markAccountRead } from '#accounts/accountsSlice';
import * as reconciliation from '#accounts/reconciliation';
import { FeatureErrorFallback } from '#components/FeatureErrorFallback';
import type { SavedFilter } from '#components/filters/SavedFilterMenuButton';
import type {
  TransactionTableColumn,
  TransactionTableColumnId,
} from '#components/transactions/table/columns';
import { TransactionList } from '#components/transactions/TransactionList';
import { validateAccountName } from '#components/util/accountValidation';
import { useAccountPreviewTransactions } from '#hooks/useAccountPreviewTransactions';
import { useAccounts } from '#hooks/useAccounts';
import { SchedulesProvider } from '#hooks/useCachedSchedules';
import { useCategories } from '#hooks/useCategories';
import { useDateFormat } from '#hooks/useDateFormat';
import { useFormat } from '#hooks/useFormat';
import { useLocalPref } from '#hooks/useLocalPref';
import { usePayees } from '#hooks/usePayees';
import { getSchedulesQuery } from '#hooks/useSchedules';
import { SelectedProviderWithItems } from '#hooks/useSelected';
import type { Actions } from '#hooks/useSelected';
import {
  SplitsExpandedProvider,
  useSplitsExpanded,
} from '#hooks/useSplitsExpanded';
import { useSyncedPref } from '#hooks/useSyncedPref';
import { useTransactionBatchActions } from '#hooks/useTransactionBatchActions';
import { useTransactionFilters } from '#hooks/useTransactionFilters';
import { calculateRunningBalancesBottomUp } from '#hooks/useTransactions';
import {
  SPECIAL_VIEW_IDS,
  useTransactionTableColumns,
} from '#hooks/useTransactionTableColumns';
import {
  openAccountCloseModal,
  pushModal,
  replaceModal,
} from '#modals/modalsSlice';
import type { ConfirmTransactionEditReason } from '#modals/modalsSlice';
import { addNotification } from '#notifications/notificationsSlice';
import { useCreatePayeeMutation } from '#payees';
import * as queries from '#queries';
import { aqlQuery } from '#queries/aqlQuery';
import { pagedQuery } from '#queries/pagedQuery';
import type { PagedQuery } from '#queries/pagedQuery';
import { useDispatch, useSelector } from '#redux';
import type { AppDispatch } from '#redux/store';
import { updateNewTransactions } from '#transactions/transactionsSlice';

import { AccountEmptyMessage } from './AccountEmptyMessage';
import { AccountHeader } from './Header';

type ConditionEntity = Partial<RuleConditionEntity> | TransactionFilterEntity;

function isTransactionFilterEntity(
  filter: ConditionEntity,
): filter is TransactionFilterEntity {
  return 'id' in filter;
}

type AllTransactionsProps = {
  account?: AccountEntity | undefined;
  transactions: TransactionEntity[];
  balances: Record<TransactionEntity['id'], IntegerAmount> | null;
  showBalances?: boolean | undefined;
  filtered?: boolean | undefined;
  children: (
    transactions: TransactionEntity[],
    balances: Record<TransactionEntity['id'], IntegerAmount> | null,
  ) => ReactElement;
};

function AllTransactions({
  account,
  transactions,
  balances,
  showBalances,
  filtered,
  children,
}: AllTransactionsProps) {
  const accountId = account?.id;
  const { dispatch: splitsExpandedDispatch } = useSplitsExpanded();
  const { previewTransactions, isLoading: isPreviewTransactionsLoading } =
    useAccountPreviewTransactions({ accountId });

  useEffect(() => {
    if (!isPreviewTransactionsLoading) {
      splitsExpandedDispatch({
        type: 'close-splits',
        ids: previewTransactions.filter(t => t.is_parent).map(t => t.id),
      });
    }
  }, [
    isPreviewTransactionsLoading,
    previewTransactions,
    splitsExpandedDispatch,
  ]);

  transactions ??= [];

  const runningBalance = useMemo(() => {
    if (!showBalances) {
      return 0;
    }

    return balances && transactions?.length > 0
      ? (balances[transactions[0].id] ?? 0)
      : 0;
  }, [showBalances, balances, transactions]);

  const prependBalances = useMemo(() => {
    if (!showBalances) {
      return null;
    }

    return Object.fromEntries(
      calculateRunningBalancesBottomUp(
        previewTransactions,
        'all',
        runningBalance,
      ),
    );
  }, [showBalances, previewTransactions, runningBalance]);

  const allTransactions = useMemo(() => {
    // Don't prepend scheduled transactions if we are filtering
    if (!filtered && previewTransactions.length > 0) {
      return previewTransactions.concat(transactions);
    }
    return transactions;
  }, [filtered, previewTransactions, transactions]);

  const allBalances = useMemo(() => {
    // Don't prepend scheduled transactions if we are filtering
    if (!filtered && prependBalances && balances) {
      return { ...prependBalances, ...balances };
    }
    return balances;
  }, [filtered, prependBalances, balances]);

  if (!previewTransactions?.length || filtered) {
    return children(transactions, balances);
  }
  return children(allTransactions, allBalances);
}

function getField(field?: string) {
  if (!field) {
    return 'date';
  }

  switch (field) {
    case 'account':
      return 'account.name';
    case 'payee':
      return 'payee.name';
    case 'category':
      return 'category.name';
    case 'payment':
      return 'amount';
    case 'deposit':
      return 'amount';
    default:
      return field;
  }
}

// The transaction a link to the account asks to bring into view, e.g. the
// other side of a transfer
function getFocusTransactionId(
  location: ReturnType<typeof useLocation>,
): TransactionEntity['id'] | null {
  const id: unknown = location.state?.focusTransactionId;
  return typeof id === 'string' ? id : null;
}

type AccountInternalProps = {
  accountId?:
    | AccountEntity['id']
    | 'onbudget'
    | 'offbudget'
    | 'uncategorized'
    | undefined;
  filterConditions: RuleConditionEntity[];
  showBalances?: boolean;
  showNetWorthChart: boolean;
  setShowNetWorthChart: (newValue: boolean) => void;
  showCleared?: boolean;
  showReconciled: boolean;
  setShowReconciled: (newValue: boolean) => void;
  showGroup: boolean;
  showExtraBalances?: boolean;
  setShowExtraBalances: (newValue: boolean) => void;
  transactionColumns: TransactionTableColumn[];
  columnOrder: TransactionTableColumnId[];
  saveColumns: (columns: TransactionTableColumn[], applyToAll: boolean) => void;
  modalShowing?: boolean;
  accounts: AccountEntity[];
  newTransactions: Array<TransactionEntity['id']>;
  matchedTransactions: Array<TransactionEntity['id']>;
  splitsExpandedDispatch: ReturnType<typeof useSplitsExpanded>['dispatch'];
  expandSplits?: boolean | undefined;
  savedFilters: TransactionFilterEntity[];
  onBatchEdit: ReturnType<typeof useTransactionBatchActions>['onBatchEdit'];
  onBatchDuplicate: ReturnType<
    typeof useTransactionBatchActions
  >['onBatchDuplicate'];
  onBatchLinkSchedule: ReturnType<
    typeof useTransactionBatchActions
  >['onBatchLinkSchedule'];
  onBatchUnlinkSchedule: ReturnType<
    typeof useTransactionBatchActions
  >['onBatchUnlinkSchedule'];
  onBatchDelete: ReturnType<typeof useTransactionBatchActions>['onBatchDelete'];
  categoryId?: string;
  location: ReturnType<typeof useLocation>;
  dateFormat: ReturnType<typeof useDateFormat>;
  currencySymbol: string;
  payees: PayeeEntity[];
  categoryGroups: CategoryGroupEntity[];
  hideFraction: boolean;
  accountsSyncing: string[];
  dispatch: AppDispatch;
  onSetTransfer: ReturnType<typeof useTransactionBatchActions>['onSetTransfer'];
  onReopenAccount: (id: AccountEntity['id']) => void;
  onUpdateAccount: (account: AccountEntity) => void;
  onUnlinkAccount: (id: AccountEntity['id']) => void;
  onSyncAndDownload: (accountId?: AccountEntity['id']) => void;
  onCreatePayee: (name: PayeeEntity['name']) => Promise<PayeeEntity['id']>;
};

type AccountInternalState = {
  search: string;
  filterConditions: ConditionEntity[];
  filterId?: SavedFilter | undefined;
  filterConditionsOp: 'and' | 'or';
  loading: boolean;
  workingHard: boolean;
  reconcileAmount: null | number;
  transactions: TransactionEntity[];
  transactionsFiltered?: boolean;
  showBalances?: boolean | undefined;
  balances: Record<TransactionEntity['id'], IntegerAmount> | null;
  showCleared?: boolean | undefined;
  prevShowCleared?: boolean | undefined;
  showReconciled: boolean;
  nameError: string;
  isAdding: boolean;
  modalShowing?: boolean;
  sort: {
    ascDesc: 'asc' | 'desc';
    field: string;
    prevField?: string | undefined;
    prevAscDesc?: 'asc' | 'desc' | undefined;
  } | null;
  filteredAmount: null | number;
  focusedTransactionId: TransactionEntity['id'] | null;
};

export type TableRef = RefObject<{
  edit: (updatedId: string | null, op?: string, someBool?: boolean) => void;
  setRowAnimation: (animation: boolean) => void;
  scrollTo: (focusId: string) => void;
  scrollToTop: () => void;
  getScrolledItem: () => string;
} | null>;

class AccountInternal extends PureComponent<
  AccountInternalProps,
  AccountInternalState
> {
  paged: PagedQuery<TransactionEntity> | null;
  rootQuery!: Query;
  currentQuery!: Query;
  table: TableRef;
  unlisten?: () => void;
  dispatchSelected?: (action: Actions) => void;
  _isOptimisticUpdate: boolean = false;
  // A transaction to bring into view once the account is loaded
  pendingFocusId: TransactionEntity['id'] | null = null;

  constructor(props: AccountInternalProps) {
    super(props);
    this.paged = null;
    this.table = createRef();

    this.state = {
      search: '',
      filterConditions: props.filterConditions || [],
      filterId: undefined,
      filterConditionsOp: 'and',
      loading: true,
      workingHard: false,
      reconcileAmount: null,
      transactions: [],
      showBalances: props.showBalances,
      balances: null,
      showCleared: props.showCleared,
      showReconciled: props.showReconciled,
      nameError: '',
      isAdding: false,
      sort: null,
      filteredAmount: null,
      focusedTransactionId: null,
    };
  }

  async componentDidMount() {
    const maybeRefetch = (tables: string[]) => {
      if (
        tables.includes('transactions') ||
        tables.includes('category_mapping') ||
        tables.includes('payee_mapping')
      ) {
        return this.refetchTransactions();
      }
    };

    const onUndo = async ({ tables, messages }: UndoState) => {
      await maybeRefetch(tables);

      // If all the messages are dealing with transactions, find the
      // first message referencing a non-deleted row so that we can
      // highlight the row
      //
      let focusId: null | string = null;
      if (
        messages.every(msg => msg.dataset === 'transactions') &&
        !messages.find(msg => msg.column === 'tombstone')
      ) {
        const focusableMsgs = messages.filter(
          msg =>
            msg.dataset === 'transactions' && !(msg.column === 'tombstone'),
        );

        focusId = focusableMsgs.length === 1 ? focusableMsgs[0].row : null;

        // Highlight the transactions
        // this.table && this.table.highlight(focusableMsgs.map(msg => msg.row));
      }

      if (this.table.current) {
        this.table.current.edit(null);

        // Focus a transaction if applicable. There is a chance if the
        // user navigated away that focusId is a transaction that has
        // been "paged off" and we won't focus it. That's ok, we just
        // do our best.
        if (focusId) {
          this.table.current.scrollTo(focusId);
        }
      }

      undo.setUndoState('undoEvent', null);
    };

    const unlistens = [listen('undo-event', onUndo)];

    this.unlisten = () => {
      unlistens.forEach(unlisten => unlisten());
    };

    // Important that any async work happens last so that the
    // listeners are set up synchronously
    this.pendingFocusId = getFocusTransactionId(this.props.location);
    this.fetchTransactions(this.state.filterConditions);

    // If there is a pending undo, apply it immediately (this happens
    // when an undo changes the location to this page)
    const lastUndoEvent = undo.getUndoState('undoEvent');
    if (lastUndoEvent) {
      void onUndo(lastUndoEvent);
    }
  }

  componentDidUpdate(prevProps: AccountInternalProps) {
    // If the active account changes - close the transaction entry mode
    if (this.state.isAdding && this.props.accountId !== prevProps.accountId) {
      this.setState({ isAdding: false });
    }

    // If the user was on a different screen and is now coming back to
    // the transactions, automatically refresh the transaction to make
    // sure we have updated state
    if (prevProps.modalShowing && !this.props.modalShowing) {
      // This is clearly a hack. Need a better way to track which
      // things are listening to transactions and refetch
      // automatically (use ActualQL?)
      setTimeout(() => {
        void this.refetchTransactions();
      }, 100);
    }

    //Resest sort/filter/search on account change
    if (this.props.accountId !== prevProps.accountId) {
      this.setState({ sort: null, search: '', filterConditions: [] });
    }
  }

  componentWillUnmount() {
    if (this.unlisten) {
      this.unlisten();
    }
    if (this.paged) {
      this.paged.unsubscribe();
    }
  }

  fetchAllIds = async () => {
    if (!this.paged) {
      return [];
    }

    const { data } = await aqlQuery(this.paged.query.select('id'));
    // Remember, this is the `grouped` split type so we need to deal
    // with the `subtransactions` property
    return data.reduce((arr: string[], t: TransactionEntity) => {
      arr.push(t.id);
      t.subtransactions?.forEach(sub => arr.push(sub.id));
      return arr;
    }, []);
  };

  refetchTransactions = async () => {
    void this.paged?.run();
  };

  fetchTransactions = (filterConditions?: ConditionEntity[]) => {
    const query = this.makeRootTransactionsQuery();
    this.rootQuery = this.currentQuery = query;
    if (filterConditions) void this.applyFilters(filterConditions);
    else this.updateQuery(query);

    if (this.props.accountId) {
      this.props.dispatch(markAccountRead({ id: this.props.accountId }));
    }
  };

  makeRootTransactionsQuery = () => {
    const accountId = this.props.accountId;

    return queries.transactions(accountId);
  };

  updateQuery(query: Query, isFiltered: boolean = false) {
    if (this.paged) {
      this.paged.unsubscribe();
    }

    // Filter out reconciled transactions if they are hidden
    // and we're not showing balances.
    if (
      !this.state.showReconciled &&
      (!this.state.showBalances || !this.canCalculateBalance())
    ) {
      query = query.filter({ reconciled: { $eq: false } });
    }

    const paged = pagedQuery<TransactionEntity>(query.select('*'), {
      onData: async (groupedData, prevData) => {
        const data = ungroupTransactions([...groupedData]);
        const firstLoad = prevData == null;

        // Fast path for optimistic updates (e.g. field edits): skip the
        // expensive aggregate DB queries (calculateBalances, getFilteredAmount)
        // and just update the transaction list in state directly. Balances and
        // filteredAmount will be refreshed on the next full DB-driven onData.
        if (this._isOptimisticUpdate) {
          this._isOptimisticUpdate = false;
          const transactionsSnapshot = data;
          const balances = this.state.showBalances
            ? await this.calculateBalances()
            : null;
          // Wrap in startTransition so React treats this as a low-priority
          // update. Without this, setState blocks the main thread for the
          // full duration of the re-render (~40–220ms with large transaction
          // lists), preventing input events from being processed and making
          // the UI feel frozen. startTransition lets React break the render
          // into chunks and yield to the browser between them, keeping the
          // UI responsive while the row update happens in the background.
          startTransition(() => {
            this.setState({
              transactions: transactionsSnapshot,
              balances,
            });
          });
          return;
        }

        if (firstLoad) {
          this.table.current?.setRowAnimation(false);

          if (isFiltered) {
            this.props.splitsExpandedDispatch({
              type: 'set-mode',
              mode: 'collapse',
            });
          } else {
            this.props.splitsExpandedDispatch({
              type: 'set-mode',
              mode: this.props.expandSplits ? 'expand' : 'collapse',
            });
          }
        }

        const balances = this.state.showBalances
          ? await this.calculateBalances()
          : null;
        const filteredAmount = await this.getFilteredAmount();
        this.setState(
          {
            transactions: data,
            transactionsFiltered: isFiltered,
            loading: false,
            workingHard: false,
            balances,
            filteredAmount,
          },
          () => {
            if (firstLoad) {
              this.table.current?.scrollToTop();
              if (paged === this.paged) {
                void this.showPendingFocus(paged);
              }
            }

            setTimeout(() => {
              this.table.current?.setRowAnimation(true);
            }, 0);
          },
        );
      },
      options: {
        pageCount: 150,
        onlySync: true,
      },
    });
    this.paged = paged;
  }

  // oxlint-disable-next-line react/no-unsafe
  UNSAFE_componentWillReceiveProps(nextProps: AccountInternalProps) {
    if (this.props.accountId !== nextProps.accountId) {
      this.pendingFocusId = getFocusTransactionId(nextProps.location);
      this.setState(
        {
          loading: true,
          focusedTransactionId: null,
          search: '',
          showBalances: nextProps.showBalances,
          balances: null,
          showCleared: nextProps.showCleared,
          showReconciled: nextProps.showReconciled,
          reconcileAmount: null,
        },
        () => {
          this.fetchTransactions();
        },
      );
    }
  }

  // Brings the pending transaction into view, loading the account up to it
  // if needed. Leaves the account at the top when the query doesn't have it,
  // e.g. while reconciled transactions are hidden.
  showPendingFocus = async (paged: PagedQuery<TransactionEntity>) => {
    const id = this.pendingFocusId;
    this.pendingFocusId = null;
    // Only in the default date order; the lookup relies on it
    if (id == null || this.state.sort != null) {
      return;
    }
    if (!this.state.transactions.some(t => t.id === id)) {
      await paged.refetchUpToRow(id, { field: 'date', order: 'desc' });
    }

    const transaction = ungroupTransactions(paged.data).find(t => t.id === id);
    if (
      transaction == null ||
      (transaction.reconciled && !this.state.showReconciled) ||
      // The query was replaced meanwhile, e.g. by a sort or another account
      !paged.isRunning
    ) {
      return;
    }
    if (transaction.parent_id) {
      this.props.splitsExpandedDispatch({
        type: 'open-split',
        id: transaction.parent_id,
      });
    }
    this.setState({ focusedTransactionId: id });
  };

  onFocusedTransactionShown = () => {
    this.setState({ focusedTransactionId: null });
  };

  onSearch = (value: string) => {
    this.paged?.unsubscribe();
    this.setState({ search: value }, this.onSearchDone);
  };

  onSearchDone = debounce(() => this.applySearch(), 150);

  applySearch = () => {
    if (this.state.search === '') {
      this.updateQuery(
        this.currentQuery,
        this.state.filterConditions.length > 0,
      );
    } else {
      this.updateQuery(
        queries.transactionsSearch(
          this.currentQuery,
          this.state.search,
          this.props.dateFormat,
          this.props.currencySymbol,
        ),
        true,
      );
    }
  };

  onSync = async () => {
    const accountId = this.props.accountId;
    const account = this.props.accounts.find(acct => acct.id === accountId);

    this.props.onSyncAndDownload(account ? account.id : accountId);
  };

  onImport = async () => {
    const accountId = this.props.accountId;
    const account = this.props.accounts.find(acct => acct.id === accountId);

    if (account) {
      const res = await window.Actual.openFileDialog({
        filters: [
          {
            name: t('Financial files'),
            extensions: ['qif', 'ofx', 'qfx', 'csv', 'tsv', 'xml'],
          },
        ],
      });

      if (res) {
        if (accountId && res?.length > 0) {
          this.props.dispatch(
            pushModal({
              modal: {
                name: 'import-transactions',
                options: {
                  accountId,
                  filename: res[0],
                  onImported: (didChange: boolean) => {
                    if (didChange) {
                      this.fetchTransactions();
                    }
                  },
                },
              },
            }),
          );
        }
      }
    }
  };

  onExport = async (accountName: string) => {
    const exportedTransactions = await send('transactions-export-query', {
      query: this.currentQuery.serialize(),
    });
    const normalizedName =
      accountName && accountName.replace(/[()]/g, '').replace(/\s+/g, '-');
    const filename = `${normalizedName || 'transactions'}.csv`;

    try {
      await window.Actual.saveFile(
        exportedTransactions,
        filename,
        t('Export transactions'),
      );
    } catch {
      this.props.dispatch(
        addNotification({
          notification: {
            type: 'error',
            message: t('Failed to export transactions.'),
          },
        }),
      );
    }
  };

  onTransactionsChange = (updatedTransaction: TransactionEntity) => {
    // Apply changes to pagedQuery data optimistically. Set the flag so that
    // onData skips the expensive aggregate DB queries for this update.
    this._isOptimisticUpdate = true;
    this.paged?.optimisticUpdate(data => {
      if (updatedTransaction._deleted) {
        return data.filter(t => t.id !== updatedTransaction.id);
      } else {
        return data.map(t => {
          return t.id === updatedTransaction.id ? updatedTransaction : t;
        });
      }
    });

    this.props.dispatch(updateNewTransactions({ id: updatedTransaction.id }));
  };

  canCalculateBalance = () => {
    const accountId = this.props.accountId;
    const account = this.props.accounts.find(
      account => account.id === accountId,
    );

    if (!account) return false;
    if (this.state.search !== '') return false;
    if (this.state.filterConditions.length > 0) return false;
    if (this.state.sort === null) {
      return true;
    } else {
      return (
        this.state.sort.field === 'date' && this.state.sort.ascDesc === 'desc'
      );
    }
  };

  async calculateBalances() {
    if (!this.canCalculateBalance() || !this.paged) {
      return null;
    }

    const { data }: { data: { id: string; balance: number }[] } =
      await aqlQuery(
        this.paged.query
          .options({ splits: 'none' })
          .select([{ balance: { $sumOver: '$amount' } }]),
      );

    return data.reduce((balances: Record<string, number>, row) => {
      balances[row.id] = row.balance;
      return balances;
    }, {});
  }

  onRunRules = async (ids: string[]) => {
    try {
      this.setState({ workingHard: true });
      // Bulk fetch transactions
      const transactions = this.state.transactions.filter(trans =>
        ids.includes(trans.id),
      );
      const changedTransactions: TransactionEntity[] = [];
      const allErrors: string[] = [];

      for (const transaction of transactions) {
        const res: TransactionEntity | null = await send('rules-run', {
          transaction,
        });
        if (res) {
          changedTransactions.push(...ungroupTransaction(res));

          // Collect formula errors
          if (res._ruleErrors && res._ruleErrors.length > 0) {
            allErrors.push(...res._ruleErrors);
          }
        }
      }

      // Show errors if any
      if (allErrors.length > 0) {
        this.props.dispatch(
          addNotification({
            notification: {
              type: 'error',
              message: `Formula errors in rules:\n${allErrors.join('\n')}`,
              sticky: true,
            },
          }),
        );
      }

      // If we have changed transactions, update them in the database
      if (changedTransactions.length > 0) {
        await send('transactions-batch-update', {
          updated: changedTransactions,
        });
      }

      // Fetch updated transactions once at the end
      this.fetchTransactions();
    } catch (error) {
      console.error('Error applying rules:', error);
      this.props.dispatch(
        addNotification({
          notification: {
            type: 'error',
            message: 'Failed to apply rules to transactions',
          },
        }),
      );
    } finally {
      this.setState({ workingHard: false });
    }
  };

  onAddTransaction = () => {
    this.setState({ isAdding: true });
  };

  onSaveName = (name: string) => {
    const accountNameError = validateAccountName(
      name,
      this.props.accountId ?? '',
      this.props.accounts,
    );
    if (accountNameError) {
      this.setState({ nameError: accountNameError });
    } else {
      const account = this.props.accounts.find(
        account => account.id === this.props.accountId,
      );
      if (!account) {
        throw new Error(`Account with ID ${this.props.accountId} not found.`);
      }
      this.props.onUpdateAccount({ ...account, name });
      this.setState({ nameError: '' });
    }
  };

  onToggleExtraBalances = () => {
    this.props.setShowExtraBalances(!this.props.showExtraBalances);
  };

  onMenuSelect = async (
    item:
      | 'link'
      | 'unlink'
      | 'close'
      | 'reopen'
      | 'export'
      | 'remove-sorting'
      | 'toggle-reconciled'
      | 'toggle-net-worth-chart'
      | 'manage-columns'
      | 'account-group',
  ) => {
    const accountId = this.props.accountId!;
    const account = this.props.accounts.find(
      account => account.id === accountId,
    )!;

    switch (item) {
      case 'link':
        this.props.dispatch(
          pushModal({
            modal: {
              name: 'add-account',
              options: {
                upgradingAccountId: accountId,
              },
            },
          }),
        );
        break;
      case 'unlink':
        this.props.dispatch(
          pushModal({
            modal: {
              name: 'confirm-unlink-account',
              options: {
                accountName: account.name,
                isViewBankSyncSettings: false,
                onUnlink: () => {
                  this.props.onUnlinkAccount(accountId);
                },
              },
            },
          }),
        );
        break;
      case 'close':
        void this.props.dispatch(openAccountCloseModal({ accountId }));
        break;
      case 'account-group':
        this.props.dispatch(
          pushModal({
            modal: {
              name: 'account-groups',
              options: {
                accountId,
              },
            },
          }),
        );
        break;
      case 'reopen':
        this.props.onReopenAccount(accountId);
        break;
      case 'export':
        const accountName = this.getAccountTitle(account, accountId);
        void this.onExport(accountName);
        break;
      case 'remove-sorting': {
        this.setState({ sort: null }, () => {
          const filterConditions = this.state.filterConditions;
          if (filterConditions.length > 0) {
            void this.applyFilters([...filterConditions]);
          } else {
            this.fetchTransactions();
          }
          if (this.state.search !== '') {
            this.onSearch(this.state.search);
          }
        });
        break;
      }
      case 'toggle-reconciled': {
        const showReconciled = !this.state.showReconciled;
        this.props.setShowReconciled(showReconciled);
        this.setState({ showReconciled }, this.applySearch);
        break;
      }
      case 'toggle-net-worth-chart':
        if (this.props.showNetWorthChart) {
          this.props.setShowNetWorthChart(false);
        } else {
          this.props.setShowNetWorthChart(true);
        }
        break;
      case 'manage-columns':
        this.onManageColumns();
        break;
      default:
    }
  };

  showAccountColumn = () => {
    const accountId = this.props.accountId;
    return !accountId || SPECIAL_VIEW_IDS.includes(accountId);
  };

  onManageColumns = () => {
    const columns = this.props.transactionColumns
      .filter(
        column =>
          (column.id !== 'account' || this.showAccountColumn()) &&
          (column.id !== 'balance' || this.canCalculateBalance()),
      )
      .map(column => {
        // Balance and cleared visibility can be temporarily overridden in
        // component state (e.g. while reconciling) and may still come from
        // the old per-account prefs, so state is the source of truth here.
        if (column.id === 'balance') {
          return { ...column, hidden: !this.state.showBalances };
        }
        if (column.id === 'cleared') {
          // During reconciliation the cleared column is temporarily forced
          // visible, so show the user's underlying preference instead
          const showCleared =
            this.state.reconcileAmount != null
              ? this.state.prevShowCleared
              : this.state.showCleared;
          return { ...column, hidden: !showCleared };
        }
        // Group visibility may come from the legacy pref fallback rather
        // than the saved config, so the resolved prop is the source of truth
        if (column.id === 'group') {
          return { ...column, hidden: !this.props.showGroup };
        }
        return column;
      });

    this.props.dispatch(
      pushModal({
        modal: {
          name: 'transaction-table-columns',
          options: {
            columns,
            onSave: this.onSaveColumns,
          },
        },
      }),
    );
  };

  onSaveColumns = (columns: TransactionTableColumn[], applyToAll: boolean) => {
    // Columns that aren't managed in the current view (e.g. the account
    // column on a single-account page) keep their previous position and
    // visibility so a save here doesn't clobber them.
    const merged = [...columns];
    this.props.transactionColumns.forEach((column, index) => {
      if (!merged.some(c => c.id === column.id)) {
        merged.splice(Math.min(index, merged.length), 0, column);
      }
    });

    this.props.saveColumns(merged, applyToAll);

    // Toggling the balance column changes which queries run, so mirror the
    // change into component state and refetch when needed.
    const balance = columns.find(column => column.id === 'balance');
    const isBalanceVisible = balance && !balance.hidden;
    if (balance && isBalanceVisible !== !!this.state.showBalances) {
      if (!isBalanceVisible) {
        this.setState({ showBalances: false, balances: null });
      } else {
        this.setState(
          {
            transactions: [],
            filterConditions: [],
            search: '',
            sort: null,
            showBalances: true,
          },
          () => {
            this.fetchTransactions();
          },
        );
      }
    }

    const cleared = columns.find(column => column.id === 'cleared');
    const isClearedVisible = cleared && !cleared.hidden;
    if (cleared && isClearedVisible !== !!this.state.showCleared) {
      // Also update prevShowCleared so finishing a reconciliation restores
      // the visibility chosen here, not the stale pre-reconcile value
      this.setState({
        showCleared: isClearedVisible,
        prevShowCleared: isClearedVisible,
      });
    }
  };

  getAccountTitle(account?: AccountEntity, id?: string) {
    const { filterName } = this.props.location.state || {};

    if (filterName) {
      return filterName;
    }

    if (!account) {
      if (id === 'onbudget') {
        return t('On Budget Accounts');
      } else if (id === 'offbudget') {
        return t('Off Budget Accounts');
      } else if (id === 'uncategorized') {
        return t('Uncategorized');
      } else if (!id) {
        return t('All Accounts');
      }
      return null;
    }

    return account.name;
  }

  getBalanceQuery(id?: string) {
    return {
      name: `balance-query-${id}`,
      query: this.makeRootTransactionsQuery().calculate({ $sum: '$amount' }),
    } as const;
  }

  getFilteredAmount = async () => {
    if (!this.paged) {
      return 0;
    }

    const { data: amount } = await aqlQuery(
      this.paged.query.calculate({ $sum: '$amount' }),
    );
    return amount;
  };

  isNew = (id: TransactionEntity['id']) => {
    return this.props.newTransactions.includes(id);
  };

  isMatched = (id: TransactionEntity['id']) => {
    return this.props.matchedTransactions.includes(id);
  };

  onCreatePayee = async (name: string) => {
    const trimmed = name.trim();
    if (trimmed !== '') {
      return await this.props.onCreatePayee(name);
    }
    return null;
  };

  lockTransactions = async () => {
    const { accountId } = this.props;
    if (!accountId) {
      return;
    }

    this.setState({ workingHard: true });

    await reconciliation.lockTransactions(accountId);
    await this.refetchTransactions();
  };

  onReconcile = async (amount: number | null) => {
    this.setState(({ showCleared }) => ({
      reconcileAmount: amount,
      showCleared: true,
      prevShowCleared: showCleared,
    }));
  };

  onDoneReconciling = async () => {
    const { accountId } = this.props;
    const account = this.props.accounts.find(
      account => account.id === accountId,
    );
    if (!account) {
      throw new Error(`Account with ID ${accountId} not found.`);
    }

    const { reconcileAmount } = this.state;

    await reconciliation.finishReconciliation(account.id, reconcileAmount, () =>
      this.lockTransactions(),
    );

    const lastReconciled = new Date().getTime().toString();
    this.props.onUpdateAccount({ ...account, last_reconciled: lastReconciled });

    this.setState(state => ({
      reconcileAmount: null,
      showCleared: state.prevShowCleared,
    }));
  };

  onCreateReconciliationTransaction = async (diff: number) => {
    const { accountId } = this.props;
    if (!accountId) {
      return;
    }

    await reconciliation.createReconciliationTransaction(
      accountId,
      diff,
      // Optimistic UI: update the transaction list before sending the data to the database
      reconciliationTransactions =>
        this.setState(state => ({
          transactions: [...reconciliationTransactions, ...state.transactions],
        })),
    );
    await this.refetchTransactions();
  };

  onShowTransactions = async (ids: string[]) => {
    void this.onApplyFilter({
      customName: t('Selected transactions'),
      queryFilter: { id: { $oneof: ids } },
    });
  };

  onBatchEdit = (name: keyof TransactionEntity, ids: string[]) => {
    void this.props.onBatchEdit({
      name,
      ids,
      onSuccess: updatedIds => {
        void this.refetchTransactions();

        if (this.table.current) {
          this.table.current.edit(updatedIds[0], 'select', false);
        }
      },
    });
  };

  onBatchDuplicate = (ids: string[]) => {
    void this.props.onBatchDuplicate({
      ids,
      onSuccess: this.refetchTransactions,
    });
  };

  onBatchDelete = (ids: string[]) => {
    void this.props.onBatchDelete({ ids, onSuccess: this.refetchTransactions });
  };

  onMakeAsSplitTransaction = async (ids: string[]) => {
    this.setState({ workingHard: true });

    const { data } = await aqlQuery(
      q('transactions')
        .filter({ id: { $oneof: ids } })
        .select('*')
        .options({ splits: 'none' }),
    );

    const transactions: TransactionEntity[] = data;

    if (!transactions || transactions.length === 0) {
      return;
    }

    const [firstTransaction] = transactions;
    const parentTransaction = {
      id: uuidv4(),
      is_parent: true,
      cleared: transactions.every(t => !!t.cleared),
      date: firstTransaction.date,
      account: firstTransaction.account,
      amount: transactions
        .map(t => t.amount)
        .reduce((total, amount) => total + amount, 0),
    };
    const childTransactions = transactions.map(t =>
      makeChild(parentTransaction, t),
    );

    await send('transactions-batch-update', {
      added: [parentTransaction],
      updated: childTransactions,
    });

    void this.refetchTransactions();
  };

  onMakeAsNonSplitTransactions = async (ids: string[]) => {
    this.setState({ workingHard: true });

    const { data } = await aqlQuery(
      q('transactions')
        .filter({ id: { $oneof: ids } })
        .select('*')
        .options({ splits: 'grouped' }),
    );

    const groupedTransactions: TransactionEntity[] = data;

    let changes: {
      updated: TransactionEntity[];
      deleted: TransactionEntity[];
    } = {
      updated: [],
      deleted: [],
    };

    const groupedTransactionsToUpdate = groupedTransactions.filter(
      t => t.is_parent,
    );

    for (const groupedTransaction of groupedTransactionsToUpdate) {
      const transactions = ungroupTransaction(groupedTransaction);
      const [parentTransaction, ...childTransactions] = transactions;

      if (ids.includes(parentTransaction.id)) {
        // Unsplit all child transactions.
        const diff = makeAsNonChildTransactions(
          childTransactions,
          transactions,
        );

        changes = {
          updated: [...changes.updated, ...diff.updated],
          deleted: [...changes.deleted, ...diff.deleted],
        };

        // Already processed the child transactions above, no need to process them below.
        continue;
      }

      // Unsplit selected child transactions.

      const selectedChildTransactions = childTransactions.filter(t =>
        ids.includes(t.id),
      );

      if (selectedChildTransactions.length === 0) {
        continue;
      }

      const diff = makeAsNonChildTransactions(
        selectedChildTransactions,
        transactions,
      );

      changes = {
        updated: [...changes.updated, ...diff.updated],
        deleted: [...changes.deleted, ...diff.deleted],
      };
    }

    await send('transactions-batch-update', changes);

    void this.refetchTransactions();

    const transactionsToSelect = changes.updated.map(t => t.id);
    this.dispatchSelected?.({
      type: 'select-all',
      ids: transactionsToSelect,
    });
  };

  onMergeTransactions = async (ids: string[]) => {
    const keptId = await send(
      'transactions-merge',
      ids.map(id => ({ id })),
    );
    await this.refetchTransactions();
    this.dispatchSelected?.({
      type: 'select-all',
      ids: [keptId],
    });
  };

  checkForReconciledTransactions = async (
    ids: string[],
    confirmReason: ConfirmTransactionEditReason,
    onConfirm: (ids: string[]) => void,
  ) => {
    const { data } = await aqlQuery(
      q('transactions')
        .filter({ id: { $oneof: ids }, reconciled: true })
        .select('*')
        .options({ splits: 'grouped' }),
    );
    const transactions = ungroupTransactions(data);
    if (transactions.length > 0) {
      this.props.dispatch(
        pushModal({
          modal: {
            name: 'confirm-transaction-edit',
            options: {
              onConfirm: () => {
                onConfirm(ids);
              },
              confirmReason,
            },
          },
        }),
      );
    } else {
      onConfirm(ids);
    }
  };

  onBatchLinkSchedule = (ids: string[]) => {
    void this.props.onBatchLinkSchedule({
      ids,
      account: this.props.accounts.find(a => a.id === this.props.accountId),
      onSuccess: this.refetchTransactions,
    });
  };

  onBatchUnlinkSchedule = (ids: string[]) => {
    void this.props.onBatchUnlinkSchedule({
      ids,
      onSuccess: this.refetchTransactions,
    });
  };

  onCreateRule = async (ids: string[]) => {
    const { data } = await aqlQuery(
      q('transactions')
        .filter({ id: { $oneof: ids } })
        .select('*')
        .options({ splits: 'grouped' }),
    );

    const transactions = ungroupTransactions(data);
    const ruleTransaction = transactions[0];
    const childTransactions = transactions.filter(
      t => t.parent_id === ruleTransaction.id,
    );

    const payeeCondition = ruleTransaction.imported_payee
      ? ({
          field: 'imported_payee',
          op: 'is',
          value: ruleTransaction.imported_payee,
          type: 'string',
        } satisfies RuleConditionEntity)
      : ({
          field: 'payee',
          op: 'is',
          value: ruleTransaction.payee!,
          type: 'id',
        } satisfies RuleConditionEntity);
    const amountCondition = {
      field: 'amount',
      op: 'isapprox',
      value: ruleTransaction.amount,
      type: 'number',
    } satisfies RuleConditionEntity;

    const rule = {
      stage: null,
      conditionsOp: 'and',
      conditions: [payeeCondition, amountCondition],
      actions: [
        ...(childTransactions.length === 0
          ? [
              {
                op: 'set',
                field: 'category',
                value: ruleTransaction.category,
                type: 'id',
                options: {
                  splitIndex: 0,
                },
              } satisfies RuleActionEntity,
            ]
          : []),
        ...childTransactions.flatMap((sub, index) => [
          {
            op: 'set-split-amount',
            value: sub.amount,
            options: {
              splitIndex: index + 1,
              method: 'fixed-amount',
            },
          } satisfies RuleActionEntity,
          {
            op: 'set',
            field: 'category',
            value: sub.category,
            type: 'id',
            options: {
              splitIndex: index + 1,
            },
          } satisfies RuleActionEntity,
        ]),
      ],
    } satisfies NewRuleEntity;

    this.props.dispatch(
      pushModal({ modal: { name: 'edit-rule', options: { rule } } }),
    );
  };

  onSetTransfer = async (ids: string[]) => {
    this.setState({ workingHard: true });
    await this.props.onSetTransfer(
      ids,
      this.props.payees,
      this.refetchTransactions,
    );
  };

  onConditionsOpChange = (value: 'and' | 'or') => {
    this.setState(state => ({
      filterConditionsOp: value,
      filterId: { ...state.filterId, status: 'changed' } as SavedFilter,
    }));
    void this.applyFilters([...this.state.filterConditions]);
    if (this.state.search !== '') {
      this.onSearch(this.state.search);
    }
  };

  onReloadSavedFilter = (savedFilter: SavedFilter, item?: string) => {
    if (item === 'reload') {
      const [savedFilter] = this.props.savedFilters.filter(
        f => f.id === this.state.filterId?.id,
      );
      this.setState({ filterConditionsOp: savedFilter.conditionsOp ?? 'and' });
      void this.applyFilters([...savedFilter.conditions]);
    } else {
      if (savedFilter.status) {
        this.setState({
          filterConditionsOp: savedFilter.conditionsOp ?? 'and',
        });
        void this.applyFilters([...(savedFilter.conditions ?? [])]);
      }
    }
    this.setState(state => ({
      filterId: { ...state.filterId, ...savedFilter },
    }));
  };

  onClearFilters = () => {
    this.setState({ filterConditionsOp: 'and' });
    this.setState({ filterId: undefined });
    void this.applyFilters([]);
    if (this.state.search !== '') {
      this.onSearch(this.state.search);
    }
  };

  onUpdateFilter = (
    oldCondition: RuleConditionEntity,
    updatedCondition: RuleConditionEntity,
  ) => {
    void this.applyFilters(
      this.state.filterConditions.map(c =>
        c === oldCondition ? updatedCondition : c,
      ),
    );
    this.setState(state => ({
      filterId: {
        ...state.filterId,
        status: state.filterId && 'changed',
      } as SavedFilter,
    }));
    if (this.state.search !== '') {
      this.onSearch(this.state.search);
    }
  };

  onDeleteFilter = (condition: RuleConditionEntity) => {
    void this.applyFilters(
      this.state.filterConditions.filter(c => c !== condition),
    );
    if (this.state.filterConditions.length === 1) {
      this.setState({ filterId: undefined, filterConditionsOp: 'and' });
    } else {
      this.setState(state => ({
        filterId: {
          ...state.filterId,
          status: state.filterId && 'changed',
        } as SavedFilter,
      }));
    }
    if (this.state.search !== '') {
      this.onSearch(this.state.search);
    }
  };

  onApplyFilter = async (conditionOrSavedFilter: ConditionEntity) => {
    let filterConditions = this.state.filterConditions;

    if (
      'customName' in conditionOrSavedFilter &&
      conditionOrSavedFilter.customName
    ) {
      filterConditions = filterConditions.filter(
        c =>
          !isTransactionFilterEntity(c) &&
          c.customName !== conditionOrSavedFilter.customName,
      );
    }

    if (isTransactionFilterEntity(conditionOrSavedFilter)) {
      // A saved filter was passed in.
      const savedFilter = conditionOrSavedFilter;
      this.setState({
        filterId: { ...savedFilter, status: 'saved' },
      });
      this.setState({ filterConditionsOp: savedFilter.conditionsOp });
      void this.applyFilters([...savedFilter.conditions]);
    } else {
      // A condition was passed in.
      const condition = conditionOrSavedFilter;
      const isDuplicate = filterConditions.some(c => isEqual(c, condition));

      if (isDuplicate) {
        return;
      }

      this.setState(state => ({
        filterId: {
          ...state.filterId,
          status: state.filterId && 'changed',
        } as SavedFilter,
      }));
      void this.applyFilters([...filterConditions, condition]);
    }

    if (this.state.search !== '') {
      this.onSearch(this.state.search);
    }
  };

  onScheduleAction = async (
    name: 'skip' | 'post-transaction' | 'post-transaction-today' | 'complete',
    ids: TransactionEntity['id'][],
  ) => {
    const scheduleIds = ids.map(id => id.split('/')[1]);

    switch (name) {
      case 'post-transaction':
        for (const id of scheduleIds) {
          await send('schedule/post-transaction', { id });
        }
        void this.refetchTransactions();
        break;
      case 'post-transaction-today':
        for (const id of scheduleIds) {
          await send('schedule/post-transaction', { id, today: true });
        }
        void this.refetchTransactions();
        break;
      case 'skip':
        for (const id of scheduleIds) {
          await send('schedule/skip-next-date', { id });
        }
        break;
      case 'complete':
        for (const id of scheduleIds) {
          await send('schedule/update', { schedule: { id, completed: true } });
        }
        break;
      default:
    }
  };

  applyFilters = async (conditions: ConditionEntity[]) => {
    if (conditions.length > 0) {
      const filteredCustomQueryFilters: Partial<RuleConditionEntity>[] =
        conditions.filter(cond => !isTransactionFilterEntity(cond));
      const customQueryFilters = filteredCustomQueryFilters.map(
        f => f.queryFilter,
      );
      const { filters: queryFilters } = await send(
        'make-filters-from-conditions',
        {
          conditions: conditions.filter(
            cond => isTransactionFilterEntity(cond) || !cond.customName,
          ),
        },
      );
      const conditionsOpKey =
        this.state.filterConditionsOp === 'or' ? '$or' : '$and';
      this.currentQuery = this.rootQuery.filter({
        [conditionsOpKey]: [...queryFilters, ...customQueryFilters],
      });

      this.setState(
        {
          filterConditions: conditions,
        },
        () => {
          this.updateQuery(this.currentQuery, true);
        },
      );
    } else {
      this.setState(
        {
          transactions: [],
          filterConditions: conditions,
        },
        () => {
          this.fetchTransactions();
        },
      );
    }

    if (this.state.sort !== null) {
      this.applySort();
    }
  };

  applySort = (
    field?: string,
    ascDesc?: 'asc' | 'desc',
    prevField?: string,
    prevAscDesc?: 'asc' | 'desc',
  ) => {
    const filterConditions = this.state.filterConditions;
    const isFiltered = filterConditions.length > 0;
    const sortField = getField(!field ? this.state.sort?.field : field);
    const sortAscDesc = !ascDesc ? this.state.sort?.ascDesc : ascDesc;
    const sortPrevField = getField(
      !prevField ? this.state.sort?.prevField : prevField,
    );
    const sortPrevAscDesc = !prevField
      ? this.state.sort?.prevAscDesc
      : prevAscDesc;

    const sortCurrentQuery = function (
      that: AccountInternal,
      sortField: string,
      sortAscDesc?: 'asc' | 'desc',
    ) {
      if (sortField === 'cleared') {
        that.currentQuery = that.currentQuery.orderBy({
          reconciled: sortAscDesc,
        });
      }

      that.currentQuery = that.currentQuery.orderBy({
        [sortField]: sortAscDesc,
      });
    };

    const sortRootQuery = function (
      that: AccountInternal,
      sortField: string,
      sortAscDesc?: 'asc' | 'desc',
    ) {
      if (sortField === 'cleared') {
        that.currentQuery = that.rootQuery.orderBy({
          reconciled: sortAscDesc,
        });
        that.currentQuery = that.currentQuery.orderBy({
          cleared: sortAscDesc,
        });
      } else {
        that.currentQuery = that.rootQuery.orderBy({
          [sortField]: sortAscDesc,
        });
      }
    };

    // sort by previously used sort field, if any
    const maybeSortByPreviousField = function (
      that: AccountInternal,
      sortPrevField: string,
      sortPrevAscDesc?: 'asc' | 'desc',
    ) {
      if (!sortPrevField) {
        return;
      }

      if (sortPrevField === 'cleared') {
        that.currentQuery = that.currentQuery.orderBy({
          reconciled: sortPrevAscDesc,
        });
      }

      that.currentQuery = that.currentQuery.orderBy({
        [sortPrevField]: sortPrevAscDesc,
      });
    };

    switch (true) {
      // called by applyFilters to sort an already filtered result
      case !field:
        sortCurrentQuery(this, sortField, sortAscDesc);
        break;

      // called directly from UI by sorting a column.
      // active filters need to be applied before sorting
      case isFiltered:
        void this.applyFilters([...filterConditions]);
        sortCurrentQuery(this, sortField, sortAscDesc);
        break;

      // called directly from UI by sorting a column.
      // no active filters, start a new root query.
      case !isFiltered:
        sortRootQuery(this, sortField, sortAscDesc);
        break;

      default:
    }

    maybeSortByPreviousField(this, sortPrevField, sortPrevAscDesc);

    // Always add sort_order as a final tiebreaker to maintain stable ordering
    // when transactions have the same values in the sorted column(s)
    this.currentQuery = this.currentQuery.orderBy({ sort_order: sortAscDesc });

    this.updateQuery(this.currentQuery, isFiltered);
  };

  onSort = (headerClicked: string, ascDesc: 'asc' | 'desc') => {
    let prevField: string | undefined;
    let prevAscDesc: 'asc' | 'desc' | undefined;
    // Query once the new sort is in state: whether running balances can be
    // shown, and so whether hidden reconciled transactions are loaded,
    // depends on it.
    const sortAndSearch = () => {
      this.applySort(headerClicked, ascDesc, prevField, prevAscDesc);
      if (this.state.search !== '') {
        this.onSearch(this.state.search);
      }
    };
    //if staying on same column but switching asc/desc
    //then keep prev the same
    if (headerClicked === this.state.sort?.field) {
      prevField = this.state.sort.prevField;
      prevAscDesc = this.state.sort.prevAscDesc;
      this.setState(
        state => ({
          sort: {
            ...state.sort,
            field: headerClicked,
            ascDesc,
          },
        }),
        sortAndSearch,
      );
    } else {
      //if switching to new column then capture state
      //of current sort column as prev
      prevField = this.state.sort?.field;
      prevAscDesc = this.state.sort?.ascDesc;
      this.setState(
        state => ({
          sort: {
            field: headerClicked,
            ascDesc,
            prevField: state.sort?.field,
            prevAscDesc: state.sort?.ascDesc,
          },
        }),
        sortAndSearch,
      );
    }
  };

  render() {
    const {
      accounts,
      categoryGroups,
      payees,
      dateFormat,
      hideFraction,
      accountsSyncing,
      showExtraBalances,
      accountId,
      categoryId,
    } = this.props;
    const {
      transactions,
      loading,
      workingHard,
      filterId,
      reconcileAmount,
      transactionsFiltered,
      showBalances,
      balances,
      showCleared,
      showReconciled,
      filteredAmount,
    } = this.state;

    const account = accounts.find(account => account.id === accountId);
    const accountName = this.getAccountTitle(account, accountId);

    if (!accountName && !loading) {
      // This is probably an account that was deleted, so redirect to
      // all accounts
      return <Navigate to="/accounts" replace />;
    }

    const category = categoryGroups
      .flatMap(g => g.categories)
      .find(category => category?.id === categoryId);

    const showEmptyMessage = !loading && !accountId && accounts.length === 0;

    const isNameEditable = accountId
      ? accountId !== 'onbudget' &&
        accountId !== 'offbudget' &&
        accountId !== 'uncategorized'
      : false;

    const balanceQuery = this.getBalanceQuery(accountId);

    const selectAllFilter = (item: TransactionEntity): boolean => {
      if (item.is_parent) {
        const children = transactions.filter(t => t.parent_id === item.id);
        return children.every(t => selectAllFilter(t));
      }
      return !item._unmatched;
    };

    return (
      <AllTransactions
        account={account}
        transactions={transactions}
        balances={balances}
        showBalances={showBalances}
        filtered={transactionsFiltered}
      >
        {(allTransactions, allBalances) => (
          <SelectedProviderWithItems
            name="transactions"
            // When reconciled transactions are hidden they are still
            // loaded (e.g. to calculate running balances), but they must
            // not be selectable. Mirror the filtering the transaction
            // table applies when rendering so that range selection
            // (shift+click) only covers visible transactions.
            items={
              showReconciled
                ? allTransactions
                : allTransactions.filter(t => !t.reconciled)
            }
            fetchAllIds={this.fetchAllIds}
            registerDispatch={dispatch => (this.dispatchSelected = dispatch)}
            selectAllFilter={selectAllFilter}
          >
            <View style={styles.page}>
              <AccountHeader
                tableRef={this.table}
                isNameEditable={isNameEditable ?? false}
                workingHard={workingHard ?? false}
                accountId={accountId}
                account={account}
                filterId={filterId}
                savedFilters={this.props.savedFilters}
                accountName={accountName}
                accountsSyncing={accountsSyncing}
                accounts={accounts}
                transactions={transactions}
                showExtraBalances={showExtraBalances ?? false}
                showReconciled={showReconciled ?? false}
                showEmptyMessage={showEmptyMessage ?? false}
                balanceQuery={balanceQuery}
                filteredAmount={filteredAmount}
                isFiltered={transactionsFiltered ?? false}
                isSorted={this.state.sort !== null}
                reconcileAmount={reconcileAmount}
                search={this.state.search}
                // @ts-expect-error fix me
                filterConditions={this.state.filterConditions}
                filterConditionsOp={this.state.filterConditionsOp}
                onSearch={this.onSearch}
                onShowTransactions={this.onShowTransactions}
                onMenuSelect={this.onMenuSelect}
                onAddTransaction={this.onAddTransaction}
                onToggleExtraBalances={this.onToggleExtraBalances}
                onSaveName={this.onSaveName}
                saveNameError={this.state.nameError}
                onReconcile={this.onReconcile}
                onDoneReconciling={this.onDoneReconciling}
                onCreateReconciliationTransaction={
                  this.onCreateReconciliationTransaction
                }
                onSync={this.onSync}
                onImport={this.onImport}
                onBatchDelete={this.onBatchDelete}
                onBatchDuplicate={this.onBatchDuplicate}
                onRunRules={this.onRunRules}
                onBatchEdit={this.onBatchEdit}
                onBatchLinkSchedule={this.onBatchLinkSchedule}
                onBatchUnlinkSchedule={this.onBatchUnlinkSchedule}
                onCreateRule={this.onCreateRule}
                onUpdateFilter={this.onUpdateFilter}
                onClearFilters={this.onClearFilters}
                onReloadSavedFilter={this.onReloadSavedFilter}
                onConditionsOpChange={this.onConditionsOpChange}
                onDeleteFilter={this.onDeleteFilter}
                onApplyFilter={this.onApplyFilter}
                onScheduleAction={this.onScheduleAction}
                onSetTransfer={this.onSetTransfer}
                onMakeAsSplitTransaction={this.onMakeAsSplitTransaction}
                onMakeAsNonSplitTransactions={this.onMakeAsNonSplitTransactions}
                onMergeTransactions={this.onMergeTransactions}
              />

              <View style={{ flex: 1 }}>
                <TransactionList
                  headerContent={undefined}
                  // @ts-expect-error - fix me
                  tableRef={this.table}
                  account={account}
                  transactions={transactions}
                  allTransactions={allTransactions}
                  loadMoreTransactions={() =>
                    this.paged && this.paged.fetchNext()
                  }
                  accounts={accounts}
                  category={category}
                  categoryGroups={categoryGroups}
                  payees={payees}
                  balances={allBalances}
                  showBalances={!!allBalances}
                  showReconciled={showReconciled}
                  showCleared={!!showCleared}
                  showGroup={this.props.showGroup}
                  showAccount={this.showAccountColumn()}
                  columnOrder={this.props.columnOrder}
                  allowReorder={
                    !!accountId &&
                    accountId !== 'offbudget' &&
                    accountId !== 'onbudget' &&
                    accountId !== 'uncategorized'
                  }
                  isAdding={this.state.isAdding}
                  isNew={this.isNew}
                  isMatched={this.isMatched}
                  isFiltered={transactionsFiltered}
                  dateFormat={dateFormat}
                  hideFraction={hideFraction}
                  renderEmpty={() =>
                    showEmptyMessage ? (
                      <AccountEmptyMessage
                        onAdd={() =>
                          this.props.dispatch(
                            replaceModal({
                              modal: { name: 'add-account', options: {} },
                            }),
                          )
                        }
                      />
                    ) : !loading ? (
                      <View
                        style={{
                          color: theme.tableText,
                          marginTop: 20,
                          textAlign: 'center',
                          fontStyle: 'italic',
                        }}
                      >
                        <Trans>No transactions</Trans>
                      </View>
                    ) : null
                  }
                  onSort={this.onSort}
                  sortField={this.state.sort?.field ?? ''}
                  ascDesc={this.state.sort?.ascDesc ?? 'asc'}
                  onChange={this.onTransactionsChange}
                  onBatchDelete={this.onBatchDelete}
                  onBatchDuplicate={this.onBatchDuplicate}
                  onBatchLinkSchedule={this.onBatchLinkSchedule}
                  onBatchUnlinkSchedule={this.onBatchUnlinkSchedule}
                  onCreateRule={this.onCreateRule}
                  onScheduleAction={this.onScheduleAction}
                  onMakeAsNonSplitTransactions={
                    this.onMakeAsNonSplitTransactions
                  }
                  onRefetch={this.refetchTransactions}
                  focusedTransactionId={this.state.focusedTransactionId}
                  onFocusedTransactionShown={this.onFocusedTransactionShown}
                  onCloseAddTransaction={() =>
                    this.setState({ isAdding: false })
                  }
                  onCreatePayee={this.onCreatePayee}
                  onApplyFilter={this.onApplyFilter}
                />
              </View>
            </View>
          </SelectedProviderWithItems>
        )}
      </AllTransactions>
    );
  }
}

type AccountHackProps = Omit<
  AccountInternalProps,
  | 'dispatch'
  | 'splitsExpandedDispatch'
  | 'onBatchEdit'
  | 'onBatchDuplicate'
  | 'onBatchLinkSchedule'
  | 'onBatchUnlinkSchedule'
  | 'onBatchDelete'
  | 'onSetTransfer'
>;

function AccountHack(props: AccountHackProps) {
  const { dispatch: splitsExpandedDispatch } = useSplitsExpanded();
  const dispatch = useDispatch();
  const {
    onBatchEdit,
    onBatchDuplicate,
    onBatchLinkSchedule,
    onBatchUnlinkSchedule,
    onBatchDelete,
    onSetTransfer,
  } = useTransactionBatchActions();

  return (
    <AccountInternal
      dispatch={dispatch}
      splitsExpandedDispatch={splitsExpandedDispatch}
      onBatchEdit={onBatchEdit}
      onBatchDuplicate={onBatchDuplicate}
      onBatchLinkSchedule={onBatchLinkSchedule}
      onBatchUnlinkSchedule={onBatchUnlinkSchedule}
      onBatchDelete={onBatchDelete}
      onSetTransfer={onSetTransfer}
      {...props}
    />
  );
}

export function Account() {
  const params = useParams();
  const location = useLocation();

  const { data: { grouped: categoryGroups } = { grouped: [] } } =
    useCategories();
  const newTransactions = useSelector(
    state => state.transactions.newTransactions,
  );
  const matchedTransactions = useSelector(
    state => state.transactions.matchedTransactions,
  );
  const { data: accounts = [] } = useAccounts();
  const { data: payees = [] } = usePayees();
  const dateFormat = useDateFormat() || 'MM/dd/yyyy';
  const currencySymbol = useFormat().currency.symbol;
  const [hideFraction] = useSyncedPref('hideFraction');
  const [expandSplits] = useLocalPref('expand-splits');
  const [showNetWorthChart, setShowNetWorthChart] = useSyncedPref(
    `show-account-${params.id}-net-worth-chart`,
  );
  const [hideReconciled, setHideReconciled] = useSyncedPref(
    `hide-reconciled-${params.id || 'all'}`, // "All accounts" view provides no ID
  );
  const [showExtraBalances, setShowExtraBalances] = useSyncedPref(
    `show-extra-balances-${params.id || 'all-accounts'}`,
  );
  const {
    transactionColumns,
    columnOrder,
    showBalances,
    showCleared,
    showGroup,
    saveColumns,
  } = useTransactionTableColumns(params.id);

  const modalShowing = useSelector(state => state.modals.modalStack.length > 0);
  const accountsSyncing = useSelector(state => state.account.accountsSyncing);
  const filterConditions = location?.state?.filterConditions || [];

  const savedFiters = useTransactionFilters();

  const schedulesQuery = useMemo(
    () => getSchedulesQuery(params.id),
    [params.id],
  );

  const { mutate: reopenAccount } = useReopenAccountMutation();
  const onReopenAccount = (id: AccountEntity['id']) => reopenAccount({ id });

  const { mutate: updateAccount } = useUpdateAccountMutation();
  const onUpdateAccount = (account: AccountEntity) =>
    updateAccount({ account });

  const { mutate: unlinkAccount } = useUnlinkAccountMutation();
  const onUnlinkAccount = (id: AccountEntity['id']) => unlinkAccount({ id });

  const { mutate: syncAndDownload } = useSyncAndDownloadMutation();
  const onSyncAndDownload = (id?: AccountEntity['id']) =>
    syncAndDownload({ id });

  const createPayee = useCreatePayeeMutation();
  const onCreatePayee = (name: PayeeEntity['name']) =>
    createPayee.mutateAsync({ name });

  return (
    <ErrorBoundary FallbackComponent={FeatureErrorFallback}>
      <SchedulesProvider query={schedulesQuery}>
        <SplitsExpandedProvider
          initialMode={expandSplits ? 'collapse' : 'expand'}
        >
          <AccountHack
            newTransactions={newTransactions}
            matchedTransactions={matchedTransactions}
            accounts={accounts}
            dateFormat={dateFormat}
            currencySymbol={currencySymbol}
            hideFraction={String(hideFraction) === 'true'}
            expandSplits={expandSplits}
            showBalances={showBalances}
            showNetWorthChart={String(showNetWorthChart) === 'true'}
            setShowNetWorthChart={val => setShowNetWorthChart(String(val))}
            showCleared={showCleared}
            showReconciled={String(hideReconciled) !== 'true'}
            setShowReconciled={val => setHideReconciled(String(!val))}
            showGroup={showGroup}
            showExtraBalances={String(showExtraBalances) === 'true'}
            setShowExtraBalances={extraBalances =>
              setShowExtraBalances(String(extraBalances))
            }
            transactionColumns={transactionColumns}
            columnOrder={columnOrder}
            saveColumns={saveColumns}
            payees={payees}
            modalShowing={modalShowing}
            accountsSyncing={accountsSyncing}
            filterConditions={filterConditions}
            categoryGroups={categoryGroups}
            accountId={params.id}
            categoryId={location?.state?.categoryId}
            location={location}
            savedFilters={savedFiters}
            onReopenAccount={onReopenAccount}
            onUpdateAccount={onUpdateAccount}
            onUnlinkAccount={onUnlinkAccount}
            onSyncAndDownload={onSyncAndDownload}
            onCreatePayee={onCreatePayee}
          />
        </SplitsExpandedProvider>
      </SchedulesProvider>
    </ErrorBoundary>
  );
}