2025-02-08 10:57:42 +08:00
|
|
|
import { useCreateReactQuery } from '@/api/createReactQuery';
|
|
|
|
import { useMemoizedFn } from 'ahooks';
|
|
|
|
import { QueryClient } from '@tanstack/react-query';
|
2025-02-08 11:52:25 +08:00
|
|
|
import { getListChats, getListChats_server, getChat, getChat_server } from './requests';
|
|
|
|
import type { BusterChatListItem, BusterChat } from '@/api/asset_interfaces';
|
2025-02-08 10:57:42 +08:00
|
|
|
|
2025-02-08 11:52:25 +08:00
|
|
|
export const useGetListChats = (params?: Parameters<typeof getListChats>[0]) => {
|
2025-02-08 10:57:42 +08:00
|
|
|
const queryFn = useMemoizedFn(() => {
|
2025-02-08 11:52:25 +08:00
|
|
|
return getListChats(params);
|
2025-02-08 10:57:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const res = useCreateReactQuery<BusterChatListItem[]>({
|
|
|
|
queryKey: ['chats', 'list', params || {}],
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
data: res.data || []
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-02-08 11:52:25 +08:00
|
|
|
export const prefetchGetListChats = async (
|
|
|
|
params?: Parameters<typeof getListChats>[0],
|
2025-02-08 10:57:42 +08:00
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
queryKey: ['chats', 'list', params || {}],
|
2025-02-08 11:52:25 +08:00
|
|
|
queryFn: () => getListChats_server(params)
|
|
|
|
});
|
|
|
|
|
|
|
|
return queryClient;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGetChat = (params: Parameters<typeof getChat>[0]) => {
|
|
|
|
const queryFn = useMemoizedFn(() => {
|
|
|
|
return getChat(params);
|
|
|
|
});
|
|
|
|
|
|
|
|
return useCreateReactQuery<BusterChat>({
|
|
|
|
queryKey: ['chats', 'get', params.id],
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetChat = async (
|
|
|
|
params: Parameters<typeof getChat>[0],
|
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
queryKey: ['chats', 'get', params.id],
|
|
|
|
queryFn: () => getChat_server(params)
|
2025-02-08 10:57:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return queryClient;
|
|
|
|
};
|