Skip to content

Commit

Permalink
♻️ (typescript) migrate reconcile file to TS (#3355)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatissJanis authored Sep 4, 2024
1 parent 0a34ede commit bc04a8c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React, { useState } from 'react';
import { Trans } from 'react-i18next';

import * as queries from 'loot-core/src/client/queries';
import { type Query } from 'loot-core/src/shared/query';
import { currencyToInteger } from 'loot-core/src/shared/util';
import { type AccountEntity } from 'loot-core/types/models';

import { SvgCheckCircle1 } from '../../icons/v2';
import { styles, theme } from '../../style';
Expand All @@ -14,20 +16,32 @@ import { View } from '../common/View';
import { useFormat } from '../spreadsheet/useFormat';
import { useSheetValue } from '../spreadsheet/useSheetValue';

type ReconcilingMessageProps = {
balanceQuery: { name: `balance-query-${string}`; query: Query };
targetBalance: number;
onDone: () => void;
onCreateTransaction: (targetDiff: number) => void;
};

export function ReconcilingMessage({
balanceQuery,
targetBalance,
onDone,
onCreateTransaction,
}) {
const cleared = useSheetValue({
name: balanceQuery.name + '-cleared',
}: ReconcilingMessageProps) {
const cleared = useSheetValue<'balance', `balance-query-${string}-cleared`>({
name: (balanceQuery.name + '-cleared') as `balance-query-${string}-cleared`,
value: 0,
query: balanceQuery.query.filter({ cleared: true }),
});
const format = useFormat();
const targetDiff = targetBalance - cleared;

const clearedBalance = format(cleared, 'financial');
const bankBalance = format(targetBalance, 'financial');
const difference =
(targetDiff > 0 ? '+' : '') + format(targetDiff, 'financial');

return (
<View
style={{
Expand Down Expand Up @@ -66,23 +80,10 @@ export function ReconcilingMessage({
<View style={{ color: theme.tableText }}>
<Text style={{ fontStyle: 'italic', textAlign: 'center' }}>
<Trans>
Your cleared balance{' '}
<strong>
{{ clearedBalance: format(cleared, 'financial') }}
</strong>{' '}
needs{' '}
<strong>
{{
difference:
(targetDiff > 0 ? '+' : '') +
format(targetDiff, 'financial'),
}}
</strong>{' '}
to match
Your cleared balance <strong>{clearedBalance}</strong> needs{' '}
<strong>{difference}</strong> to match
<br /> your bank&apos;s balance of{' '}
<Text style={{ fontWeight: 700 }}>
{{ bankBalance: format(targetBalance, 'financial') }}
</Text>
<Text style={{ fontWeight: 700 }}>{bankBalance}</Text>
</Trans>
</Text>
</View>
Expand All @@ -104,15 +105,25 @@ export function ReconcilingMessage({
);
}

export function ReconcileMenu({ account, onReconcile, onClose }) {
type ReconcileMenuProps = {
account: AccountEntity;
onReconcile: (amount: number | null) => void;
onClose: () => void;
};

export function ReconcileMenu({
account,
onReconcile,
onClose,
}: ReconcileMenuProps) {
const balanceQuery = queries.accountBalance(account);
const clearedBalance = useSheetValue({
name: balanceQuery.name + '-cleared',
const clearedBalance = useSheetValue<'account', `balance-${string}-cleared`>({
name: (balanceQuery.name + '-cleared') as `balance-${string}-cleared`,
value: null,
query: balanceQuery.query.filter({ cleared: true }),
});
const format = useFormat();
const [inputValue, setInputValue] = useState(null);
const [inputValue, setInputValue] = useState<string | null>(null);
const [inputFocused, setInputFocused] = useState(false);

function onSubmit() {
Expand Down
9 changes: 9 additions & 0 deletions packages/desktop-client/src/components/spreadsheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Spreadsheets = {

// Account fields
balance: number;
[key: `balance-${string}-cleared`]: number | null;
'accounts-balance': number;
'budgeted-accounts-balance': number;
'offbudget-accounts-balance': number;
Expand Down Expand Up @@ -62,6 +63,14 @@ export type Spreadsheets = {
goal: number;
'long-goal': number;
};
[`balance`]: {
// Common fields
'uncategorized-amount': number;
'uncategorized-balance': number;

// Balance fields
[key: `balance-query-${string}-cleared`]: number;
};
};

export type SheetNames = keyof Spreadsheets & string;
Expand Down
43 changes: 14 additions & 29 deletions packages/loot-core/src/client/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,73 +115,61 @@ export function makeTransactionSearchQuery(
});
}

export function accountBalance(
acct: AccountEntity,
): Binding<'account', 'balance'> {
export function accountBalance(acct: AccountEntity) {
return {
name: accountParametrizedField('balance')(acct.id),
query: q('transactions')
.filter({ account: acct.id })
.options({ splits: 'none' })
.calculate({ $sum: '$amount' }),
};
} satisfies Binding<'account', 'balance'>;
}

export function accountBalanceCleared(
acct: AccountEntity,
): Binding<'account', 'balanceCleared'> {
export function accountBalanceCleared(acct: AccountEntity) {
return {
name: accountParametrizedField('balanceCleared')(acct.id),
query: q('transactions')
.filter({ account: acct.id, cleared: true })
.options({ splits: 'none' })
.calculate({ $sum: '$amount' }),
};
} satisfies Binding<'account', 'balanceCleared'>;
}

export function accountBalanceUncleared(
acct: AccountEntity,
): Binding<'account', 'balanceUncleared'> {
export function accountBalanceUncleared(acct: AccountEntity) {
return {
name: accountParametrizedField('balanceUncleared')(acct.id),
query: q('transactions')
.filter({ account: acct.id, cleared: false })
.options({ splits: 'none' })
.calculate({ $sum: '$amount' }),
};
} satisfies Binding<'account', 'balanceUncleared'>;
}

export function allAccountBalance(): Binding<'account', 'accounts-balance'> {
export function allAccountBalance() {
return {
query: q('transactions')
.filter({ 'account.closed': false })
.calculate({ $sum: '$amount' }),
name: 'accounts-balance',
};
} satisfies Binding<'account', 'accounts-balance'>;
}

export function budgetedAccountBalance(): Binding<
'account',
'budgeted-accounts-balance'
> {
export function budgetedAccountBalance() {
return {
name: `budgeted-accounts-balance`,
query: q('transactions')
.filter({ 'account.offbudget': false, 'account.closed': false })
.calculate({ $sum: '$amount' }),
};
} satisfies Binding<'account', 'budgeted-accounts-balance'>;
}

export function offbudgetAccountBalance(): Binding<
'account',
'offbudget-accounts-balance'
> {
export function offbudgetAccountBalance() {
return {
name: `offbudget-accounts-balance`,
query: q('transactions')
.filter({ 'account.offbudget': true, 'account.closed': false })
.calculate({ $sum: '$amount' }),
};
} satisfies Binding<'account', 'offbudget-accounts-balance'>;
}

export function categoryBalance(category: CategoryEntity, month: string) {
Expand Down Expand Up @@ -249,14 +237,11 @@ export function uncategorizedBalance() {
};
}

export function uncategorizedCount<SheetName extends SheetNames>(): Binding<
SheetName,
'uncategorized-amount'
> {
export function uncategorizedCount<SheetName extends SheetNames>() {
return {
name: 'uncategorized-amount',
query: uncategorizedQuery.calculate({ $count: '$id' }),
};
} satisfies Binding<SheetName, 'uncategorized-amount'>;
}

export const rolloverBudget = {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3355.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MatissJanis]
---

TypeScript: migrate Reconcile file.

0 comments on commit bc04a8c

Please sign in to comment.