"use client";
import { useState } from "react";
import { Info, TrendingUp, TrendingDown, AlertTriangle } from "lucide-react";
import { cn } from "@/lib/utils";
interface NutritionBarProps {
label: string;
value: string;
unit?: string;
percentage?: number;
isHigh?: boolean;
isLow?: boolean;
recommended?: string;
className?: string;
showTooltip?: boolean;
}
export function NutritionBar({
label,
value,
unit = "",
percentage = 0,
isHigh = false,
isLow = false,
recommended,
className,
showTooltip = true,
}: NutritionBarProps) {
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
// Calcular cor baseada no valor e se é alto/baixo
const getBarColor = () => {
if (isHigh) {
return "bg-gradient-to-r from-red-400 to-red-600";
}
if (isLow) {
return "bg-gradient-to-r from-green-400 to-green-600";
}
if (percentage > 80) {
return "bg-gradient-to-r from-orange-400 to-orange-600";
}
if (percentage > 60) {
return "bg-gradient-to-r from-yellow-400 to-yellow-600";
}
return "bg-gradient-to-r from-blue-400 to-blue-600";
};
// Calcular largura da barra baseada na porcentagem
const barWidth = Math.min(Math.max(percentage, 5), 100);
// Determinar ícone baseado no status
const getIcon = () => {
if (isHigh) return ;
if (isLow) return ;
return null;
};
// Determinar cor do texto baseada no status
const getTextColor = () => {
if (isHigh) return "text-red-600 dark:text-red-400";
if (isLow) return "text-green-600 dark:text-green-400";
return "text-gray-900 dark:text-white";
};
return (
{/* Linha principal: Informação nutricional à esquerda, porcentagem à direita */}
{/* Informação nutricional à esquerda */}
{label}:
{value} {unit}
{getIcon() && (
{getIcon()}
)}
{showTooltip && (
)}
{/* Separador visual */}
{/* Porcentagem à direita */}
{percentage > 0 && (
{Math.round(percentage)}%
)}
{/* Barra de progresso */}
{/* Tooltip informativo */}
{showTooltip && isTooltipVisible && (
{label}
{isHigh &&
}
{isLow &&
}
Valor: {value} {unit}
{recommended && (
Recomendado: {recommended}
)}
{isHigh && (
⚠️ Alto valor - consumir com moderação
)}
{isLow && (
✅ Baixo valor - boa opção
)}
{/* Seta do tooltip */}
)}
);
}
interface NutritionGridProps {
nutritionData: {
energy?: string;
fat?: string;
carbohydrates?: string;
proteins?: string;
sugars?: string;
fiber?: string;
sodium?: string;
};
className?: string;
}
export function NutritionGrid({
nutritionData,
className,
}: NutritionGridProps) {
// Valores de referência para calcular porcentagens (valores diários recomendados)
const referenceValues = {
energy: 2000, // kcal
fat: 65, // g
carbohydrates: 300, // g
proteins: 50, // g
sugars: 50, // g
fiber: 25, // g
sodium: 2.3, // g
};
// Extrair valor numérico da string
const extractNumericValue = (value: string | undefined): number => {
if (!value) return 0;
const match = value.match(/(\d+(?:\.\d+)?)/);
return match ? parseFloat(match[1]) : 0;
};
// Calcular porcentagem baseada no valor de referência
const calculatePercentage = (
value: string | undefined,
reference: number
): number => {
const numericValue = extractNumericValue(value);
return (numericValue / reference) * 100;
};
// Determinar se um valor é alto ou baixo
const isHighValue = (
value: string | undefined,
reference: number
): boolean => {
const percentage = calculatePercentage(value, reference);
return percentage > 80;
};
const isLowValue = (
value: string | undefined,
reference: number
): boolean => {
const percentage = calculatePercentage(value, reference);
return percentage < 20;
};
const nutritionItems = [
{
label: "Energia",
value: nutritionData.energy,
unit: "kcal",
percentage: calculatePercentage(
nutritionData.energy,
referenceValues.energy
),
isHigh: isHighValue(nutritionData.energy, referenceValues.energy),
isLow: isLowValue(nutritionData.energy, referenceValues.energy),
recommended: "2000 kcal/dia",
},
{
label: "Gorduras",
value: nutritionData.fat,
unit: "g",
percentage: calculatePercentage(nutritionData.fat, referenceValues.fat),
isHigh: isHighValue(nutritionData.fat, referenceValues.fat),
isLow: isLowValue(nutritionData.fat, referenceValues.fat),
recommended: "65g/dia",
},
{
label: "Carboidratos",
value: nutritionData.carbohydrates,
unit: "g",
percentage: calculatePercentage(
nutritionData.carbohydrates,
referenceValues.carbohydrates
),
isHigh: isHighValue(
nutritionData.carbohydrates,
referenceValues.carbohydrates
),
isLow: isLowValue(
nutritionData.carbohydrates,
referenceValues.carbohydrates
),
recommended: "300g/dia",
},
{
label: "Proteínas",
value: nutritionData.proteins,
unit: "g",
percentage: calculatePercentage(
nutritionData.proteins,
referenceValues.proteins
),
isHigh: isHighValue(nutritionData.proteins, referenceValues.proteins),
isLow: isLowValue(nutritionData.proteins, referenceValues.proteins),
recommended: "50g/dia",
},
{
label: "Açúcares",
value: nutritionData.sugars,
unit: "g",
percentage: calculatePercentage(
nutritionData.sugars,
referenceValues.sugars
),
isHigh: isHighValue(nutritionData.sugars, referenceValues.sugars),
isLow: isLowValue(nutritionData.sugars, referenceValues.sugars),
recommended: "50g/dia",
},
{
label: "Fibras",
value: nutritionData.fiber,
unit: "g",
percentage: calculatePercentage(
nutritionData.fiber,
referenceValues.fiber
),
isHigh: isHighValue(nutritionData.fiber, referenceValues.fiber),
isLow: isLowValue(nutritionData.fiber, referenceValues.fiber),
recommended: "25g/dia",
},
{
label: "Sódio",
value: nutritionData.sodium,
unit: "g",
percentage: calculatePercentage(
nutritionData.sodium,
referenceValues.sodium
),
isHigh: isHighValue(nutritionData.sodium, referenceValues.sodium),
isLow: isLowValue(nutritionData.sodium, referenceValues.sodium),
recommended: "2.3g/dia",
},
];
return (
Informação Nutricional
Valores diários de referência
{nutritionItems.map((item) => (
))}
{/* Legenda */}
Como interpretar os valores:
);
}