Files
chad/new-client/src/widgets/chat/api/qChatMessages.ts
2026-05-29 04:28:09 +06:00

30 lines
892 B
TypeScript

import type { ChatMessage, ResponseError } from '@shared/api/generated-chad-api.ts'
import type { QueryKey } from '@tanstack/vue-query'
import api from '@shared/api/client.ts'
import { useInfiniteQuery } from '@tanstack/vue-query'
interface Response {
messages: ChatMessage[]
nextCursor?: string
}
type PageParam = string | undefined
export function qChatMessages() {
return useInfiniteQuery<Response, ResponseError, ChatMessage[], QueryKey, PageParam>({
queryKey: ['chat-messages'],
queryFn: async ({ pageParam }) => {
const response = await api.chad.chatMessages({ cursor: pageParam, limit: 25 })
return response.data
},
select: (data) => {
return data.pages.flatMap(page => page.messages).toReversed()
},
initialPageParam: undefined,
getNextPageParam: (lastPage) => {
return lastPage.nextCursor
},
shallow: true,
})
}