import { useEffect, useMemo, useState } from 'react' import { getAccount, listTransactions, type Account, type Transaction, } from '../db' import { balanceOf, formatCents, formatDate, signedAmount, } from '../format' import { back, navigate } from '../router' import { BackButton, Header, Screen } from '../components/Screen' export function LedgerPage(props: { accountId: string reloadKey: number }) { const [account, setAccount] = useState(undefined) const [txns, setTxns] = useState(null) useEffect(() => { let alive = true Promise.all([ getAccount(props.accountId), listTransactions(props.accountId), ]).then(([a, t]) => { if (!alive) return setAccount(a ?? null) setTxns(t) }) return () => { alive = false } }, [props.accountId, props.reloadKey]) const balance = useMemo(() => (txns ? balanceOf(txns) : 0), [txns]) if (account === null) { return ( back('/')} />} /> } >
Account not found
It may have been deleted.
) } return ( back('/')} />} /> } footer={ } >
Balance 0 ? ' credit' : balance < 0 ? ' debit' : '') } > {formatCents(balance)}
Activity
{txns === null ? (
Loading…
) : txns.length === 0 ? (
No transactions yet
Add your first credit or debit to see it here.
) : (
{txns.map((t) => { const signed = signedAmount(t) return (
{t.note || (t.type === 'credit' ? 'Credit' : 'Debit')}
{formatDate(t.createdAt)}
{formatCents(signed, { signed: true })}
) })}
)}
) }