mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 14:11:13 -05:00
Fix unit conversion logic
This commit is contained in:
@@ -10,7 +10,8 @@ import {
|
|||||||
convertValueToMetric,
|
convertValueToMetric,
|
||||||
convertValueFromMetric,
|
convertValueFromMetric,
|
||||||
detectUnitType,
|
detectUnitType,
|
||||||
getMetricUnit
|
getMetricUnit,
|
||||||
|
getDisplayUnit
|
||||||
} from "@/lib/units";
|
} from "@/lib/units";
|
||||||
|
|
||||||
interface CoasterStat {
|
interface CoasterStat {
|
||||||
@@ -86,7 +87,7 @@ export function CoasterStatsEditor({
|
|||||||
onChange(newStats);
|
onChange(newStats);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get display value (convert from metric if needed)
|
// Get display value (convert from metric to user's preferred units)
|
||||||
const getDisplayValue = (stat: CoasterStat): string => {
|
const getDisplayValue = (stat: CoasterStat): string => {
|
||||||
if (!stat.stat_value || !stat.unit) return String(stat.stat_value || '');
|
if (!stat.stat_value || !stat.unit) return String(stat.stat_value || '');
|
||||||
|
|
||||||
@@ -96,9 +97,12 @@ export function CoasterStatsEditor({
|
|||||||
const unitType = detectUnitType(stat.unit);
|
const unitType = detectUnitType(stat.unit);
|
||||||
if (unitType === 'unknown') return String(stat.stat_value);
|
if (unitType === 'unknown') return String(stat.stat_value);
|
||||||
|
|
||||||
// Assume stored value is in metric, convert to user's preferred units
|
// stat.unit is the metric unit (e.g., "km/h")
|
||||||
const metricUnit = getMetricUnit(stat.unit);
|
// Get the display unit based on user preference (e.g., "mph" for imperial)
|
||||||
const displayValue = convertValueFromMetric(numValue, stat.unit, metricUnit);
|
const displayUnit = getDisplayUnit(stat.unit, preferences.measurement_system);
|
||||||
|
|
||||||
|
// Convert from metric to display unit
|
||||||
|
const displayValue = convertValueFromMetric(numValue, displayUnit, stat.unit);
|
||||||
|
|
||||||
return String(displayValue);
|
return String(displayValue);
|
||||||
};
|
};
|
||||||
@@ -157,24 +161,25 @@ export function CoasterStatsEditor({
|
|||||||
value={getDisplayValue(stat)}
|
value={getDisplayValue(stat)}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const inputValue = e.target.value;
|
const inputValue = e.target.value;
|
||||||
// If unit is recognized, convert to metric for storage
|
|
||||||
if (stat.unit) {
|
|
||||||
const numValue = parseFloat(inputValue);
|
const numValue = parseFloat(inputValue);
|
||||||
if (!isNaN(numValue)) {
|
|
||||||
const metricValue = convertValueToMetric(numValue, stat.unit);
|
if (!isNaN(numValue) && stat.unit) {
|
||||||
|
// Determine what unit the user is entering (based on their preference)
|
||||||
|
const displayUnit = getDisplayUnit(stat.unit, preferences.measurement_system);
|
||||||
|
// Convert from user's input unit to metric for storage
|
||||||
|
const metricValue = convertValueToMetric(numValue, displayUnit);
|
||||||
updateStat(index, 'stat_value', metricValue);
|
updateStat(index, 'stat_value', metricValue);
|
||||||
} else {
|
} else {
|
||||||
updateStat(index, 'stat_value', 0);
|
updateStat(index, 'stat_value', numValue || 0);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
updateStat(index, 'stat_value', parseFloat(inputValue) || 0);
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
placeholder="0"
|
placeholder="0"
|
||||||
/>
|
/>
|
||||||
|
{stat.unit && detectUnitType(stat.unit) !== 'unknown' && (
|
||||||
<p className="text-xs text-muted-foreground mt-1">
|
<p className="text-xs text-muted-foreground mt-1">
|
||||||
Using {preferences.measurement_system} units
|
Enter in {getDisplayUnit(stat.unit, preferences.measurement_system)}
|
||||||
</p>
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<Label className="text-xs">Unit</Label>
|
<Label className="text-xs">Unit</Label>
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import {
|
|||||||
convertValueToMetric,
|
convertValueToMetric,
|
||||||
convertValueFromMetric,
|
convertValueFromMetric,
|
||||||
detectUnitType,
|
detectUnitType,
|
||||||
getMetricUnit
|
getMetricUnit,
|
||||||
|
getDisplayUnit
|
||||||
} from "@/lib/units";
|
} from "@/lib/units";
|
||||||
|
|
||||||
interface TechnicalSpec {
|
interface TechnicalSpec {
|
||||||
@@ -65,7 +66,7 @@ export function TechnicalSpecsEditor({
|
|||||||
onChange(newSpecs);
|
onChange(newSpecs);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get display value (convert from metric if needed)
|
// Get display value (convert from metric to user's preferred units)
|
||||||
const getDisplayValue = (spec: TechnicalSpec): string => {
|
const getDisplayValue = (spec: TechnicalSpec): string => {
|
||||||
if (!spec.spec_value || !spec.unit || spec.spec_type !== 'number') return spec.spec_value;
|
if (!spec.spec_value || !spec.unit || spec.spec_type !== 'number') return spec.spec_value;
|
||||||
|
|
||||||
@@ -75,9 +76,12 @@ export function TechnicalSpecsEditor({
|
|||||||
const unitType = detectUnitType(spec.unit);
|
const unitType = detectUnitType(spec.unit);
|
||||||
if (unitType === 'unknown') return spec.spec_value;
|
if (unitType === 'unknown') return spec.spec_value;
|
||||||
|
|
||||||
// Assume stored value is in metric, convert to user's preferred units
|
// spec.unit is the metric unit (e.g., "km/h")
|
||||||
const metricUnit = getMetricUnit(spec.unit);
|
// Get the display unit based on user preference (e.g., "mph" for imperial)
|
||||||
const displayValue = convertValueFromMetric(numValue, spec.unit, metricUnit);
|
const displayUnit = getDisplayUnit(spec.unit, preferences.measurement_system);
|
||||||
|
|
||||||
|
// Convert from metric to display unit
|
||||||
|
const displayValue = convertValueFromMetric(numValue, displayUnit, spec.unit);
|
||||||
|
|
||||||
return String(displayValue);
|
return String(displayValue);
|
||||||
};
|
};
|
||||||
@@ -135,25 +139,25 @@ export function TechnicalSpecsEditor({
|
|||||||
value={getDisplayValue(spec)}
|
value={getDisplayValue(spec)}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const inputValue = e.target.value;
|
const inputValue = e.target.value;
|
||||||
// If type is number and unit is recognized, convert to metric for storage
|
|
||||||
if (spec.spec_type === 'number' && spec.unit) {
|
|
||||||
const numValue = parseFloat(inputValue);
|
const numValue = parseFloat(inputValue);
|
||||||
if (!isNaN(numValue)) {
|
|
||||||
const metricValue = convertValueToMetric(numValue, spec.unit);
|
// If type is number and unit is recognized, convert to metric for storage
|
||||||
|
if (spec.spec_type === 'number' && spec.unit && !isNaN(numValue)) {
|
||||||
|
// Determine what unit the user is entering (based on their preference)
|
||||||
|
const displayUnit = getDisplayUnit(spec.unit, preferences.measurement_system);
|
||||||
|
// Convert from user's input unit to metric for storage
|
||||||
|
const metricValue = convertValueToMetric(numValue, displayUnit);
|
||||||
updateSpec(index, 'spec_value', String(metricValue));
|
updateSpec(index, 'spec_value', String(metricValue));
|
||||||
} else {
|
} else {
|
||||||
updateSpec(index, 'spec_value', inputValue);
|
updateSpec(index, 'spec_value', inputValue);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
updateSpec(index, 'spec_value', inputValue);
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
placeholder="Value"
|
placeholder="Value"
|
||||||
type={spec.spec_type === 'number' ? 'number' : 'text'}
|
type={spec.spec_type === 'number' ? 'number' : 'text'}
|
||||||
/>
|
/>
|
||||||
{spec.spec_type === 'number' && spec.unit && (
|
{spec.spec_type === 'number' && spec.unit && detectUnitType(spec.unit) !== 'unknown' && (
|
||||||
<p className="text-xs text-muted-foreground mt-1">
|
<p className="text-xs text-muted-foreground mt-1">
|
||||||
Using {preferences.measurement_system} units
|
Enter in {getDisplayUnit(spec.unit, preferences.measurement_system)}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -205,3 +205,21 @@ export function getMetricUnit(unit: string): string {
|
|||||||
return unit;
|
return unit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get display unit based on unit type and measurement system
|
||||||
|
export function getDisplayUnit(metricUnit: string, system: MeasurementSystem): string {
|
||||||
|
const unitType = detectUnitType(metricUnit);
|
||||||
|
|
||||||
|
switch (unitType) {
|
||||||
|
case 'speed':
|
||||||
|
return system === 'imperial' ? 'mph' : 'km/h';
|
||||||
|
case 'distance':
|
||||||
|
return system === 'imperial' ? 'ft' : 'm';
|
||||||
|
case 'height':
|
||||||
|
return system === 'imperial' ? 'in' : 'cm';
|
||||||
|
case 'weight':
|
||||||
|
return system === 'imperial' ? 'lbs' : 'kg';
|
||||||
|
default:
|
||||||
|
return metricUnit;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user