|
| 1 | +import React from "react"; |
| 2 | +import { Product } from "@/types/product"; |
| 3 | +import { colours } from "@/styles/colours"; |
| 4 | + |
| 5 | +interface NutritionTabContentProps { |
| 6 | + product: Product; |
| 7 | + animatedNutrition: number; |
| 8 | +} |
| 9 | + |
| 10 | +const NutritionTabContent: React.FC<NutritionTabContentProps> = ({ |
| 11 | + product, |
| 12 | + animatedNutrition, |
| 13 | +}) => { |
| 14 | + return ( |
| 15 | + <div className="w-full flex flex-col items-center opacity-0 animate-fade-in" style={{ animationDelay: '0.05s' }}> |
| 16 | + <h2 |
| 17 | + className="text-lg font-bold mb-2 self-start" |
| 18 | + style={{ color: colours.text.primary }} |
| 19 | + > |
| 20 | + Nutrition |
| 21 | + </h2> |
| 22 | + <div className="flex flex-col items-center justify-center gap-4 w-full"> |
| 23 | + {/* Nutrition Grade Circle */} |
| 24 | + <div className="flex flex-col items-center gap-2"> |
| 25 | + <span className="relative inline-block w-24 h-24 align-middle"> |
| 26 | + <svg width="96" height="96" viewBox="0 0 96 96" className="absolute top-0 left-0" style={{ zIndex: 1 }}> |
| 27 | + <circle |
| 28 | + cx="48" cy="48" r="40" |
| 29 | + fill="none" |
| 30 | + stroke={(() => { |
| 31 | + const score = animatedNutrition; |
| 32 | + if (score <= 2) return colours.score.low; |
| 33 | + if (score <= 3) return colours.score.medium; |
| 34 | + return colours.score.high; |
| 35 | + })()} |
| 36 | + strokeWidth="8" |
| 37 | + strokeDasharray={Math.PI * 2 * 40} |
| 38 | + strokeDashoffset={Math.PI * 2 * 40 * (1 - (animatedNutrition / 5))} |
| 39 | + strokeLinecap="round" |
| 40 | + style={{ |
| 41 | + transition: 'stroke-dashoffset 0.7s cubic-bezier(0.4,0,0.2,1), stroke 0.7s cubic-bezier(0.4,0,0.2,1)', |
| 42 | + transform: 'rotate(-90deg)', |
| 43 | + transformOrigin: 'center center', |
| 44 | + }} |
| 45 | + /> |
| 46 | + </svg> |
| 47 | + <div className="absolute inset-0 flex flex-col items-center justify-center"> |
| 48 | + <span |
| 49 | + className="text-4xl font-bold" |
| 50 | + style={{ |
| 51 | + color: (() => { |
| 52 | + const score = animatedNutrition; |
| 53 | + if (score <= 2) return colours.score.low; |
| 54 | + if (score <= 3) return colours.score.medium; |
| 55 | + return colours.score.high; |
| 56 | + })() |
| 57 | + }} |
| 58 | + > |
| 59 | + {product.combinedNutritionGrade?.toUpperCase() || '?'} |
| 60 | + </span> |
| 61 | + </div> |
| 62 | + </span> |
| 63 | + <span |
| 64 | + className="text-sm font-medium" |
| 65 | + style={{ color: colours.text.secondary }} |
| 66 | + > |
| 67 | + Nutrition Grade |
| 68 | + </span> |
| 69 | + </div> |
| 70 | + |
| 71 | + {/* Nutrition Macros per 100g */} |
| 72 | + {product.nutritionMacros && Object.values(product.nutritionMacros).some(value => value !== undefined) && ( |
| 73 | + <div className="w-full mt-4"> |
| 74 | + <h3 |
| 75 | + className="text-md font-semibold mb-3 text-center" |
| 76 | + style={{ color: colours.text.primary }} |
| 77 | + > |
| 78 | + Nutrition Facts (per 100g) |
| 79 | + </h3> |
| 80 | + <div className="grid grid-cols-2 gap-3 w-full max-w-sm mx-auto"> |
| 81 | + {product.nutritionMacros.energy !== undefined && ( |
| 82 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 83 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Energy</span> |
| 84 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.energy} kcal</span> |
| 85 | + </div> |
| 86 | + )} |
| 87 | + {product.nutritionMacros.proteins !== undefined && ( |
| 88 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 89 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Protein</span> |
| 90 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.proteins}g</span> |
| 91 | + </div> |
| 92 | + )} |
| 93 | + {product.nutritionMacros.carbohydrates !== undefined && ( |
| 94 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 95 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Carbs</span> |
| 96 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.carbohydrates}g</span> |
| 97 | + </div> |
| 98 | + )} |
| 99 | + {product.nutritionMacros.sugars !== undefined && ( |
| 100 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 101 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Sugars</span> |
| 102 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.sugars}g</span> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + {product.nutritionMacros.fat !== undefined && ( |
| 106 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 107 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Fat</span> |
| 108 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.fat}g</span> |
| 109 | + </div> |
| 110 | + )} |
| 111 | + {product.nutritionMacros.saturatedFat !== undefined && ( |
| 112 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 113 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Sat. Fat</span> |
| 114 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.saturatedFat}g</span> |
| 115 | + </div> |
| 116 | + )} |
| 117 | + {product.nutritionMacros.fiber !== undefined && ( |
| 118 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 119 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Fiber</span> |
| 120 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{product.nutritionMacros.fiber}g</span> |
| 121 | + </div> |
| 122 | + )} |
| 123 | + {product.nutritionMacros.sodium !== undefined && ( |
| 124 | + <div className="flex justify-between items-center p-2 rounded-lg" style={{ backgroundColor: colours.content.surfaceSecondary }}> |
| 125 | + <span className="text-sm font-medium" style={{ color: colours.text.secondary }}>Sodium</span> |
| 126 | + <span className="text-sm font-bold" style={{ color: colours.text.primary }}>{(product.nutritionMacros.sodium * 1000).toFixed(0)}mg</span> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export default NutritionTabContent; |
0 commit comments