-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat: add infinite query example #69
Open
kroucher
wants to merge
7
commits into
trpc:main
Choose a base branch
from
kroucher:next
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04decd5
feat: add infinite query example
kroucher 06629de
fix: remove console.log
kroucher 84c7002
fix: add loading div for initial load
kroucher b3c86fe
fix: bottomofpage div positioning
kroucher 5e5f749
fix: linting
kroucher 4756cf5
fix: height on bottomofpage div
kroucher c7fe0f6
fix: initial loading state
kroucher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { trpc } from 'utils/trpc'; | ||
|
||
export default function Page() { | ||
const bottomOfPage = useRef<HTMLDivElement>(null); | ||
const myQuery = trpc.infiniteQueryRouter.getItems.useInfiniteQuery( | ||
{ | ||
limit: 10, | ||
}, | ||
{ | ||
getNextPageParam: (lastPage) => lastPage.nextCursor, | ||
}, | ||
); | ||
const { data: posts, fetchNextPage, isFetchingNextPage } = myQuery; | ||
|
||
// Setup optional intersection observer to fetch next page when bottom of page is reached. | ||
// This is not needed if you have a "Load more" button or similar. | ||
useEffect(() => { | ||
const bottomDiv = bottomOfPage.current; | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
if (entries[0].isIntersecting) { | ||
fetchNextPage(); | ||
} | ||
}, | ||
{ threshold: 1 }, | ||
); | ||
if (bottomDiv) { | ||
observer.observe(bottomDiv); | ||
} | ||
return () => { | ||
if (bottomDiv) { | ||
observer.unobserve(bottomDiv); | ||
} | ||
}; | ||
}, [fetchNextPage]); | ||
|
||
return ( | ||
<> | ||
<h2 className="text-3xl font-bold py-8">Top Recent Posts from /r/Aww</h2> | ||
<div className="flex flex-col space-y-3 py-2 min-h-screen"> | ||
{posts ? ( | ||
posts.pages.map((page) => { | ||
return page.items.data.children.map((post) => ( | ||
<article | ||
key={post.data.id} | ||
className="bg-white shadow overflow-hidden sm:rounded-lg p-4 flex justify-start items-center space-x-2" | ||
> | ||
{/* upvote/downvote buttons */} | ||
<div className="flex flex-col gap-1"> | ||
<button className="w-6 h-6 flex items-center justify-center bg-gray-200 rounded-md group"> | ||
<svg | ||
className="w-4 h-4 text-gray-500 group-hover:text-orange-500" | ||
fill="currentColor" | ||
viewBox="0 0 20 20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
d="M10 17a1 1 0 01-1-1V7.414l-2.293 2.293a1 1 0 01-1.32.083l-.094-.083a1 1 0 01-.083-1.32l.083-.094 4-4a1 1 0 011.32-.083l.094.083 4 4a1 1 0 01-1.32 1.497l-.094-.083L11 7.414V16a1 1 0 01-1 1z" | ||
clipRule="evenodd" | ||
/> | ||
</svg> | ||
</button> | ||
<button className="w-6 h-6 flex items-center justify-center bg-gray-200 rounded-md group"> | ||
<svg | ||
className="w-4 h-4 text-gray-500 group-hover:text-orange-500" | ||
fill="currentColor" | ||
viewBox="0 0 20 20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
d="M10 3a1 1 0 011 1v8.586l2.293-2.293a1 1 0 011.32-.083l.094.083a1 1 0 01.083 1.32l-.083.094-4 4a1 1 0 01-1.32.083l-.094-.083-4-4a1 1 0 011.32-1.497l.094.083L9 13.586V5a1 1 0 011-1z" | ||
clipRule="evenodd" | ||
/> | ||
</svg> | ||
</button> | ||
</div> | ||
{/* post image */} | ||
<div className="flex-shrink-0"> | ||
<img | ||
className="h-16 w-16 rounded-md" | ||
src={post.data.thumbnail} | ||
alt={post.data.title} | ||
/> | ||
</div> | ||
{/* post content */} | ||
<div> | ||
{/* post title */} | ||
<div className="flex-1"> | ||
<a | ||
href={`https://www.reddit.com${post.data.permalink}`} | ||
target="_blank" | ||
rel="noreferrer" | ||
className="block hover:text-orange-500 text-sm" | ||
> | ||
{post.data.title} | ||
</a> | ||
</div> | ||
{/* post meta */} | ||
<div className="flex items-center text-xs sm:text-sm text-gray-500"> | ||
<span className="whitespace-pre-wrap"> | ||
Posted by{' '} | ||
<span className="font-medium">{post.data.author}</span>{' '} | ||
{new Date(post.data.created * 1000).toLocaleDateString()} | ||
</span> | ||
</div> | ||
</div> | ||
</article> | ||
)); | ||
}) | ||
) : ( | ||
<div>Loading...</div> | ||
)} | ||
|
||
<div ref={bottomOfPage} className="h-10" /> | ||
|
||
{isFetchingNextPage && <div>Loading...</div>} | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ExampleProps } from 'utils/ExamplePage'; | ||
|
||
export const meta: ExampleProps = { | ||
title: 'Infinite Query', | ||
href: '/infinite-query', | ||
// This is only enabled on the client as it will cause hydration error otherwise | ||
// The problem is that RSC won't send the right header whilst the client will, leading to inconsistent behavior | ||
clientOnly: true, | ||
summary: ( | ||
<> | ||
<p>Paginated queries with tRPC</p> | ||
</> | ||
), | ||
detail: ( | ||
<> | ||
<p>Using tRPC & paginated queries.</p> | ||
</> | ||
), | ||
files: [ | ||
{ title: 'Page', path: 'feature/infinite-query/index.tsx' }, | ||
{ title: 'Router', path: 'feature/infinite-query/router.tsx' }, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { t } from 'server/trpc/trpc'; | ||
import { z } from 'zod'; | ||
|
||
export type Items = { | ||
items: { | ||
data: { | ||
children: { | ||
data: { | ||
id: string; | ||
title: string; | ||
selftext: string; | ||
thumbnail: string; | ||
score: number; | ||
permalink: string; | ||
created: number; | ||
author: string; | ||
}; | ||
}[]; | ||
}; | ||
}; | ||
nextCursor?: string; | ||
}; | ||
|
||
export const infiniteQueryRouter = t.router({ | ||
getItems: t.procedure | ||
.input( | ||
z.object({ | ||
limit: z.number().min(1).max(100), | ||
cursor: z.string().optional(), | ||
}), | ||
) | ||
.query(async ({ input }) => { | ||
const { cursor, limit } = input; | ||
// set cursor to the previously returned nextCursor, or an empty string | ||
let currentCursor = cursor || ''; | ||
// fetch data from API | ||
do { | ||
const items = await fetch( | ||
`https://www.reddit.com/r/Aww/top.json?limit=${limit}?after=${currentCursor}`, | ||
).then((res) => res.json()); | ||
|
||
// Set the cursor to the 'after' value from the response if it exists, which is the cursor for the next page | ||
if (items.data.after) { | ||
currentCursor = items.data.after; | ||
} else { | ||
// If there is no 'after' value, we've reached the end of the list | ||
currentCursor = ''; | ||
} | ||
|
||
// Return the items and the cursor for the next page | ||
return { | ||
items, | ||
nextCursor: currentCursor, | ||
} as Items; | ||
} while (currentCursor !== '' || currentCursor !== undefined); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import FeaturePage from 'feature/infinite-query'; | ||
import { meta } from 'feature/infinite-query/meta'; | ||
import { ExamplePage } from 'utils/ExamplePage'; | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<ExamplePage {...meta}> | ||
<FeaturePage /> | ||
</ExamplePage> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The cursor should be validated so you can't inject anything into the url