Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
71 changes: 71 additions & 0 deletions src/components/MemberMoneyDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useState } from 'react'
import { useAppData } from '../state/AppDataContext'
import { allocateBillShares } from '../lib/shares'
import { dateKeyInTimeZone, formatMoney } from '../lib/utils'
import { Avatar, Badge, Modal } from './ui'
import type { Member } from '../types'

export function MemberMoneyDetails({ member, netCents, onClose }: { member: Member; netCents: number; onClose: () => void }) {
const { data } = useAppData()
const [filter, setFilter] = useState('all')
const month = dateKeyInTimeZone(new Date(), data.household.timezone).slice(0, 7)
const name = (id: string) => data.members.find(person => person.id === id)?.displayName ?? 'Household member'
const rows: { id: string; kind: string; title: string; date: string; detail: string; amount: number; status?: string }[] = []
for (const expense of data.expenses) {
const paid = expense.payers.filter(share => share.memberId === member.id).reduce((sum, share) => sum + share.amountCents, 0)
const used = expense.beneficiaries.filter(share => share.memberId === member.id).reduce((sum, share) => sum + share.amountCents, 0)
if (!paid && !used) continue
rows.push({ id: `expense:${expense.id}`, kind: 'Purchases', title: expense.title, date: expense.purchasedAt,
detail: `Paid ${formatMoney(paid)} · Share ${formatMoney(used)}`, amount: expense.reversed ? 0 : paid - used, status: expense.reversed ? 'Deleted' : undefined })
}
for (const period of data.billPeriods) {
const bill = data.bills.find(item => item.id === period.billId)
if (!bill) continue
const payee = period.payeeMemberId ?? bill.payeeMemberId
const shares = period.shares?.length ? period.shares : allocateBillShares(period.amountCents ?? 0, bill)
const ownShare = shares.find(share => share.memberId === member.id)?.amountCents ?? 0
if (member.id !== payee && !shares.some(share => share.memberId === member.id)) continue
const future = period.periodMonth.slice(0, 7) > month
const pending = period.amountCents == null
const credit = shares.filter(share => share.memberId !== payee).reduce((sum, share) => sum + share.amountCents, 0)
rows.push({ id: `bill:${period.id}`, kind: 'Bills', title: bill.name, date: period.periodMonth,
detail: member.id === payee ? `Paid ${formatMoney(period.amountCents ?? 0)} · Own share ${formatMoney(ownShare)}` : `Share ${formatMoney(ownShare)} · Owed to ${name(payee)}`,
amount: pending || future ? 0 : member.id === payee ? credit : -ownShare, status: pending ? 'Price pending' : future ? 'Upcoming' : undefined })
}
for (const payment of data.settlements) {
if (payment.fromMemberId !== member.id && payment.toMemberId !== member.id) continue
const sent = payment.fromMemberId === member.id
rows.push({ id: `payment:${payment.id}`, kind: 'Payments', title: `${sent ? 'Sent to' : 'Received from'} ${name(sent ? payment.toMemberId : payment.fromMemberId)}`, date: payment.createdAt,
detail: `${formatMoney(payment.amountCents)}${payment.note ? ` · ${payment.note}` : ''}`, amount: payment.status === 'confirmed' ? (sent ? payment.amountCents : -payment.amountCents) : 0, status: payment.status })
}
for (const infraction of data.infractions.filter(item => item.memberId === member.id)) {
rows.push({ id: `penalty:${infraction.id}`, kind: 'Penalties', title: infraction.taskTitle,
date: infraction.resolvedAt ?? infraction.disputeDeadline,
detail: `Penalty ${formatMoney(infraction.amountCents)}`, status: infraction.status,
amount: infraction.status === 'upheld' || infraction.status === 'paid' ? -infraction.amountCents : 0 })
}
for (const payment of data.fundPayments.filter(item => item.memberId === member.id)) {
rows.push({ id: `fund:${payment.id}`, kind: 'Payments', title: 'Paid into penalty account', date: payment.createdAt,
detail: formatMoney(payment.amountCents), status: payment.status, amount: payment.status === 'confirmed' ? payment.amountCents : 0 })
}
const remainder = netCents - rows.reduce((sum, row) => sum + row.amount, 0)
const visible = rows.filter(row => filter === 'all' || row.kind === filter).sort((a, b) => b.date.localeCompare(a.date))
return <Modal open onClose={onClose} title={`${member.displayName}'s balance`} description="All months. Positive amounts add credit; negative amounts add owing.">
<div className="flex items-center gap-4 rounded-xl bg-(--sage-2) p-4">
<Avatar initials={member.initials} color={member.color} imageUrl={member.avatarUrl} size="md" />
<div><span className="block text-xs text-(--muted)">{netCents > 0 ? 'Is owed' : netCents < 0 ? 'Owes' : 'Settled'}</span><strong className="text-3xl tabular-nums">{formatMoney(Math.abs(netCents))}</strong></div>
</div>
<div className="mt-4 flex flex-wrap gap-2" role="group" aria-label="Filter balance history">
{['all', 'Purchases', 'Bills', 'Payments', 'Penalties'].map(option => <button key={option} type="button" aria-pressed={filter === option} onClick={() => setFilter(option)} className={`rounded-full border border-(--line) px-3 py-2 text-xs font-semibold ${filter === option ? 'bg-(--forest) text-white' : 'bg-(--surface) text-(--ink)'}`}>{option === 'all' ? 'Everything' : option}</button>)}
</div>
<div className="mt-3 divide-y divide-(--line)">
{visible.map(row => <div key={row.id} className="grid grid-cols-[minmax(0,1fr)_auto] gap-3 py-4">
<div className="min-w-0"><span className="text-[.68rem] text-(--muted)">{row.kind} · {row.date.slice(0, 10)}</span><strong className="block text-sm">{row.title}</strong><p className="mt-1 break-words text-xs text-(--muted)">{row.detail}</p>{row.status && <div className="mt-2"><Badge>{row.status}</Badge></div>}</div>
<strong className={`text-sm tabular-nums ${row.amount < 0 ? 'text-(--coral)' : 'text-(--forest)'}`}>{formatMoney(row.amount, true)}</strong>
</div>)}
{!visible.length && <p className="py-8 text-center text-sm text-(--muted)">No {filter === 'all' ? 'activity' : filter.toLowerCase()} yet.</p>}
</div>
{remainder !== 0 && <div className="mt-3 flex justify-between gap-4 rounded-xl bg-(--surface) p-3 text-xs"><span>Other ledger adjustments<br /><span className="text-(--muted)">Balance entries outside the activity shown above</span></span><strong className="tabular-nums">{formatMoney(remainder, true)}</strong></div>}
<p className="mt-4 text-xs text-(--muted)">Pending and rejected payments do not change the balance. Upcoming bills count when their month begins.</p>
</Modal>
}
8 changes: 6 additions & 2 deletions src/components/MoneyDashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PurchaseDetails } from './PurchaseDetails'
import { MemberMoneyDetails } from './MemberMoneyDetails'
import { ShareBar, SharePeek } from './ShareBreakdown'
import { billBalanceAdjustments, billBreakdown, expenseBreakdown } from '../lib/shares'
import { useMemo, useState } from 'react'
Expand Down Expand Up @@ -163,6 +164,7 @@ export function MoneyDashboard({
openReceipt,
} = useAppData()
const [detailId, setDetailId] = useState<string | null>(null)
const [memberDetailId, setMemberDetailId] = useState<string | null>(null)
const [peekId, setPeekId] = useState<string | null>(null)
const selectedExpense = data.expenses.find(e => e.id === detailId)
const [activityFilter, setActivityFilter] = useState<'all' | 'expense' | 'settlement'>('all')
Expand All @@ -171,6 +173,7 @@ export function MoneyDashboard({
const [expandedBillId, setExpandedBillId] = useState<string | null>(null)
const currentMemberId = data.household.currentMemberId
const currentMember = data.members.find((member) => member.id === currentMemberId)!
const detailMember = data.members.find(member => member.id === memberDetailId)
const currentBalance = data.balances.find((balance) => balance.memberId === currentMemberId)
const demoBillBalances = demoMode ? billBalanceAdjustments(data.bills, data.billPeriods, data.household.timezone) : new Map<string, number>()
const balanceFor = (memberId: string, netCents: number) => netCents + (demoBillBalances.get(memberId) ?? 0)
Expand Down Expand Up @@ -201,6 +204,7 @@ export function MoneyDashboard({
return (
<div className="grid gap-5.5">
{selectedExpense && <PurchaseDetails key={selectedExpense.id} expense={selectedExpense} onClose={() => setDetailId(null)} />}
{detailMember && <MemberMoneyDetails key={detailMember.id} member={detailMember} netCents={balanceFor(detailMember.id, data.balances.find(balance => balance.memberId === detailMember.id)?.netCents ?? 0)} onClose={() => setMemberDetailId(null)} />}
<header className="flex items-end justify-between gap-8 max-[700px]:flex-col max-[700px]:items-start max-[700px]:gap-4">
<div className="grid gap-1">
<h1 className="page-title">Shared money</h1>
Expand Down Expand Up @@ -232,7 +236,7 @@ export function MoneyDashboard({
{data.balances.map((balance) => {
const member = data.members.find((item) => item.id === balance.memberId)!
return (
<div className="flex min-w-0 items-center gap-3" key={member.id}>
<button type="button" aria-label={`View balance for ${member.displayName}`} onClick={() => setMemberDetailId(member.id)} className="flex min-w-0 items-center gap-3 rounded-xl border-0 bg-transparent p-2 text-left hover:bg-(--sage-2) focus-visible:outline-2 focus-visible:outline-(--forest)" key={member.id}>
<Avatar initials={member.initials} color={member.color} imageUrl={member.avatarUrl} size="md" />
<div className="min-w-0">
<strong className="block truncate text-[.9rem]">{member.displayName}</strong>
Expand All @@ -241,7 +245,7 @@ export function MoneyDashboard({
{formatMoney(balanceFor(member.id, balance.netCents), true)}
</strong>
</div>
</div>
</button>
)
})}
</div>
Expand Down
19 changes: 19 additions & 0 deletions tests/e2e/member-money.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from '@playwright/test'

test('opens own and roommate balance histories with payment filters', async ({ page }) => {
await page.goto('/money')
await page.getByRole('button', { name: 'View balance for Brandon' }).click()
const dialog = page.getByRole('dialog', { name: "Brandon's balance" })
await expect(dialog).toBeVisible()
await expect(dialog.getByText('Monthly rent', { exact: true }).first()).toBeVisible()
await dialog.getByRole('button', { name: 'Payments', exact: true }).click()
await expect(dialog.getByText('Received from Noah')).toBeVisible()
await expect(dialog.getByText('pending', { exact: true })).toBeVisible()
await expect(dialog.getByText('Monthly rent', { exact: true })).toHaveCount(0)
await dialog.getByRole('button', { name: 'Close' }).click()
await page.getByRole('button', { name: 'View balance for Noah' }).click()
const other = page.getByRole('dialog', { name: "Noah's balance" })
await other.getByRole('button', { name: 'Payments', exact: true }).click()
await expect(other.getByText('Sent to Brandon')).toBeVisible()
await expect(other.getByText('pending', { exact: true })).toBeVisible()
})
Loading