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] API calls for Recent Bookmarks Sidebar #1099

Conversation

HarshitT00
Copy link
Contributor

✨ Codu Pull Request 💻

Fixes #(issue)

Pull Request details

Getting data and status in same query which is previously done saperately.

@HarshitT00 HarshitT00 requested a review from a team as a code owner October 9, 2024 15:25
Copy link
Contributor

coderabbitai bot commented Oct 9, 2024

Walkthrough

The pull request modifies the SideBarSavedPosts component in components/SideBar/SideBarSavedPosts.tsx. It consolidates the query results from api.post.myBookmarks.useQuery() into a single destructured assignment, simplifying the handling of loading and error states. The rendering logic is updated to utilize a new variable, limitedBookmarks, which slices the bookmarks array to show a maximum of three items. The conditions for displaying bookmarks and handling no bookmarks have been streamlined, enhancing the component's clarity and efficiency.

Changes

File Change Summary
components/SideBar/SideBarSavedPosts.tsx Consolidated query results into a single destructured assignment; updated rendering logic to use limitedBookmarks and streamlined no bookmarks condition.

Possibly related PRs

🐇 In the sidebar where bookmarks lay,
Three little treasures come out to play.
With a click and a glance, they shine so bright,
Saving the best for a cozy night.
No more clutter, just joy to see,
Recently saved posts, as happy as can be! 🌟


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

vercel bot commented Oct 9, 2024

@HarshitT00 is attempting to deploy a commit to the Codú Team on Vercel.

A member of the Team first needs to authorize it.

@HarshitT00 HarshitT00 marked this pull request as draft October 9, 2024 15:26
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 and nitpick comments (2)
components/SideBar/SideBarSavedPosts.tsx (2)

15-15: Good addition of limitedBookmarks, consider backend implementation

The introduction of limitedBookmarks is a good improvement. It centralizes the logic for limiting the number of bookmarks and handles potential undefined values well.

However, as noted in the TODO comment, consider implementing this slicing on the backend for better performance, especially as the number of bookmarks grows.

Would you like me to create a GitHub issue to track the implementation of backend slicing for bookmarks?


Line range hint 35-62: Improved success state rendering with a minor suggestion

The refactoring of the success state rendering is a significant improvement. The use of limitedBookmarks and the simplified conditional rendering enhance code clarity and maintainability.

However, there's a small improvement we can make:

In the SideBarSavedArticlePreview component, consider using a more unique key instead of just id. If id is guaranteed to be unique across all bookmarks, it's fine. But if there's a chance it might not be, you could create a more unique key by combining id and slug:

<SideBarSavedArticlePreview
  key={`${id}-${slug}`}
  // ... other props
/>

This ensures that each rendered component has a truly unique key, which is important for React's reconciliation process.

🧰 Tools
🪛 Biome

[error] 26-26: Missing key property for this element in iterable.

The order of the items may change, and having a key can help React identify which item was moved.
Check the React documentation.

(lint/correctness/useJsxKeyInIterable)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 751a74a and 28de4e6.

📒 Files selected for processing (1)
  • components/SideBar/SideBarSavedPosts.tsx (2 hunks)
🧰 Additional context used
🪛 Biome
components/SideBar/SideBarSavedPosts.tsx

[error] 26-26: Missing key property for this element in iterable.

The order of the items may change, and having a key can help React identify which item was moved.
Check the React documentation.

(lint/correctness/useJsxKeyInIterable)

🔇 Additional comments (3)
components/SideBar/SideBarSavedPosts.tsx (3)

9-9: Improved data fetching logic

The consolidation of the query result destructuring is a good improvement. It simplifies the code and directly uses the status from the query, which provides more detailed information about the query state.


29-33: Improved error state handling

The error state rendering using the status variable is consistent with earlier changes and improves code readability. The error message is clear and provides actionable information to the user.


Line range hint 1-94: Overall assessment: Significant improvements with minor suggestions

This PR successfully addresses its objectives by consolidating API calls and improving the efficiency of the SideBarSavedPosts component. The changes enhance code readability, simplify logic, and improve error handling.

Key improvements:

  1. Consolidated query result destructuring
  2. Introduction of limitedBookmarks for cleaner rendering logic
  3. Consistent use of status for conditional rendering
  4. Simplified success state rendering

Minor suggestions for further improvement:

  1. Implement backend slicing for bookmarks
  2. Optimize loading state rendering
  3. Use a more unique key for SideBarSavedArticlePreview components

These changes align well with the PR objectives and the AI-generated summary. Great work on improving this component!

🧰 Tools
🪛 Biome

[error] 26-26: Missing key property for this element in iterable.

The order of the items may change, and having a key can help React identify which item was moved.
Check the React documentation.

(lint/correctness/useJsxKeyInIterable)

Comment on lines +23 to 28
{status === "loading" &&
Children.toArray(
Array.from({ length: howManySavedToShow }, () => {
return <LoadingSkeleton />;
}),
)}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider optimizing the loading state rendering

The use of status for conditional rendering is good and consistent with earlier changes. However, the current implementation of loading skeletons can be optimized.

Instead of using Children.toArray(), consider using the index as a key, or better yet, create a simple array with a map function:

{status === "loading" && 
  Array.from({ length: howManySavedToShow }).map((_, index) => (
    <LoadingSkeleton key={index} />
  ))
}

This approach is more efficient and doesn't require the use of Children.toArray().

🧰 Tools
🪛 Biome

[error] 26-26: Missing key property for this element in iterable.

The order of the items may change, and having a key can help React identify which item was moved.
Check the React documentation.

(lint/correctness/useJsxKeyInIterable)

@@ -6,45 +6,43 @@ import SideBarSavedArticlePreview from "./SideBarSavedArticlePreview";
import Link from "next/link";

export default React.memo(function SideBarSavedPosts() {
let { data: bookmarks } = api.post.myBookmarks.useQuery();
const { status: bookmarkStatus } = api.post.myBookmarks.useQuery();
const { data: bookmarks, status } = api.post.myBookmarks.useQuery();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this line alone should be good! Maybe we can pass the amount of bookmarks to the useQuery() so that we only get the right amount back rather than doing this on the frontend?

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.

2 participants