-
-
Notifications
You must be signed in to change notification settings - Fork 173
[FIX] API calls for Recent Bookmarks Sidebar #1099
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider optimizing the loading state rendering The use of Instead of using {status === "loading" &&
Array.from({ length: howManySavedToShow }).map((_, index) => (
<LoadingSkeleton key={index} />
))
}This approach is more efficient and doesn't require the use of 🧰 Tools🪛 Biome
|
||
| {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 || ""} | ||
|
|
@@ -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"> | ||
|
|
||
There was a problem hiding this comment.
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?