-
Notifications
You must be signed in to change notification settings - Fork 24
feat(ui): implement cursor-based pagination for messages #98
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
Open
gabrycina
wants to merge
2
commits into
main
Choose a base branch
from
feature/cursor-pagination-ui-clean
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
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,30 @@ | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| type SpinnerProps = { | ||
| className?: string; | ||
| }; | ||
|
|
||
| export function Spinner({ className }: SpinnerProps) { | ||
| return ( | ||
| <svg | ||
| className={cn('animate-spin', className)} | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| fill="none" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <circle | ||
| className="opacity-25" | ||
| cx="12" | ||
| cy="12" | ||
| r="10" | ||
| stroke="currentColor" | ||
| strokeWidth="4" | ||
| /> | ||
| <path | ||
| className="opacity-75" | ||
| fill="currentColor" | ||
| d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" | ||
| /> | ||
| </svg> | ||
| ); | ||
| } |
This file contains hidden or 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,84 @@ | ||
| import { useInfiniteQuery } from '@tanstack/react-query'; | ||
|
|
||
| import type AgentexSDK from 'agentex'; | ||
| import type { | ||
| MessageListPaginatedResponse, | ||
| TaskMessage, | ||
| } from 'agentex/resources'; | ||
|
|
||
| // Re-export for use in other hooks | ||
| export type { MessageListPaginatedResponse }; | ||
|
|
||
| export const infiniteTaskMessagesKeys = { | ||
| all: ['infiniteTaskMessages'] as const, | ||
| byTaskId: (taskId: string) => | ||
| [...infiniteTaskMessagesKeys.all, taskId] as const, | ||
| }; | ||
|
|
||
| export type InfiniteTaskMessagesData = { | ||
| messages: TaskMessage[]; | ||
| hasMore: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Fetches task messages with infinite scroll pagination. | ||
| * | ||
| * Uses cursor-based pagination to efficiently load older messages | ||
| * as the user scrolls up. Messages are returned in chronological order | ||
| * (oldest first) for display. | ||
| * | ||
| * @param agentexClient - AgentexSDK - The SDK client used to fetch messages | ||
| * @param taskId - string - The unique ID of the task whose messages to retrieve | ||
| * @param limit - number - Number of messages to fetch per page (default: 50) | ||
| * @returns UseInfiniteQueryResult with messages and pagination controls | ||
| * | ||
| * @example | ||
| * const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = | ||
| * useInfiniteTaskMessages({ agentexClient, taskId }); | ||
| * | ||
| * // Load more older messages when user scrolls to top | ||
| * if (hasNextPage && !isFetchingNextPage) { | ||
| * fetchNextPage(); | ||
| * } | ||
| */ | ||
| export function useInfiniteTaskMessages({ | ||
| agentexClient, | ||
| taskId, | ||
| limit = 50, | ||
| }: { | ||
| agentexClient: AgentexSDK; | ||
| taskId: string; | ||
| limit?: number; | ||
| }) { | ||
| return useInfiniteQuery({ | ||
| queryKey: infiniteTaskMessagesKeys.byTaskId(taskId), | ||
| queryFn: async ({ pageParam }): Promise<MessageListPaginatedResponse> => { | ||
| return agentexClient.messages.listPaginated({ | ||
| task_id: taskId, | ||
| limit, | ||
| direction: 'older', | ||
| ...(pageParam && { cursor: pageParam }), | ||
| }); | ||
| }, | ||
| getNextPageParam: lastPage => { | ||
| // Return next_cursor if there are more messages | ||
| return lastPage.has_more ? lastPage.next_cursor : undefined; | ||
| }, | ||
| initialPageParam: undefined as string | undefined, | ||
| enabled: !!taskId, | ||
| // Transform pages into a flat, chronologically ordered array | ||
| select: data => { | ||
| // Flatten all pages and reverse to get chronological order | ||
| const allMessages = data.pages.flatMap(page => page.data); | ||
| // Messages come newest first, so reverse for chronological order | ||
| const chronologicalMessages = allMessages.slice().reverse(); | ||
|
|
||
| return { | ||
| pages: data.pages, | ||
| pageParams: data.pageParams, | ||
| messages: chronologicalMessages, | ||
| hasMore: data.pages[data.pages.length - 1]?.has_more ?? false, | ||
| }; | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
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.
We already have an implementation of a loading spinner for infinite scrolling in the task-sidebar — lets choose one or the other to keep it consistent