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

Trap sidebar links when page has a dirty editor #20161

Closed
wants to merge 2 commits into from

Conversation

karwosts
Copy link
Contributor

Proposed change

If automation editor is dirty, ask for a confirmation before following through any of the sidebar links.

I'll frequently edit an automation and want to hop over into devtools to test some template or check some state, only to realize a moment later that I've lost all my unsaved changes without warning.

I used a sessionStorage for this, though I'm not sure if that's the correct way to implement this kind of global lock or not.

Only added it to automation editor for the moment, but if we like it as a proof of concept should probably also be added to scripts, scenes, card editors, view editors, etc.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

@silamon
Copy link
Contributor

silamon commented Mar 23, 2024

Very conceptual idea: I would implement this as an abstract class, let's say LitElementWithUnload, then let the HaAutomationEditor implement that. LitElementWithUnload should be responsible for knowing if the page is dirty at the moment you want to leave the page. It should also be responsible for showing the confirm dialog. Then in the HassRouterPage where the routing between urls in handled internally, check the element of instance LitElementWithUnload we're replacing and show the dialog. If the dialog is cancelled, return early and stay on the page. If that would work, we would just have to replace LitElement with LitElementWithUnload to have a page show a confirm dialog on leave.

@karwosts
Copy link
Contributor Author

Sounds like it might be a neat idea but it's a little technically confusing to me, I'm not so familiar with routers and what they do.

I do notice that if I return early from the update function of the hass-page-router it does appear to stay on the current page as desired, but the browser still acts as if it has moved on to the new page (the URL bar shows the new page we prevented loading, and it populates a new history item). Maybe that can be mitigated, but I'm not sure of the details.

@silamon
Copy link
Contributor

silamon commented Mar 24, 2024

I'll see if I can put that idea into proof of concept code.

@silamon
Copy link
Contributor

silamon commented Apr 6, 2024

I can't seem to let this work in the lit framework. It's quite hard to catch it whenever the page is being destructed since it destructs top down starting with the general config page layout, then as a child the automation picker and editor, but then it's too late to trap it and show a dialog.

}
const url = (ev.currentTarget as any).href;
ev.preventDefault();
const leave = await this._confirmDirty();
Copy link
Member

Choose a reason for hiding this comment

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

If we don't leave, we have to reset the selected panel in the listbox.

@@ -224,6 +226,13 @@ class HaSidebar extends SubscribeMixin(LitElement) {
})
private _hiddenPanels: string[] = [];

@storage({
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using storage, cant we just send an event up from the automation page, this feature doesnt have to survive page reload etc?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a context may be more suitable given the ha-sidebar is 3 levels higher than the automation page where we know if it's dirty or not?

Copy link
Member

Choose a reason for hiding this comment

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

yeah 👍

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 think a context may be more suitable

Do you mind to elaborate a bit? I'm not very familiar with this concept.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can find more about it here: https://lit.dev/docs/data/context/
We already use it to prevent us bringing registries (like the device and entity registry) to be passed through each component. It's a bit the same like storages except it's kept in-memory thus not persistent, allowing to inject it into a components makes the context available.

The consumer will be the ha-sidebar, the provider should be the automation-editor component where _dirty is available to know if there are unsaved changes, and then lastly you have the context object which should keep the boolean value.

It's not the easiest thing from lit to get working unfortunately.

Copy link
Member

@bramkragten bramkragten Apr 12, 2024

Choose a reason for hiding this comment

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

The consumer should be higher in the tree than the provider I think, as it works with events.

Another thing that will work well for this probably is Signals. Currently a proposal with ponyfill: https://github.com/tc39/proposal-signals

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The consumer should be higher in the tree than the provider I think, as it works with events.

Are you saying sidebar cannot be the context consumer, as it is not strictly a parent element of the thing providing the context?

Tried adding @consume in ha-sidebar but never got it to pick up anything.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, thats what I'm saying

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 did try doing this with events, and got it partially working, but I seem to have got stuck on one aspect.

I can send an event from automation page whether or not the page is dirty or clean, and that is working, but if I'm on the automation page and it is dirty, and I press the browser back button, then what seems to happen is the browser leaves the automation page, but the sidebar still thinks something is dirty so it is still showing the confirm dialog even though we're not on a dirty page anymore.

In the current proposal with @storage I can clear the dirty stored flag on disconnectedCallback when we're leaving the automation page for any reason. If I try firing an event to clear dirty from disconnectedCallback, it doesn't seem to reach anywhere, presumably because it's no longer connected anymore. So I don't know how to reset the dirty state when we're leaving a dirty page via browser back. (I can't show a confirm dialog on back button either, but I'm ignoring that for now)

@bramkragten bramkragten self-assigned this May 6, 2024
Copy link

github-actions bot commented Aug 4, 2024

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Aug 4, 2024
@silamon silamon removed the stale label Aug 4, 2024
Copy link

github-actions bot commented Nov 2, 2024

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 2, 2024
Copy link
Contributor

coderabbitai bot commented Nov 2, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@karwosts karwosts removed the stale label Nov 3, 2024
@bramkragten
Copy link
Member

Can we handle this on a more global level, so not just in the sidebar but by listening to the history API events?

That way we can do something similar to the built in beforeunload event (which we should probably also implement here).

Have the element fire an event up, that it is dirty (or clean again) somewhere in the top level this is catched, when there is a popstate event or a location-changed event we prevent the call to updateRoute and ask the user to confirm if they want to navigate away. If they want to navigate away we call updateRoute, if not we add a history.pushState to the previous url.

@bramkragten
Copy link
Member

Replaced by #23170

@karwosts karwosts deleted the trap-dirty-sidebar branch December 11, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants