A local-first personal finance app
♻️ (typescript) Refactor Accounts/Balances to tsx and Remove ts-strict-ignore from Accounts/Account (#4047)
Travis Lesickace0ca60bcf5b
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsxindex 4ef75e8289..601834aa35 100644--- a/packages/desktop-client/src/components/accounts/Account.tsx+++ b/packages/desktop-client/src/components/accounts/Account.tsx@@ -1,4 +1,3 @@-// @ts-strict-ignoreimport React, {PureComponent,type MutableRefObject,@@ -332,8 +331,8 @@ class AccountInternal extends PureComponent<AccountInternalState> {paged: PagedQuery<TransactionEntity> | null;- rootQuery: Query;- currentQuery: Query;+ rootQuery!: Query;+ currentQuery!: Query;table: TableRef;unlisten?: () => void;dispatchSelected?: (action: Actions) => void;@@ -383,7 +382,7 @@ class AccountInternal extends PureComponent<// first message referencing a non-deleted row so that we can// highlight the row//- let focusId: null | string;+ let focusId: null | string = null;if (messages.every(msg => msg.dataset === 'transactions') &&!messages.find(msg => msg.column === 'tombstone')@@ -536,7 +535,7 @@ class AccountInternal extends PureComponent<this.setState({transactions: data,- transactionCount: this.paged?.totalCount,+ transactionCount: this.paged?.totalCount ?? 0,transactionsFiltered: isFiltered,loading: false,workingHard: false,@@ -678,14 +677,17 @@ class AccountInternal extends PureComponent<const account = this.props.accounts.find(account => account.id === accountId,);- return (- account &&- this.state.search === '' &&- this.state.filterConditions.length === 0 &&- (this.state.sort === null ||- (this.state.sort.field === 'date' &&- this.state.sort.ascDesc === 'desc'))- );++ 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() {@@ -713,7 +715,7 @@ class AccountInternal extends PureComponent<onSaveName = (name: string) => {const accountNameError = validateAccountName(name,- this.props.accountId,+ this.props.accountId ?? '',this.props.accounts,);if (accountNameError) {@@ -722,7 +724,7 @@ class AccountInternal extends PureComponent<const account = this.props.accounts.find(account => account.id === this.props.accountId,);- this.props.updateAccount({ ...account, name });+ this.props.updateAccount({ ...account, name } as AccountEntity);this.setState({ editingName: false, nameError: '' });}};@@ -920,9 +922,9 @@ class AccountInternal extends PureComponent<await this.refetchTransactions();};- onReconcile = async (balance: number) => {+ onReconcile = async (amount: number | null) => {this.setState(({ showCleared }) => ({- reconcileAmount: balance,+ reconcileAmount: amount,showCleared: true,prevShowCleared: showCleared,}));@@ -1301,14 +1303,16 @@ class AccountInternal extends PureComponent<onConditionsOpChange = (value: 'and' | 'or') => {this.setState({ filterConditionsOp: value });- this.setState({ filterId: { ...this.state.filterId, status: 'changed' } });+ this.setState({+ filterId: { ...this.state.filterId, status: 'changed' } as SavedFilter,+ });this.applyFilters([...this.state.filterConditions]);if (this.state.search !== '') {this.onSearch(this.state.search);}};- onReloadSavedFilter = (savedFilter: SavedFilter, item: string) => {+ onReloadSavedFilter = (savedFilter: SavedFilter, item?: string) => {if (item === 'reload') {const [savedFilter] = this.props.savedFilters.filter(f => f.id === this.state.filterId?.id,@@ -1320,7 +1324,7 @@ class AccountInternal extends PureComponent<this.setState({filterConditionsOp: savedFilter.conditionsOp ?? 'and',});- this.applyFilters([...savedFilter.conditions]);+ this.applyFilters([...(savedFilter.conditions ?? [])]);}}this.setState({ filterId: { ...this.state.filterId, ...savedFilter } });@@ -1348,7 +1352,7 @@ class AccountInternal extends PureComponent<filterId: {...this.state.filterId,status: this.state.filterId && 'changed',- },+ } as SavedFilter,});if (this.state.search !== '') {this.onSearch(this.state.search);@@ -1365,7 +1369,7 @@ class AccountInternal extends PureComponent<filterId: {...this.state.filterId,status: this.state.filterId && 'changed',- },+ } as SavedFilter,});}if (this.state.search !== '') {@@ -1402,7 +1406,7 @@ class AccountInternal extends PureComponent<filterId: {...this.state.filterId,status: this.state.filterId && 'changed',- },+ } as SavedFilter,});this.applyFilters([...filterConditions, condition]);}@@ -1660,11 +1664,11 @@ class AccountInternal extends PureComponent<const showEmptyMessage = !loading && !accountId && accounts.length === 0;- const isNameEditable =- accountId &&- accountId !== 'onbudget' &&- accountId !== 'offbudget' &&- accountId !== 'uncategorized';+ const isNameEditable = accountId+ ? accountId !== 'onbudget' &&+ accountId !== 'offbudget' &&+ accountId !== 'uncategorized'+ : false;const balanceQuery = this.getBalanceQuery(accountId);@@ -1687,9 +1691,9 @@ class AccountInternal extends PureComponent<<View style={styles.page}><AccountHeadertableRef={this.table}- editingName={editingName}- isNameEditable={isNameEditable}- workingHard={workingHard}+ editingName={editingName ?? false}+ isNameEditable={isNameEditable ?? false}+ workingHard={workingHard ?? false}account={account}filterId={filterId}savedFilters={this.props.savedFilters}@@ -1698,15 +1702,15 @@ class AccountInternal extends PureComponent<failedAccounts={failedAccounts}accounts={accounts}transactions={transactions}- showBalances={showBalances}- showExtraBalances={showExtraBalances}- showCleared={showCleared}- showReconciled={showReconciled}- showEmptyMessage={showEmptyMessage}+ showBalances={showBalances ?? false}+ showExtraBalances={showExtraBalances ?? false}+ showCleared={showCleared ?? false}+ showReconciled={showReconciled ?? false}+ showEmptyMessage={showEmptyMessage ?? false}balanceQuery={balanceQuery}- canCalculateBalance={this.canCalculateBalance}+ canCalculateBalance={this?.canCalculateBalance ?? undefined}filteredAmount={filteredAmount}- isFiltered={transactionsFiltered}+ isFiltered={transactionsFiltered ?? false}isSorted={this.state.sort !== null}reconcileAmount={reconcileAmount}search={this.state.search}diff --git a/packages/desktop-client/src/components/accounts/Balance.jsx b/packages/desktop-client/src/components/accounts/Balance.tsxsimilarity index 69%rename from packages/desktop-client/src/components/accounts/Balance.jsxrename to packages/desktop-client/src/components/accounts/Balance.tsxindex 741a1fa96a..6c13620038 100644--- a/packages/desktop-client/src/components/accounts/Balance.jsx+++ b/packages/desktop-client/src/components/accounts/Balance.tsx@@ -5,8 +5,9 @@ import { useHover } from 'usehooks-ts';import { isPreviewId } from 'loot-core/shared/transactions';import { useCachedSchedules } from 'loot-core/src/client/data-hooks/schedules';-import { q } from 'loot-core/src/shared/query';+import { q, type Query } from 'loot-core/src/shared/query';import { getScheduledAmount } from 'loot-core/src/shared/schedules';+import { type AccountEntity } from 'loot-core/types/models';import { useSelectedItems } from '../../hooks/useSelected';import { SvgArrowButtonRight1 } from '../../icons/v2';@@ -15,11 +16,22 @@ import { Button } from '../common/Button2';import { Text } from '../common/Text';import { View } from '../common/View';import { PrivacyFilter } from '../PrivacyFilter';+import { type Binding } from '../spreadsheet';import { CellValue, CellValueText } from '../spreadsheet/CellValue';import { useFormat } from '../spreadsheet/useFormat';import { useSheetValue } from '../spreadsheet/useSheetValue';-function DetailedBalance({ name, balance, isExactBalance = true }) {+type DetailedBalanceProps = {+ name: string;+ balance: number;+ isExactBalance?: boolean;+};++function DetailedBalance({+ name,+ balance,+ isExactBalance = true,+}: DetailedBalanceProps) {const format = useFormat();return (<Text@@ -42,13 +54,18 @@ function DetailedBalance({ name, balance, isExactBalance = true }) {);}-function SelectedBalance({ selectedItems, account }) {+type SelectedBalanceProps = {+ selectedItems: Set<string>;+ account?: AccountEntity;+};++function SelectedBalance({ selectedItems, account }: SelectedBalanceProps) {const { t } = useTranslation();const name = `selected-balance-${[...selectedItems].join('-')}`;- const rows = useSheetValue({- name,+ const rows = useSheetValue<'balance', `selected-transactions-${string}`>({+ name: name as `selected-transactions-${string}`,query: q('transactions').filter({id: { $oneof: [...selectedItems] },@@ -56,18 +73,18 @@ function SelectedBalance({ selectedItems, account }) {}).select('id'),});- const ids = new Set((rows || []).map(r => r.id));+ const ids = new Set((rows || []).map((r: { id: string }) => r.id));const finalIds = [...selectedItems].filter(id => !ids.has(id));- let balance = useSheetValue({- name: name + '-sum',+ let balance = useSheetValue<'balance', `selected-balance-${string}`>({+ name: (name + '-sum') as `selected-balance-${string}`,query: q('transactions').filter({ id: { $oneof: finalIds } }).options({ splits: 'all' }).calculate({ $sum: '$amount' }),});- let scheduleBalance = null;+ let scheduleBalance = 0;const { isLoading, schedules = [] } = useCachedSchedules();@@ -95,14 +112,10 @@ function SelectedBalance({ selectedItems, account }) {}}- if (balance == null) {- if (scheduleBalance == null) {- return null;- } else {- balance = scheduleBalance;- }- } else if (scheduleBalance != null) {- balance += scheduleBalance;+ if (!balance && !scheduleBalance) {+ return null;+ } else {+ balance = (balance ?? 0) + scheduleBalance;}return (@@ -114,38 +127,59 @@ function SelectedBalance({ selectedItems, account }) {);}-function FilteredBalance({ filteredAmount }) {+type FilteredBalanceProps = {+ filteredAmount?: number | null;+};++function FilteredBalance({ filteredAmount }: FilteredBalanceProps) {const { t } = useTranslation();return (<DetailedBalancename={t('Filtered balance:')}- balance={filteredAmount || 0}+ balance={filteredAmount ?? 0}isExactBalance={true}/>);}-function MoreBalances({ balanceQuery }) {+type MoreBalancesProps = {+ balanceQuery: { name: `balance-query-${string}`; query: Query };+};++function MoreBalances({ balanceQuery }: MoreBalancesProps) {const { t } = useTranslation();- const cleared = useSheetValue({- name: balanceQuery.name + '-cleared',+ const cleared = useSheetValue<'balance', `balance-query-${string}-cleared`>({+ name: (balanceQuery.name + '-cleared') as `balance-query-${string}-cleared`,query: balanceQuery.query.filter({ cleared: true }),});- const uncleared = useSheetValue({- name: balanceQuery.name + '-uncleared',+ const uncleared = useSheetValue<+ 'balance',+ `balance-query-${string}-uncleared`+ >({+ name: (balanceQuery.name ++ '-uncleared') as `balance-query-${string}-uncleared`,query: balanceQuery.query.filter({ cleared: false }),});return (<View style={{ flexDirection: 'row' }}>- <DetailedBalance name={t('Cleared total:')} balance={cleared} />- <DetailedBalance name={t('Uncleared total:')} balance={uncleared} />+ <DetailedBalance name={t('Cleared total:')} balance={cleared ?? 0} />+ <DetailedBalance name={t('Uncleared total:')} balance={uncleared ?? 0} /></View>);}+type BalancesProps = {+ balanceQuery: { name: `balance-query-${string}`; query: Query };+ showExtraBalances: boolean;+ onToggleExtraBalances: () => void;+ account?: AccountEntity;+ isFiltered: boolean;+ filteredAmount?: number | null;+};+export function Balances({balanceQuery,showExtraBalances,@@ -153,7 +187,7 @@ export function Balances({account,isFiltered,filteredAmount,-}) {+}: BalancesProps) {const selectedItems = useSelectedItems();const buttonRef = useRef(null);const isButtonHovered = useHover(buttonRef);@@ -177,7 +211,15 @@ export function Balances({paddingBottom: 1,}}>- <CellValue binding={{ ...balanceQuery, value: 0 }} type="financial">+ <CellValue+ binding={+ { ...balanceQuery, value: 0 } as Binding<+ 'balance',+ `balance-query-${string}`+ >+ }+ type="financial"+ >{props => (<CellValueText{...props}diff --git a/packages/desktop-client/src/components/accounts/Header.tsx b/packages/desktop-client/src/components/accounts/Header.tsxindex 4a66ebc45d..1866534b56 100644--- a/packages/desktop-client/src/components/accounts/Header.tsx+++ b/packages/desktop-client/src/components/accounts/Header.tsx@@ -54,7 +54,7 @@ type AccountHeaderProps = {isNameEditable: boolean;workingHard: boolean;accountName: string;- account: AccountEntity;+ account?: AccountEntity;filterId?: SavedFilter;savedFilters: TransactionFilterEntity[];accountsSyncing: string[];@@ -67,10 +67,10 @@ type AccountHeaderProps = {showReconciled: boolean;showEmptyMessage: boolean;balanceQuery: ComponentProps<typeof ReconcilingMessage>['balanceQuery'];- reconcileAmount: number;- canCalculateBalance: () => boolean;+ reconcileAmount?: number | null;+ canCalculateBalance?: () => boolean;isFiltered: boolean;- filteredAmount: number;+ filteredAmount?: number | null;isSorted: boolean;search: string;filterConditions: RuleConditionEntity[];@@ -429,7 +429,7 @@ export function AccountHeader({{account ? (<View style={{ flex: '0 0 auto' }}><MenuButton- aria-label="Account menu"+ aria-label={t('Account menu')}ref={triggerRef}onPress={() => setMenuOpen(true)}/>@@ -443,7 +443,9 @@ export function AccountHeader({<AccountMenuaccount={account}canSync={canSync}- canShowBalances={canCalculateBalance()}+ canShowBalances={+ canCalculateBalance ? canCalculateBalance() : false+ }isSorted={isSorted}showBalances={showBalances}showCleared={showCleared}@@ -458,7 +460,7 @@ export function AccountHeader({) : (<View style={{ flex: '0 0 auto' }}><MenuButton- aria-label="Account menu"+ aria-label={t('Account menu')}ref={triggerRef}onPress={() => setMenuOpen(true)}/>@@ -551,7 +553,7 @@ function AccountSyncSidebar({}type AccountNameFieldProps = {- account: AccountEntity;+ account?: AccountEntity;accountName: string;isNameEditable: boolean;editingName: boolean;diff --git a/packages/desktop-client/src/components/spreadsheet/index.ts b/packages/desktop-client/src/components/spreadsheet/index.tsindex e2999c7dc9..a95f80a37c 100644--- a/packages/desktop-client/src/components/spreadsheet/index.ts+++ b/packages/desktop-client/src/components/spreadsheet/index.ts@@ -78,7 +78,9 @@ export type Spreadsheets = {'uncategorized-balance': number;// Balance fields- [key: `balance-query-${string}-cleared`]: number;+ [key: `balance-query-${string}`]: number;+ [key: `selected-transactions-${string}`]: Array<{ id: string }>;+ [key: `selected-balance-${string}`]: number;};};diff --git a/upcoming-release-notes/4047.md b/upcoming-release-notes/4047.mdnew file mode 100644index 0000000000..4321452c07--- /dev/null+++ b/upcoming-release-notes/4047.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [tlesicka]+---++TypeScript: Refactor Accounts/Balances to tsx and Remove ts-strict-ignore from Accounts/Account