Skip to content

Commit

Permalink
fix chart
Browse files Browse the repository at this point in the history
  • Loading branch information
quinn committed Feb 13, 2024
1 parent 58f74c1 commit be5c0d1
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/components/pages/ChartPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,22 @@ export function ChartPage() {

const filteredData = useMemo(() => {
if (!data) return null
let amount = 0

const startDate = sub(new Date(), { days: dayRange })
const frames = []
const results = [...data.results]
const results: Record<string, typeof data.results> = {}
let amount = 0

for (const transaction of data.results) {
const transactions = results[transaction.date] ?? []
results[transaction.date] = transactions
transactions.push(transaction)
}

for (let i = 0; i < dayRange; i++) {
const date = format(add(startDate, { days: i }), 'yyyy-MM-dd')
const transactions = []

while (results[0] && results[0].date === date) {
const transaction = results.shift()
if (!transaction) continue
if (transaction.amount < 0) continue
transactions.push(transaction)
amount += transaction.amount
}

const transactions: typeof data.results = results[date] ?? []
amount += transactions.reduce((a, b) => a + Math.max(0, b.amount), 0)
const frame = { date, amount, transactions }
frames.push(frame)
}
Expand Down

0 comments on commit be5c0d1

Please sign in to comment.