-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseMessage.ts
More file actions
40 lines (36 loc) · 1.06 KB
/
useMessage.ts
File metadata and controls
40 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { getMessages, getMyMessageDetail, getMyMessages } from '@/apis/message'
import { CACHE_TIME, STALE_TIME } from '@/constants/cache'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
const MESSAGE_SIZE = 10
export const useGetMessages = () => {
return useInfiniteQuery({
queryKey: ['messages'],
queryFn: ({ pageParam = 0 }) => getMessages(pageParam, MESSAGE_SIZE),
getNextPageParam: (lastPage, allPages) => {
if (lastPage && lastPage.openedLetters.length < MESSAGE_SIZE) {
return undefined
}
return allPages.length
},
initialPageParam: 0,
staleTime: STALE_TIME,
gcTime: CACHE_TIME,
})
}
export const useGetMyMessages = () => {
return useQuery({
queryKey: ['my-messages'],
queryFn: () => getMyMessages(),
staleTime: STALE_TIME,
gcTime: CACHE_TIME,
})
}
export const useGetMyMessageDetail = (id: string) => {
return useQuery({
queryKey: ['my-message-detail', id],
queryFn: () => getMyMessageDetail(id),
staleTime: STALE_TIME,
gcTime: CACHE_TIME,
retry: 0,
})
}