eletrotupi / tcc/ frontend/lib/utils/history.tse733ebff3c2657c7f9c381347d45c2e015958b5a
857 B | 30 lines Raw
1
// TODO: add a barrel import here ffs
2
import type { HistoryCard, HistoryCategory } from '@/lib/history/types'
3

4
export function mergeAndSort(cards: HistoryCard[][]): HistoryCard[] {
5
  return cards
6
  .flat()
7
  .sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())
8
}
9

10
export function groupByDate(cards: HistoryCard[]): Record<string, HistoryCard[]> {
11
  return cards.reduce<Record<string, HistoryCard[]>>((acc, card) => {
12
    const key = new Date(card.timestamp).toLocaleDateString('pt-BR', {
13
      weekday: 'long',
14
      day: 'numeric',
15
      month: 'long',
16
    });
17

18
    (acc[key] ??= []).push(card)
19

20
    return acc
21
  }, {})
22
}
23

24
export function filterByCategory(
25
  cards: HistoryCard[],
26
  active: HistoryCategory | 'all',
27
): HistoryCard[] {
28
  if (active === 'all') return cards
29
    return cards.filter(c => c.category === active)
30
}