import React, { useState, useMemo } from 'react'; import { BeakerIcon } from '../components/icons'; type WaterUnit = 'L' | 'gal'; type ConcentrateUnit = 'ml' | 'tsp' | 'tbsp'; type Strength = 'full' | 'half' | 'quarter'; const STRENGTH_MULTIPLIERS: Record = { full: 1, half: 0.5, quarter: 0.25, }; const CONVERSION_TO_ML = { ml: 1, tsp: 4.92892, tbsp: 14.7868, }; const CONVERSION_TO_LITER = { L: 1, gal: 3.78541, }; const FertilizerMixerView: React.FC = () => { const [waterAmount, setWaterAmount] = useState(1); const [waterUnit, setWaterUnit] = useState('gal'); const [concentrateAmount, setConcentrateAmount] = useState(1); const [concentrateUnit, setConcentrateUnit] = useState('tsp'); const [strength, setStrength] = useState('half'); const [baseWaterAmount, setBaseWaterAmount] = useState(1); const [baseWaterUnit, setBaseWaterUnit] = useState('gal'); const result = useMemo(() => { try { const desiredWaterL = waterAmount * CONVERSION_TO_LITER[waterUnit]; const baseWaterL = baseWaterAmount * CONVERSION_TO_LITER[baseWaterUnit]; const baseConcentrateMl = concentrateAmount * CONVERSION_TO_ML[concentrateUnit]; if (baseWaterL === 0) return { amount: 0, unit: 'ml' }; const ratio = baseConcentrateMl / baseWaterL; // ml of concentrate per L of water const strengthMultiplier = STRENGTH_MULTIPLIERS[strength]; const neededMl = desiredWaterL * ratio * strengthMultiplier; // Return result in a user-friendly unit if (neededMl < 15 && concentrateUnit !== 'ml') return { amount: neededMl / CONVERSION_TO_ML.tsp, unit: 'tsp' }; if (neededMl < 45 && concentrateUnit !== 'ml') return { amount: neededMl / CONVERSION_TO_ML.tbsp, unit: 'tbsp' }; return { amount: neededMl, unit: 'ml' }; } catch (e) { return { amount: 0, unit: 'ml' }; } }, [waterAmount, waterUnit, concentrateAmount, concentrateUnit, strength, baseWaterAmount, baseWaterUnit]); const renderUnitSelector = (value: string, setter: (val: any) => void, units: string[]) => (
{units.map(unit => ( ))}
); return (

Fertilizer Mixer

Calculate the perfect fertilizer dilution every time. No more over or under-feeding.

1. Dosage from Bottle

Enter the recommended full-strength dose from the fertilizer label.

setConcentrateAmount(Number(e.target.value))} className="w-full p-2 border rounded-md" /> {renderUnitSelector(concentrateUnit, setConcentrateUnit, ['ml', 'tsp', 'tbsp'])}

per

setBaseWaterAmount(Number(e.target.value))} className="w-full p-2 border rounded-md" /> {renderUnitSelector(baseWaterUnit, setBaseWaterUnit, ['L', 'gal'])}

2. Your Watering Can

How much water are you preparing?

setWaterAmount(Number(e.target.value))} className="w-full p-2 border rounded-md" /> {renderUnitSelector(waterUnit, setWaterUnit, ['L', 'gal'])}

3. Desired Strength

Your Custom Mix

Add this much concentrate to your watering can:

{result.amount.toFixed(2)}

{result.unit}

This creates a {strength}-strength solution of {waterAmount} {waterUnit} of fertilizer.

); }; export default FertilizerMixerView;