eletrotupi / tcc/ frontend/lib/utils/history.tsa1a9079e3159130a372ea537b25611ee5d274ef9
1.0 KB | 36 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(
11
  cards: HistoryCard[],
12
  now: Date = new Date(),
13
): Record<string, HistoryCard[]> {
14
  return cards.reduce<Record<string, HistoryCard[]>>((acc, card) => {
15
    const date = new Date(card.timestamp);
16
    const includeYear = date.getFullYear() !== now.getFullYear();
17
    const key = date.toLocaleDateString('pt-BR', {
18
      weekday: 'long',
19
      day: 'numeric',
20
      month: 'long',
21
      ...(includeYear ? { year: 'numeric' } : {}),
22
    });
23

24
    (acc[key] ??= []).push(card)
25

26
    return acc
27
  }, {})
28
}
29

30
export function filterByCategory(
31
  cards: HistoryCard[],
32
  active: HistoryCategory | 'all',
33
): HistoryCard[] {
34
  if (active === 'all') return cards
35
    return cards.filter(c => c.category === active)
36
}