Skip to content

Commit 52ddd59

Browse files
committed
fix: cleaned up brand tab code
1 parent 9c05f77 commit 52ddd59

File tree

3 files changed

+57
-63
lines changed

3 files changed

+57
-63
lines changed

src/components/product/tabs/BrandTabContent.tsx

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,49 @@ interface BrandTabContentProps {
1010
calculateBrandStats: BrandStats | null;
1111
}
1212

13+
// Helper function to get color based on score
14+
const getScoreColor = (score: number): string => {
15+
if (score <= 2) return colours.score.low;
16+
if (score <= 3) return colours.score.medium;
17+
return colours.score.high;
18+
};
19+
20+
// Helper function to get background color with opacity
21+
const getScoreBackgroundColor = (score: number): string => {
22+
return getScoreColor(score) + '20'; // 20% opacity
23+
};
24+
25+
// Performance metrics configuration
26+
const PERFORMANCE_METRICS = [
27+
{
28+
label: 'Price',
29+
color: '#fef3c7', /* yellow-100 */
30+
svg: '/pound-svgrepo-com.svg'
31+
},
32+
{
33+
label: 'Quality',
34+
color: '#fee2e2', /* red-100 */
35+
svg: '/quality-supervision-svgrepo-com.svg'
36+
},
37+
{
38+
label: 'Nutrition',
39+
color: '#dbeafe', /* blue-100 */
40+
svg: '/meal-svgrepo-com.svg'
41+
},
42+
{
43+
label: 'Sustainability',
44+
color: '#ecfccb', /* lime-100 */
45+
svg: '/leaf-svgrepo-com.svg'
46+
}
47+
];
48+
1349
const BrandTabContent: React.FC<BrandTabContentProps> = ({
1450
animatedBrand,
1551
calculateBrandStats,
1652
}) => {
53+
const strokeColor = getScoreColor(animatedBrand);
54+
const backgroundColor = getScoreBackgroundColor(animatedBrand);
55+
1756
return (
1857
<div className="w-full flex flex-col items-center opacity-0 animate-fade-in px-1" style={{ animationDelay: '0.05s' }}>
1958
<h2
@@ -49,31 +88,16 @@ const BrandTabContent: React.FC<BrandTabContentProps> = ({
4988
<div
5089
className="relative w-16 h-16 rounded-full border-2 border-dashed flex items-center justify-center"
5190
style={{
52-
borderColor: (() => {
53-
const score = animatedBrand;
54-
if (score <= 2) return colours.score.low;
55-
if (score <= 3) return colours.score.medium;
56-
return colours.score.high;
57-
})(),
58-
backgroundColor: (() => {
59-
const score = animatedBrand;
60-
if (score <= 2) return colours.score.low + '20';
61-
if (score <= 3) return colours.score.medium + '20';
62-
return colours.score.high + '20';
63-
})(),
91+
borderColor: strokeColor,
92+
backgroundColor: backgroundColor,
6493
}}
6594
>
6695
<span className="relative inline-block w-12 h-12 align-middle">
6796
<svg width="48" height="48" viewBox="0 0 48 48" className="absolute top-0 left-0" style={{ zIndex: 1 }}>
6897
<circle
6998
cx="24" cy="24" r="18"
7099
fill="none"
71-
stroke={(() => {
72-
const score = animatedBrand;
73-
if (score <= 2) return colours.score.low;
74-
if (score <= 3) return colours.score.medium;
75-
return colours.score.high;
76-
})()}
100+
stroke={strokeColor}
77101
strokeWidth="3"
78102
strokeDasharray={Math.PI * 2 * 18}
79103
strokeDashoffset={Math.PI * 2 * 18 * (1 - (animatedBrand / 5))}
@@ -88,14 +112,7 @@ const BrandTabContent: React.FC<BrandTabContentProps> = ({
88112
<div className="absolute inset-0 flex flex-col items-center justify-center">
89113
<span
90114
className="text-xl font-bold"
91-
style={{
92-
color: (() => {
93-
const score = animatedBrand;
94-
if (score <= 2) return colours.score.low;
95-
if (score <= 3) return colours.score.medium;
96-
return colours.score.high;
97-
})()
98-
}}
115+
style={{ color: strokeColor }}
99116
>
100117
{animatedBrand}
101118
</span>
@@ -126,38 +143,15 @@ const BrandTabContent: React.FC<BrandTabContentProps> = ({
126143
{calculateBrandStats ? (
127144
<div className="w-full">
128145
<div className="space-y-3">
129-
{[
130-
{
131-
label: 'Price',
132-
value: calculateBrandStats.price,
133-
color: '#fef3c7', /* yellow-100 */
134-
svg: '/pound-svgrepo-com.svg'
135-
},
136-
{
137-
label: 'Quality',
138-
value: calculateBrandStats.quality,
139-
color: '#fee2e2', /* red-100 */
140-
svg: '/quality-supervision-svgrepo-com.svg'
141-
},
142-
{
143-
label: 'Nutrition',
144-
value: calculateBrandStats.nutrition,
145-
color: '#dbeafe', /* blue-100 */
146-
svg: '/meal-svgrepo-com.svg'
147-
},
148-
{
149-
label: 'Sustainability',
150-
value: calculateBrandStats.sustainability,
151-
color: '#ecfccb', /* lime-100 */
152-
svg: '/leaf-svgrepo-com.svg'
153-
}
154-
].map((stat, index) => {
155-
const percentage = ((stat.value - 1) / 4) * 100;
146+
{PERFORMANCE_METRICS.map((metric, index) => {
147+
const value = calculateBrandStats[metric.label.toLowerCase() as keyof BrandStats] as number;
148+
const percentage = ((value - 1) / 4) * 100;
149+
156150
return (
157-
<div key={stat.label} className="flex items-center gap-3">
151+
<div key={metric.label} className="flex items-center gap-3">
158152
<Image
159-
src={stat.svg}
160-
alt={stat.label}
153+
src={metric.svg}
154+
alt={metric.label}
161155
width={16}
162156
height={16}
163157
className="flex-shrink-0"
@@ -176,7 +170,7 @@ const BrandTabContent: React.FC<BrandTabContentProps> = ({
176170
className="transition-all duration-1000 ease-out absolute inset-0 rounded-lg opacity-0 animate-fade-in"
177171
style={{
178172
width: `${percentage}%`,
179-
backgroundColor: stat.color,
173+
backgroundColor: metric.color,
180174
border: `2px solid ${colours.card.border}`,
181175
animationDelay: `${0.3 + index * 0.1}s`
182176
}}
@@ -187,7 +181,7 @@ const BrandTabContent: React.FC<BrandTabContentProps> = ({
187181
className="text-xs font-medium w-8 text-right flex-shrink-0"
188182
style={{ color: colours.text.primary }}
189183
>
190-
{stat.value.toFixed(1)}
184+
{value.toFixed(1)}
191185
</span>
192186
</div>
193187
);

src/components/product/tabs/PriceSpectrum.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ const PriceSpectrum: React.FC<PriceSpectrumProps> = ({
9090
const minPrice = Math.max(priceStats.min, 0.01); /* Avoid division by zero */
9191
const maxPrice = Math.max(priceStats.max, productPrice, 0.01);
9292

93+
/* Scale between 5% and 95% (to be pretty) */
9394
const scale = (price: number) => {
94-
if (price === minPrice) return 5; /* Align with left edge of spectrum */
95-
if (price === maxPrice) return 95; /* Align with right edge of spectrum */
96-
97-
/* Scale between 5% and 95% for other values */
95+
if (price === minPrice) return 5;
96+
if (price === maxPrice) return 95;
9897
const range = maxPrice - minPrice;
9998
if (range === 0) return 50; /* If all prices are the same, center it */
99+
100100
return 5 + ((price - minPrice) / range) * 90;
101101
};
102102

src/components/product/tabs/SustainabilityTabContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const SustainabilityTabContent: React.FC<SustainabilityTabContentProps> = ({
4141
const ecoscore = product.ecoInformation?.ecoscore;
4242
const strokeColor = getEcoscoreColor(ecoscore);
4343
const backgroundColor = getEcoscoreBackgroundColor(ecoscore);
44-
const scorePercentage = animatedSustainability / 5; // Use animated value for ring animation
44+
const scorePercentage = animatedSustainability / 5;
4545
const displayScore = formatEcoscoreDisplay(ecoscore);
4646

4747
return (

0 commit comments

Comments
 (0)