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 collapse & expand all groups #20891

Merged
merged 2 commits into from
May 29, 2024
Merged

Add collapse & expand all groups #20891

merged 2 commits into from
May 29, 2024

Conversation

bramkragten
Copy link
Member

@bramkragten bramkragten commented May 28, 2024

Proposed change

CleanShot 2024-05-28 at 11 07 39@2x

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
    • Added options to "Collapse all" and "Expand all" groups in data tables for better group management.
  • UI Improvements
    • Updated the user interface to include new menu items and icons for collapsing and expanding all groups in data tables.
  • Localization
    • Added translations for "Collapse all" and "Expand all" options in the English language file.

Copy link
Contributor

coderabbitai bot commented May 28, 2024

Walkthrough

Walkthrough

The updates introduce new functionality to the HaDataTable component, allowing users to expand or collapse all groups within the data table. This enhancement is supported by new methods in the HaDataTable class and additional UI elements and translations to facilitate group management.

Changes

Files Change Summary
src/components/data-table/ha-data-table.ts Added expandAllGroups() and collapseAllGroups() methods to manage group expansion and collapse.
src/layouts/hass-tabs-subpage-data-table.ts Introduced _collapseAllGroups and _expandAllGroups methods, added menu items and icons for these actions.
src/translations/en.json Added translations for "Collapse all" and "Expand all" options.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant UI as User Interface
    participant DT as HaDataTable
    participant Tabs as HassTabsSubpageDataTable

    User->>UI: Click "Expand all"
    UI->>Tabs: Trigger expand all event
    Tabs->>DT: Call expandAllGroups()
    DT->>DT: Expand all groups
    DT-->>Tabs: Groups expanded

    User->>UI: Click "Collapse all"
    UI->>Tabs: Trigger collapse all event
    Tabs->>DT: Call collapseAllGroups()
    DT->>DT: Collapse all groups
    DT-->>Tabs: Groups collapsed
Loading

Recent Review Details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits Files that changed from the base of the PR and between 758d138 and d664269.
Files selected for processing (1)
  • src/components/data-table/ha-data-table.ts (1 hunks)
Additional Context Used
Biome (19)
src/components/data-table/ha-data-table.ts (19)

57-57: Unexpected any. Specify a different type.


70-70: Unexpected any. Specify a different type.


94-94: Unexpected any. Specify a different type.


220-220: Forbidden non-null assertion.


234-239: Prefer for...of instead of forEach.


236-236: Avoid the delete operator which can impact performance.


237-237: Avoid the delete operator which can impact performance.


535-535: Forbidden non-null assertion.


539-539: Avoid the delete operator which can impact performance.


567-592: Prefer for...of instead of forEach.


619-619: Unexpected any. Specify a different type.


651-651: Unexpected any. Specify a different type.


680-680: Unexpected any. Specify a different type.


722-722: Unexpected any. Specify a different type.


741-742: Forbidden non-null assertion.


745-745: Forbidden non-null assertion.


749-750: Avoid the delete operator which can impact performance.


2-11: Some named imports are only used as types.


30-31: All these imports are only used as types.

Additional comments not posted (2)
src/components/data-table/ha-data-table.ts (2)

733-736: Add method to expand all groups.

The expandAllGroups method correctly resets the _collapsedGroups array and fires a collapsed-changed event. This is a straightforward and effective implementation for expanding all groups.


745-750: Handling undefined group names.

The previous review comment about avoiding the delete operator due to performance concerns is still valid. Consider using a more efficient method to handle undefined groups.


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 full the 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 Configration 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: 2

src/components/data-table/ha-data-table.ts Show resolved Hide resolved
Comment on lines 733 to 750
public expandAllGroups() {
this._collapsedGroups = [];
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}

public collapseAllGroups() {
if (!this.groupColumn) {
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The methods expandAllGroups and collapseAllGroups are well-implemented to manage the visibility of grouped items. However, consider handling the scenario where groupColumn is not set in collapseAllGroups to prevent unnecessary processing.

-    if (!this.groupColumn) {
-      return;
-    }
+    if (!this.groupColumn || !this.data.some(item => item[this.groupColumn])) {
+      console.warn('No group column set or no data to group.');
+      return;
+    }

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.

Suggested change
public expandAllGroups() {
this._collapsedGroups = [];
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
public collapseAllGroups() {
if (!this.groupColumn) {
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
public expandAllGroups() {
this._collapsedGroups = [];
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
public collapseAllGroups() {
if (!this.groupColumn || !this.data.some(item => item[this.groupColumn])) {
console.warn('No group column set or no data to group.');
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}

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 comments (4)
src/components/data-table/ha-data-table.ts (4)

Line range hint 57-57: Specify explicit types instead of any.

Using any type reduces the benefits of TypeScript's static type checking. Where possible, replace any with more specific types to improve code safety and clarity.

Also applies to: 70-70, 94-94, 619-619, 651-651, 680-680, 722-722


Line range hint 220-220: Avoid non-null assertions.

Non-null assertions can lead to runtime errors if the assumptions about data presence are incorrect. Consider adding null checks or using optional chaining to make the code more robust.

Also applies to: 535-535, 741-742, 745-745


Line range hint 234-239: Prefer for...of loops over forEach.

Using for...of loops can make the code cleaner and easier to understand, especially when dealing with asynchronous operations or when needing to break out of the loop early.

Also applies to: 567-592


Line range hint 2-11: Optimize imports.

Some imports are only used as types. Consider importing them using import type to clarify that they are only used for type checking and not included in the JavaScript output.

Also applies to: 30-31

Comment on lines +738 to +753
public collapseAllGroups() {
if (
!this.groupColumn ||
!this.data.some((item) => item[this.groupColumn!])
) {
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add method to collapse all groups.

-    if (
-      !this.groupColumn ||
-      !this.data.some((item) => item[this.groupColumn!])
-    ) {
-      return;
-    }
+    if (!this.groupColumn) {
+      console.warn('No group column set.');
+      return;
+    }
+    if (!this.data.some((item) => item[this.groupColumn])) {
+      console.warn('No data to group.');
+      return;
+    }

Consider separating the checks for groupColumn and data existence into separate conditions for clearer error messaging. Also, avoid using non-null assertions as they can lead to runtime errors if assumptions about data presence are incorrect.


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.

Suggested change
public collapseAllGroups() {
if (
!this.groupColumn ||
!this.data.some((item) => item[this.groupColumn!])
) {
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}
public collapseAllGroups() {
if (!this.groupColumn) {
console.warn('No group column set.');
return;
}
if (!this.data.some((item) => item[this.groupColumn])) {
console.warn('No data to group.');
return;
}
const grouped = groupBy(this.data, (item) => item[this.groupColumn!]);
if (grouped.undefined) {
// undefined is a reserved group name
grouped[UNDEFINED_GROUP_KEY] = grouped.undefined;
delete grouped.undefined;
}
this._collapsedGroups = Object.keys(grouped);
fireEvent(this, "collapsed-changed", { value: this._collapsedGroups });
}

@frenck frenck added the Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label May 28, 2024
@piitaya piitaya merged commit 5a2977f into dev May 29, 2024
14 checks passed
@piitaya piitaya deleted the add-collapse-expand-all-groups branch May 29, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants