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

Migrate add/edit resources dialog to @material/web #21933

Merged
merged 21 commits into from
Sep 16, 2024
Merged

Conversation

wendevlin
Copy link
Contributor

@wendevlin wendevlin commented Sep 9, 2024

Breaking change

Proposed change

  • Make resources editable without "Advanced mode" (it is hidden behind a submenu in the dashboards table)
  • Make resources add/edit dialog fit designs
  • Introduce ha-dialog-new which uses @material/web dialog

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

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:

Summary by CodeRabbit

  • New Features

    • Introduced a new dialog component (HaMdDialog) that enhances user interaction with customizable behavior and improved visual design.
    • Added a disableCancelAction property to prevent dialog closure via escape key or outside click, enhancing user control.
  • User Interface Enhancements

    • Updated dialog structure to include a header with a close button for improved usability.
    • Enhanced accessibility with dynamically generated dialog titles and aria-labels based on resource parameters.
    • Improved responsiveness of dialogs with new styling adjustments for smaller screens.
    • Included support for dialog elements in browsers lacking native support through a new polyfill.

@wendevlin wendevlin self-assigned this Sep 9, 2024
Copy link
Contributor

coderabbitai bot commented Sep 9, 2024

Warning

Rate limit exceeded

@wendevlin has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 20 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 2678c8e and 82c2144.

Walkthrough

Walkthrough

The changes introduce a new custom dialog component, HaMdDialog, which enhances dialog functionality in the web application. This component includes properties for user interaction, such as the ability to prevent closure via the ESC key or scrim click. The existing resource management interface has been updated to replace the previous dialog component with HaMdDialog, improving the structure and behavior of the dialog, including a close button and dynamic title generation.

Changes

Files Change Summary
package.json Added dialog-polyfill dependency at version 0.5.6 for improved dialog support in older browsers.
src/components/ha-md-dialog.ts Introduced HaMdDialog component extending MdDialog, added disableCancelAction property, and defined custom styles and global registration for the component.
src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts Replaced ha-dialog with ha-md-dialog, added close button, introduced _dialogClosed method, and updated rendering logic for improved layout and accessibility.

Assessment against linked issues

Objective Addressed Explanation
Use sentence case for the title The changes do not include updates to the title casing as specified.
Change title to Add resource or Edit resource The title changes for adding or editing resources are not implemented.
Change warning title and text The specified warning title and text changes have not been addressed.
Remove Delete button and move action to the table The changes do not indicate the removal of the delete button as requested.
Add cancel button and rename action button The implementation of a cancel button is unclear based on the provided changes.

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 generate interesting stats about this repository and render them as a table.
    -- @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 or @coderabbitai title 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (3)
src/panels/config/lovelace/resources/show-dialog-lovelace-resource-detail.ts (1)

Action Required: Update or Remove removeResource References

The removeResource method is still being used in src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts. Please review and update the codebase to ensure all references to this method are consistent with the recent interface changes to prevent potential runtime errors.

  • File: src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts
    • Lines: References to removeResource method found.
Analysis chain

Line range hint 3-10: Confirm removal of removeResource method.

The removal of the removeResource method from the LovelaceResourceDetailsDialogParams interface aligns with the PR's objectives. Ensure that all references to this method in other components are updated or removed to prevent runtime errors.

Run the following script to verify the usage of removeResource:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that no other components are still using the removed `removeResource` method.

# Test: Search for the method usage. Expect: No occurrences.
rg --type typescript -A 5 $'removeResource'

Length of output: 77


Script:

#!/bin/bash
# Description: Verify that no other components are still using the removed `removeResource` method.

# Test: Search for the method usage across all TypeScript files. Expect: No occurrences.
rg 'removeResource' --glob '*.ts' -A 5

Length of output: 1334

src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts (2)

112-126: Approve the delete button configuration with a suggestion for improvement.

The delete button configuration is well-implemented and aligns with the PR objectives. However, consider enhancing accessibility by adding an aria-label attribute to the ha-icon-button for better screen reader support.


256-295: Approve the resource deletion method with a suggestion for consistency.

The _removeResource method is well-implemented, providing clear user confirmation steps and handling errors effectively. Consider using async/await consistently in the catch block instead of returning false directly to handle potential asynchronous operations in error handling more robustly.

src/components/ha-dialog-new.ts Outdated Show resolved Hide resolved
@silamon
Copy link
Contributor

silamon commented Sep 9, 2024

I've introduced the "xxx-new" components as first, and I've been regretting it since I did that. Why? I thought we could migrate way faster and then when all usages are away, use the old name again. However, until today, none of those introductions have been fully replaced and we have multiple "xxx-new" components now. People that start making frontend (knx, hacs, ...) based on this frontend start using these components since theme variables are defined in the component. Long story short, I would suggest introducing a "ha-dialog-m3" instead of "ha-dialog-new".

@piitaya
Copy link
Member

piitaya commented Sep 9, 2024

The font line-height and letter-spacing is not the same inside the dialog. Looks like md-dialog is overriding some style of the content.

Before

CleanShot 2024-09-09 at 17 33 14

After

CleanShot 2024-09-09 at 17 33 24

@bramkragten
Copy link
Member

@silamon

I've introduced the "xxx-new" components as first, and I've been regretting it since I did that. Why? I thought we could migrate way faster and then when all usages are away, use the old name again. However, until today, none of those introductions have been fully replaced and we have multiple "xxx-new" components now. People that start making frontend (knx, hacs, ...) based on this frontend start using these components since theme variables are defined in the component. Long story short, I would suggest introducing a "ha-dialog-m3" instead of "ha-dialog-new".

What is the issue with others using the new components? They are building their own packages and don't use our bundle, so it will not break if we migrate the names. If they bump frontend they will indeed have to rename the components too, but that is a simple search and replace? (On a separate note, I do hate the -new name 😄 )

@silamon
Copy link
Contributor

silamon commented Sep 10, 2024

@silamon

I've introduced the "xxx-new" components as first, and I've been regretting it since I did that. Why? I thought we could migrate way faster and then when all usages are away, use the old name again. However, until today, none of those introductions have been fully replaced and we have multiple "xxx-new" components now. People that start making frontend (knx, hacs, ...) based on this frontend start using these components since theme variables are defined in the component. Long story short, I would suggest introducing a "ha-dialog-m3" instead of "ha-dialog-new".

What is the issue with others using the new components? They are building their own packages and don't use our bundle, so it will not break if we migrate the names. If they bump frontend they will indeed have to rename the components too, but that is a simple search and replace? (On a separate note, I do hate the -new name 😄 )

I just tried to explain that I do hate the "-new" components as well and suggest to make them somehow mention material 3. When material 4 will come, if ever, we will probably end up with "ha-dialog-new-new". 😆 Those that build frontends based on our frontend will have to change the name once, right now, and twice when we decide to go back to "ha-dialog". In some cases, it is not a search and replace, for example the button menu group. The dialog will be used much more common, so I would prefer "ha-m3-dialog" or "ha-dialog-m3", perhaps give those components an own folder (material 3) for every component where material 3 is adopted already.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
src/panels/config/lovelace/resources/ha-config-lovelace-resources.ts (1)

255-293: LGTM, but consider improving error handling.

The _removeResource method looks good overall. It handles the resource deletion process appropriately, including user confirmations and updating the internal state.

However, the error handling could be improved. Instead of returning false in the catch block, consider showing an error dialog to the user with a more specific error message.

Here's a suggestion to improve the error handling:

  try {
    await deleteResource(this.hass!, resource.id);
    this._resources = this._resources!.filter(({ id }) => id !== resource.id);
    // ...
  } catch (err: any) {
-   return false;
+   showAlertDialog(this, {
+     title: this.hass!.localize(
+       "ui.panel.config.lovelace.resources.error_deleting_resource"
+     ),
+     text: err.message,
+   });
  }

Make sure to add the corresponding localization key for the error dialog title.

@bramkragten
Copy link
Member

We should migrate everything to the new one before we start with ha-dialog-new-new 😉

But I get what you say, projects that have not updated the new ones will need to update ones we rename the new ones...

I suggest ha-md-dialog

@wendevlin
Copy link
Contributor Author

@bramkragten @silamon I update the name to ha-md-dialog 👍

@bramkragten I did all of your requested changes except type. There is just the type=alert option which defines: dialogs that need to communicate an important message and require a user's response. I don't see the resource dialog fit into this.

@piitaya I was able to set the line-height md variable, but there is no variable for letter-spacing. Should I keep it like it is or set the letter-spacing explicit for the ha-md-dialog?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts (1)

99-107: Improve accessibility of the dialog header.

The dialog header could benefit from additional accessibility enhancements:

  • Add a type attribute to the ha-dialog-header element to provide semantic meaning.
  • Add an aria-label attribute to the ha-dialog-header element to provide a clear and concise label for screen readers.

Here's an example of how you can improve the accessibility of the dialog header:

-<ha-dialog-header slot="headline">
+<ha-dialog-header slot="headline" type="heading" aria-label="Resource dialog header">

…detail.ts


Fix dialogClosed method naming

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts (1)

63-70: LGTM, but address the static analysis hint.

The code changes are approved.

However, the static analysis tool reports that _dialogClosed is declared but its value is never read. This is because _dialogClosed is used as an event handler for the closed event on the ha-md-dialog component, so it is not directly called in the code.

To address the static analysis hint, add a comment to explain the purpose of _dialogClosed:

// Event handler for the `closed` event on the `ha-md-dialog` component.
private _dialogClosed(): void {
  this._params = undefined;
  fireEvent(this, "dialog-closed", { dialog: this.localName });
}
Tools
GitHub Check: Lint and check format

[failure] 63-63:
'_dialogClosed' is declared but its value is never read.

@github-actions github-actions bot added the Build Related to building the code label Sep 12, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts (3)

46-47: Address the accessibility concerns.

As mentioned in the previous review comment, consider adding a type attribute and an aria-label to the ha-md-dialog element for better accessibility, since an element is used in the headline slot.


128-144: Ensure the cancel button is shown on all screen sizes.

As discussed in the previous review comments, the cancel button should be:

  • Shown on all screen sizes.
  • Sticky at the bottom along with the submit button on mobile.

Please update the styles to ensure that the cancel button is always visible and positioned correctly on different screen sizes.


63-70: Consider swapping the public and private methods.

As suggested in the previous review comment, consider making the _dialogClosed method public and the current public closeDialog method private. This way, public close calls will trigger the necessary animations and ensure a consistent user experience.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a07a2a0 and e46ff38.

Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (3)
  • package.json (1 hunks)
  • src/components/ha-md-dialog.ts (1 hunks)
  • src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • package.json
  • src/components/ha-md-dialog.ts
Additional comments not posted (10)
src/panels/config/lovelace/resources/dialog-lovelace-resource-detail.ts (10)

2-3: LGTM!

The imports are correctly added and necessary for using Lit and decorators in the class.


5-9: LGTM!

The imports are correctly added and necessary for using the new ha-md-dialog component and related functionality.


11-11: LGTM!

The import is correctly added and necessary for using the ha-icon-button component.


46-47: LGTM!

The private property _dialog is correctly added using the @query decorator and will be used to reference the ha-md-dialog element.


63-70: LGTM!

The changes improve the code organization by:

  • Making the _dialogClosed method private and using it to handle the dialog closed logic.
  • Updating the public closeDialog method to directly close the dialog.

This refactoring enhances the clarity and maintainability of the code.


77-91: LGTM!

The changes improve the accessibility and user experience by dynamically generating the dialog title and aria-label based on the resource parameters. The localization ensures that the strings are displayed in the user's preferred language.


93-106: LGTM!

The changes update the dialog to use the new ha-md-dialog component and improve the dialog structure by:

  • Adding a header with a close button and a dynamic title.
  • Using slots for better composition and flexibility.

This enhances the user experience and aligns with the Material Design guidelines.


107-107: LGTM!

The change improves the dialog structure by using a slot for the content. This allows for better composition and flexibility.


128-144: LGTM!

The changes improve the dialog actions by:

  • Adding a cancel button for better user experience.
  • Updating the submit button text to reflect the action (create or update).
  • Disabling the submit button based on the form state to prevent invalid submissions.

These enhancements align with the Material Design guidelines and provide a more intuitive user experience.


145-145: LGTM!

The change is necessary to properly close the ha-md-dialog element.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5df1580 and 69e85f2.

Files selected for processing (1)
  • src/components/ha-md-dialog.ts (1 hunks)
Additional comments not posted (3)
src/components/ha-md-dialog.ts (3)

1-143: The HaMdDialog component implementation looks good overall!

The component extends the MdDialog from the Material Web library and introduces useful features like the disableCancelAction property. The event handling and polyfill loading ensure compatibility across browsers, and the custom styles enhance the appearance and responsiveness of the dialog.


72-99: The _handleCancel method implementation looks good.

The method correctly checks the disableCancelAction property and prevents the default cancel action if needed. The rotation animation provides a nice visual feedback to the user.


35-37: Consider replacing the animate API usage with CSS animations.

As mentioned in the past review comments, the animate API might not be supported by older browsers. It would be better to replace it with CSS animations for better browser support.

Replace the quick property usage with an explicit check for the animate API support:

-if (this.animate === undefined) {
-  this.quick = true;
-}
+this.quick = !this.animate;

Likely invalid or redundant comment.

src/components/ha-md-dialog.ts Show resolved Hide resolved
@bramkragten bramkragten enabled auto-merge (squash) September 16, 2024 12:16
@bramkragten bramkragten merged commit 9e4dc0d into dev Sep 16, 2024
9 checks passed
@bramkragten bramkragten deleted the add-edit-resources branch September 16, 2024 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Related to building the code cla-signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Add and Edit resource dialog
5 participants