mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
fix(frontend): Optimistically cache individual conversations from paginated results (#11510)
This commit is contained in:
parent
694ac74bb9
commit
3ec8d70d04
@ -1,14 +1,29 @@
|
||||
import { useInfiniteQuery } from "@tanstack/react-query";
|
||||
import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import ConversationService from "#/api/conversation-service/conversation-service.api";
|
||||
import { useIsAuthed } from "./use-is-authed";
|
||||
|
||||
export const usePaginatedConversations = (limit: number = 20) => {
|
||||
const { data: userIsAuthenticated } = useIsAuthed();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ["user", "conversations", "paginated", limit],
|
||||
queryFn: ({ pageParam }) =>
|
||||
ConversationService.getUserConversations(limit, pageParam),
|
||||
queryFn: async ({ pageParam }) => {
|
||||
const result = await ConversationService.getUserConversations(
|
||||
limit,
|
||||
pageParam,
|
||||
);
|
||||
|
||||
// Optimistically populate individual conversation caches
|
||||
result.results.forEach((conversation) => {
|
||||
queryClient.setQueryData(
|
||||
["user", "conversation", conversation.conversation_id],
|
||||
conversation,
|
||||
);
|
||||
});
|
||||
|
||||
return result;
|
||||
},
|
||||
enabled: !!userIsAuthenticated,
|
||||
getNextPageParam: (lastPage) => lastPage.next_page_id,
|
||||
initialPageParam: undefined as string | undefined,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user