eletrotupi / tcc/ frontend/lib/utils/time.ts94a9e9492999dae406897bdab0c1e6e25eabe0cb
1.1 KB | 25 lines Raw
1
import { format, isToday, isYesterday } from 'date-fns';
2
import { ptBR } from 'date-fns/locale';
3

4
type TimeBadgeVariant = 'morning' | 'afternoon' | 'night' | 'latenight';
5

6
export const TIME_BADGES: Record<TimeBadgeVariant, { label: string; backgroundColor: string; color: string }> = {
7
  morning:   { label: 'Manhã',     backgroundColor: '#dcfce7', color: '#16a34a' },
8
  afternoon: { label: 'Tarde',     backgroundColor: '#f1f5f9', color: '#475569' },
9
  night:     { label: 'Noite',     backgroundColor: '#dbeafe', color: '#1d4ed8' },
10
  latenight: { label: 'Madrugada', backgroundColor: '#f3e8ff', color: '#7e22ce' },
11
};
12

13
export const getTimeBadge = (date: Date) => {
14
  const h = date.getHours();
15
  if (h >= 6  && h < 12) return TIME_BADGES.morning;
16
  if (h >= 12 && h < 18) return TIME_BADGES.afternoon;
17
  if (h >= 18 && h < 24) return TIME_BADGES.night;
18
  return TIME_BADGES.latenight;
19
}
20

21
export const formatMoment = (date: Date): string => {
22
  if (isToday(date))     return `Hoje às ${format(date, "HH'h'mm")}`;
23
  if (isYesterday(date)) return `Ontem às ${format(date, "HH'h'mm")}`;
24
  return format(date, "d 'de' MMM 'às' HH'h'mm", { locale: ptBR });
25
}