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

feat(Picker): remain group headers in the list while filtering #1449

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

kubaczerwinski77
Copy link
Contributor

@kubaczerwinski77 kubaczerwinski77 commented Dec 4, 2024

Resolves: https://livechatinc.atlassian.net/browse/HD-4644

Description

Make group headers visible in the option list while filtering with search phrase.

Storybook

https://feature-HD-4644--613a8e945a5665003a05113b.chromatic.com

Checklist

Obligatory:

  • Self review (use this as your final check for proposed changes before requesting the review)
  • Add correct label
  • Assign pull request with the correct issue

Summary by CodeRabbit

  • New Features

    • Enhanced item filtering in the Picker component to include groupHeader items in search results.
    • Improved logic to prevent consecutive groupHeader items from appearing in the results.
  • Bug Fixes

    • Refined filtering process for better search accuracy and readability while maintaining existing functionality.

Copy link
Contributor

coderabbitai bot commented Dec 4, 2024

Walkthrough

The changes in this pull request modify the usePickerItems hook to enhance the filtering logic for items based on the search phrase. Notably, items with the groupHeader property are now included in the filtered results, with additional logic to prevent consecutive groupHeader items from appearing. The filtering process has been refactored for improved readability, while maintaining existing functionalities related to item selection and state management.

Changes

File Path Change Summary
packages/react-components/src/components/Picker/hooks/usePickerItems.ts Modified filtering logic to include groupHeader items, refactored search logic for clarity.

Assessment against linked issues

Objective Addressed Explanation
Group headers should remain in the option list while filtering (HD-4644)

🪧 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.

@kubaczerwinski77 kubaczerwinski77 requested review from a team December 4, 2024 11:40
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 (2)
packages/react-components/src/components/Picker/hooks/usePickerItems.ts (2)

84-97: Optimize search performance and handle empty groups

The filtering logic can be improved for better performance and user experience.

Consider these improvements:

 items = items
   .filter((item) => {
+    const search = searchPhrase.toLowerCase();
+    
     if (item.groupHeader) {
-      return true;
+      // Only show group headers if they have matching children
+      return items.some(child => 
+        !child.groupHeader && 
+        child.groupId === item.key && 
+        (child.name.toLowerCase().includes(search) || 
+         child.secondaryText?.toLowerCase().includes(search))
+      );
     }

-    const search = searchPhrase.toLowerCase();
     const itemName = item.name.toLowerCase();
-    const itemSecondaryText = item.secondaryText?.toLowerCase();
-
-    return (
-      itemName.includes(search) || itemSecondaryText?.includes(search)
-    );
+    return itemName.includes(search) || 
+           (item.secondaryText?.toLowerCase() || '').includes(search);
   })

98-104: Improve readability of group header cleanup logic

The consecutive group header removal logic is correct but could be more expressive.

Consider this more readable approach:

-        .filter(
-          (item, index, array) =>
-            !(
-              item.groupHeader &&
-              (array[index + 1]?.groupHeader || index === array.length - 1)
-            )
-        );
+        .filter((item, index, array) => {
+          if (!item.groupHeader) return true;
+          
+          const isLastItem = index === array.length - 1;
+          const nextItemIsHeader = array[index + 1]?.groupHeader;
+          
+          return !(isLastItem || nextItemIsHeader);
+        });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ceb0529 and d3fcf76.

📒 Files selected for processing (1)
  • packages/react-components/src/components/Picker/hooks/usePickerItems.ts (1 hunks)
🔇 Additional comments (1)
packages/react-components/src/components/Picker/hooks/usePickerItems.ts (1)

84-104: Verify group header visibility behavior

The implementation keeps group headers visible but might show empty groups. Let's verify the expected behavior.

✅ Verification successful

Group header behavior is correctly implemented

The implementation is verified through test cases that explicitly handle group headers:

  • Test case demonstrates proper group header handling with "Group A title header" and "Group B title header"
  • Headers remain visible while filtering disabled items
  • The filter implementation correctly preserves group structure
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find related test files and component usage
echo "Looking for related test files..."
fd -e test.ts -e test.tsx . | grep -i "picker"

echo "Finding component usage examples..."
rg -l "usePickerItems" --type ts --type tsx

Length of output: 276


Script:

#!/bin/bash
# Search for test files with improved pattern
fd "picker.*\.(test|spec)\.(ts|tsx)$" .

# Search for component usage with correct ripgrep syntax
echo "Finding component usage..."
rg -t typescript -t javascriptreact -t typescriptreact "usePickerItems"

# Look for picker-related test cases
echo "Searching for picker test cases..."
rg -t typescript -t typescriptreact "describe.*Picker|test.*Picker" -A 2

# Check implementation details
echo "Checking implementation details..."
rg -t typescript -t typescriptreact "groupHeader.*filter" -A 5 -B 5

Length of output: 1056


Script:

#!/bin/bash
# Check test cases for group header behavior
cat ./packages/react-components/src/components/Picker/Picker.spec.tsx

# Check implementation in PickerList
cat ./packages/react-components/src/components/Picker/components/PickerList.spec.tsx

# Look for usePickerItems implementation and tests
echo "Searching for usePickerItems tests..."
find . -type f -name "*.spec.tsx" -o -name "*.test.tsx" | xargs grep -l "usePickerItems"

Length of output: 12408

Copy link
Contributor

@ashbork ashbork left a comment

Choose a reason for hiding this comment

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

coderabbit has a point, we could make these filters more readable, but that's fine

@kubaczerwinski77 kubaczerwinski77 merged commit 8326e38 into main Dec 5, 2024
8 checks passed
@kubaczerwinski77 kubaczerwinski77 deleted the feature/HD-4644 branch December 5, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants