-
Notifications
You must be signed in to change notification settings - Fork 0
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
[ERA-7681] - Map Drawing Tools #725
Changes from 15 commits
95b8762
4c7a435
045b1de
8a71a46
d2b315d
a7b0d7c
7705bfa
91be1e8
81234d7
5cd996c
d444c15
30b52ee
964d485
ee0a97f
008f6fa
98d5d8c
bf4670e
101b815
437326b
ee3ccf1
d228b94
f5e1ff1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
REACT_APP_DAS_HOST=https://develop.pamdas.org | ||
REACT_APP_GA_TRACKING_ID=UA-12413928-16 | ||
REACT_APP_MOCK_EVENTS_API=true | ||
REACT_APP_MOCK_EVENTS_API=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import React, { memo, Fragment, useContext, useMemo, useEffect } from 'react'; | ||
import { MapContext, Layer, Source } from 'react-mapbox-gl'; | ||
|
||
import React, { memo, useContext, useMemo, useEffect } from 'react'; | ||
import { MapContext } from '../App'; | ||
|
||
import { TILE_LAYER_SOURCE_TYPES, LAYER_IDS, MAX_ZOOM, MIN_ZOOM } from '../constants'; | ||
import { useMapLayer, useMapSource } from '../hooks'; | ||
|
||
import { calcConfigForMapAndSourceFromLayer } from '../utils/layers'; | ||
|
||
|
@@ -14,13 +14,31 @@ const RASTER_SOURCE_OPTIONS = { | |
'tileSize': 256, | ||
}; | ||
|
||
const RenderFunction = ({ children }) => <>{children}</>; | ||
|
||
const SourceComponent = ({ id, tileUrl, sourceConfig }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We ended up removing a
We only need to call the hook once and now we can create as many sources (and layers) as we want with a memoized function (so it doesn't trigger renders) :) Similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just render props, a well-known and proven React pattern. Iterative hook generation, however, is an anti-pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I don't know what I'm missing, but I don't see the render prop pattern here. As far as I know that pattern looks like this: Iterative hook generation definitely sounds like an antipattern, my suggestion was not to call or generate hooks within a loop, but just call it once and get a memoized function that let us create layers and sources freely. I don't see why that is an antipattern, it's literally what standard hooks like It was a suggestion though, I hadn't seen anything like this and it felt confusing at first, but if you like it that's ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah sort of -- except I'm using it for You're right that I misread your suggestion. I still like my pattern better 🙃 hahahaha but I'm open to discussing further if you have strong feelings |
||
const config = useMemo(() => ({ | ||
...RASTER_SOURCE_OPTIONS, | ||
tiles: [ | ||
tileUrl, | ||
], | ||
...sourceConfig, | ||
}), [sourceConfig, tileUrl]); | ||
|
||
useMapSource(id, null, config); | ||
|
||
return null; | ||
}; | ||
|
||
const TileLayerRenderer = (props) => { | ||
const { layers, currentBaseLayer } = props; | ||
|
||
const activeLayer = layers.find(({ id }) => id === currentBaseLayer.id); | ||
|
||
const map = useContext(MapContext); | ||
|
||
const activeLayer = useMemo(() => | ||
layers.find(({ id }) => id === currentBaseLayer.id) | ||
, [currentBaseLayer.id, layers]); | ||
|
||
const { mapConfig, sourceConfig } = useMemo(() => | ||
calcConfigForMapAndSourceFromLayer(currentBaseLayer) | ||
, [currentBaseLayer]); | ||
|
@@ -32,20 +50,22 @@ const TileLayerRenderer = (props) => { | |
} | ||
}, [map, mapConfig]); | ||
|
||
return <Fragment> | ||
{layers | ||
.filter(layer => TILE_LAYER_SOURCE_TYPES.includes(layer.attributes.type)) | ||
.map(layer => <Source key={layer.id} id={`layer-source-${layer.id}`} tileJsonSource={{ | ||
...RASTER_SOURCE_OPTIONS, | ||
tiles: [ | ||
layer.attributes.url, | ||
], | ||
...sourceConfig, | ||
}} > | ||
</Source>)} | ||
|
||
{!!activeLayer && <Layer before={FEATURE_FILLS} id={`tile-layer-${activeLayer.id}`} key={activeLayer.id} sourceId={`layer-source-${activeLayer.id}`} type="raster" />} | ||
</Fragment>; | ||
useMapLayer( | ||
`tile-layer-${activeLayer?.id}`, | ||
'raster', | ||
`layer-source-${activeLayer?.id}`, | ||
undefined, | ||
undefined, | ||
{ before: FEATURE_FILLS, condition: !!activeLayer } | ||
); | ||
|
||
return layers | ||
.filter(layer => TILE_LAYER_SOURCE_TYPES.includes(layer.attributes.type)) | ||
.map(layer => | ||
<RenderFunction key={layer.id}> | ||
<SourceComponent id={`layer-source-${layer.id}`} sourceConfig={sourceConfig} tileUrl={layer.attributes.url} /> | ||
</RenderFunction> | ||
); | ||
}; | ||
|
||
export default memo(TileLayerRenderer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm what's this for? Can't we just render the
SourceComponent
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope.
https://www.patterns.dev/posts/render-props-pattern/