Files
gpx.studio/website/src/lib/components/WithUnits.svelte

57 lines
1.9 KiB
Svelte
Raw Normal View History

2024-04-24 16:12:50 +02:00
<script lang="ts">
import {
celsiusToFahrenheit,
getConvertedDistance,
getConvertedElevation,
getConvertedVelocity,
getDistanceUnits,
getElevationUnits,
getVelocityUnits,
secondsToHHMMSS,
} from '$lib/units';
2025-06-21 21:07:36 +02:00
import { i18n } from '$lib/i18n.svelte';
2025-10-17 23:54:45 +02:00
import { settings } from '$lib/logic/settings';
2024-04-24 16:12:50 +02:00
2025-06-21 21:07:36 +02:00
let {
value,
type,
showUnits = true,
decimals = undefined,
class: className = '',
}: {
value: number;
type: 'distance' | 'elevation' | 'speed' | 'temperature' | 'time';
showUnits?: boolean;
decimals?: number;
class?: string;
} = $props();
2024-05-04 15:10:30 +02:00
const { distanceUnits, velocityUnits, temperatureUnits } = settings;
2024-04-24 16:12:50 +02:00
</script>
2025-06-21 21:07:36 +02:00
<span class={className}>
{#if type === 'distance'}
{getConvertedDistance(value, $distanceUnits).toFixed(decimals ?? 2)}
{showUnits ? getDistanceUnits($distanceUnits) : ''}
{:else if type === 'elevation'}
{getConvertedElevation(value, $distanceUnits).toFixed(decimals ?? 0)}
{showUnits ? getElevationUnits($distanceUnits) : ''}
{:else if type === 'speed'}
{#if $velocityUnits === 'speed'}
{getConvertedVelocity(value, $velocityUnits, $distanceUnits).toFixed(decimals ?? 2)}
{showUnits ? getVelocityUnits($velocityUnits, $distanceUnits) : ''}
{:else}
{secondsToHHMMSS(getConvertedVelocity(value, $velocityUnits, $distanceUnits))}
{showUnits ? getVelocityUnits($velocityUnits, $distanceUnits) : ''}
{/if}
{:else if type === 'temperature'}
{#if $temperatureUnits === 'celsius'}
2025-06-21 21:07:36 +02:00
{value} {showUnits ? i18n._('units.celsius') : ''}
{:else}
2025-06-21 21:07:36 +02:00
{celsiusToFahrenheit(value)} {showUnits ? i18n._('units.fahrenheit') : ''}
{/if}
{:else if type === 'time'}
{secondsToHHMMSS(value)}
{/if}
2024-05-13 19:43:10 +02:00
</span>