-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #966 from WatWowMap/client-distance-unit
refactor: move distance unit to client
- Loading branch information
Showing
13 changed files
with
117 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @ts-check | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { formatDistance } from '@services/functions/formatDistance' | ||
|
||
import { useStorage } from './useStorage' | ||
|
||
/** @returns {(meters: number) => string} */ | ||
export function useFormatDistance() { | ||
const { i18n } = useTranslation() | ||
const unit = useStorage((s) => s.settings.distanceUnit) | ||
|
||
return (meters) => formatDistance(meters, unit, i18n.language) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @ts-check | ||
import { useStorage } from '@hooks/useStorage' | ||
|
||
const METERS_PER_MILE = 1609.344 | ||
|
||
/** @param {number} meters */ | ||
export function formatDistance( | ||
meters, | ||
unit = useStorage.getState().settings.distanceUnit, | ||
locale = localStorage.getItem('i18nextLng') || 'en', | ||
) { | ||
const safe = meters || 0 | ||
const distance = unit === 'miles' ? safe / METERS_PER_MILE : safe / 1000 | ||
|
||
const numFormatter = new Intl.NumberFormat(locale, { | ||
unitDisplay: 'short', | ||
unit: unit.replace(/s$/, ''), | ||
style: 'unit', | ||
maximumFractionDigits: 2, | ||
}) | ||
|
||
return numFormatter.format(distance) | ||
} |