buster/web/src/api/buster_rest/chats/queryRequests.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-02-08 10:57:42 +08:00
import { useCreateReactQuery } from '@/api/createReactQuery';
import { useMemoizedFn } from 'ahooks';
import { QueryClient } from '@tanstack/react-query';
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
export const useGetListChats = (params?: Parameters<typeof getListChats>[0]) => {
2025-02-08 10:57:42 +08:00
const queryFn = useMemoizedFn(() => {
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 || []
};
};
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 || {}],
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;
};