eletrotupi / tcc/ frontend/components/ui/DailyEnergyWidget.tsxfb784c6226618a02a32f1f7d619d8844b6b623ef
1.8 KB | 59 lines Raw
1
import { InsightMetricCard } from "@/components/insights/InsightMetricCard";
2
import { useInsight } from "@/hooks/useInsights.queries";
3
import { Colors } from "@/constants/theme";
4

5
const ENERGY_SCALE_MAX = 10;
6

7
export const DailyEnergyWidget = () => {
8
  const { data: insight, isLoading } = useInsight("DAILY_ENERGY");
9

10
  const metadata = insight?.metadata;
11
  const avgEnergy = metadata?.avgEnergy;
12
  const diffEnergy = metadata?.diffEnergy ?? null;
13

14
  return (
15
    <InsightMetricCard
16
      icon="flash"
17
      title="Energia"
18
      isLoading={isLoading}
19
      emptyMessage="Não há dados disponíveis para o dia de hoje"
20
      value={avgEnergy != null ? formatEnergy(avgEnergy) : undefined}
21
      band={avgEnergy != null ? energyBand(avgEnergy) : undefined}
22
      windowLabel={metadata?.windowLabel}
23
      barRatio={
24
        avgEnergy != null
25
          ? Math.max(0, Math.min(1, avgEnergy / ENERGY_SCALE_MAX))
26
          : undefined
27
      }
28
      comparison={formatEnergyDiff(diffEnergy)}
29
      comparisonColor={energyDiffColor(diffEnergy)}
30
    />
31
  );
32
};
33

34
function formatEnergy(value: number): string {
35
  return value.toFixed(1).replace(".", ",");
36
}
37

38
// Same low/high read as the mood history badge: alta from 7 up, baixa below 4
39
function energyBand(value: number): string {
40
  if (value < 4) return "Baixa";
41
  if (value < 7) return "Média";
42

43
  return "Alta";
44
}
45

46
// Positive means more energy than yesterday; nothing to compare reads null
47
function formatEnergyDiff(diff: number | null): string | null {
48
  if (diff == null) return null;
49

50
  const sign = diff > 0 ? "+" : diff < 0 ? "-" : "";
51

52
  return `${sign}${Math.abs(diff).toFixed(1).replace(".", ",")} que ontem`;
53
}
54

55
function energyDiffColor(diff: number | null): string {
56
  if (diff == null || diff === 0) return Colors.light.textSecondary;
57

58
  return diff > 0 ? Colors.light.tint : Colors.light.danger;
59
}