-
-
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.
refactor: move distance unit to client
- Loading branch information
1 parent
ca08a69
commit 40a5c3c
Showing
13 changed files
with
116 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,22 @@ | ||
// @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 distance = unit === 'miles' ? meters / METERS_PER_MILE : meters / 1000 | ||
|
||
const numFormatter = new Intl.NumberFormat(locale, { | ||
unitDisplay: 'short', | ||
unit: unit.replace(/s$/, ''), | ||
style: 'unit', | ||
maximumFractionDigits: 2, | ||
}) | ||
|
||
return numFormatter.format(distance) | ||
} |