eletrotupi / tcc/ frontend/lib/history/mappers.ts258151993396b0b11c8543ebb8fad1053d759bc3
2.2 KB | 71 lines Raw
1
import type { HistoryCard } from "@/lib/history/types";
2
import type { MoodEntry, SleepRecord, Trigger } from "@/lib/api";
3
import { getMood } from "@/constants/moods";
4
import { parse, setHours } from "date-fns";
5

6
export function mapMoodToHistoryCard(mood: MoodEntry): HistoryCard {
7
  const moodLabel = getMood(mood.selectedMood);
8
  const components = mood.moodComponents
9
    .map((c) => c.component.charAt(0) + c.component.slice(1).toLowerCase())
10
    .join(", ");
11

12
  const summary = [components || null, mood.annotation || null]
13
    .filter(Boolean)
14
    .join(" · ");
15

16
  return {
17
    id: `mood-${mood.id}`,
18
    category: "mood",
19
    timestamp: mood.moment.toString(),
20
    title: moodLabel?.label ?? "Humor",
21
    summary: summary || "Sem detalhes",
22
    badge: undefined,
23
    raw: mood,
24
  };
25
}
26

27
// Sleep Records
28

29
function sleepQualityBadge(hours: number): HistoryCard["badge"] {
30
  if (hours >= 7) return { label: "Excelente", variant: "success" };
31
  if (hours >= 5) return { label: "Regular", variant: "warning" };
32

33
  return {
34
    label: "Ruim",
35
    variant: "danger",
36
  };
37
}
38

39
export function mapSleepToHistoryCard(record: SleepRecord): HistoryCard {
40
  const hours = Math.floor(record.average);
41
  const minutes = Math.round((record.average - hours) * 60);
42
  const duration = `${hours}h ${minutes.toString().padStart(2, "0")}min`;
43
  // Force local noon so timezone shifts can't bleed into the previous day
44
  const dateStr = typeof record.date === 'string'
45
    ? record.date.slice(0, 10)           // "2026-05-24"
46
    : record.date.toISOString().slice(0, 10)
47

48
  const timestamp = `${dateStr}T12:00:00`  // local noon, no UTC funny business
49

50
  return {
51
    id: `sleep-${record.id}`,
52
    category: "sleep",
53
    timestamp,
54
    title: "Sono",
55
    summary: [duration, record.annotations].filter(Boolean).join(" · "),
56
    badge: sleepQualityBadge(record.average),
57
    raw: record,
58
  };
59
}
60

61
export function mapTriggerToHistoryCard(record: Trigger): HistoryCard {
62
  return {
63
    id: `trigger-${record.id}`,
64
    category: "trigger",
65
    timestamp: record.moment.toString(),
66
    title: "Gatilho",
67
    summary: [record.category, record.comment].filter(Boolean).join(" · "),
68
    badge: undefined,
69
    raw: record,
70
  };
71
}