buskerudbyen / cycling-norway

3 stars 2 forks source link

State refactoring #122

Closed michaeljohansen closed 1 month ago

michaeljohansen commented 4 months ago

While making the widget it became apparent that having most state in the URL is problematic when the widget is included on other pages where we end up manipulating the URLs of the page where the widget is included. Widget users probably won't like that the widget messes with their URLs.

It would probably be a good idea to rely less on the URL for state, at least in widget mode. Not sure this project can support two different state systems, so one idea is to allow setting initial state (e.g. start/destination) through the URL, but then keeping that state internally and updating internally rather than updating the URL continuously. This would also make the history list prettier for users.

My simple suggestion is to keep all state in a useReducer as far up in the app as possible (e.g. in Map.tsx)`. Gather state there, ensure all state updates are well defined, and make it easy to look up state anywhere in the app.

Beck-berry commented 2 months ago

@tsobuskerudbyen Can you reconsider using the URL for collecting the status info of the layers? How could we keep the purpose of #58 and make the browser history cleaner?

send locations, zoom level and map displays over URLs

I would suggest to introduce a button to the website with a text something like "Copy URL with layer information" that would satisfy the usecase of #58, otherwise the URL would only contain the locations and the zoom level (only on the website, not the widget #137) as before.

tsobuskerudbyen commented 2 months ago

I agree. Good idea @Beck-berry.

Beck-berry commented 1 month ago

I added a Share button to the right corner menu. (It does not show up in widget mode.) Image When its clicked, an alert shows up, saying, that the URL has been copied (and shows the exact URL that has been copied). When the user opens this copied URL, the layers will be set as they were on the original window and the URL clears up. The layers search param only stays for as long as the information is needed. The from and the to params are updating as before on the site. (Not in widget mode.) Let me know if you'd like to remove that too.