function CurrencyConverter() { const [rates, setRates] = useState({ USD: 1, EUR: 0.92, GBP: 0.79, INR: 83.12, JPY: 149.50, AUD: 1.52, CAD: 1.36, CHF: 0.88, CNY: 7.24 }); const [activeCurrencies, setActiveCurrencies] = useState(['USD', 'EUR', 'GBP', 'INR', 'JPY', 'AUD']); const [values, setValues] = useState({}); const [loading, setLoading] = useState(false); const [lastUpdated, setLastUpdated] = useState(null); const [showAddCurrency, setShowAddCurrency] = useState(false); const allCurrencies = { USD: { name: 'US Dollar', symbol: '$' }, EUR: { name: 'Euro', symbol: '€' }, GBP: { name: 'British Pound', symbol: '£' }, INR: { name: 'Indian Rupee', symbol: '₹' }, JPY: { name: 'Japanese Yen', symbol: '¥' }, AUD: { name: 'Australian Dollar', symbol: '$' }, CAD: { name: 'Canadian Dollar', symbol: '$' }, CHF: { name: 'Swiss Franc', symbol: 'Fr' }, CNY: { name: 'Chinese Yuan', symbol: '¥' }, KRW: { name: 'South Korean Won', symbol: '₩' }, SGD: { name: 'Singapore Dollar', symbol: '$' }, HKD: { name: 'Hong Kong Dollar', symbol: '$' }, NZD: { name: 'New Zealand Dollar', symbol: '$' }, SEK: { name: 'Swedish Krona', symbol: 'kr' }, NOK: { name: 'Norwegian Krone', symbol: 'kr' }, MXN: { name: 'Mexican Peso', symbol: '$' }, BRL: { name: 'Brazilian Real', symbol: 'R$' }, ZAR: { name: 'South African Rand', symbol: 'R' }, RUB: { name: 'Russian Ruble', symbol: '₽' }, TRY: { name: 'Turkish Lira', symbol: '₺' } }; const fetchLiveRates = async () => { setLoading(true); try { // Using exchangerate-api.com free API const response = await fetch('https://api.exchangerate-api.com/v4/latest/USD'); const data = await response.json(); if (data && data.rates) { setRates(data.rates); setLastUpdated(new Date().toLocaleString()); alert('✅ Exchange rates updated successfully!'); } } catch (error) { alert('❌ Failed to fetch live rates. Please check your internet connection.'); } finally { setLoading(false); } }; const convertCurrency = (fromCurrency, value) => { if (!value || value === '') { setValues({}); return; } const amount = parseFloat(value); const newValues = { [fromCurrency]: value }; activeCurrencies.forEach(currency => { if (currency !== fromCurrency) { // Convert from source currency to USD, then to target currency const usdAmount = amount / rates[fromCurrency]; const convertedAmount = usdAmount * rates[currency]; newValues[currency] = convertedAmount.toFixed(2); } }); setValues(newValues); }; const addCurrency = (currency) => { if (!activeCurrencies.includes(currency)) { setActiveCurrencies([...activeCurrencies, currency]); setShowAddCurrency(false); } }; const removeCurrency = (currency) => { if (activeCurrencies.length > 2) { setActiveCurrencies(activeCurrencies.filter(c => c !== currency)); const newValues = { ...values }; delete newValues[currency]; setValues(newValues); } else { alert('You must have at least 2 currencies!'); } }; const availableCurrencies = Object.keys(allCurrencies).filter(c => !activeCurrencies.includes(c)); return (

💱 Currency Converter

{lastUpdated && (
✅ Last updated: {lastUpdated}
)}

Enter amount in any currency for real-time conversion

{activeCurrencies.map(currency => (
{activeCurrencies.length > 2 && ( )}
convertCurrency(currency, e.target.value)} placeholder="0.00" />
))}
{availableCurrencies.length > 0 && (
{!showAddCurrency ? ( ) : (
Select Currency to Add:
{availableCurrencies.map(currency => ( ))}
)}
)}
{lastUpdated ? 'Using live exchange rates. Click "Get Live Rates" to refresh.' : 'Using approximate rates. Click "Get Live Rates" for current market rates.' }
); } // Export to global registry window.CurrencyConverter = CurrencyConverter;