eletrotupi / tcc/ frontend/components/ui/DailySleepWidget.tsxc901c35af9c84b6a29e5185545ced1129f33b8f4
1.4 KB | 48 lines Raw
1
import { InsightMetricCard } from "@/components/insights/InsightMetricCard";
2
import { useInsight } from "@/hooks/useInsights.queries";
3
import { Colors } from "@/constants/theme";
4

5
export const DailySleepWidget = () => {
6
  const { data: insight, isLoading } = useInsight("DAILY_SLEEP");
7

8
  const metadata = insight?.metadata;
9
  const diffMinutes = metadata?.diffMinutes ?? null;
10

11
  return (
12
    <InsightMetricCard
13
      icon="moon"
14
      title="Sono"
15
      iconColor={Colors.light.accentDarkPurple}
16
      iconBackground={Colors.light.accentPurple}
17
      isLoading={isLoading}
18
      emptyMessage="Não há dados disponíveis para o dia de hoje"
19
      value={
20
        metadata?.totalHours != null
21
          ? formatHours(metadata.totalHours)
22
          : undefined
23
      }
24
      windowLabel={metadata?.windowLabel}
25
      detail={metadata?.napStr || undefined}
26
      comparison={metadata?.diffStr || null}
27
      comparisonColor={
28
        diffMinutes != null && diffMinutes < 0
29
          ? Colors.light.danger
30
          : Colors.light.tint
31
      }
32
    />
33
  );
34
};
35

36
// A single record is capped at 15h, but a day sums them, so guard the display
37
const MAX_SLEEP_HOURS = 24;
38

39
function formatHours(hours: number): string {
40
  if (hours >= MAX_SLEEP_HOURS) return `${MAX_SLEEP_HOURS}h+`;
41

42
  const minutes = Math.round(hours * 60);
43
  const h = Math.floor(minutes / 60);
44

45
  if (minutes % 60 === 0) return `${h}h`;
46

47
  return `${h}h ${minutes % 60}min`;
48
}