Skip to content
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

Reset AoIs #707

Merged
merged 7 commits into from
Oct 25, 2023
Merged

Conversation

nerik
Copy link
Contributor

@nerik nerik commented Oct 20, 2023

This adds a reset button as suggested here

Unfortunately, I couldn't get this to work. The control needs access to the map ref, which is stored in the MapsContext (reachable via the useMaps hook), but useThemedControls creates a separated React root, hence without access to any context.

I've been looking for hours for a clean solution but I'm stuck, sorry :(

@nerik nerik requested a review from danielfdsilva October 20, 2023 14:57
@netlify
Copy link

netlify bot commented Oct 20, 2023

Deploy Preview for veda-ui ready!

Name Link
🔨 Latest commit 733f10c
🔍 Latest deploy log https://app.netlify.com/sites/veda-ui/deploys/6538edc4b8290c0008ffe1f7
😎 Deploy Preview https://deploy-preview-707--veda-ui.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@nerik nerik marked this pull request as ready for review October 20, 2023 14:58
Comment on lines 41 to 57
export default function ResetAoIControl() {
useThemedControl(
() => {
return <ResetAoI />;
},
{
position: 'top-left'
}
);
return null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to access the context here and pass it as a prop to ResetAoI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion: d102d80

However instance doesn't exist on this map ref for some reason.

I could create a specific context for this map implementation in order for the draw control to expose its internals for other draw-related controls, but this really feels like a hack.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is instance coming from?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous ref implementation had an instance because we added it since it served our needs.

useImperativeHandle(ref, () => ({
resize: () => {
mapRef.current?.resize();
mapCompareRef.current?.resize();
},
instance: mapRef.current,
compareInstance: mapCompareRef.current
}));

And then the map stored the draw control so we can act on it, but none of this is default.

const newMbDraw = new MapboxDraw({
modes: MapboxDraw.modes,
displayControlsDefault: false,
styles: computeDrawStyles(theme)
});
mbDrawRef.current = newMbDraw;
mbMap.addControl(newMbDraw, 'top-left');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A solution for this implementation could be to add a reference to the control when it is mounted:

@app/scripts/components/common/map/controls/aoi/index.tsx

useControl<MapboxDraw>(
-    () => {
+    ({map}) => {
      control.current = new MapboxDraw(props);
+      map._drawControl = control.current;
      return control.current;
    },

Then you could access it via map._drawControl on the reset button

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However for the clear button I don't think you need to access the trash method. Using this without a selection does nothing.
I'd imagine that deleting all the feature via the atom would do the trick.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used deleteAll: d102d80, it works!

I'd imagine that deleting all the feature via the atom would do the trick.
Sadly no, you have to manually keep the atom and the mbdraw state in sync

I have taken the opportunity to clean up that flow with the upload AoI feature: 66a9f32

@nerik nerik requested a review from danielfdsilva October 20, 2023 16:05
@danielfdsilva
Copy link
Collaborator

I had a brief conversation with Erik at the end of last week and this is at a point where it is working.
However the result is that this implementation relies both on Mapbox draw imperative methods and react reactive code.

For example:

const onReset = useCallback(() => {
const mbDraw = map?._drawControl;
if (!mbDraw) return;
mbDraw.deleteAll();
aoiDeleteAll();
}, [map, aoiDeleteAll]);

Here, the features are being deleted from the draw plugin and at the same time from the atom that stores them.

There no absolutely clean way to do this though. We are working with a mapbox plugin that is imperative by nature.
A way to reduce this problem is to create custom controls for the deletion and then based on those set the features on the plugin. (deleting would become setting and empty feature array).
This was the approach used in the current analysis. The controls fire AOI events that are captured and processed.

This same behavior could be migrated to this implementation but it would require some additional work and changes. Do you have any thoughts @hanbyul-here @sandrahoang686? Do you feel that this is worth it?
cc @j08lue

@hanbyul-here
Copy link
Collaborator

We would eventually want to have one place that syncs these states, but I think it is acceptable to file this as a tech debt and move on for now.

@danielfdsilva danielfdsilva force-pushed the feature/exploration-map-aoi-delete branch from 2f1e5f0 to 479b719 Compare October 25, 2023 10:16
@danielfdsilva
Copy link
Collaborator

Rebased to fix the conflict and logged the tech debt.
Will merge.

@danielfdsilva danielfdsilva force-pushed the feature/exploration-map-aoi-delete branch from 479b719 to 733f10c Compare October 25, 2023 10:28
@danielfdsilva danielfdsilva merged commit 5ff51d3 into feature/exploration Oct 25, 2023
7 checks passed
@danielfdsilva danielfdsilva deleted the feature/exploration-map-aoi-delete branch October 25, 2023 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants