aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAnurag <anurag4884@gmail.com>2024-02-28 21:13:25 +0530
committerGitHub <noreply@github.com>2024-02-28 10:43:25 -0500
commit2f991e680bea37d1f97ea77df2343200f3899817 (patch)
tree2f043e4aafaa99340b8fac6153f1741d9dbb7ab2 /src/lib
parent2af066038351ab0634c359a688fe712a539992b4 (diff)
feat: initialise a new totals tab with basic UI (#94)
* feat: initialise a new totals tab with basic UI * fix: update group tabs and add stats page * fix: styling within the new elements * Prettier * Display active user expenses only if active user is set --------- Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/hooks.ts14
-rw-r--r--src/lib/totals.ts66
2 files changed, 80 insertions, 0 deletions
diff --git a/src/lib/hooks.ts b/src/lib/hooks.ts
index 4d3ad08..490eccf 100644
--- a/src/lib/hooks.ts
+++ b/src/lib/hooks.ts
@@ -48,3 +48,17 @@ export function useBaseUrl() {
}, [])
return baseUrl
}
+
+/**
+ * @returns The active user, or `null` until it is fetched from local storage
+ */
+export function useActiveUser(groupId: string) {
+ const [activeUser, setActiveUser] = useState<string | null>(null)
+
+ useEffect(() => {
+ const activeUser = localStorage.getItem(`${groupId}-activeUser`)
+ if (activeUser) setActiveUser(activeUser)
+ }, [groupId])
+
+ return activeUser
+}
diff --git a/src/lib/totals.ts b/src/lib/totals.ts
new file mode 100644
index 0000000..797318c
--- /dev/null
+++ b/src/lib/totals.ts
@@ -0,0 +1,66 @@
+import { getGroupExpenses } from '@/lib/api'
+
+export function getTotalGroupSpending(
+ expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>,
+): number {
+ return expenses.reduce(
+ (total, expense) =>
+ !expense.isReimbursement ? total + expense.amount : total + 0,
+ 0,
+ )
+}
+
+export function getTotalActiveUserPaidFor(
+ activeUserId: string | null,
+ expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>,
+): number {
+ return expenses.reduce(
+ (total, expense) =>
+ expense.paidBy.id === activeUserId ? total + expense.amount : total + 0,
+ 0,
+ )
+}
+
+export function getTotalActiveUserShare(
+ activeUserId: string | null,
+ expenses: NonNullable<Awaited<ReturnType<typeof getGroupExpenses>>>,
+): number {
+ let total = 0
+
+ expenses.forEach((expense) => {
+ const paidFors = expense.paidFor
+ const userPaidFor = paidFors.find(
+ (paidFor) => paidFor.participantId === activeUserId,
+ )
+
+ if (!userPaidFor) {
+ // If the active user is not involved in the expense, skip it
+ return
+ }
+
+ switch (expense.splitMode) {
+ case 'EVENLY':
+ // Divide the total expense evenly among all participants
+ total += expense.amount / paidFors.length
+ break
+ case 'BY_AMOUNT':
+ // Directly add the user's share if the split mode is BY_AMOUNT
+ total += userPaidFor.shares
+ break
+ case 'BY_PERCENTAGE':
+ // Calculate the user's share based on their percentage of the total expense
+ total += (expense.amount * userPaidFor.shares) / 10000 // Assuming shares are out of 10000 for percentage
+ break
+ case 'BY_SHARES':
+ // Calculate the user's share based on their shares relative to the total shares
+ const totalShares = paidFors.reduce(
+ (sum, paidFor) => sum + paidFor.shares,
+ 0,
+ )
+ total += (expense.amount * userPaidFor.shares) / totalShares
+ break
+ }
+ })
+
+ return parseFloat(total.toFixed(2))
+}