a1a9079e3159130a372ea537b25611ee5d274ef9
// TODO: add a barrel import here ffs
import type { HistoryCard, HistoryCategory } from '@/lib/history/types'
export function mergeAndSort(cards: HistoryCard[][]): HistoryCard[] {
return cards
.flat()
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())
}
export function groupByDate(
cards: HistoryCard[],
now: Date = new Date(),
): Record<string, HistoryCard[]> {
return cards.reduce<Record<string, HistoryCard[]>>((acc, card) => {
const date = new Date(card.timestamp);
const includeYear = date.getFullYear() !== now.getFullYear();
const key = date.toLocaleDateString('pt-BR', {
weekday: 'long',
day: 'numeric',
month: 'long',
...(includeYear ? { year: 'numeric' } : {}),
});
(acc[key] ??= []).push(card)
return acc
}, {})
export function filterByCategory(
active: HistoryCategory | 'all',
): HistoryCard[] {
if (active === 'all') return cards
return cards.filter(c => c.category === active)