Skip to content

Commit

Permalink
Show URL polygons in the map on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
nerik committed Oct 6, 2023
1 parent 530acb1 commit 9bca0b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
23 changes: 18 additions & 5 deletions app/scripts/components/common/map/controls/aoi/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { useAtomValue } from 'jotai';
import { useRef } from 'react';
import { useControl } from 'react-map-gl';
import { aoisFeaturesAtom } from '$components/exploration/atoms/atoms';

// import type { MapRef } from 'react-map-gl';

type DrawControlProps = ConstructorParameters<typeof MapboxDraw>[0] & {
type DrawControlProps = {
onCreate?: (evt: { features: object[] }) => void;
onUpdate?: (evt: { features: object[]; action: string }) => void;
onDelete?: (evt: { features: object[] }) => void;
onSelectionChange?: (evt: { selectedFeatures: object[] }) => void;
};
} & MapboxDraw.DrawOptions;

export default function DrawControl(props: DrawControlProps) {
const control = useRef<MapboxDraw>();
const aoisFeatures = useAtomValue(aoisFeaturesAtom);

useControl<MapboxDraw>(
() => new MapboxDraw(props),
() => {
control.current = new MapboxDraw(props);
return control.current;
},
({ map }: { map: any }) => {
map.on('draw.create', props.onCreate);
map.on('draw.update', props.onUpdate);
map.on('draw.delete', props.onDelete);
map.on('draw.selectionchange', props.onSelectionChange);
map.on('load', () => {
control.current?.set({
type: 'FeatureCollection',
features: aoisFeatures
});
});
},
({ map }: { map: any }) => {
map.off('draw.create', props.onCreate);
Expand Down
12 changes: 10 additions & 2 deletions app/scripts/components/common/map/controls/hooks/use-aois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import { useAtomValue, useSetAtom } from 'jotai';
import { useCallback } from 'react';
import { Polygon } from 'geojson';
import { toAoIid } from '../../utils';
import { aoisDeleteAtom, aoisFeaturesAtom, aoisSetSelectedAtom, aoisUpdateGeometryAtom } from '$components/exploration/atoms/atoms';
import {
aoisDeleteAtom,
aoisFeaturesAtom,
aoisSetSelectedAtom,
aoisUpdateGeometryAtom
} from '$components/exploration/atoms/atoms';

export default function useAois() {
const features = useAtomValue(aoisFeaturesAtom);

const aoisUpdateGeometry = useSetAtom(aoisUpdateGeometryAtom);
const onUpdate = useCallback(
(e) => {
const updates = e.features.map((f) => ({ id: toAoIid(f.id), geometry: f.geometry as Polygon }));
const updates = e.features.map((f) => ({
id: toAoIid(f.id),
geometry: f.geometry as Polygon
}));
aoisUpdateGeometry(updates);
},
[aoisUpdateGeometry]
Expand Down
1 change: 1 addition & 0 deletions app/scripts/components/common/map/mapbox-style-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ const MapboxStyleOverride = css`
})});
}
}
.mapbox-gl-draw_trash.mapbox-gl-draw_trash::before {
background-image: url(${({ theme }) =>
iconDataURI(CollecticonTrashBin, {
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/components/exploration/components/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export function ExplorationMap(props: { comparing: boolean }) {
.slice()
.reverse();

const { onUpdate, onDelete, onSelectionChange, features } = useAois();
console.log(features);
const { onUpdate, onDelete, onSelectionChange } = useAois();
// console.log(features);

return (
<Map id='exploration' projection={projection}>
Expand Down Expand Up @@ -91,7 +91,7 @@ export function ExplorationMap(props: { comparing: boolean }) {
controls={{
polygon: true,
trash: true
}}
} as any}
defaultMode='draw_polygon'
onCreate={onUpdate}
onUpdate={onUpdate}
Expand Down

0 comments on commit 9bca0b4

Please sign in to comment.