diff options
author | Anton Tananaev <anton@traccar.org> | 2022-05-02 18:08:33 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-05-02 18:08:33 -0700 |
commit | 23189d6bc945ab4e26e336a42dce138d0bbb3523 (patch) | |
tree | ed0b1410206a1dd0bfa3dbe05be5dd799be81dd7 /modern/src/components | |
parent | f52be84522da7f8e1c2b19d3484250e275252799 (diff) | |
download | trackermap-web-23189d6bc945ab4e26e336a42dce138d0bbb3523.tar.gz trackermap-web-23189d6bc945ab4e26e336a42dce138d0bbb3523.tar.bz2 trackermap-web-23189d6bc945ab4e26e336a42dce138d0bbb3523.zip |
Remove position formatter
Diffstat (limited to 'modern/src/components')
-rw-r--r-- | modern/src/components/PositionValue.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/modern/src/components/PositionValue.js b/modern/src/components/PositionValue.js new file mode 100644 index 00000000..b9e573fe --- /dev/null +++ b/modern/src/components/PositionValue.js @@ -0,0 +1,51 @@ +import React from 'react'; +import { formatAlarm, formatBoolean, formatCoordinate, formatCourse, formatDistance, formatNumber, formatPercentage, formatSpeed, formatTime } from '../common/formatter'; +import { useAttributePreference, usePreference } from '../common/preferences'; +import { useTranslation } from '../LocalizationProvider'; + +const PositionValue = ({ position, property, attribute }) => { + const t = useTranslation(); + + const key = property || attribute; + const value = property ? position[property] : position.attributes[attribute]; + + const distanceUnit = useAttributePreference('distanceUnit'); + const speedUnit = useAttributePreference('speedUnit'); + const coordinateFormat = usePreference('coordinateFormat'); + + const formatValue = () => { + switch (key) { + case 'fixTime': + case 'deviceTime': + case 'serverTime': + return formatTime(value); + case 'latitude': + return formatCoordinate('latitude', value, coordinateFormat); + case 'longitude': + return formatCoordinate('longitude', value, coordinateFormat); + case 'speed': + return formatSpeed(value, speedUnit, t); + case 'course': + return formatCourse(value); + case 'batteryLevel': + return formatPercentage(value); + case 'alarm': + return formatAlarm(value, t); + case 'odometer': + case 'distance': + case 'totalDistance': + return formatDistance(value, distanceUnit, t) + default: + if (typeof value === 'number') { + return formatNumber(value); + } if (typeof value === 'boolean') { + return formatBoolean(value, t); + } + return value; + } + } + + return (<>{formatValue(value)}</>); +}; + +export default PositionValue; |