mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-06 04:26:13 +01:00
Show the impact of an expense on the active user's balance (#139)
* 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>
This commit is contained in:
43
src/app/groups/[groupId]/expenses/active-user-balance.tsx
Normal file
43
src/app/groups/[groupId]/expenses/active-user-balance.tsx
Normal file
@@ -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>
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
import { ActiveUserBalance } from '@/app/groups/[groupId]/expenses/active-user-balance'
|
||||||
import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
|
import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { SearchBar } from '@/components/ui/search-bar'
|
import { SearchBar } from '@/components/ui/search-bar'
|
||||||
@@ -151,6 +152,9 @@ export function ExpenseList({
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
<div className="text-xs text-muted-foreground">
|
||||||
|
<ActiveUserBalance {...{ groupId, currency, expense }} />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col justify-between items-end">
|
<div className="flex flex-col justify-between items-end">
|
||||||
<div
|
<div
|
||||||
|
|||||||
31
src/components/money.tsx
Normal file
31
src/components/money.tsx
Normal file
@@ -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>
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user