/** * @ai-summary TCO (Total Cost of Ownership) display component * Right-justified display showing lifetime cost and cost per mile/km */ import React, { useEffect, useState } from 'react'; import { TCOResponse } from '../types/vehicles.types'; import { vehiclesApi } from '../api/vehicles.api'; interface TCODisplayProps { vehicleId: string; tcoEnabled?: boolean; } // Currency symbol mapping const CURRENCY_SYMBOLS: Record = { USD: '$', EUR: '€', GBP: '£', CAD: 'CA$', AUD: 'A$', }; export const TCODisplay: React.FC = ({ vehicleId, tcoEnabled }) => { const [tco, setTco] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!tcoEnabled) { setTco(null); return; } const fetchTCO = async () => { setIsLoading(true); setError(null); try { const data = await vehiclesApi.getTCO(vehicleId); setTco(data); } catch (err: any) { console.error('Failed to fetch TCO:', err); setError('Unable to load TCO data'); } finally { setIsLoading(false); } }; fetchTCO(); }, [vehicleId, tcoEnabled]); // Don't render if TCO is disabled if (!tcoEnabled) { return null; } // Loading state if (isLoading) { return (
); } // Error state if (error) { return (
{error}
); } // No data if (!tco) { return null; } const currencySymbol = CURRENCY_SYMBOLS[tco.currencyCode] || tco.currencyCode; // Format currency with proper separators const formatCurrency = (value: number): string => { return value.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, }); }; return (
{currencySymbol}{formatCurrency(tco.lifetimeTotal)}
Lifetime Total
{tco.costPerDistance > 0 && ( <>
{currencySymbol}{formatCurrency(tco.costPerDistance)}/{tco.distanceUnit}
Cost per {tco.distanceUnit}
)} {/* Cost breakdown tooltip/details */}
{tco.purchasePrice > 0 && (
Purchase: {currencySymbol}{formatCurrency(tco.purchasePrice)}
)} {tco.insuranceCosts > 0 && (
Insurance: {currencySymbol}{formatCurrency(tco.insuranceCosts)}
)} {tco.registrationCosts > 0 && (
Registration: {currencySymbol}{formatCurrency(tco.registrationCosts)}
)} {tco.taxCosts > 0 && (
Tax: {currencySymbol}{formatCurrency(tco.taxCosts)}
)} {tco.otherCosts > 0 && (
Other: {currencySymbol}{formatCurrency(tco.otherCosts)}
)} {tco.fuelCosts > 0 && (
Fuel: {currencySymbol}{formatCurrency(tco.fuelCosts)}
)} {tco.maintenanceCosts > 0 && (
Maintenance: {currencySymbol}{formatCurrency(tco.maintenanceCosts)}
)}
); };