import React from 'react' import type { GeoJSONFeatureWithSourceLayer } from '@maplibre/maplibre-gl-inspect' export type InspectFeature = GeoJSONFeatureWithSourceLayer & { inspectModeCounter?: number counter?: number } function displayValue(value: string | number | Date | object | undefined) { if (typeof value === 'undefined' || value === null) return value; if (value instanceof Date) return value.toLocaleString(); if (typeof value === 'object' || typeof value === 'number' || typeof value === 'string') return value.toString(); return value; } function renderKeyValueTableRow(key: string, value: string | undefined) { return {key} {value} } function renderFeature(feature: InspectFeature, idx: number) { return {feature.layer['source']}: {feature.layer['source-layer']}{feature.inspectModeCounter && × {feature.inspectModeCounter}} {renderKeyValueTableRow("$type", feature.geometry.type)} {renderKeyValueTableRow("Feature ID", displayValue(feature.id))} {Object.keys(feature.properties).map(propertyName => { const property = feature.properties[propertyName]; return renderKeyValueTableRow(propertyName, displayValue(property)) })} } function removeDuplicatedFeatures(features: InspectFeature[]) { const uniqueFeatures: InspectFeature[] = []; features.forEach(feature => { const featureIndex = uniqueFeatures.findIndex(feature2 => { return feature.layer['source-layer'] === feature2.layer['source-layer'] && JSON.stringify(feature.properties) === JSON.stringify(feature2.properties) }) if(featureIndex === -1) { uniqueFeatures.push(feature) } else { if('inspectModeCounter' in uniqueFeatures[featureIndex]) { uniqueFeatures[featureIndex].inspectModeCounter!++ } else { uniqueFeatures[featureIndex].inspectModeCounter = 2 } } }) return uniqueFeatures } type FeaturePropertyPopupProps = { features: InspectFeature[] }; class FeaturePropertyPopup extends React.Component { render() { const features = removeDuplicatedFeatures(this.props.features) return
{features.map(renderFeature)}
} } export default FeaturePropertyPopup