feature: metric imperial choice#1867
Conversation
…l for conversion. Uses the conversion in some places.
ErikSin
left a comment
There was a problem hiding this comment.
Overall looks good, there are just 3 small but blocking comments:
- The functions should not be dealing with the decimal conversions
- I don't think m/s should be converted to mph
- using 0 in your functions to get the unit is awkward.
| export const UnitSystemStoreContext = createContext<UnitSystemStore | null>( | ||
| null, | ||
| ); | ||
| export const UnitSystemStoreProvider = UnitSystemStoreContext.Provider; |
There was a problem hiding this comment.
Provider will be deprecated in the next release, so lets start NOT using it.
| export function metersPerSecondOrConversion( | ||
| mps: number, | ||
| unitSystem: UnitSystem, | ||
| ): {value: string; unit: string} { | ||
| if (unitSystem === 'imperial') { | ||
| return {value: (mps * MS_TO_MPH).toFixed(2), unit: 'mph'}; | ||
| } | ||
| return {value: mps.toFixed(2), unit: 'm/s'}; | ||
| } |
There was a problem hiding this comment.
Why are we converting meter per second into miles per hour? In terms of scale, meters and miles are quite different. Wouldn't ft/s be the more appropriate conversion here?
| meters: number, | ||
| unitSystem: UnitSystem, | ||
| decimals = 0, | ||
| ): {value: string; unit: string} { |
There was a problem hiding this comment.
We should strongly type the unit for all of these since string is too general. Currently there is no way of typescript to know whether it is feet, meters, mph, mps....etc since it can be ANY string.
type Unit = "ft" | "m"
export function metersOrConversion(...params): {value: string; unit: Unit}(....rest)| <SelectOne | ||
| value={unitSystem} | ||
| options={options} | ||
| onChange={selected => { | ||
| setUnitSystem(selected); | ||
| }} | ||
| /> |
There was a problem hiding this comment.
Can you actually get rid of this SelectOne in this file?. This was migrated over from Mapeo, and its an unnecessary abstraction. I think we should just copy and paste the code from Select One twice - one for each unit.
| export function kmOrConversion( | ||
| km: number, | ||
| unitSystem: UnitSystem, | ||
| ): {value: string; unit: string} { |
There was a problem hiding this comment.
same comment about strongly typing unit here
| export function metersPerSecondOrConversion( | ||
| mps: number, | ||
| unitSystem: UnitSystem, | ||
| ): {value: string; unit: string} { |
There was a problem hiding this comment.
same comment about strongly typing unit here
There was a problem hiding this comment.
I think all these functions should NOT deal with the decimal places. the amount of decimal places can be dealt with directly with the consuming components. I know it makes the code a little more verbose, but simpler functions that do less things are better for testing purposes and avoids subtle bugs down the line
|
|
||
| const manualLocation = metadata?.manualLocation; | ||
| const lengthUnit = metersOrConversion(0, unitSystem).unit; | ||
| const speedUnit = metersPerSecondOrConversion(0, unitSystem).unit; |
There was a problem hiding this comment.
this is a bit akward, calling the same function with the dummy value 0.
Maybe just create 2 helper functions in unitConversion.ts
export function getMetersUnit(unitSystem: UnitSystem): Unit {
return unitSystem === 'imperial' ? 'ft' : 'm';
}
export function getSpeedUnit(unitSystem: UnitSystem): Unit{
return unitSystem === 'imperial' ? 'mph' : 'm/s';
}| ? (() => { | ||
| const {value, unit} = kmOrConversion( | ||
| warningInfo.distanceKm ?? 0, | ||
| unitSystem, | ||
| ); | ||
| return t(m.distanceAway, { | ||
| distance: `${value} ${unit}`, | ||
| }); | ||
| })() |
There was a problem hiding this comment.
This is a style thing, so I am not asking you to change it. But I personally think IIFE in react are really hard to read. Perhaps something we should discuss next week when we are together to determine if there are certain style things that should/can be avoided.
There was a problem hiding this comment.
I agree.
Easier to read when not IIFE, for the most part. I left 1 in (in LocationView) because it was very simple and prevented extra function calls.
7e1c3cb
…cond now. No select one component. Replaces context.provider. Helpers for getting units. Call sites format numbers.
ErikSin
left a comment
There was a problem hiding this comment.
2 non blocking comments. Overall looking good, thanks for doing that
| export function UnitSystemStoreProvider({ | ||
| value, | ||
| children, | ||
| }: { | ||
| value: UnitSystemStore; | ||
| children: React.ReactNode; | ||
| }) { | ||
| return React.createElement(UnitSystemStoreContext, {value}, children); | ||
| } |
There was a problem hiding this comment.
We shouldn't be using createElement here. In general we should either use a jsx component instead of trying to force an react element inside of a non jsx component.
I think the better thing to do here is to NOT export a Provider and just use the Context directlty in AppProviders.tsx
eg:
//In AppProvider.tsx
return (
<UnitSystemStoreContext value={unitSystemStore}>
...rest of the providers
</UnitSystemStoreContext> )| function formatDistance( | ||
| meters: number, | ||
| unitSystem: UnitSystem, | ||
| formatNumber: (value: number, opts: object) => string, |
There was a problem hiding this comment.
This relates to your comment about intl. I think we either should use formatNumber for everything or not at all. Using it only for this component is a little confusing, expecially since we are dealing with the formatting manually in all the other components. I would argue to not use formatNumber here and just do it manually like the rest of the components
There was a problem hiding this comment.
Should we have a ticket to update all numbers to use intl?
There was a problem hiding this comment.
i don't know. Do you think we should use intl? The manual formatting seems fine to me, but im not sure i fully understand the benefits of using intl
There was a problem hiding this comment.
I think it is some differences of commas and periods? Not a huge difference but somewhat of a difference. https://www.smartick.com/blog/other-contents/curiosities/decimal-separators/
German, French, Spanish: 1.234,56 (period as thousands separator, comma as decimal)
English: 1,234.56 (comma as thousands separator, period as decimal)
Maybe a low priority ticket? Find out what others think? I don't have strong opinions about it.
…out intl number formatting since it is only used in one place.

closes #1818
Did I miss any?