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 resource filter for contributions #2596

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open

Conversation

bob0005
Copy link
Collaborator

@bob0005 bob0005 commented Dec 25, 2024

Summary by CodeRabbit

  • New Features
    • Introduced a SelectResource component for selecting resource types.
    • Enhanced contribution display with clickable player addresses that copy to clipboard.
  • Improvements
    • Optimized contributor sorting and filtering based on selected resources.
    • Improved state management for resource selection.
  • Bug Fixes
    • Clarified selection state management when resources are deselected.

Copy link

vercel bot commented Dec 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
eternum ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 25, 2024 1:55pm
eternum-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 25, 2024 1:55pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
eternum-landing ⬜️ Ignored (Inspect) Visit Preview Dec 25, 2024 1:55pm

Copy link
Contributor

coderabbitai bot commented Dec 25, 2024

Walkthrough

The pull request introduces a new SelectResource component that enhances the ContributionSummary component by allowing users to select specific resource types. It adds a selectedResource state variable, optimizes contributor sorting with useMemo, and implements a utility function copyPlayerAddressToClipboard that supports optional hexadecimal formatting. The changes improve the filtering logic for contributions and update the user interface to facilitate better resource management and interaction.

Changes

File Change Summary
client/src/ui/components/hyperstructures/ContributionSummary.tsx - Added selectedResource state variable
- Refactored sortedContributors using useMemo
- Enhanced contributor filtering logic
- Updated rendering to include clickable player addresses
client/src/ui/elements/SelectResource.tsx - Modified onClick handler for Cross component to reset selection
- Updated onValueChange in Select to handle resource deselection
client/src/ui/utils/utils.tsx - Extended copyPlayerAddressToClipboard with optional hex parameter
- Added support for hexadecimal address formatting

Possibly related PRs

Suggested reviewers

  • aymericdelab
  • ponderingdemocritus

Poem

🐰 Hop, hop, resources in sight,

Selecting with digital might!

Clipboard's embrace, addresses clear,

Contributions dancing without fear.

Code's magic, a rabbit's delight! 🌟


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

Copy link
Contributor

@mentatbot mentatbot bot left a comment

Choose a reason for hiding this comment

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

The PR adds a nice resource filtering feature to the contributions view. The implementation is solid but could benefit from a few improvements:

  1. The useMemo dependencies need to be fixed to prevent stale data
  2. The clipboard copy notification could be improved
  3. Consider adding keyboard navigation to the resource filter
  4. Consider persisting the filter state in URL parameters
  5. Adding a count of filtered vs total contributors would improve UX

Thanks for using MentatBot. Give comments a 👍 or 👎 to help me improve!

}
return b.percentage - a.percentage;
}),
[groupedContributions, selectedResource],
Copy link
Contributor

Choose a reason for hiding this comment

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

The useMemo dependency array should include hyperstructureEntityId and resourceContributions since they are used in the memoized calculation

Suggested change
[groupedContributions, selectedResource],
[groupedContributions, selectedResource, hyperstructureEntityId, resourceContributions],

@@ -258,9 +258,9 @@ export const formatTime = (
return parts.join(" ");
};

export const copyPlayerAddressToClipboard = (address: ContractAddress, name: string) => {
export const copyPlayerAddressToClipboard = (address: ContractAddress, name: string, hex: boolean = false) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using a more modern notification system like toast instead of alert() for better UX

Copy link

Failed to generate code suggestions for PR

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

🧹 Nitpick comments (1)
client/src/ui/components/hyperstructures/ContributionSummary.tsx (1)

53-74: Efficient filtering & sorting logic.

  • Filtering by selectedResource in lines 62-63 is well-structured, but ensure a resource ID of 0 isn’t inadvertently filtered out.
  • Sorting logic in lines 65-72 successfully prioritizes the selected resource, otherwise uses overall percentage.

Consider robust null checks for resources[selectedResource] to avoid potential undefined references if there's data mismatch.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d668b3e and 71868f8.

📒 Files selected for processing (3)
  • client/src/ui/components/hyperstructures/ContributionSummary.tsx (3 hunks)
  • client/src/ui/elements/SelectResource.tsx (1 hunks)
  • client/src/ui/utils/utils.tsx (1 hunks)
🔇 Additional comments (7)
client/src/ui/elements/SelectResource.tsx (1)

79-82: Confirm string vs. numeric consistency when clearing resource selection.

The state variable selectedResource is tracked as a string in this component but is conveyed to onSelect(null) as a numeric-or-null type outside. This works fine for clearing, but ensure consumer code properly manages string vs. number states in other parts of the code as well.

client/src/ui/utils/utils.tsx (1)

261-263: Optional hex parameter adds flexibility, looks good.

Allowing the address to be copied in hexadecimal format is a valuable enhancement. Just ensure any calling site verifies if hex should indeed be true or sticks to the default false.

client/src/ui/components/hyperstructures/ContributionSummary.tsx (5)

4-4: Import of SelectResource is clear.

The import statement correctly reflects the new SelectResource component. Code style looks consistent with the rest of the imports.


5-5: Importing the modified utility function.

Invoking copyPlayerAddressToClipboard with the new hex flag is straightforward. Ensure consistent usage throughout the app if multiple address formats are required.


7-7: Memo & state usage for performance.

The introduction of useState and useMemo is appropriate for optimizing re-renders. This can help prevent unnecessary recalculations when the resource selection changes.


50-50: Initialize resource selection state to null.

Using null to denote no resource is selected aligns well with the onSelect API in SelectResource. Good consistency between parent and child.


86-117: Displaying contributor details and copying addresses.

  • Adding the SelectResource above the contributors list is intuitive, ensuring the user picks a resource filter before the list is shown.
  • The clickable address with copyPlayerAddressToClipboard(..., true) is a neat enhancement. Verify that a hex representation is always desired in this context.
  • The .filter() call in lines 105-106 elegantly checks either the selected resource or all resources.

Unit tests verifying the filter and copy functionalities would further solidify correctness.

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

🧹 Nitpick comments (2)
client/src/ui/components/hyperstructures/ContributionSummary.tsx (2)

97-102: Add error handling for address copy operation

The click handler for copying addresses should include error handling to provide feedback to users when the operation fails.

 <div
-  onClick={() => copyPlayerAddressToClipboard(ContractAddress(playerAddress), addressName, true)}
+  onClick={() => {
+    try {
+      copyPlayerAddressToClipboard(ContractAddress(playerAddress), addressName, true);
+    } catch (error) {
+      console.error('Failed to copy address:', error);
+      // Consider adding a toast notification here
+    }
+  }}
   className="text-sm font-bold cursor-pointer hover:text-gold/50"
 >
   {addressName}
 </div>

Line range hint 10-119: Consider breaking down the component for better maintainability

The ContributionSummary component currently handles multiple responsibilities (filtering, sorting, and rendering). Consider splitting it into smaller, focused components:

  1. ContributorsList: Handle the list rendering
  2. ContributorCard: Handle individual contributor display
  3. ContributionSorter: Handle the sorting logic

This would improve maintainability and make the code more testable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 71868f8 and 813bb2d.

📒 Files selected for processing (1)
  • client/src/ui/components/hyperstructures/ContributionSummary.tsx (4 hunks)
🔇 Additional comments (3)
client/src/ui/components/hyperstructures/ContributionSummary.tsx (3)

Line range hint 4-20: LGTM: Clean import organization and state management

The new imports and state management are well-structured and follow React best practices.


43-48: LGTM: Well-implemented filtering logic

The resource filtering implementation is clean and type-safe, with proper handling of the selectedResource state.


55-75: Add missing dependencies to useMemo

The dependency array is missing hyperstructureEntityId and resourceContributions which are used in the memoized calculation.

-    [groupedContributions, selectedResource],
+    [groupedContributions, selectedResource, hyperstructureEntityId, resourceContributions],

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.

1 participant