Skip to content

Commit acacb49

Browse files
committed
Change month headers logic to use map instead of reduce and improve styling
1 parent 89030e2 commit acacb49

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/components/Expense/ExpenseList.tsx

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SplitType } from '@prisma/client';
22
import { type inferRouterOutputs } from '@trpc/server';
33
import Image from 'next/image';
44
import Link from 'next/link';
5-
import React, { type JSX } from 'react';
5+
import React from 'react';
66
import {
77
CURRENCY_CONVERSION_ICON,
88
CategoryIcon,
@@ -37,41 +37,47 @@ export const ExpenseList: React.FC<{
3737

3838
const { i18n } = useTranslationWithUtils();
3939

40+
let lastDate: Date | null = null;
41+
4042
return (
4143
<>
42-
{expenses.reduce((acc, e, idx, arr) => {
43-
const prev = arr[idx - 1];
44-
const currMonth = new Date(e.expenseDate).getMonth();
45-
const prevMonth = prev ? new Date(prev.expenseDate).getMonth() : null;
46-
const isFirstOfMonth = idx === 0 || currMonth !== prevMonth;
47-
48-
if (isFirstOfMonth) {
49-
acc.push(
50-
<div key={`month-${e.id}`}>
51-
{new Intl.DateTimeFormat(i18n.language, { month: 'long', year: 'numeric' }).format(
52-
e.expenseDate,
53-
)}
54-
</div>,
55-
);
44+
{expenses.map((e) => {
45+
const currentDate = e.expenseDate;
46+
let isFirstOfMonth = false;
47+
48+
if (
49+
lastDate === null ||
50+
currentDate.getMonth() !== lastDate.getMonth() ||
51+
currentDate.getFullYear() !== lastDate.getFullYear()
52+
) {
53+
isFirstOfMonth = true;
5654
}
5755

56+
lastDate = currentDate;
57+
5858
const isSettlement = e.splitType === SplitType.SETTLEMENT;
5959
const isCurrencyConversion = e.splitType === SplitType.CURRENCY_CONVERSION;
6060

61-
acc.push(
62-
<Link
63-
href={`/${isGroup ? 'groups' : 'balances'}/${contactId}/expenses/${e.id}`}
64-
key={e.id}
65-
className="flex items-center justify-between py-2"
66-
>
67-
{isSettlement && <Settlement e={e} userId={userId} />}
68-
{isCurrencyConversion && <CurrencyConversion e={e} userId={userId} />}
69-
{!isSettlement && !isCurrencyConversion && <Expense e={e} userId={userId} />}
70-
</Link>,
61+
return (
62+
<React.Fragment key={e.id}>
63+
{isFirstOfMonth && (
64+
<div className="pt-4 text-xs font-semibold text-gray-500 uppercase">
65+
{new Intl.DateTimeFormat(i18n.language, { month: 'long', year: 'numeric' }).format(
66+
currentDate,
67+
)}
68+
</div>
69+
)}
70+
<Link
71+
href={`/${isGroup ? 'groups' : 'balances'}/${contactId}/expenses/${e.id}`}
72+
className="flex items-center justify-between py-2"
73+
>
74+
{isSettlement && <Settlement e={e} userId={userId} />}
75+
{isCurrencyConversion && <CurrencyConversion e={e} userId={userId} />}
76+
{!isSettlement && !isCurrencyConversion && <Expense e={e} userId={userId} />}
77+
</Link>
78+
</React.Fragment>
7179
);
72-
73-
return acc;
74-
}, [] as JSX.Element[])}
80+
})}
7581
</>
7682
);
7783
};

0 commit comments

Comments
 (0)