1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import React, { useEffect, useState } from 'react';
import { Link } from '@material-ui/core';
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 [address, setAddress] = useState();
useEffect(() => {
setAddress(position.address);
}, [position]);
const showAddress = async () => {
const query = new URLSearchParams({
latitude: position.latitude,
longitude: position.longitude,
});
const response = await fetch(`/api/server/geocode?${query.toString()}`);
if (response.ok) {
setAddress(await response.text());
}
};
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;
}
};
if (property === 'address') {
if (address) {
return (<>{address}</>);
} else {
return (<Link onClick={showAddress}>{t('sharedShowAddress')}</Link>)
}
} else {
return (<>{formatValue(value)}</>);
}
};
export default PositionValue;
|