'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 []; // Filter out zero or negative values for log scale const validConversions = conversions.filter(c => c.value > 0); if (validConversions.length === 0) { return conversions.map(c => ({ ...c, percentage: 0 })); } // Use logarithmic scale for better visualization across different magnitudes const logValues = validConversions.map(c => Math.log10(c.value)); const minLog = Math.min(...logValues); const maxLog = Math.max(...logValues); const logRange = maxLog - minLog; return conversions.map(c => { if (c.value <= 0) return { ...c, percentage: 0 }; const logValue = Math.log10(c.value); // Normalize to 0-100 range, with minimum bar width of 5% const percentage = logRange === 0 ? 100 : Math.max(5, ((logValue - minLog) / logRange) * 100); return { ...c, percentage, }; }); }, [conversions]); if (conversions.length === 0) { return (
Enter a value to see conversions
); } return (
{withPercentages.map(item => (
{item.unitInfo.plural} {formatNumber(item.value)} {item.unit}
{/* Percentage indicator */}
50 ? 'white' : 'inherit', }} > {item.percentage < 100 && ( 50 ? 'text-white' : 'text-muted-foreground'}> {Math.round(item.percentage)}% )}
))}
); }