-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |