'use client'; import { useMemo } from 'react'; import { type ConversionResult } from '@/lib/units'; import { formatNumber, cn } from '@/lib/utils'; interface VisualComparisonProps { conversions: ConversionResult[]; color: string; } export default function VisualComparison({ conversions, color, }: VisualComparisonProps) { // Calculate percentages for visual bars using logarithmic scale const withPercentages = useMemo(() => { if (conversions.length === 0) return []; // Get all values const values = conversions.map(c => Math.abs(c.value)); const maxValue = Math.max(...values); const minValue = Math.min(...values.filter(v => v > 0)); if (maxValue === 0 || !isFinite(maxValue)) { return conversions.map(c => ({ ...c, percentage: 0 })); } // Use logarithmic scale for better visualization return conversions.map(c => { const absValue = Math.abs(c.value); if (absValue === 0 || !isFinite(absValue)) { return { ...c, percentage: 2 }; // Show minimal bar } // Logarithmic scale const logValue = Math.log10(absValue); const logMax = Math.log10(maxValue); const logMin = minValue > 0 ? Math.log10(minValue) : logMax - 6; // 6 orders of magnitude range const logRange = logMax - logMin; let percentage: number; if (logRange === 0) { percentage = 100; } else { percentage = ((logValue - logMin) / logRange) * 100; // Ensure bars are visible - minimum 3%, maximum 100% percentage = Math.max(3, Math.min(100, percentage)); } return { ...c, percentage, }; }); }, [conversions]); if (conversions.length === 0) { return (