Skip to content

Commit

Permalink
Merge branch 'master' into ez/add-types-side-panel-root
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeny Zhgulev <evgeny.zhgulev@actionengine.com>
  • Loading branch information
jagerts committed May 12, 2022
2 parents 495a965 + ca45cef commit c22a144
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/components/geocoder-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import {FlyToInterpolator} from '@deck.gl/core';
import KeplerGlSchema from 'schemas';
import {getCenterAndZoomFromBounds} from 'utils/projection-utils';

import Geocoder from './geocoder/geocoder';
import Geocoder, { Result } from './geocoder/geocoder';
import {
GEOCODER_DATASET_NAME,
GEOCODER_LAYER_ID,
GEOCODER_GEO_OFFSET,
GEOCODER_ICON_COLOR,
GEOCODER_ICON_SIZE
} from 'constants/default-settings';
import {MapState} from 'reducers';
import {MapState, Viewport} from 'reducers';

const ICON_LAYER = {
id: GEOCODER_LAYER_ID,
Expand Down Expand Up @@ -133,7 +133,7 @@ export default function GeocoderPanelFactory(): ComponentType<GeocoderPanelProps
this.props.removeDataset(GEOCODER_DATASET_NAME);
}

onSelected = (viewport = null, geoItem) => {
onSelected = (viewport: Viewport | null = null, geoItem: Result) => {
const {
center: [lon, lat],
text,
Expand Down
33 changes: 0 additions & 33 deletions src/components/geocoder/geocoder.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import React, {useCallback, useMemo, useState} from 'react';
import styled from 'styled-components';
import classnames from 'classnames';
import MapboxClient from 'mapbox';
import {injectIntl} from 'react-intl';
import {injectIntl, IntlShape} from 'react-intl';
import {WebMercatorViewport} from 'viewport-mercator-project';
import KeyEvent from 'constants/keyevent';
import {Input} from 'components/common/styled-components';
import {Search, Delete} from 'components/common/icons';
import {Viewport} from 'reducers/map-state-updaters';

// matches only valid coordinates
const COORDINATE_REGEX_STRING =
Expand All @@ -35,9 +36,9 @@ const COORDINATE_REGEX = RegExp(COORDINATE_REGEX_STRING);

const PLACEHOLDER = 'Enter an address or coordinates, ex 37.79,-122.40';

let debounceTimeout = null;
let debounceTimeout: NodeJS.Timeout | null = null;

export const testForCoordinates = query => {
export const testForCoordinates = (query: string): [true, number, number] | [false, string] => {
const isValid = COORDINATE_REGEX.test(query.trim());

if (!isValid) {
Expand Down Expand Up @@ -106,8 +107,35 @@ const StyledContainer = styled.div`
}
`;

export interface Result {
center: [number, number];
place_name: string;
bbox?: [number, number, number, number];
text?: string;
}

export type Results = ReadonlyArray<Result>;

type GeocoderProps = {
mapboxApiAccessToken: string;
className?: string;
limit?: number;
timeout?: number;
formatItem?: (item: Result) => string;
viewport?: Viewport;
onSelected: (viewport: Viewport | null, item: Result) => void;
onDeleteMarker?: () => void;
transitionDuration?: number;
pointZoom?: number;
width?: number;
};

type IntlProps = {
intl: IntlShape;
};

/** @type {import('./geocoder').GeocoderComponent} */
const GeoCoder = ({
const GeoCoder: React.FC<GeocoderProps & IntlProps> = ({
mapboxApiAccessToken,
className = '',
limit = 5,
Expand All @@ -125,7 +153,7 @@ const GeoCoder = ({
const [showResults, setShowResults] = useState(false);
const [showDelete, setShowDelete] = useState(false);
/** @type {import('./geocoder').Results} */
const initialResults = [];
const initialResults: Result[] = [];
const [results, setResults] = useState(initialResults);
const [selectedIndex, setSelectedIndex] = useState(0);

Expand All @@ -135,11 +163,14 @@ const GeoCoder = ({
event => {
const queryString = event.target.value;
setInputValue(queryString);
const [hasValidCoordinates, longitude, latitude] = testForCoordinates(queryString);
if (hasValidCoordinates) {
const resultCoordinates = testForCoordinates(queryString);
if (resultCoordinates[0]) {
const [_, latitude, longitude] = resultCoordinates;
setResults([{center: [latitude, longitude], place_name: queryString}]);
} else {
clearTimeout(debounceTimeout);
if (debounceTimeout) {
clearTimeout(debounceTimeout);
}
debounceTimeout = setTimeout(async () => {
if (limit > 0 && Boolean(queryString)) {
try {
Expand Down Expand Up @@ -173,7 +204,7 @@ const GeoCoder = ({
let newViewport = new WebMercatorViewport(viewport);
const {bbox, center} = item;

newViewport = bbox
const resultViewport = bbox
? newViewport.fitBounds([
[bbox[0], bbox[1]],
[bbox[2], bbox[3]]
Expand All @@ -184,7 +215,7 @@ const GeoCoder = ({
zoom: pointZoom
};

const {longitude, latitude, zoom} = newViewport;
const {longitude, latitude, zoom} = resultViewport;

onSelected({...viewport, ...{longitude, latitude, zoom, transitionDuration}}, item);

Expand All @@ -198,7 +229,7 @@ const GeoCoder = ({
const onMarkDeleted = useCallback(() => {
setShowDelete(false);
setInputValue('');
onDeleteMarker();
onDeleteMarker?.();
}, [onDeleteMarker]);

const onKeyDown = useCallback(
Expand Down
8 changes: 4 additions & 4 deletions src/layers/s2-geometry-layer/s2-geometry-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
VisConfigRange
} from '../layer-factory';
import S2LayerIcon from './s2-layer-icon';
import {getS2Center} from './s2-utils';
import {getS2Center, validS2Token} from './s2-utils';

export type S2GeometryLayerVisConfigSettings = {
opacity: VisConfigNumber;
Expand Down Expand Up @@ -241,8 +241,7 @@ export default class S2GeometryLayer extends Layer {
for (let i = 0; i < filteredIndex.length; i++) {
const index = filteredIndex[i];
const token = getS2Token({index});

if (token) {
if (validS2Token(token)) {
data.push({
index,
token
Expand All @@ -257,9 +256,10 @@ export default class S2GeometryLayer extends Layer {
const centroids = dataContainer.reduce(
(acc, entry, index) => {
const s2Token = getS2Token({index});
if (s2Token) {
if (validS2Token(s2Token)) {
acc.push(getS2Center(s2Token));
}

return acc;
},
[],
Expand Down
6 changes: 6 additions & 0 deletions src/layers/s2-geometry-layer/s2-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export function getS2Center(s2Token) {
const {lat, lng} = S2.idToLatLng(s2Id.toString());
return [lng, lat];
}

const re = new RegExp('^[0-9a-z]*$', 'g');
// simple test to see if token only contains numbers and letters
export function validS2Token(token) {
return typeof token === 'string' && Boolean(token.match(re));
}

0 comments on commit c22a144

Please sign in to comment.