Skip to content

refactor: position page initial loading#126

Merged
antoncoding merged 2 commits intomasterfrom
opt/position-loading
Mar 21, 2025
Merged

refactor: position page initial loading#126
antoncoding merged 2 commits intomasterfrom
opt/position-loading

Conversation

@antoncoding
Copy link
Copy Markdown
Owner

@antoncoding antoncoding commented Mar 21, 2025

Summary by CodeRabbit

  • New Features

    • Enhanced the interface by adding distinct loading indicators for positions data and earnings, ensuring users receive clear feedback during data retrieval.
    • Improved the display and grouping of earnings information for a more intuitive viewing experience.
  • Refactor

    • Streamlined the data fetching and processing logic for positions and earnings to provide a smoother, more responsive interface.
    • Updated loading state management for better clarity and granularity in user feedback.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Mar 21, 2025

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

Name Status Preview Comments Updated (UTC)
monarch ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 21, 2025 6:16am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 21, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

The changes update the loading state handling in the Positions components and refactor earnings calculations. The UI now distinguishes between positions and earnings loading states. Several hooks no longer use a custom hook (usePositionSnapshot) but call the fetchPositionSnapshot function directly from a new utility file. Earnings grouping and collateral processing have been streamlined with new functions in the utility module, and an unused hook file has been removed.

Changes

Files Change Summary
app/.../PositionsContent.tsx, app/.../PositionsSummaryTable.tsx Renamed loading state (isLoadingisPositionsLoading), added isEarningsLoading prop, updated conditional rendering, and refactored earnings grouping and display logic.
src/hooks/.../usePositionReport.ts, src/hooks/.../useUserPosition.ts, src/hooks/.../useUserPositions.ts Removed use of usePositionSnapshot hook; now directly import and use fetchPositionSnapshot from the utils module.
src/hooks/.../useUserPositionsSummaryData.ts Removed calculateEarningsFromPeriod; split loading state into positions and earnings; updated state updates for position earnings.
src/hooks/.../usePositionSnapshot.ts Removed the custom hook usePositionSnapshot along with its associated types and error handling logic.
src/utils/.../positions.ts Added new utility file with functions and types for fetching position snapshots, calculating earnings, grouping positions, processing collaterals, and initializing positions with empty earnings.

Sequence Diagram(s)

sequenceDiagram
    participant C as Component (e.g., useUserPosition)
    participant U as Utils (fetchPositionSnapshot)
    participant A as API (Position Snapshot API)
    
    C->>U: Call fetchPositionSnapshot(marketId, userAddress, chainId, blockNumber)
    U->>A: Send GET request for snapshot data
    A-->>U: Return snapshot response
    U-->>C: Return processed position snapshot (or null)
Loading

Possibly related PRs

  • feat: rebalance #57: Updates loading state management in PositionsContent and PositionsSummaryTable, similar to the current changes.
  • feat: report #95: Modifies earnings loading and state management in PositionsSummaryTable, aligning with the revised approach.
  • feat: show interest earned (PNL) #88: Introduces the isLoadingEarnings prop and adjusts earnings handling, directly relating to the updates made.

Poem

Code now flows with states defined,
Positions and earnings clearly aligned.
Hooks refined with a simpler call,
Utility functions now serve us all.
A small step forward in every line,
Our code sings true, crisp and fine.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 66003c1 and 45b895a.

📒 Files selected for processing (1)
  • src/hooks/useUserPositionsSummaryData.ts (5 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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
Copy Markdown

@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)
src/hooks/useUserPositionsSummaryData.ts (1)

113-148: Sequential updates may affect performance
Processing positions in a for-loop is more granular but might slow down overall loading. Consider parallel computation if real-time partial updates are not critical.

src/utils/positions.ts (1)

223-312: Grouping by loan assets
Combines positions properly. Might consider using BigInt consistently for supply comparisons.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d50e446 and 66003c1.

📒 Files selected for processing (8)
  • app/positions/components/PositionsContent.tsx (3 hunks)
  • app/positions/components/PositionsSummaryTable.tsx (6 hunks)
  • src/hooks/usePositionReport.ts (1 hunks)
  • src/hooks/usePositionSnapshot.ts (0 hunks)
  • src/hooks/useUserPosition.ts (1 hunks)
  • src/hooks/useUserPositions.ts (1 hunks)
  • src/hooks/useUserPositionsSummaryData.ts (5 hunks)
  • src/utils/positions.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • src/hooks/usePositionSnapshot.ts
🧰 Additional context used
🧬 Code Definitions (2)
src/hooks/useUserPositionsSummaryData.ts (1)
src/utils/positions.ts (1) (1)
  • initializePositionsWithEmptyEarnings (356-368)
app/positions/components/PositionsSummaryTable.tsx (2)
src/utils/positions.ts (3) (3)
  • groupPositionsByLoanAsset (230-312)
  • processCollaterals (320-348)
  • getGroupedEarnings (205-221)
src/utils/balance.ts (1) (1)
  • formatBalance (21-23)
🔇 Additional comments (29)
app/positions/components/PositionsContent.tsx (3)

46-47: Good separation of loading states

Splitting the loading state into position-specific and earnings-specific states provides better granularity for UI feedback.


147-147: Updated conditional rendering with specific loading state

Correctly updated the conditional rendering to use the more specific isPositionsLoading state.


176-176: Added earnings loading state to child component

Properly passed the new earnings loading state to the table component.

src/hooks/useUserPositions.ts (1)

7-7: Direct import of utility function

Removing the hook dependency in favor of a direct utility import simplifies the code structure.

src/hooks/usePositionReport.ts (1)

7-7: Simplified import pattern

Replaced hook usage with direct function import, keeping consistent with the refactoring pattern.

src/hooks/useUserPosition.ts (1)

5-5: Direct utility function import

Consistent with changes in other files - replaced hook with direct function import.

src/hooks/useUserPositionsSummaryData.ts (5)

4-7: Rebranded function usage is consistent
The aliasing of calculateEarningsFromPeriod to calculateEarnings and usage of initializePositionsWithEmptyEarnings looks coherent and aligns with the rest of the code references.


42-46: Clear separation of loading states
Splitting position loading from earnings loading clarifies progress indicators and is easy to follow.


96-102: Immediate initialization for smoother UI
Initializing empty earnings allows the interface to show placeholders without blocking, which improves user experience.


104-105: Concise comment
The note "Calculate real earnings in the background" is self-explanatory and helpful.


161-162: Return statements look clean
Exporting isPositionsLoading and isEarningsLoading clarifies the hook’s API.

app/positions/components/PositionsSummaryTable.tsx (7)

9-9: Loader import is straightforward
Using PulseLoader for indicating progress is fine.


17-22: Relevant imports
Bringing in EarningsPeriod, grouping, and processing functions is aligned with the updated logic.


45-45: Optional loading indicator
isLoadingEarnings? is a convenient flag to control UI feedback.


83-85: Grouping approach
groupPositionsByLoanAsset usage is direct and keeps the table logic tidy.


87-89: Collateral post-processing
processCollaterals is neatly applied to shape the data for display.


192-192: Centralized earnings retrieval
Calling getGroupedEarnings here is concise and consistent.


231-235: Loader fallback
Displaying PulseLoader when isLoadingEarnings is true ensures immediate feedback for the user.

src/utils/positions.ts (11)

1-3: General imports
No issues with importing Address or formatBalance.


5-12: Types import
Bringing type definitions is consistent.


14-20: PositionSnapshot type
Straightforward structure, no concerns.


22-30: PositionResponse type
Fits well with the fetch logic.


32-83: Snapshot fetching
Basic error handling is in place. Consider handling timeouts or network errors more thoroughly if needed.


85-159: Earnings calculation
calculateEarningsFromPeriod leverages snapshots well. BigInt usage is valid.


161-169: Defined enum
EarningsPeriod is clear and easy to extend.


171-196: Simple period retrieval
getEarningsForPeriod is straightforward.


198-221: getGroupedEarnings
Aggregates position earnings elegantly.


314-348: Collateral processing
Sorting and consolidating the “others” category is tidy.


350-368: Initialization with empty earnings
Makes sense for immediate zero-state display.

@antoncoding antoncoding merged commit 14ea7ce into master Mar 21, 2025
2 of 4 checks passed
@antoncoding antoncoding deleted the opt/position-loading branch March 21, 2025 06:13
@coderabbitai coderabbitai Bot mentioned this pull request Apr 29, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Oct 24, 2025
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