Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions components/SideBar/SideBarSavedPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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?


const howManySavedToShow = 3;
const totalNumberSaved = bookmarks?.length;

// @TODO query the backend to get the last 3 instead of slice
if (bookmarks) bookmarks = bookmarks.slice(0, howManySavedToShow);
const limitedBookmarks = bookmarks?.slice(0, howManySavedToShow) ?? [];

return (
<div className="w-full">
<h3 className="mb-4 mt-8 text-2xl font-semibold leading-6 tracking-wide">
Recent bookmarks
</h3>
<div className="w-full">
{bookmarkStatus === "loading" &&
{status === "loading" &&
Children.toArray(
Array.from({ length: howManySavedToShow }, () => {
return <LoadingSkeleton />;
}),
)}
Comment on lines +23 to 28
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)

{bookmarkStatus === "error" && (
{status === "error" && (
<p className="py-4 font-medium">
Something went wrong fetching your saved posts... Refresh the page.
</p>
)}

{bookmarks &&
bookmarkStatus === "success" &&
bookmarks.map(
({
id,
slug,
title,
user: { name, username },
published,
readTimeMins,
}) => {
return (
{status === "success" &&
(limitedBookmarks.length ? (
limitedBookmarks.map(
({
id,
slug,
title,
user: { name, username },
published,
readTimeMins,
}) => (
<SideBarSavedArticlePreview
key={id}
username={username || ""}
Expand All @@ -54,15 +52,14 @@ export default React.memo(function SideBarSavedPosts() {
date={published}
readTime={readTimeMins}
/>
);
},
)}
{bookmarkStatus === "success" && bookmarks?.length === 0 && (
<p className="py-4 font-medium">
Recently Saved posts will be displayed in this section for easy
access.
</p>
)}
),
)
) : (
<p className="py-4 font-medium">
Recently Saved posts will be displayed in this section for easy
access.
</p>
))}
</div>
{(totalNumberSaved && totalNumberSaved > howManySavedToShow && (
<Link href="/saved" className="secondary-button w-full">
Expand Down
Loading