eletrotupi / tcc/ frontend/components/ui/DailySleepWidget.tsx5a1373aed06bccb3f57d57b91da01d9500c1ce91
1.2 KB | 43 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
function formatHours(hours: number): string {
37
  const h = Math.floor(hours);
38
  const m = Math.round((hours - h) * 60);
39

40
  if (m === 0) return `${h}h`;
41

42
  return `${h}h ${m}min`;
43
}