eletrotupi / tcc/ frontend/lib/utils/history.tsb78a1b29b5a9a72cd1ed7a2d7213f1a609d61b04
883 B | 31 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
    // XXX: Hacky as fuck
19
    (acc[key] ??= []).push(card)
20

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

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