Skip to content

Commit

Permalink
[Maps] Surface data request errors (elastic#200860)
Browse files Browse the repository at this point in the history
## Summary

This PR makes it to so data request errors are surfaced to users, as a
warning.

Closes: elastic#195294
  • Loading branch information
kowalczyk-krzysztof authored Nov 26, 2024
1 parent a1e97b1 commit 1f57890
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useEffect, useState } from 'react';
import { EuiCallOut, EuiFlexGroup, EuiFlexItem, EuiText, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { DynamicStyleProperty } from '../../properties/dynamic_style_property';

interface Props {
error: Error;
style: DynamicStyleProperty<object>;
}

export const StyleError = ({ error, style }: Props) => {
const [label, setLabel] = useState('');
const styleName = style.getDisplayStyleName();

useEffect(() => {
let canceled = false;
const getLabel = async () => {
const field = style.getField();
if (!field) {
return;
}

const fieldLabel = await field.getLabel();

if (canceled) {
return;
}

setLabel(fieldLabel);
};

getLabel();

return () => {
canceled = true;
};
}, [style]);

return (
<div>
<EuiFlexGroup gutterSize="xs" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiToolTip position="top" title={styleName} content={label}>
<EuiText className="eui-textTruncate" size="xs" style={{ maxWidth: '180px' }}>
<small>
<strong>{label}</strong>
</small>
</EuiText>
</EuiToolTip>
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexGroup direction="column" gutterSize="none">
<EuiCallOut
title={i18n.translate('xpack.maps.vectorStyleLegend.fetchStyleMetaDataError', {
defaultMessage: 'Unable to fetch style meta data',
})}
color="warning"
iconType="warning"
>
<p>{error.message}</p>
</EuiCallOut>
</EuiFlexGroup>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import React from 'react';
import { EuiText } from '@elastic/eui';
import { euiThemeVars } from '@kbn/ui-theme';
import { StyleError } from './style_error';
import {
DynamicStyleProperty,
IDynamicStyleProperty,
} from '../../properties/dynamic_style_property';
import { FIELD_ORIGIN } from '../../../../../../common/constants';
import { Mask } from '../../../../layers/vector_layer/mask';
import { IStyleProperty } from '../../properties/style_property';
Expand All @@ -33,12 +38,22 @@ export function VectorStyleLegend({
const legendRows = [];

for (let i = 0; i < styles.length; i++) {
const row = styles[i].renderLegendDetailRow({
isLinesOnly,
isPointsOnly,
symbolId,
svg,
});
const styleMetaDataRequest = styles[i].isDynamic()
? (styles[i] as IDynamicStyleProperty<object>).getStyleMetaDataRequest()
: undefined;

const error = styleMetaDataRequest?.getError();

const row = error ? (
<StyleError error={error} style={styles[i] as DynamicStyleProperty<object>} />
) : (
styles[i].renderLegendDetailRow({
isLinesOnly,
isPointsOnly,
symbolId,
svg,
})
);

legendRows.push(
<div key={i} className="vectorStyleLegendSpacer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import _ from 'lodash';
import React from 'react';
import { FeatureCollection } from 'geojson';
import type { FeatureIdentifier, Map as MbMap } from '@kbn/mapbox-gl';
import { DataRequest } from '../../../util/data_request';
import { AbstractStyleProperty, IStyleProperty } from './style_property';
import { DEFAULT_SIGMA } from '../vector_style_defaults';
import {
Expand Down Expand Up @@ -97,6 +98,7 @@ export interface IDynamicStyleProperty<T> extends IStyleProperty<T> {
mbMap: MbMap,
mbSourceId: string
): boolean;
getStyleMetaDataRequest(): DataRequest | undefined;
}

export class DynamicStyleProperty<T extends object>
Expand All @@ -122,6 +124,11 @@ export class DynamicStyleProperty<T extends object>
this._getFieldFormatter = getFieldFormatter;
}

getStyleMetaDataRequest() {
const dataRequestId = this._getStyleMetaDataRequestId(this.getFieldName());
return dataRequestId ? this._layer.getDataRequest(dataRequestId) : undefined;
}

getValueSuggestions = async (query: string) => {
return this._field === null
? []
Expand All @@ -147,12 +154,7 @@ export class DynamicStyleProperty<T extends object>
}

_getRangeFieldMetaFromStyleMetaRequest(): RangeFieldMeta | null {
const dataRequestId = this._getStyleMetaDataRequestId(this.getFieldName());
if (!dataRequestId) {
return null;
}

const styleMetaDataRequest = this._layer.getDataRequest(dataRequestId);
const styleMetaDataRequest = this.getStyleMetaDataRequest();
if (!styleMetaDataRequest || !styleMetaDataRequest.hasData()) {
return null;
}
Expand All @@ -177,12 +179,7 @@ export class DynamicStyleProperty<T extends object>
return null;
}

const dataRequestId = this._getStyleMetaDataRequestId(this.getFieldName());
if (!dataRequestId) {
return null;
}

const styleMetaDataRequest = this._layer.getDataRequest(dataRequestId);
const styleMetaDataRequest = this.getStyleMetaDataRequest();
if (!styleMetaDataRequest || !styleMetaDataRequest.hasData()) {
return null;
}
Expand All @@ -202,12 +199,7 @@ export class DynamicStyleProperty<T extends object>
}

_getCategoryFieldMetaFromStyleMetaRequest() {
const dataRequestId = this._getStyleMetaDataRequestId(this.getFieldName());
if (!dataRequestId) {
return [];
}

const styleMetaDataRequest = this._layer.getDataRequest(dataRequestId);
const styleMetaDataRequest = this.getStyleMetaDataRequest();
if (!styleMetaDataRequest || !styleMetaDataRequest.hasData()) {
return [];
}
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/maps/public/classes/util/data_request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class DataRequest {
return this._descriptor.dataRequestToken;
}

getError(): Error | undefined {
return this._descriptor.error;
}

renderError(): ReactNode {
if (!this._descriptor.error) {
return null;
Expand Down

0 comments on commit 1f57890

Please sign in to comment.