mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 15:11:12 -05:00
Fix unit conversion in filters
This commit is contained in:
@@ -13,6 +13,12 @@ import { FilterSection } from '@/components/filters/FilterSection';
|
|||||||
import { FilterMultiSelectCombobox } from '@/components/filters/FilterMultiSelectCombobox';
|
import { FilterMultiSelectCombobox } from '@/components/filters/FilterMultiSelectCombobox';
|
||||||
import { MultiSelectOption } from '@/components/ui/multi-select-combobox';
|
import { MultiSelectOption } from '@/components/ui/multi-select-combobox';
|
||||||
import { Ride } from '@/types/database';
|
import { Ride } from '@/types/database';
|
||||||
|
import { useUnitPreferences } from '@/hooks/useUnitPreferences';
|
||||||
|
import {
|
||||||
|
convertValueToMetric,
|
||||||
|
convertValueFromMetric,
|
||||||
|
getDisplayUnit
|
||||||
|
} from '@/lib/units';
|
||||||
|
|
||||||
export interface RideFilterState {
|
export interface RideFilterState {
|
||||||
categories: string[];
|
categories: string[];
|
||||||
@@ -79,6 +85,9 @@ interface RideFiltersProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function RideFilters({ filters, onFiltersChange, rides }: RideFiltersProps) {
|
export function RideFilters({ filters, onFiltersChange, rides }: RideFiltersProps) {
|
||||||
|
const { preferences } = useUnitPreferences();
|
||||||
|
const system = preferences.measurement_system;
|
||||||
|
|
||||||
// Fetch unique filter options
|
// Fetch unique filter options
|
||||||
const { data: locations } = useQuery({
|
const { data: locations } = useQuery({
|
||||||
queryKey: ['filter-locations'],
|
queryKey: ['filter-locations'],
|
||||||
@@ -221,6 +230,27 @@ export function RideFilters({ filters, onFiltersChange, rides }: RideFiltersProp
|
|||||||
{ label: 'Other', value: 'other' },
|
{ label: 'Other', value: 'other' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Calculate display ranges based on user preferences
|
||||||
|
const speedUnit = getDisplayUnit('km/h', system);
|
||||||
|
const heightUnit = getDisplayUnit('m', system);
|
||||||
|
const lengthUnit = getDisplayUnit('m', system);
|
||||||
|
|
||||||
|
// Convert metric filter ranges to display units for sliders
|
||||||
|
const displaySpeedMin = Math.round(convertValueFromMetric(0, speedUnit, 'km/h'));
|
||||||
|
const displaySpeedMax = Math.round(convertValueFromMetric(200, speedUnit, 'km/h'));
|
||||||
|
const displayHeightMin = Math.round(convertValueFromMetric(0, heightUnit, 'm'));
|
||||||
|
const displayHeightMax = Math.round(convertValueFromMetric(150, heightUnit, 'm'));
|
||||||
|
const displayLengthMin = Math.round(convertValueFromMetric(0, lengthUnit, 'm'));
|
||||||
|
const displayLengthMax = Math.round(convertValueFromMetric(3000, lengthUnit, 'm'));
|
||||||
|
|
||||||
|
// Convert current metric filter values to display units
|
||||||
|
const currentSpeedMin = Math.round(convertValueFromMetric(filters.minSpeed, speedUnit, 'km/h'));
|
||||||
|
const currentSpeedMax = Math.round(convertValueFromMetric(filters.maxSpeed, speedUnit, 'km/h'));
|
||||||
|
const currentHeightMin = Math.round(convertValueFromMetric(filters.minHeight, heightUnit, 'm'));
|
||||||
|
const currentHeightMax = Math.round(convertValueFromMetric(filters.maxHeight, heightUnit, 'm'));
|
||||||
|
const currentLengthMin = Math.round(convertValueFromMetric(filters.minLength, lengthUnit, 'm'));
|
||||||
|
const currentLengthMax = Math.round(convertValueFromMetric(filters.maxLength, lengthUnit, 'm'));
|
||||||
|
|
||||||
const resetFilters = () => {
|
const resetFilters = () => {
|
||||||
onFiltersChange(defaultRideFilters);
|
onFiltersChange(defaultRideFilters);
|
||||||
};
|
};
|
||||||
@@ -382,30 +412,45 @@ export function RideFilters({ filters, onFiltersChange, rides }: RideFiltersProp
|
|||||||
<div className="grid grid-cols-1 gap-4">
|
<div className="grid grid-cols-1 gap-4">
|
||||||
<FilterRangeSlider
|
<FilterRangeSlider
|
||||||
label="Speed"
|
label="Speed"
|
||||||
value={[filters.minSpeed, filters.maxSpeed]}
|
value={[currentSpeedMin, currentSpeedMax]}
|
||||||
onChange={([min, max]) => onFiltersChange({ ...filters, minSpeed: min, maxSpeed: max })}
|
onChange={([displayMin, displayMax]) => {
|
||||||
min={0}
|
// Convert display values back to metric for storage
|
||||||
max={200}
|
const metricMin = Math.round(convertValueToMetric(displayMin, speedUnit));
|
||||||
step={5}
|
const metricMax = Math.round(convertValueToMetric(displayMax, speedUnit));
|
||||||
unit=" km/h"
|
onFiltersChange({ ...filters, minSpeed: metricMin, maxSpeed: metricMax });
|
||||||
|
}}
|
||||||
|
min={displaySpeedMin}
|
||||||
|
max={displaySpeedMax}
|
||||||
|
step={system === 'metric' ? 5 : 2}
|
||||||
|
formatValue={(v) => `${v} ${speedUnit}`}
|
||||||
/>
|
/>
|
||||||
<FilterRangeSlider
|
<FilterRangeSlider
|
||||||
label="Height"
|
label="Height"
|
||||||
value={[filters.minHeight, filters.maxHeight]}
|
value={[currentHeightMin, currentHeightMax]}
|
||||||
onChange={([min, max]) => onFiltersChange({ ...filters, minHeight: min, maxHeight: max })}
|
onChange={([displayMin, displayMax]) => {
|
||||||
min={0}
|
// Convert display values back to metric for storage
|
||||||
max={150}
|
const metricMin = Math.round(convertValueToMetric(displayMin, heightUnit));
|
||||||
step={5}
|
const metricMax = Math.round(convertValueToMetric(displayMax, heightUnit));
|
||||||
unit=" m"
|
onFiltersChange({ ...filters, minHeight: metricMin, maxHeight: metricMax });
|
||||||
|
}}
|
||||||
|
min={displayHeightMin}
|
||||||
|
max={displayHeightMax}
|
||||||
|
step={system === 'metric' ? 5 : 10}
|
||||||
|
formatValue={(v) => `${v} ${heightUnit}`}
|
||||||
/>
|
/>
|
||||||
<FilterRangeSlider
|
<FilterRangeSlider
|
||||||
label="Length"
|
label="Length"
|
||||||
value={[filters.minLength, filters.maxLength]}
|
value={[currentLengthMin, currentLengthMax]}
|
||||||
onChange={([min, max]) => onFiltersChange({ ...filters, minLength: min, maxLength: max })}
|
onChange={([displayMin, displayMax]) => {
|
||||||
min={0}
|
// Convert display values back to metric for storage
|
||||||
max={3000}
|
const metricMin = Math.round(convertValueToMetric(displayMin, lengthUnit));
|
||||||
step={50}
|
const metricMax = Math.round(convertValueToMetric(displayMax, lengthUnit));
|
||||||
unit=" m"
|
onFiltersChange({ ...filters, minLength: metricMin, maxLength: metricMax });
|
||||||
|
}}
|
||||||
|
min={displayLengthMin}
|
||||||
|
max={displayLengthMax}
|
||||||
|
step={system === 'metric' ? 50 : 100}
|
||||||
|
formatValue={(v) => `${v} ${lengthUnit}`}
|
||||||
/>
|
/>
|
||||||
<FilterRangeSlider
|
<FilterRangeSlider
|
||||||
label="Inversions"
|
label="Inversions"
|
||||||
|
|||||||
Reference in New Issue
Block a user