import { useState, useMemo } from 'react'; interface SavingsCalculatorProps { actualCarbon: number; actualMiles: number; produceWeightKg: number; conventionalData?: ConventionalBaseline; } export interface ConventionalBaseline { avgCarbonPerKg: number; // Default: 2.5 kg CO2 per kg produce avgMilesPerKg: number; // Default: 2400 km average food miles avgWaterLitersPerKg: number; // Default: 500 liters per kg avgFoodWastePercent: number; // Default: 30% waste } const DEFAULT_CONVENTIONAL: ConventionalBaseline = { avgCarbonPerKg: 2.5, avgMilesPerKg: 2400, avgWaterLitersPerKg: 500, avgFoodWastePercent: 30, }; const EQUIVALENTS = { carbonPerTree: 21, // kg CO2 absorbed per tree per year carbonPerCarMile: 0.404, // kg CO2 per mile driven carbonPerFlight: 250, // kg CO2 per hour of flight waterPerShower: 75, // liters per 5-min shower carbonPerGallon: 8.89, // kg CO2 per gallon of gasoline }; export default function SavingsCalculator({ actualCarbon, actualMiles, produceWeightKg, conventionalData = DEFAULT_CONVENTIONAL, }: SavingsCalculatorProps) { const [showDetails, setShowDetails] = useState(false); // Calculate conventional equivalents const conventionalCarbon = produceWeightKg * conventionalData.avgCarbonPerKg; const conventionalMiles = produceWeightKg * conventionalData.avgMilesPerKg; const conventionalWater = produceWeightKg * conventionalData.avgWaterLitersPerKg; const conventionalWaste = produceWeightKg * (conventionalData.avgFoodWastePercent / 100); // Calculate savings const carbonSaved = conventionalCarbon - actualCarbon; const milesSaved = conventionalMiles - actualMiles; const wasteSaved = conventionalWaste * 0.75; // Assume 75% less waste with local // Calculate percentage reductions const carbonReduction = conventionalCarbon > 0 ? (carbonSaved / conventionalCarbon) * 100 : 0; const milesReduction = conventionalMiles > 0 ? (milesSaved / conventionalMiles) * 100 : 0; // Convert to tangible equivalents const equivalents = useMemo(() => ({ treesEquivalent: carbonSaved / EQUIVALENTS.carbonPerTree, carMilesEquivalent: carbonSaved / EQUIVALENTS.carbonPerCarMile, flightHoursEquivalent: carbonSaved / EQUIVALENTS.carbonPerFlight, showersEquivalent: (conventionalWater - conventionalWater * 0.1) / EQUIVALENTS.waterPerShower, gallonsGasoline: carbonSaved / EQUIVALENTS.carbonPerGallon, }), [carbonSaved, conventionalWater]); // Calculate annual projections (assuming monthly data) const annualMultiplier = 12; const annualCarbonSaved = carbonSaved * annualMultiplier; const annualMilesSaved = milesSaved * annualMultiplier; // Rating based on carbon reduction const getRating = (reduction: number): { stars: number; label: string; color: string } => { if (reduction >= 90) return { stars: 5, label: 'Outstanding', color: 'text-green-600' }; if (reduction >= 75) return { stars: 4, label: 'Excellent', color: 'text-emerald-600' }; if (reduction >= 50) return { stars: 3, label: 'Great', color: 'text-lime-600' }; if (reduction >= 25) return { stars: 2, label: 'Good', color: 'text-yellow-600' }; return { stars: 1, label: 'Getting Started', color: 'text-orange-600' }; }; const rating = getRating(carbonReduction); return (
See your environmental impact vs conventional
{rating.label}
You've reduced carbon by {carbonReduction.toFixed(0)}% compared to conventional!
{carbonSaved.toFixed(1)}
kg CO2 Saved
{carbonReduction.toFixed(0)}% reduction
{milesSaved.toFixed(0)}
km Saved
{milesReduction.toFixed(0)}% reduction
| Metric | Your Impact | Conventional | Saved |
|---|---|---|---|
| Carbon Footprint | {actualCarbon.toFixed(2)} kg | {conventionalCarbon.toFixed(2)} kg | -{carbonSaved.toFixed(2)} kg |
| Food Miles | {actualMiles.toFixed(0)} km | {conventionalMiles.toFixed(0)} km | -{milesSaved.toFixed(0)} km |
| Est. Food Waste | {(conventionalWaste * 0.25).toFixed(2)} kg | {conventionalWaste.toFixed(2)} kg | -{wasteSaved.toFixed(2)} kg |
{equivalents.treesEquivalent.toFixed(1)}
Trees planted (annual equivalent)
{equivalents.carMilesEquivalent.toFixed(0)}
Car miles avoided
{equivalents.flightHoursEquivalent.toFixed(1)}
Flight hours equivalent
{equivalents.showersEquivalent.toFixed(0)}
Showers worth of water
{equivalents.gallonsGasoline.toFixed(1)}
Gallons of gas saved
{carbonReduction.toFixed(0)}%
Better for the planet
{annualCarbonSaved.toFixed(0)} kg CO2
Carbon saved per year
{annualMilesSaved.toFixed(0)} km
Food miles saved per year
Based on current consumption of {produceWeightKg.toFixed(1)} kg produce
Keep sourcing local produce to maximize your environmental impact!
Every kilometer saved makes a difference