eletrotupi / tcc/ frontend/components/ui/DailyEnergyWidget.tsx5a1373aed06bccb3f57d57b91da01d9500c1ce91
1.5 KB | 50 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
      windowLabel={metadata?.windowLabel}
22
      barRatio={
23
        avgEnergy != null
24
          ? Math.max(0, Math.min(1, avgEnergy / ENERGY_SCALE_MAX))
25
          : undefined
26
      }
27
      comparison={formatEnergyDiff(diffEnergy)}
28
      comparisonColor={energyDiffColor(diffEnergy)}
29
    />
30
  );
31
};
32

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

37
// Positive means more energy than yesterday; nothing to compare reads null
38
function formatEnergyDiff(diff: number | null): string | null {
39
  if (diff == null) return null;
40

41
  const sign = diff > 0 ? "+" : diff < 0 ? "-" : "";
42

43
  return `${sign}${Math.abs(diff).toFixed(1).replace(".", ",")} que ontem`;
44
}
45

46
function energyDiffColor(diff: number | null): string {
47
  if (diff == null || diff === 0) return Colors.light.textSecondary;
48

49
  return diff > 0 ? Colors.light.tint : Colors.light.danger;
50
}