import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { RideTechnicalSpec } from "@/types/database"; import { Wrench } from "lucide-react"; import { useUnitPreferences } from "@/hooks/useUnitPreferences"; import { convertValueFromMetric, detectUnitType, getMetricUnit, getDisplayUnit } from "@/lib/units"; interface TechnicalSpecificationsProps { specifications?: RideTechnicalSpec[]; } export const TechnicalSpecifications = ({ specifications }: TechnicalSpecificationsProps) => { const { preferences } = useUnitPreferences(); if (!specifications || specifications.length === 0) { return null; } const getDisplayValue = (spec: RideTechnicalSpec) => { // If no unit, return as-is if (!spec.unit) { return spec.spec_value; } const unitType = detectUnitType(spec.unit); if (unitType === 'unknown') { return `${spec.spec_value} ${spec.unit}`; } const numericValue = parseFloat(spec.spec_value); if (isNaN(numericValue)) { return spec.spec_value; } // spec.unit is the metric unit stored in DB (e.g., "km/h") // Get the target display unit based on user preference (e.g., "mph" for imperial) const displayUnit = getDisplayUnit(spec.unit, preferences.measurement_system); // Convert from metric (spec.unit) to display unit const convertedValue = convertValueFromMetric( numericValue, displayUnit, // Target unit (mph, ft, in) spec.unit // Source metric unit (km/h, m, cm) ); return `${convertedValue.toLocaleString()} ${displayUnit}`; }; // Group specs by category const groupedSpecs = specifications.reduce((acc, spec) => { const category = spec.category || 'General'; if (!acc[category]) { acc[category] = []; } acc[category].push(spec); return acc; }, {} as Record); return ( Technical Specifications
{Object.entries(groupedSpecs).map(([category, specs]) => (

{category}

{specs .sort((a, b) => a.display_order - b.display_order) .map((spec) => (
{spec.spec_name} {getDisplayValue(spec)}
))}
))}
); };