Skip to content

Commit

Permalink
Show the impact of an expense on the active user's balance (#139)
Browse files Browse the repository at this point in the history
* Add devcontainer configuration for codespace support

* Show the impact of an expense on the active user's balance

* Run prettier

* Put the balance on a different line

---------

Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
  • Loading branch information
dcbr and scastiel committed Apr 13, 2024
1 parent 1ad4703 commit 1cd2b27
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/app/groups/[groupId]/expenses/active-user-balance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use client'
import { Money } from '@/components/money'
import { getBalances } from '@/lib/balances'
import { useActiveUser } from '@/lib/hooks'

type Props = {
groupId: string
currency: string
expense: Parameters<typeof getBalances>[0][number]
}

export function ActiveUserBalance({ groupId, currency, expense }: Props) {
const activeUserId = useActiveUser(groupId)
if (activeUserId === null || activeUserId === '' || activeUserId === 'None') {
return null
}

const balances = getBalances([expense])
let fmtBalance = <>You are not involved</>
if (Object.hasOwn(balances, activeUserId)) {
const balance = balances[activeUserId]
let balanceDetail = <></>
if (balance.paid > 0 && balance.paidFor > 0) {
balanceDetail = (
<>
{' ('}
<Money {...{ currency, amount: balance.paid }} />
{' - '}
<Money {...{ currency, amount: balance.paidFor }} />
{')'}
</>
)
}
fmtBalance = (
<>
Your balance:{' '}
<Money {...{ currency, amount: balance.total }} bold colored />
{balanceDetail}
</>
)
}
return <div className="text-xs text-muted-foreground">{fmtBalance}</div>
}
4 changes: 4 additions & 0 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client'
import { ActiveUserBalance } from '@/app/groups/[groupId]/expenses/active-user-balance'
import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
import { Button } from '@/components/ui/button'
import { SearchBar } from '@/components/ui/search-bar'
Expand Down Expand Up @@ -151,6 +152,9 @@ export function ExpenseList({
</Fragment>
))}
</div>
<div className="text-xs text-muted-foreground">
<ActiveUserBalance {...{ groupId, currency, expense }} />
</div>
</div>
<div className="flex flex-col justify-between items-end">
<div
Expand Down
31 changes: 31 additions & 0 deletions src/components/money.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client'
import { cn, formatCurrency } from '@/lib/utils'

type Props = {
currency: string
amount: number
bold?: boolean
colored?: boolean
}

export function Money({
currency,
amount,
bold = false,
colored = false,
}: Props) {
return (
<span
className={cn(
colored && amount <= 1
? 'text-red-600'
: colored && amount >= 1
? 'text-green-600'
: '',
bold && 'font-bold',
)}
>
{formatCurrency(currency, amount)}
</span>
)
}

0 comments on commit 1cd2b27

Please sign in to comment.