mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 03:31:13 -05:00
Fix display conversion logic
This commit is contained in:
@@ -2,7 +2,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|||||||
import { RideCoasterStat } from "@/types/database";
|
import { RideCoasterStat } from "@/types/database";
|
||||||
import { TrendingUp } from "lucide-react";
|
import { TrendingUp } from "lucide-react";
|
||||||
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
|
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
|
||||||
import { convertValueFromMetric, detectUnitType, getMetricUnit } from "@/lib/units";
|
import { convertValueFromMetric, detectUnitType, getMetricUnit, getDisplayUnit } from "@/lib/units";
|
||||||
|
|
||||||
interface CoasterStatisticsProps {
|
interface CoasterStatisticsProps {
|
||||||
statistics?: RideCoasterStat[];
|
statistics?: RideCoasterStat[];
|
||||||
@@ -25,18 +25,18 @@ export const CoasterStatistics = ({ statistics }: CoasterStatisticsProps) => {
|
|||||||
return `${stat.stat_value.toLocaleString()} ${stat.unit}`;
|
return `${stat.stat_value.toLocaleString()} ${stat.unit}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const metricUnit = getMetricUnit(stat.unit);
|
// stat.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(stat.unit, preferences.measurement_system);
|
||||||
|
|
||||||
|
// Convert from metric (stat.unit) to display unit
|
||||||
const convertedValue = convertValueFromMetric(
|
const convertedValue = convertValueFromMetric(
|
||||||
stat.stat_value,
|
stat.stat_value,
|
||||||
stat.unit,
|
displayUnit, // Target unit (mph, ft, in)
|
||||||
metricUnit
|
stat.unit // Source metric unit (km/h, m, cm)
|
||||||
);
|
);
|
||||||
|
|
||||||
const targetUnit = preferences.measurement_system === 'metric'
|
return `${convertedValue.toLocaleString()} ${displayUnit}`;
|
||||||
? metricUnit
|
|
||||||
: stat.unit;
|
|
||||||
|
|
||||||
return `${convertedValue.toLocaleString()} ${targetUnit}`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Group stats by category
|
// Group stats by category
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|||||||
import { RideTechnicalSpec } from "@/types/database";
|
import { RideTechnicalSpec } from "@/types/database";
|
||||||
import { Wrench } from "lucide-react";
|
import { Wrench } from "lucide-react";
|
||||||
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
|
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
|
||||||
import { convertValueFromMetric, detectUnitType, getMetricUnit } from "@/lib/units";
|
import { convertValueFromMetric, detectUnitType, getMetricUnit, getDisplayUnit } from "@/lib/units";
|
||||||
|
|
||||||
interface TechnicalSpecificationsProps {
|
interface TechnicalSpecificationsProps {
|
||||||
specifications?: RideTechnicalSpec[];
|
specifications?: RideTechnicalSpec[];
|
||||||
@@ -26,24 +26,24 @@ export const TechnicalSpecifications = ({ specifications }: TechnicalSpecificati
|
|||||||
return `${spec.spec_value} ${spec.unit}`;
|
return `${spec.spec_value} ${spec.unit}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const metricUnit = getMetricUnit(spec.unit);
|
|
||||||
const numericValue = parseFloat(spec.spec_value);
|
const numericValue = parseFloat(spec.spec_value);
|
||||||
|
|
||||||
if (isNaN(numericValue)) {
|
if (isNaN(numericValue)) {
|
||||||
return spec.spec_value;
|
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(
|
const convertedValue = convertValueFromMetric(
|
||||||
numericValue,
|
numericValue,
|
||||||
spec.unit,
|
displayUnit, // Target unit (mph, ft, in)
|
||||||
metricUnit
|
spec.unit // Source metric unit (km/h, m, cm)
|
||||||
);
|
);
|
||||||
|
|
||||||
const targetUnit = preferences.measurement_system === 'metric'
|
return `${convertedValue.toLocaleString()} ${displayUnit}`;
|
||||||
? metricUnit
|
|
||||||
: spec.unit;
|
|
||||||
|
|
||||||
return `${convertedValue.toLocaleString()} ${targetUnit}`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Group specs by category
|
// Group specs by category
|
||||||
|
|||||||
Reference in New Issue
Block a user