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

Add picker for badges #21436

Merged
merged 4 commits into from
Jul 19, 2024
Merged

Add picker for badges #21436

merged 4 commits into from
Jul 19, 2024

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Jul 19, 2024

Proposed change

Add badge picker. It supports same feature as the card one :

  • paste a badge
  • select build-in badge, custom badge and manual config
  • select by entity (a suggestion is created using the entity badge)

For now, there is only one official badge (entity) as state-label will be removed.

Demo

CleanShot.2024-07-19.at.15.42.40.mp4

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:

Summary by CodeRabbit

  • New Features

    • Introduced HuiBadgePicker for enhanced badge selection and management within the Lovelace dashboard.
    • Added dialogs for creating and suggesting badges, streamlining user interactions.
    • Implemented a new function for dynamically generating badge configurations.
  • Improvements

    • Updated badge management functionalities for better clarity and usability.
    • Enhanced storage management by changing keys to reflect a focus on dashboard-related features.
  • User Interface

    • Expanded translation entries for improved user guidance in badge management.
    • Modified CSS grid layouts for better responsiveness in card and badge pickers.
  • Bug Fixes

    • Improved error handling in badge creation processes.

@piitaya piitaya changed the title Badge picker Add picker for badges Jul 19, 2024
Copy link
Contributor

coderabbitai bot commented Jul 19, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.
Walkthrough

Walkthrough

The recent updates enhance the Lovelace UI by introducing new badge management features, refining existing configurations, and improving user interaction. Key changes include the mandatory type property in badge configurations, the addition of badge creation and suggestion dialogs, and the restructuring of storage keys to better reflect dashboard functionalities. These enhancements streamline the badge configuration process, improve user experience, and provide a more intuitive interface for managing badges.

Changes

Files Change Summary
src/data/lovelace/config/badge.ts Changed type property from optional to required in the LovelaceBadgeConfig interface to improve type safety.
src/panels/lovelace/common/generate-lovelace-config.ts Introduced computeBadges function to generate badge configurations based on entity IDs.
src/panels/lovelace/components/hui-badge-edit-mode.ts, src/panels/lovelace/components/hui-card-edit-mode.ts, src/panels/lovelace/components/hui-card-options.ts, src/panels/lovelace/editor/conditional-card-editor.ts, src/panels/lovelace/editor/stack-card-editor.ts, src/panels/lovelace/editor/card-editor/hui-card-picker.ts Updated storage key from "lovelaceClipboard" to "dashboardCardClipboard" across multiple components to reflect a shift in context.
src/panels/lovelace/create-element/create-badge-element.ts Added tryCreateBadgeElement function for enhanced error handling during badge creation.
src/panels/lovelace/editor/badge-editor/hui-badge-picker.ts Introduced HuiBadgePicker component for selecting and managing badges, utilizing LitElement for a responsive UI.
src/panels/lovelace/editor/badge-editor/hui-dialog-create-badge.ts, src/panels/lovelace/editor/badge-editor/hui-dialog-suggest-badge.ts Implemented dialogs for creating and suggesting badges, enhancing the UI for badge management.
src/panels/lovelace/editor/card-editor/hui-card-picker.ts, src/panels/lovelace/editor/types.ts Introduced new interfaces for badges and updated properties for better handling of badge-related configurations.
src/panels/lovelace/views/hui-view.ts Streamlined badge creation process by directly invoking showCreateBadgeDialog without fetching a default configuration.
src/translations/en.json Added new sections and entries to improve user interaction with suggested badges and overall badge management in the UI.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BadgeManager
    participant BadgePicker
    participant Dialog

    User->>BadgeManager: Request to create badge
    BadgeManager->>BadgePicker: Show badge options
    BadgePicker-->>User: Display available badges
    User->>BadgePicker: Select badge
    BadgePicker->>Dialog: Open badge creation dialog
    Dialog-->>User: Prompt for badge configuration
    User->>Dialog: Submit badge configuration
    Dialog->>BadgeManager: Create badge with config
    BadgeManager-->>User: Confirmation of badge creation
Loading

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>.
    • 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 show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 (1)
src/translations/en.json (1)

5589-5592: Ensure translation keys are correctly defined.

The keys ui::panel::lovelace::editor::suggest_card::header and ui::panel::lovelace::editor::suggest_card::add are only referenced within the suggest_badge section and do not appear to be defined elsewhere. Please verify if these keys are defined elsewhere in the file or if they need to be defined.

  • Verify the existence and definition of the keys ui::panel::lovelace::editor::suggest_card::header and ui::panel::lovelace::editor::suggest_card::add.
Analysis chain

Ensure translation keys are correctly referenced.

The new suggest_badge section references keys from suggest_card. Verify that these keys exist and are correctly referenced.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the referenced translation keys exist.

# Test: Search for the referenced keys. Expect: Both keys should be present in the codebase.
rg --type json --json 'ui::panel::lovelace::editor::suggest_card::header'
rg --type json --json 'ui::panel::lovelace::editor::suggest_card::add'

Length of output: 1994

Comment on lines +215 to +233
private _handleBadgePicked(ev) {
const config = ev.detail.config;
if (this._params!.entities && this._params!.entities.length) {
if (Object.keys(config).includes("entities")) {
config.entities = this._params!.entities;
} else if (Object.keys(config).includes("entity")) {
config.entity = this._params!.entities[0];
}
}

showEditBadgeDialog(this, {
lovelaceConfig: this._params!.lovelaceConfig,
saveConfig: this._params!.saveConfig,
path: this._params!.path,
badgeConfig: config,
});

this.closeDialog();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

_handleBadgePicked method: Use optional chaining.

The method can be simplified by using optional chaining.

-    if (this._params!.entities && this._params!.entities.length) {
+    if (this._params?.entities?.length) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private _handleBadgePicked(ev) {
const config = ev.detail.config;
if (this._params!.entities && this._params!.entities.length) {
if (Object.keys(config).includes("entities")) {
config.entities = this._params!.entities;
} else if (Object.keys(config).includes("entity")) {
config.entity = this._params!.entities[0];
}
}
showEditBadgeDialog(this, {
lovelaceConfig: this._params!.lovelaceConfig,
saveConfig: this._params!.saveConfig,
path: this._params!.path,
badgeConfig: config,
});
this.closeDialog();
}
private _handleBadgePicked(ev) {
const config = ev.detail.config;
if (this._params?.entities?.length) {
if (Object.keys(config).includes("entities")) {
config.entities = this._params.entities;
} else if (Object.keys(config).includes("entity")) {
config.entity = this._params.entities[0];
}
}
showEditBadgeDialog(this, {
lovelaceConfig: this._params!.lovelaceConfig,
saveConfig: this._params!.saveConfig,
path: this._params!.path,
badgeConfig: config,
});
this.closeDialog();
}
Tools
Biome

[error] 217-217: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

@piitaya
Copy link
Member Author

piitaya commented Jul 19, 2024

@coderabbitai pause

@@ -1,7 +1,7 @@
import { Condition } from "../../../panels/lovelace/common/validate-condition";

export interface LovelaceBadgeConfig {
type?: string;
type: string;
Copy link
Member

Choose a reason for hiding this comment

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

There will still be a lot of configs around that won't have a type, we should deal with those so changing this type might not be wise?

@bramkragten bramkragten merged commit 179245e into dev Jul 19, 2024
13 checks passed
@bramkragten bramkragten deleted the badge-picker branch July 19, 2024 18:56
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.

2 participants