Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep old behaviour in RoutePath when we just have coordinates (and no… #1250

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/map/MapRouteCoordinates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useTheme } from '@mui/styles';
import { useId, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { map } from './core/MapView';

const MapRouteCoordinates = ({ name, coordinates, deviceId }) => {
const id = useId();

const theme = useTheme();

const reportColor = useSelector((state) => {
const attributes = state.devices.items[deviceId]?.attributes;
if (attributes) {
const color = attributes['web.reportColor'];
if (color) {
return color;
}
}
return theme.palette.geometry.main;
});

useEffect(() => {
map.addSource(id, {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [],
},
},
});
map.addLayer({
source: id,
id: `${id}-line`,
type: 'line',
layout: {
'line-join': 'round',
'line-cap': 'round',
},
paint: {
'line-color': ['get', 'color'],
'line-width': 2,
},
});
if (name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one the name is always provided, so we can probably make it mandatory.

map.addLayer({
source: id,
id: `${id}-title`,
type: 'symbol',
layout: {
'text-field': '{name}',
'text-size': 12,
},
paint: {
'text-halo-color': 'white',
'text-halo-width': 1,
},
});
}

return () => {
if (map.getLayer(`${id}-title`)) {
map.removeLayer(`${id}-title`);
}
if (map.getLayer(`${id}-line`)) {
map.removeLayer(`${id}-line`);
}
if (map.getSource(id)) {
map.removeSource(id);
}
};
}, []);

useEffect(() => {
map.getSource(id)?.setData({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates,
},
properties: {
name,
color: reportColor,
},
});
}, [theme, coordinates]);

return null;
};

export default MapRouteCoordinates;
51 changes: 26 additions & 25 deletions src/map/MapRoutePath.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
import { useTheme } from '@mui/styles';
import { useId, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { map } from './core/MapView';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check the warnings and errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any warnings

import { getSpeedColor } from '../common/util/colors';

Check warning on line 4 in src/map/MapRoutePath.js

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module '../common/util/colors'

Check failure on line 4 in src/map/MapRoutePath.js

View workflow job for this annotation

GitHub Actions / build

Missing file extension for "../common/util/colors"

const MapRoutePath = ({ name, positions, coordinates }) => {
const MapRoutePath = ({ name, positions }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like name is never used. We can probably remove it from here.

const id = useId();

const theme = useTheme();

const reportColor = useSelector((state) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the color is based on the position speed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it means you're ignoring the settings.

const position = positions?.find(() => true);
if (position) {
const attributes = state.devices.items[position.deviceId]?.attributes;
if (attributes) {
const color = attributes['web.reportColor'];
if (color) {
return color;
}
}
}
return theme.palette.geometry.main;
});

useEffect(() => {
map.addSource(id, {
type: 'geojson',
Expand Down Expand Up @@ -76,21 +62,36 @@
}, []);

useEffect(() => {
if (!coordinates) {
coordinates = positions.map((item) => [item.longitude, item.latitude]);
const coordinates = positions.map((item) => [item.longitude, item.latitude]);
const speeds = positions.map((item) => item.speed);
const maxSpeed = speeds.reduce((a, b) => Math.max(a, b), -Infinity);
const features = [];
for (let i = 0; i < coordinates.length - 1; i += 1) {
features.push({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [coordinates[i], coordinates[i + 1]],
},
properties: {
color: getSpeedColor(
theme.palette.success.main,
theme.palette.warning.main,
theme.palette.error.main,
speeds[i + 1],
maxSpeed,
),
},
});
}
map.getSource(id)?.setData({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates,
},
type: 'FeatureCollection',
features,
properties: {
name,
color: reportColor,
},
});
}, [theme, positions, coordinates, reportColor]);
}, [theme, positions]);

return null;
};
Expand Down
15 changes: 11 additions & 4 deletions src/map/MapRoutePoints.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useId, useCallback, useEffect } from 'react';
import { useTheme } from '@mui/styles';
import { map } from './core/MapView';
import { getSpeedColor } from '../common/util/colors';

Check warning on line 4 in src/map/MapRoutePoints.js

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module '../common/util/colors'

Check failure on line 4 in src/map/MapRoutePoints.js

View workflow job for this annotation

GitHub Actions / build

Missing file extension for "../common/util/colors"

const MapRoutePoints = ({ positions, onClick }) => {
const id = useId();
const theme = useTheme();

const onMouseEnter = () => map.getCanvas().style.cursor = 'pointer';
const onMouseLeave = () => map.getCanvas().style.cursor = '';
Expand All @@ -27,11 +30,13 @@
id,
type: 'symbol',
source: id,
paint: {
'text-color': ['get', 'color'],
},
layout: {
'icon-image': 'arrow',
'icon-allow-overlap': true,
'icon-rotate': ['get', 'rotation'],
'icon-rotation-alignment': 'map',
'text-field': '▲',
'text-allow-overlap': true,
'text-rotate': ['get', 'rotation'],
},
});

Expand All @@ -54,6 +59,7 @@
}, [onMarkerClick]);

useEffect(() => {
const maxSpeed = positions.map((p) => p.speed).reduce((a, b) => Math.max(a, b), -Infinity);
map.getSource(id)?.setData({
type: 'FeatureCollection',
features: positions.map((position, index) => ({
Expand All @@ -66,6 +72,7 @@
index,
id: position.id,
rotation: position.course,
color: getSpeedColor(theme.palette.success.main, theme.palette.warning.main, theme.palette.error.main, position.speed, maxSpeed),
},
})),
});
Expand Down
2 changes: 0 additions & 2 deletions src/map/core/preloadImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { grey } from '@mui/material/colors';
import createPalette from '@mui/material/styles/createPalette';
import { loadImage, prepareIcon } from './mapUtil';

import arrowSvg from '../../resources/images/arrow.svg';
import directionSvg from '../../resources/images/direction.svg';
import backgroundSvg from '../../resources/images/background.svg';
import animalSvg from '../../resources/images/icon/animal.svg';
Expand Down Expand Up @@ -65,7 +64,6 @@ export default async () => {
const background = await loadImage(backgroundSvg);
mapImages.background = await prepareIcon(background);
mapImages.direction = await prepareIcon(await loadImage(directionSvg));
mapImages.arrow = await prepareIcon(await loadImage(arrowSvg));
await Promise.all(Object.keys(mapIcons).map(async (category) => {
const results = [];
['info', 'success', 'error', 'neutral'].forEach((color) => {
Expand Down
5 changes: 3 additions & 2 deletions src/reports/CombinedReportPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import PageLayout from '../common/components/PageLayout';
import ReportsMenu from './components/ReportsMenu';
import { useCatch } from '../reactHelper';
import MapView from '../map/core/MapView';
import MapRoutePath from '../map/MapRoutePath';
import useReportStyles from './common/useReportStyles';
import TableShimmer from '../common/components/TableShimmer';
import MapCamera from '../map/MapCamera';
import MapGeofence from '../map/MapGeofence';
import { formatTime } from '../common/util/formatter';
import { prefixString } from '../common/util/stringUtils';
import MapMarkers from '../map/MapMarkers';
import MapRouteCoordinates from '../map/MapRouteCoordinates';

const CombinedReportPage = () => {
const classes = useReportStyles();
Expand Down Expand Up @@ -60,10 +60,11 @@ const CombinedReportPage = () => {
<MapView>
<MapGeofence />
{items.map((item) => (
<MapRoutePath
<MapRouteCoordinates
key={item.deviceId}
name={devices[item.deviceId].name}
coordinates={item.route}
deviceId={item.deviceId}
/>
))}
<MapMarkers markers={createMarkers()} />
Expand Down
4 changes: 0 additions & 4 deletions src/resources/images/arrow.svg

This file was deleted.

Loading