Skip to content

Commit ce12b36

Browse files
committed
feat: move tracker marker when hovering over chart
1 parent bdbcead commit ce12b36

File tree

4 files changed

+63
-19
lines changed

4 files changed

+63
-19
lines changed

apps/client/src/Components/Molecules/DataLora/AssetChart.tsx

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import useStyles from "./Map.styles";
55

66
interface Props {
77
coords: TrackerItem[][];
8+
setActiveMarkerTimestamp: (timestamp: string) => void;
89
}
910

10-
export const AssetsChart: FC<Props> = ({ coords }) => {
11+
export const AssetsChart: FC<Props> = ({
12+
coords,
13+
setActiveMarkerTimestamp,
14+
}) => {
1115
const { classes } = useStyles();
1216

1317
const asset1 = coords[0]; // Traccar is not sending batteryLevel attribute.
1418
const data = asset1
1519
? asset1.map((item) => ({
1620
battery: item.batteryLevel, // ? parseInt(item.battery, 10) : -1,
21+
time: item.time,
1722
// temperature: item.temperature, // ? parseInt(item.temperature, 10) : -1,
1823
}))
1924
: [];
@@ -27,6 +32,15 @@ export const AssetsChart: FC<Props> = ({ coords }) => {
2732
dataKey="battery"
2833
stroke="#8884d8"
2934
dot={false}
35+
activeDot={{
36+
onMouseOver: (_fn, point) => {
37+
const p = point as { payload?: { time?: string } };
38+
setActiveMarkerTimestamp(p?.payload?.time ?? "");
39+
},
40+
onMouseOut: (_fn, point) => {
41+
setActiveMarkerTimestamp("");
42+
},
43+
}}
3044
/>
3145
<YAxis
3246
yAxisId="left"
@@ -35,20 +49,19 @@ export const AssetsChart: FC<Props> = ({ coords }) => {
3549
width={75}
3650
domain={[0, 100]}
3751
/>
38-
{/* <Line
39-
yAxisId="right"
40-
type="monotone"
41-
dataKey="temperature"
42-
stroke="#888400"
43-
/> */}
44-
{/* <YAxis
45-
yAxisId="right"
46-
orientation="right"
47-
label="°C"
48-
width={75}
49-
/> */}
50-
<XAxis dataKey="time" />
52+
<XAxis
53+
dataKey="time"
54+
tickFormatter={(value) =>
55+
new Date(value.toString()).toLocaleString("en-uk", {
56+
hour: "2-digit",
57+
minute: "2-digit",
58+
})
59+
}
60+
/>
5161
<Tooltip
62+
labelFormatter={(value) =>
63+
new Date(value.toString()).toLocaleString("en-uk")
64+
}
5265
formatter={(value, name) => {
5366
if (name === "battery") {
5467
return `${value}%`;

apps/client/src/Components/Molecules/DataLora/Map.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC } from "react";
1+
import { FC, useState } from "react";
22
import { MapContainer } from "react-leaflet";
33
import { AssetsChart } from "./AssetChart";
44
import useStyles from "./Map.styles";
@@ -14,10 +14,16 @@ const Map: FC<Props> = ({ showChart = false }) => {
1414
const { coords, update, isLoading, toggleQueryType, queryType } =
1515
useLocQuery();
1616

17+
const [activeMarkerTimestamp, setActiveMarkerTimestamp] =
18+
useState<string>("");
19+
1720
return (
1821
<div className={classes.map}>
1922
<MapContainer zoom={20}>
20-
<MapContent coords={coords} />
23+
<MapContent
24+
coords={coords}
25+
activeMarkerTimestamp={activeMarkerTimestamp}
26+
/>
2127
</MapContainer>
2228
<div className="custom-controls">
2329
<button onClick={() => update()} disabled={isLoading}>
@@ -27,7 +33,12 @@ const Map: FC<Props> = ({ showChart = false }) => {
2733
{queryType}
2834
</button>
2935
</div>
30-
{showChart && coords && <AssetsChart coords={coords} />}
36+
{showChart && coords && (
37+
<AssetsChart
38+
coords={coords}
39+
setActiveMarkerTimestamp={setActiveMarkerTimestamp}
40+
/>
41+
)}
3142
</div>
3243
);
3344
};

apps/client/src/Components/Molecules/DataLora/MapContent.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const TILES_LAYER_DARK =
1414

1515
interface Props {
1616
coords: TrackerItem[][];
17+
activeMarkerTimestamp: string;
1718
}
1819

1920
const LINE_COLORS = ["#3488ff", "green", "red", "yellow", "purple"] as const;
@@ -44,7 +45,7 @@ const hasValidCoords = (item: TrackerItem): item is TrackerItem =>
4445
typeof item.loc[0] === "number" &&
4546
typeof item.loc[1] === "number";
4647

47-
const MapContent: FC<Props> = ({ coords }) => {
48+
const MapContent: FC<Props> = ({ coords, activeMarkerTimestamp }) => {
4849
const [markers, setMarkers] = useState<TrackerItem[] | null>(null);
4950
const map = useMap();
5051

@@ -64,6 +65,25 @@ const MapContent: FC<Props> = ({ coords }) => {
6465
updateBoundsAndMarker();
6566
}, [coords, updateBoundsAndMarker]);
6667

68+
useEffect(() => {
69+
setMarkers((prevMarkers) => {
70+
if (prevMarkers) {
71+
const activeCoord = coords[0].find(
72+
(item) => item.time === activeMarkerTimestamp
73+
);
74+
const [firstMarker, ...otherMarkers] = prevMarkers;
75+
return [
76+
{
77+
...firstMarker,
78+
loc: activeCoord?.loc ?? firstMarker.loc,
79+
},
80+
...otherMarkers,
81+
];
82+
}
83+
return prevMarkers;
84+
});
85+
}, [activeMarkerTimestamp, coords]);
86+
6787
return (
6888
<>
6989
<TileLayer

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "homeremote",
3-
"version": "3.7.8",
3+
"version": "3.7.9",
44
"license": "MIT",
55
"scripts": {
66
"writeGitInfo": "ts-node --project ./tsconfig.node.json writeGitInfo.ts",

0 commit comments

Comments
 (0)