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

fix: underway votes type #1625

Merged
merged 7 commits into from
Nov 3, 2024
Merged

Conversation

AMIRKHANEF
Copy link
Member

@AMIRKHANEF AMIRKHANEF commented Nov 2, 2024

Close: #1562

Summary by CodeRabbit

  • New Features

    • Introduced a tooltip component for displaying information about locked amounts, enhancing user experience.
    • Improved vertical alignment of balance display in the voting section for better readability.
  • Bug Fixes

    • Enhanced type safety in voting-related functions to prevent errors during evaluations.
  • Refactor

    • Streamlined component structure for improved clarity and maintainability.
    • Simplified logic in the getLocks function for consistent handling of locked variables.

Copy link
Contributor

coderabbitai bot commented Nov 2, 2024

Walkthrough

The pull request introduces several enhancements to the governance components within the application. Key changes include the creation of a new tooltip component for displaying locked amounts, refactoring of the VotingInformation component to improve prop handling, and adjustments to type safety in utility functions. Additionally, layout modifications are made to the MyVote component for better alignment. The getLocks function in the useAccountLocks hook is simplified to ensure consistent handling of locked amounts. Overall, the changes focus on improving readability, maintainability, and type safety.

Changes

File Path Change Summary
packages/extension-polkagate/src/fullscreen/governance/delegate/partial/AmountWithOptionsAndLockAmount.tsx Added AlreadyLockedTooltipText component; refactored VotingInformation to accept props directly; improved tooltip logic; streamlined imports and JSX structure.
packages/extension-polkagate/src/fullscreen/governance/post/myVote/index.tsx Added alignItems='center' to <Grid> for vertical alignment; no changes to existing logic.
packages/extension-polkagate/src/fullscreen/governance/post/myVote/util.tsx Updated isAye and getConviction functions for type handling; changed prior and delegating types from any to unknown.
packages/extension-polkagate/src/hooks/useAccountLocks.ts Simplified getLocks function by unconditionally assigning the locked variable.

Assessment against linked issues

Objective Addressed Explanation
Fix wrong locked amount information on Delegate page (#1562)

Possibly related PRs

🐇 "In the fields where the votes sway,
A tooltip now shows what to say.
With locks and amounts, all clear as day,
Our governance shines in a better way!
So hop along, let’s make it bright,
For every vote counts, and that feels right!" 🌟


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

@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

🧹 Outside diff range and nitpick comments (3)
packages/extension-polkagate/src/fullscreen/governance/delegate/partial/AmountWithOptionsAndLockAmount.tsx (2)

41-41: Remove debug console.log statement.

Debug logging should be removed before merging to production.

-  console.log('accountLocks :::', accountLocks);

131-131: Remove debug console.log statement.

Debug logging should be removed before merging to production.

-  console.log('accountLocks:', accountLocks);
packages/extension-polkagate/src/hooks/useAccountLocks.ts (1)

58-59: Consider adding tests for lock conviction scenarios.

To prevent future regressions and ensure correct handling of locked amounts, consider adding test cases that verify the behavior for different voting scenarios (approved, rejected, ongoing) and their corresponding conviction types.

Would you like me to help generate comprehensive test cases for the getLocks function?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e38ff29 and 40e24b9.

📒 Files selected for processing (4)
  • packages/extension-polkagate/src/fullscreen/governance/delegate/partial/AmountWithOptionsAndLockAmount.tsx (4 hunks)
  • packages/extension-polkagate/src/fullscreen/governance/post/myVote/index.tsx (1 hunks)
  • packages/extension-polkagate/src/fullscreen/governance/post/myVote/util.tsx (2 hunks)
  • packages/extension-polkagate/src/hooks/useAccountLocks.ts (1 hunks)
🔇 Additional comments (8)
packages/extension-polkagate/src/fullscreen/governance/post/myVote/index.tsx (2)

65-65: LGTM: Improved vertical alignment

The addition of alignItems='center' to the Grid container improves the vertical alignment consistency of the balance and conviction information.


68-68: Verify the type casting of voteBalance

The casting of voteBalance through unknown to BN type seems like a workaround. This could potentially mask type-related issues.

Let's verify the type definitions and usage:

packages/extension-polkagate/src/fullscreen/governance/post/myVote/util.tsx (2)

69-69: Please clarify the TODO comment and provide proper typing.

The comment "needs to be fixed" suggests this is a temporary solution. Given that this PR aims to fix locked amount information, we should:

  1. Determine the correct type for delegating
  2. Update the type accordingly
  3. Remove the TODO comment

The commented-out code below suggests there's more work needed here.

Let's analyze the usage to determine the correct type:

#!/bin/bash
# Search for the structure of delegating data to determine proper typing
ast-grep --pattern 'delegating: $_'

Would you like help determining the correct type for the delegating property?


63-63: LGTM! Verify usage of prior property.

The type change from any to unknown improves type safety. This is a good change that enforces proper type checking.

Let's verify the impact of this type change:

✅ Verification successful

Let me analyze the usage patterns to verify the impact of changing prior from any to unknown.


Based on the search results, I can see that the prior property is used in the Vote interface in packages/extension-polkagate/src/fullscreen/governance/post/myVote/util.tsx and is primarily used in the governance voting context. The type change from any to unknown is a type-safety improvement that doesn't appear to cause any issues, as the property is mostly used for type checking and passing through the voting system without direct manipulation of its values.

Type change to unknown is safe and properly implemented.

The codebase shows that the prior property is accessed in a type-safe manner through the governance voting system, and the change to unknown aligns with TypeScript best practices without breaking existing functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of the prior property to ensure they handle the unknown type correctly
rg -A 3 "\.prior\b" 

Length of output: 131


Script:

#!/bin/bash
# Search for usages of prior property with more context and case-insensitive
rg -i "prior" -A 5 --type typescript

# Also search for Vote interface usage
ast-grep --pattern 'Vote {
  $$$
}'

# Search for Vote type references
rg "Vote[\[\]\s{}<>]" --type typescript

Length of output: 186


Script:

#!/bin/bash
# Search for usages of prior property with more context
rg -i "prior" -A 5

# Also search for Vote interface usage and type references
rg "Vote[\[\]\s{}<>]"

# Find TypeScript/JavaScript files that might use the Vote interface
fd "\.(ts|tsx|js|jsx)$" --exec grep -l "Vote"

Length of output: 42496

packages/extension-polkagate/src/fullscreen/governance/delegate/partial/AmountWithOptionsAndLockAmount.tsx (3)

33-38: LGTM! Well-structured type definition.

The AlreadyLockedTooltipTextProps interface clearly defines the required props for the tooltip component.


Line range hint 128-157: LGTM! Well-structured component implementation.

The component properly handles props and maintains good separation of concerns. The use of translation and child components is well organized.


40-80: Verify the locked amount calculation logic.

The new AlreadyLockedTooltipText component provides detailed information about locked amounts, which should help address the core issue. Let's verify the calculation logic:

✅ Verification successful

Based on the code analysis, I can now generate the final response:

The locked amount calculation and display implementation is correct and properly handles all cases.

The implementation correctly:

  • Uses getAlreadyLockedValue to calculate total locked amount by sorting locks by amount and type
  • Properly displays lock details including reference IDs, amounts, multipliers, and expiry times
  • Handles edge cases like 'None' locks and ongoing locks
  • Correctly formats amounts using amountToHuman with proper decimal places
  • Maintains type safety through proper TypeScript interfaces

The tooltip implementation aligns with the PR's objective of fixing wrong locked amount information by providing clear, detailed breakdowns of all locks.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for related locked amount calculations
rg -A 5 "getLocks|lockedAmount|votingBalance" --type typescript

Length of output: 100


Script:

#!/bin/bash
# Search for related locked amount calculations without file type restriction
rg -A 5 "getLocks|lockedAmount|votingBalance"

# Also search for specific lock-related functions
ast-grep --pattern 'function $_(locks$_) {
  $$$
}'

Length of output: 37447


Script:

#!/bin/bash
# Search for lock-related functions and their implementations
ast-grep --pattern 'function getAlreadyLockedValue($_) {
  $$$
}'

# Also search for lock-related imports and constants
rg "import.*Lock|const.*Lock" -A 2

Length of output: 42111


Script:

#!/bin/bash
# Search for getAlreadyLockedValue implementation
rg -A 10 "getAlreadyLockedValue" --no-filename

# Search for lock amount calculation logic
rg -A 5 "total.*lock|lock.*total" --no-filename

Length of output: 12227

packages/extension-polkagate/src/hooks/useAccountLocks.ts (1)

58-59: Verify the impact of unconditional conviction type assignment.

The change to always set locked = vote.conviction.type regardless of vote outcome looks correct, as it better reflects the actual conviction type chosen by the voter. This should help fix the wrong locked amount information issue mentioned in #1562.

Let's verify this doesn't affect other parts of the codebase:

✅ Verification successful

Based on the gathered information, I can now generate the final response:

The change to unconditionally set conviction type is correct and safe.

The code analysis reveals that:

  • The conviction type is only used for UI display purposes, showing lock information in delegate-related components
  • The UI components correctly handle and display the conviction-based locks (e.g., in SupportUs.tsx: "tokens will be temporarily locked based on your chosen conviction level")
  • The useTimeToUnlock hook, which depends on these locks, works with the total amounts and end blocks, not the conviction type itself
  • No test files were found that verify this behavior, suggesting it's primarily a UI-focused change
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any UI components that might depend on the locked value
rg -l "locked.*conviction" --type ts --type tsx

# Search for test files that verify lock behavior
rg -l "getLocks.*conviction" --type test

Length of output: 149


Script:

#!/bin/bash
# Search for any UI components that might depend on the locked value, using proper file extensions
rg -l "locked.*conviction" -g '*.ts' -g '*.tsx'

# Search for test files that verify lock behavior
rg -l "getLocks.*conviction" -g '*.test.ts' -g '*.spec.ts'

# Let's also check for any direct usage of useAccountLocks hook
rg -l "useAccountLocks" -g '*.ts' -g '*.tsx'

# And look for any similar conviction type assignments
ast-grep --pattern 'locked = $_.conviction.type'

Length of output: 1743


Script:

#!/bin/bash
# Let's check the context of how locked conviction is used in the UI components
rg "locked.*conviction" -A 5 -B 5 packages/extension-polkagate/src/fullscreen/governance/old-SupportUs.tsx packages/extension-polkagate/src/fullscreen/governance/SupportUs.tsx

# Let's also check how the useAccountLocks hook is used in delegate-related components
rg "useAccountLocks" -A 3 -B 3 packages/extension-polkagate/src/fullscreen/governance/delegate/

# And check the implementation of useTimeToUnlock which might depend on this behavior
cat packages/extension-polkagate/src/hooks/useTimeToUnlock.tsx

Length of output: 17057

@AMIRKHANEF AMIRKHANEF force-pushed the fixUnderwayVotesType branch from b19e8c0 to 84e7c9a Compare November 3, 2024 05:30
@Nick-1979 Nick-1979 merged commit 42be4e2 into PolkaGate:main Nov 3, 2024
5 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 3, 2024
## [0.21.5](v0.21.4...v0.21.5) (2024-11-03)

### Bug Fixes

* underway votes type ([#1625](#1625)) ([42be4e2](42be4e2))
@AMIRKHANEF AMIRKHANEF deleted the fixUnderwayVotesType branch November 3, 2024 12:16
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.

fix: wrong locked amount information on Delegate page
2 participants