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

feat: serialize platform state to the url #245

Merged
merged 6 commits into from
Jul 4, 2022
Merged

feat: serialize platform state to the url #245

merged 6 commits into from
Jul 4, 2022

Conversation

fspoettel
Copy link
Contributor

@fspoettel fspoettel commented Apr 9, 2022

closes #76

This is a proof-of-concept for serializing platform state to the URL.

  • events
  • time ranges
  • filters & colors

It's based on three components:

  1. URLState class that deals with serialization to the URL.
  2. Redux middleware that observes certain actions and updates URL state.
  3. Redux action that applies the serialized URL state on load.

I considered a few other approaches but ultimately settled on this for a few reasons:

  1. introducing a router library would be a huge change and likely break downstream maps. this implementation is comparatively isolated.
  2. the app state is too big and complex to write it to the URL in its full form. we need to map it to a simpler representation.

In addition, I made the following design decisions:

  • decoupling UrlState from the middleware allows for behavior changes if we discover that we'd rather serialize state manually (e.g. with a share button).
  • history.replaceState is used istd. of pushState. When interacting with the map, a lot of different events might be accessed, rendering the browser back button useless if the platform is linked to. replaceState seems preferable.

I would appreciate early feedback.

@fspoettel fspoettel changed the title feat: persist event selection in url feat: serialize platform state to the url Apr 9, 2022
@fspoettel
Copy link
Contributor Author

fspoettel commented Apr 11, 2022

@breezykermo I would appreciate a high-level review of the PR in the current state - does this approach make sense to you?

There a couple of TODOs that I'll address before merging but the code generally works.

The PR is missing support for category and narrative because I don't know how to test these with the data I have. If you can point me to how to configure these, I'll cover them as well.

@breezykermo
Copy link
Member

Sure thing, I'll review this afternoon (EST)

Copy link
Member

@breezykermo breezykermo left a comment

Choose a reason for hiding this comment

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

@fspoettel this is a great start! I've made some comments inline below. My main comment is about the internal DSL you are using to project values serialized in URL query parameters to the state. Let me know if my comments are unclear or need further explanation

src/store/plugins/urlState/urlState.js Outdated Show resolved Hide resolved
src/store/plugins/urlState/urlState.js Outdated Show resolved Hide resolved
src/store/plugins/urlState/urlState.js Outdated Show resolved Hide resolved
@fspoettel fspoettel marked this pull request as ready for review April 18, 2022 10:39
@fspoettel fspoettel requested a review from breezykermo April 18, 2022 11:30
Copy link
Member

@breezykermo breezykermo left a comment

Choose a reason for hiding this comment

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

Thanks again for this contribution @fspoettel, I'm excited to see this in action! I have put some preliminary comments inline, and I will look to test this feature with a few of our configurations at some point this week to test it out. Very excited to see it in action.

src/reducers/index.js Show resolved Hide resolved
src/selectors/helpers.js Outdated Show resolved Hide resolved
src/store/plugins/urlState/applyUrlState.js Outdated Show resolved Hide resolved
Comment on lines +34 to +40
* Schema specifies how redux state maps to the url and vice versa.
* `trigger`: action that triggers a call to `dehydrate()`
* `type`: type of the mapped URL property
* `dehydrate()`: maps redux state to url state.
* `rehydrate()`:
* maps url state to redux state.
* !for performance reasons, this function works with a mutable ref to `state`!
Copy link
Member

Choose a reason for hiding this comment

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

Very clear explanation, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

is there any option to not use mutable ref to state, given that JS is pass by reference for objects and arrays? I just ask as I think that this final line might be a bit confusing / could use further explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could return a change object here and deepMerge it in the reducer. This is more overhead though. Your call.

dehydrate(state) {
return getSelected(state).map(({ id }) => id);
},
// TODO: determine time range if `range` not set.
Copy link
Member

Choose a reason for hiding this comment

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

Is this TODO necessary before we can merge?

Copy link
Contributor Author

@fspoettel fspoettel Apr 19, 2022

Choose a reason for hiding this comment

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

No, I recorded smaller inprovements with TODO so that we can merge sooner.

dehydrate(state) {
return selectActiveColorSets(state);
},
// TODO: color parent if all children checked.
Copy link
Member

Choose a reason for hiding this comment

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

Likewise here: should this be completed?

src/store/plugins/urlState/schema.js Show resolved Hide resolved
@fspoettel
Copy link
Contributor Author

did you have a chance to test this yet @breezykermo?

@breezykermo
Copy link
Member

@fspoettel my apologies for the long delay- this looks really great! As I'm working on a small project with timemap in the coming days, I am going to merge this and incorporate it as part of the supported feature set going forward. If there are any issues I'll hotfix in that project.

@breezykermo breezykermo merged commit 66a1426 into forensic-architecture:main Jul 4, 2022
@fspoettel fspoettel deleted the feat/serialize-url-state branch July 4, 2022 17:41
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.

Serialize state of the platform as URL
2 participants