From 5a62f4ab21b077ac63a26da87a86138913a11b4b Mon Sep 17 00:00:00 2001 From: HarelM Date: Fri, 22 Dec 2023 23:59:51 +0200 Subject: [PATCH] Improve types, migrate another file --- ... => MapMaplibreGlFeaturePropertyPopup.tsx} | 35 ++++++++++++------- src/components/MapMaplibreGlLayerPopup.tsx | 19 +++++----- 2 files changed, 32 insertions(+), 22 deletions(-) rename src/components/{MapMaplibreGlFeaturePropertyPopup.jsx => MapMaplibreGlFeaturePropertyPopup.tsx} (70%) diff --git a/src/components/MapMaplibreGlFeaturePropertyPopup.jsx b/src/components/MapMaplibreGlFeaturePropertyPopup.tsx similarity index 70% rename from src/components/MapMaplibreGlFeaturePropertyPopup.jsx rename to src/components/MapMaplibreGlFeaturePropertyPopup.tsx index 761499ff..2634a78d 100644 --- a/src/components/MapMaplibreGlFeaturePropertyPopup.jsx +++ b/src/components/MapMaplibreGlFeaturePropertyPopup.tsx @@ -1,9 +1,18 @@ import React from 'react' -import PropTypes from 'prop-types' import Block from './Block' import FieldString from './FieldString' -function displayValue(value) { +export type InspectFeature = { + id: string + properties: {[key:string]: any} + layer: {[key:string]: any} + geometry: GeoJSON.Geometry + sourceLayer: string + inspectModeCounter?: number + counter?: number +} + +function displayValue(value: string | number | Date | object) { if (typeof value === 'undefined' || value === null) return value; if (value instanceof Date) return value.toLocaleString(); if (typeof value === 'object' || @@ -12,7 +21,7 @@ function displayValue(value) { return value; } -function renderProperties(feature) { +function renderProperties(feature: InspectFeature) { return Object.keys(feature.properties).map(propertyName => { const property = feature.properties[propertyName] return @@ -21,13 +30,13 @@ function renderProperties(feature) { }) } -function renderFeatureId(feature) { +function renderFeatureId(feature: InspectFeature) { return } -function renderFeature(feature, idx) { +function renderFeature(feature: InspectFeature, idx: number) { return
{feature.layer['source']}: {feature.layer['source-layer']}{feature.inspectModeCounter && × {feature.inspectModeCounter}}
@@ -38,8 +47,8 @@ function renderFeature(feature, idx) {
} -function removeDuplicatedFeatures(features) { - let uniqueFeatures = []; +function removeDuplicatedFeatures(features: InspectFeature[]) { + let uniqueFeatures: InspectFeature[] = []; features.forEach(feature => { const featureIndex = uniqueFeatures.findIndex(feature2 => { @@ -50,8 +59,8 @@ function removeDuplicatedFeatures(features) { if(featureIndex === -1) { uniqueFeatures.push(feature) } else { - if(uniqueFeatures[featureIndex].hasOwnProperty('inspectModeCounter')) { - uniqueFeatures[featureIndex].inspectModeCounter++ + if('inspectModeCounter' in uniqueFeatures[featureIndex]) { + uniqueFeatures[featureIndex].inspectModeCounter!++ } else { uniqueFeatures[featureIndex].inspectModeCounter = 2 } @@ -61,11 +70,11 @@ function removeDuplicatedFeatures(features) { return uniqueFeatures } -class FeaturePropertyPopup extends React.Component { - static propTypes = { - features: PropTypes.array - } +type FeaturePropertyPopupProps = { + features: InspectFeature[] +}; +class FeaturePropertyPopup extends React.Component { render() { const features = removeDuplicatedFeatures(this.props.features) return
diff --git a/src/components/MapMaplibreGlLayerPopup.tsx b/src/components/MapMaplibreGlLayerPopup.tsx index e7e49b3b..1a1fe2e9 100644 --- a/src/components/MapMaplibreGlLayerPopup.tsx +++ b/src/components/MapMaplibreGlLayerPopup.tsx @@ -1,18 +1,19 @@ import React from 'react' import IconLayer from './IconLayer' +import type {InspectFeature} from './MapMaplibreGlFeaturePropertyPopup'; -function groupFeaturesBySourceLayer(features: any[]) { - const sources = {} as any +function groupFeaturesBySourceLayer(features: InspectFeature[]) { + const sources: {[key: string]: InspectFeature[]} = {} - let returnedFeatures = {} as any; + let returnedFeatures: {[key: string]: number} = {} features.forEach(feature => { if(returnedFeatures.hasOwnProperty(feature.layer.id)) { returnedFeatures[feature.layer.id]++ - const featureObject = sources[feature.layer['source-layer']].find((f: any) => f.layer.id === feature.layer.id) + const featureObject = sources[feature.layer['source-layer']].find((f: InspectFeature) => f.layer.id === feature.layer.id) - featureObject.counter = returnedFeatures[feature.layer.id] + featureObject!.counter = returnedFeatures[feature.layer.id] } else { sources[feature.layer['source-layer']] = sources[feature.layer['source-layer']] || [] sources[feature.layer['source-layer']].push(feature) @@ -25,13 +26,13 @@ function groupFeaturesBySourceLayer(features: any[]) { } type FeatureLayerPopupProps = { - onLayerSelect(...args: unknown[]): unknown - features: any[] + onLayerSelect(layerId: string): unknown + features: InspectFeature[] zoom?: number }; class FeatureLayerPopup extends React.Component { - _getFeatureColor(feature: any, _zoom?: number) { + _getFeatureColor(feature: InspectFeature, _zoom?: number) { // Guard because openlayers won't have this if (!feature.layer.paint) { return; @@ -75,7 +76,7 @@ class FeatureLayerPopup extends React.Component { const sources = groupFeaturesBySourceLayer(this.props.features) const items = Object.keys(sources).map(vectorLayerId => { - const layers = sources[vectorLayerId].map((feature: any, idx: number) => { + const layers = sources[vectorLayerId].map((feature: InspectFeature, idx: number) => { const featureColor = this._getFeatureColor(feature, this.props.zoom); return