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

70 lines
1.9 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';
2025-02-18 11:50:21 +08:00
import type { BusterChatListItem } from '@/api/asset_interfaces';
import { queryKeys } from '@/api/query_keys';
import { updateChatToIChat } from '@/utils/chat';
2025-02-08 10:57:42 +08:00
export const useGetListChats = (params?: Parameters<typeof getListChats>[0]) => {
2025-02-18 11:50:21 +08:00
const queryFn = useMemoizedFn((): Promise<BusterChatListItem[]> => {
return getListChats(params);
2025-02-08 10:57:42 +08:00
});
2025-02-18 11:50:21 +08:00
const res = useCreateReactQuery({
...queryKeys.chatsGetList(params),
2025-02-08 10:57:42 +08:00
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({
2025-02-18 11:50:21 +08:00
...queryKeys.chatsGetList(params),
queryFn: () => getListChats_server(params)
});
return queryClient;
};
export const useGetChat = (params: Parameters<typeof getChat>[0]) => {
2025-02-18 11:50:21 +08:00
const queryFn = useMemoizedFn(async () => {
const chat = await getChat(params);
const iChat = updateChatToIChat(chat, true).iChat;
return iChat;
});
2025-02-18 11:50:21 +08:00
return useCreateReactQuery({
...queryKeys.chatsGetChat(params.id),
queryFn,
enabled: true
});
};
export const prefetchGetChat = async (
params: Parameters<typeof getChat>[0],
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
2025-02-18 11:50:21 +08:00
...queryKeys.chatsGetChat(params.id),
queryFn: async () => {
const chat = await getChat_server(params);
const iChat = updateChatToIChat(chat, true).iChat;
return iChat;
}
2025-02-08 10:57:42 +08:00
});
return queryClient;
};