Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "last reconciled" timestamp to accounts #4459

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/desktop-client/src/components/accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,13 @@ class AccountInternal extends PureComponent<

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;

const { data } = await runQuery(
Expand All @@ -1034,6 +1041,13 @@ class AccountInternal extends PureComponent<
await this.lockTransactions();
}

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

this.setState({
reconcileAmount: null,
showCleared: this.state.prevShowCleared,
Expand Down
49 changes: 47 additions & 2 deletions packages/desktop-client/src/components/accounts/Balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useTranslation } from 'react-i18next';
import { Button } from '@actual-app/components/button';
import { Text } from '@actual-app/components/text';
import { View } from '@actual-app/components/view';
import { formatDistanceToNow } from 'date-fns';
import * as locales from 'date-fns/locale';
import { useHover } from 'usehooks-ts';

import { useCachedSchedules } from 'loot-core/client/data-hooks/schedules';
Expand All @@ -12,7 +14,9 @@ import { getScheduledAmount } from 'loot-core/shared/schedules';
import { isPreviewId } from 'loot-core/shared/transactions';
import { type AccountEntity } from 'loot-core/types/models';

import { useGlobalPref } from '../../hooks/useGlobalPref';
import { useSelectedItems } from '../../hooks/useSelected';
import { SvgLockClosed } from '../../icons/v1';
import { SvgArrowButtonRight1 } from '../../icons/v2';
import { theme } from '../../style';
import { PrivacyFilter } from '../PrivacyFilter';
Expand Down Expand Up @@ -143,11 +147,23 @@ function FilteredBalance({ filteredAmount }: FilteredBalanceProps) {
);
}

const tsToRelativeTime = (ts: string | null, language: string): string => {
if (!ts) return 'Unknown';

const parsed = new Date(parseInt(ts, 10));
const locale =
locales[language.replace('-', '') as keyof typeof locales] ??
locales['enUS'];

return formatDistanceToNow(parsed, { addSuffix: true, locale });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use formatDistanceToNowStrict which will not include the prefix "about".

};

type MoreBalancesProps = {
balanceQuery: { name: `balance-query-${string}`; query: Query };
lastReconciled?: string | null;
};

function MoreBalances({ balanceQuery }: MoreBalancesProps) {
function MoreBalances({ balanceQuery, lastReconciled }: MoreBalancesProps) {
const { t } = useTranslation();

const cleared = useSheetValue<'balance', `balance-query-${string}-cleared`>({
Expand All @@ -163,10 +179,33 @@ function MoreBalances({ balanceQuery }: MoreBalancesProps) {
query: balanceQuery.query.filter({ cleared: false }),
});

const [language] = useGlobalPref('language');

return (
<View style={{ flexDirection: 'row' }}>
<DetailedBalance name={t('Cleared total:')} balance={cleared ?? 0} />
<DetailedBalance name={t('Uncleared total:')} balance={uncleared ?? 0} />
<Text
style={{
marginLeft: 15,
borderRadius: 4,
padding: '4px 6px',
color: theme.pillText,
backgroundColor: theme.pillBackground,
}}
>
<SvgLockClosed
style={{
width: 11,
height: 11,
color: theme.pillText,
marginRight: 5,
}}
/>
{lastReconciled
? `${t('Reconciled')} ${tsToRelativeTime(lastReconciled, language || 'en-US')}`
: t('Not yet reconciled')}
</Text>
</View>
);
}
Expand Down Expand Up @@ -251,7 +290,13 @@ export function Balances({
}}
/>
</Button>
{showExtraBalances && <MoreBalances balanceQuery={balanceQuery} />}

{showExtraBalances && (
<MoreBalances
balanceQuery={balanceQuery}
lastReconciled={account?.last_reconciled}
/>
)}

{selectedItems.size > 0 && (
<SelectedBalance selectedItems={selectedItems} account={account} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Spreadsheets = {
'offbudget-accounts-balance': number;
balanceCleared: number;
balanceUncleared: number;
lastReconciled: string | null;
};
category: {
// Common fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN TRANSACTION;

ALTER TABLE accounts ADD COLUMN last_reconciled text;

COMMIT;
1 change: 1 addition & 0 deletions packages/loot-core/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function generateAccount(
name,
offbudget: offbudget ? 1 : 0,
sort_order: 0,
last_reconciled: null,
tombstone: 0,
closed: 0,
...emptySyncFields(),
Expand Down
13 changes: 11 additions & 2 deletions packages/loot-core/src/server/accounts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ export type AccountHandlers = {
'account-unlink': typeof unlinkAccount;
};

async function updateAccount({ id, name }: Pick<AccountEntity, 'id' | 'name'>) {
await db.update('accounts', { id, name });
async function updateAccount({
id,
name,
last_reconciled,
}: Pick<AccountEntity, 'id' | 'name'> &
Partial<Pick<AccountEntity, 'last_reconciled'>>) {
await db.update('accounts', {
id,
name,
...(last_reconciled && { last_reconciled }),
});
return {};
}

Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/server/aql/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const schema = {
account_id: f('string'),
official_name: f('string'),
account_sync_source: f('string'),
last_reconciled: f('string'),
last_sync: f('string'),
},
categories: {
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/types/models/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type AccountEntity = {
offbudget: 0 | 1;
closed: 0 | 1;
sort_order: number;
last_reconciled: string | null;
tombstone: 0 | 1;
} & (_SyncFields<true> | _SyncFields<false>);

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4459.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Features
authors: [tostasmistas]
---

Add "last reconciled" timestamp to accounts