Repository navigation
Add pages to the feed so every prompt can be reached #160
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { pagesToShow } from "./FeedPagination"; | ||
|
|
||
| describe("pagesToShow", () => { | ||
| it("shows every page when they fit", () => { | ||
| expect(pagesToShow(1, 2)).toEqual([1, 2]); | ||
| expect(pagesToShow(4, 7)).toEqual([1, 2, 3, 4, 5, 6, 7]); | ||
| }); | ||
|
|
||
| it("keeps the first, last and nearby pages when they don't", () => { | ||
| expect(pagesToShow(1, 20)).toEqual([1, 2, null, 20]); | ||
| expect(pagesToShow(10, 20)).toEqual([1, null, 9, 10, 11, null, 20]); | ||
| expect(pagesToShow(20, 20)).toEqual([1, null, 19, 20]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { ChevronLeft, ChevronRight } from "lucide-react"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| interface FeedPaginationProps { | ||
| page: number; | ||
| pageSize: number; | ||
| totalRows: number; | ||
| onPageChange: (page: number) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Which pages to offer: all of them when they fit, otherwise the first, the | ||
| * last and the ones either side of the current page, with gaps as null. | ||
| */ | ||
| export function pagesToShow(page: number, totalPages: number): (number | null)[] { | ||
| if (totalPages <= 7) return Array.from({ length: totalPages }, (_, i) => i + 1); | ||
|
|
||
| const nearby = [1, page - 1, page, page + 1, totalPages] | ||
| .filter((p) => p >= 1 && p <= totalPages) | ||
| .filter((p, i, all) => all.indexOf(p) === i); | ||
|
|
||
| return nearby.flatMap((p, i) => (i > 0 && p - nearby[i - 1] > 1 ? [null, p] : [p])); | ||
| } | ||
|
|
||
| const CIRCLE = | ||
| "inline-flex h-10 w-10 items-center justify-center rounded-full text-sm font-medium tabular-nums transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"; | ||
|
|
||
| export function FeedPagination({ page, pageSize, totalRows, onPageChange }: FeedPaginationProps) { | ||
| const totalPages = Math.max(Math.ceil(totalRows / pageSize), page); | ||
|
|
||
| return ( | ||
| <nav aria-label="Feed pages" className="mt-10 sm:mt-14 flex items-center justify-center gap-1 sm:gap-1.5"> | ||
| {/* invisible, not removed, so the numbers don't shift between pages */} | ||
| <button | ||
| type="button" | ||
| aria-label="Previous page" | ||
| onClick={() => onPageChange(page - 1)} | ||
| className={cn(CIRCLE, "mr-1 sm:mr-2 bg-secondary/50 text-foreground hover:bg-secondary", page === 1 && "invisible")} | ||
| > | ||
| <ChevronLeft className="h-4 w-4" /> | ||
| </button> | ||
|
|
||
| {pagesToShow(page, totalPages).map((p, i) => | ||
| p === null ? ( | ||
| <span key={`gap-${i}`} aria-hidden="true" className="w-6 text-center text-sm text-muted-foreground"> | ||
| … | ||
| </span> | ||
| ) : ( | ||
| <button | ||
| key={p} | ||
| type="button" | ||
| aria-label={`Page ${p}`} | ||
| aria-current={p === page ? "page" : undefined} | ||
| onClick={() => onPageChange(p)} | ||
| className={cn( | ||
| CIRCLE, | ||
| p === page | ||
| ? "bg-primary text-primary-foreground" | ||
| : "text-muted-foreground hover:bg-secondary hover:text-foreground" | ||
| )} | ||
| > | ||
| {p} | ||
| </button> | ||
| ) | ||
| )} | ||
|
|
||
| <button | ||
| type="button" | ||
| aria-label="Next page" | ||
| onClick={() => onPageChange(page + 1)} | ||
| className={cn(CIRCLE, "ml-1 sm:ml-2 bg-secondary/50 text-foreground hover:bg-secondary", page >= totalPages && "invisible")} | ||
| > | ||
| <ChevronRight className="h-4 w-4" /> | ||
| </button> | ||
| </nav> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,14 +33,24 @@ export interface PromptWithDetails { | |
| ratingCount?: number; | ||
| } | ||
|
|
||
| export const FEED_PAGE_SIZE = 50; | ||
|
|
||
| // Sorting happens here rather than in the query, so a page is a slice of the | ||
| // sorted rows. Every page has to slice the same list or prompts repeat and go | ||
| // missing between pages, so early pages all share this many rows. Past this | ||
| // many prompts, Trending and Most copied need sorting in the database (#143). | ||
| const MIN_ROWS_TO_SORT = 200; | ||
|
|
||
| export function usePrompts(options?: { | ||
| selectedTags?: string[]; | ||
| searchQuery?: string; | ||
| sortBy?: "trending" | "newest" | "most_copied"; | ||
| limit?: number; | ||
| page?: number; | ||
| }) { | ||
| const { user, sessionLoading } = useAuth(); | ||
| const { selectedTags = [], searchQuery = "", sortBy = "trending", limit = 50 } = options || {}; | ||
| const { selectedTags = [], searchQuery = "", sortBy = "trending", limit = FEED_PAGE_SIZE, page = 1 } = options || {}; | ||
| const rowsToFetch = Math.max(page * limit * 2, MIN_ROWS_TO_SORT); | ||
|
|
||
| const trimmedQuery = searchQuery.trim(); | ||
| const isSearch = Boolean(trimmedQuery || selectedTags.length > 0); | ||
|
|
@@ -72,26 +82,26 @@ export function usePrompts(options?: { | |
| return score(b) - score(a); | ||
| } | ||
| }) | ||
| .slice(0, limit); | ||
| .slice((page - 1) * limit, page * limit); | ||
| }, | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| [tagsKey, sortBy, limit] | ||
| [tagsKey, sortBy, limit, page] | ||
| ); | ||
|
|
||
| return useQuery({ | ||
| const feedQuery = { | ||
| // The user id is in the key because isLiked and isSaved depend on it. | ||
| // When searching, include query and tags in the key to fetch via searchPrompts. | ||
| queryKey: isSearch | ||
| ? ["prompts", "search", trimmedQuery, tagsKey, limit, user?.id ?? null] | ||
| : promptsQueryKey(limit, user?.id), | ||
| ? ["prompts", "search", trimmedQuery, tagsKey, rowsToFetch, user?.id ?? null] | ||
| : promptsQueryKey(rowsToFetch, user?.id), | ||
| queryFn: async () => { | ||
| const { prompts: allPrompts, error } = isSearch | ||
| ? await searchPrompts({ | ||
| query: trimmedQuery || undefined, | ||
| tags: selectedTags.length > 0 ? selectedTags : undefined, | ||
| limit: limit * 2, | ||
| limit: rowsToFetch, | ||
| }) | ||
| : await getAllPrompts(limit * 2); | ||
| : await getAllPrompts(rowsToFetch); | ||
|
Comment on lines
+102
to
+104
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.
|
||
|
|
||
| if (error) throw error; | ||
|
|
||
|
|
@@ -145,13 +155,24 @@ export function usePrompts(options?: { | |
|
|
||
| return enrichedPrompts; | ||
| }, | ||
| select: selectFeed, | ||
| placeholderData: (previousData) => previousData, | ||
| placeholderData: (previousData: PromptWithDetails[] | undefined) => previousData, | ||
| // Waits only for the stored session, which is read locally. Waiting on the | ||
| // profile fetch as well held the whole feed back behind two extra round | ||
| // trips it does not need. | ||
| enabled: !sessionLoading, | ||
| }; | ||
|
|
||
| const query = useQuery({ ...feedQuery, select: selectFeed }); | ||
| const { data: rowCount = 0 } = useQuery({ | ||
| ...feedQuery, | ||
| select: (rows: PromptWithDetails[]) => rows.length, | ||
| }); | ||
|
|
||
| return { | ||
| ...query, | ||
| hasNextPage: rowCount > page * limit, | ||
| totalRows: rowCount, | ||
| }; | ||
| } | ||
|
|
||
| export function useTopCreators(limit = 6) { | ||
|
|
||
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.
Trending and Most copied repeat or skip prompts after page 2 once the feed exceeds 200 prompts.
src/hooks/usePrompts.ts:53sets the fetch size topage * limit * 2. Pages 1 and 2 fetch the newest 200 rows. Page 3 fetches the newest 300 rows. The hook sorts each window in memory and then slices it at line 85. When Trending or Most copied ranks a row from the larger window above rows already shown, those rows shift across the page boundary. One prompt then repeats, and another is skipped. Rows older than the newest window also cannot rank, even on page 1.Newest is not affected, because its top 200 rows are a prefix of its top 300 rows. The defect does not occur while the feed has 200 or fewer prompts. The code comment at lines 38-41 already notes this limit (
#143).Sort in the database and page with
.range(), or take a stable snapshot of one ordered list. Each page then reads from the same ordering.