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

Add option to provide custom labels to tooltip and use legend labels for the tooltip value; COUNTRY=rbd #1286

81 changes: 55 additions & 26 deletions frontend/src/components/MapView/Layers/layer-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
AdminLevelDataLayerProps,
PointDataLayerProps,
} from 'config/types';
import { addPopupData } from 'context/tooltipStateSlice';
import { PopupData, addPopupData } from 'context/tooltipStateSlice';
import { findFeature, getEvtCoords, getLayerMapId } from 'utils/map-utils';
import { getRoundedData } from 'utils/data-utils';
import { i18nTranslator } from 'i18n';
Expand Down Expand Up @@ -85,23 +85,46 @@ export const addPopupParams = (

const coordinates = getEvtCoords(evt);

const { dataField, featureInfoProps, title } = layer;
const {
dataField,
featureInfoProps,
title,
dataLabel,
displaySource,
legend,
} = layer;

// adminLevelLayer uses data field by default.
const propertyField: string = dataField
? `properties.${dataField}`
: 'properties.data';

// by default add `dataField` to the tooltip if it is not within the feature_info_props dictionary.
if (!Object.keys(featureInfoProps || {}).includes(dataField)) {
dispatch(
addPopupData({
[title]: {
data: getRoundedData(get(feature, propertyField), t),
coordinates,
},
}),
);
// By default, we add `dataField` to the tooltip if it is not within the feature_info_props dictionary.
// If a custom dataLabel is provided, we'll make sure to use that before the value
// If displaySource is set to `legend_label`, use the matching legend label for the dataField.
const useCustomLabel = !!dataLabel || displaySource === 'legend_label';
if (
useCustomLabel ||
!Object.keys(featureInfoProps || {}).includes(dataField)
) {
const customDisplayData =
displaySource === 'legend_label' &&
legend.find(
legendItem => legendItem.value === get(feature, propertyField),
)?.label;
const displayData = customDisplayData
? `${t(`${customDisplayData}`)}`
: getRoundedData(get(feature, propertyField), t);

const popupDataRows: PopupData = {
...(dataLabel ? { [title]: { data: null, coordinates } } : {}),
[dataLabel ?? title]: {
data: displayData,
coordinates,
},
};

dispatch(addPopupData(popupDataRows));
}

// Add feature_info_props as extra fields to the tooltip
Expand All @@ -120,22 +143,28 @@ export const addPopupParams = (
});
}

// temporary fix for the admin level
const possibleAdminLevelData: PopupData = adminLevel
? {
'Admin Level': {
data: feature.properties.adminLevel,
coordinates,
},
}
: {};

const featureInfoPropsData = getFeatureInfoPropsData(
featureInfoPropsWithFallback || {},
coordinates,
feature,
);

dispatch(
addPopupData({
// temporary fix for the admin level
...(adminLevel
? {
'Admin Level': {
data: feature.properties.adminLevel,
coordinates,
},
}
: {}),
...getFeatureInfoPropsData(
featureInfoPropsWithFallback || {},
coordinates,
feature,
),
// Only if we're providing a custom label, put the data before admin level
...(!useCustomLabel ? possibleAdminLevelData : {}),
...featureInfoPropsData,
...(useCustomLabel ? possibleAdminLevelData : {}),
Comment on lines +165 to +167
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that change expected for CH where the adminLevel ends up at the bottom?

Before:
Screenshot 2024-06-27 at 3 01 56 PM

After:
Screenshot 2024-06-27 at 3 02 01 PM

Copy link
Collaborator Author

@gislawill gislawill Jun 27, 2024

Choose a reason for hiding this comment

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

Good catch, I wouldn't consider this expected. I'm not certain that I know where we'd prefer "Admin Level" to go in the tooltip order.

@wadhwamatic, for tooltips showing the "IFPRI / ACLED Conflict Analysis - 2023" layer, do you have a preference for where the Admin Level is displayed?

Mali, Tombouctou, Tombouctou
IFPRI / ACLED Conflict Analysis - 2023
Admin Level: 2 (this is where it was rendered prior to this change)
Classification: High
Deadliness score: 84
Deadliness rank: 47
Danger score: 34
Danger rank: 16
Diffusion score: 2
Diffusion rank: 65
Fragmentation score: 4
Fragmentation rank: 6
Admin Level: 2 (this is where it's currently rendered with this change)

Copy link
Member

Choose a reason for hiding this comment

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

@gislawill - we can keep admin level at the bottom. In the issue I'll write up today, we should also address the need for this. It's not always necessary to display it.

}),
);
};
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/components/MapView/MapTooltip/PopupContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ const PopupContent = ({
return true;
})
.map(([key, value]) => {
// If the data is undefined, null, or an empty string, we do not render the data (only render the key)
const isKeyValuePair = [undefined, null, ''].every(
item => item !== value.data,
);
return (
<Fragment key={key}>
<div>
Expand All @@ -180,10 +184,10 @@ const PopupContent = ({
color="inherit"
className={classes.text}
>
{`${t(key)}: `}
{isKeyValuePair ? `${t(key)}: ` : t(key)}
</Typography>
)}
{key !== 'Population in phase 1' && (
{key !== 'Population in phase 1' && isKeyValuePair && (
<Typography
display="inline"
variant="h4"
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/MapView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Column, quoteAndEscapeCell } from 'utils/analysis-utils';
import { TableRow } from 'context/analysisResultStateSlice';
import { AdminBoundaryParams, EWSParams } from 'context/datasetStateSlice';
import { MapRef, Point } from 'react-map-gl/maplibre';
import { PopupData } from 'context/tooltipStateSlice';
import { getExtent } from './Layers/raster-utils';

// TODO: maplibre: fix feature
Expand Down Expand Up @@ -156,7 +157,7 @@ export function getFeatureInfoPropsData(
featureInfoProps: FeatureInfoObject,
coordinates: number[],
feature: any,
) {
): PopupData {
const [keys, metaDataKeys] = sortKeys(featureInfoProps);
const { properties } = feature;

Expand Down
Loading
Loading