2025-03-08 07:02:56 +08:00
|
|
|
import { useMemoizedFn } from '@/hooks';
|
2025-03-11 23:33:57 +08:00
|
|
|
import { QueryClient, useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import {
|
|
|
|
getListChats,
|
|
|
|
getListChats_server,
|
|
|
|
getChat,
|
|
|
|
getChat_server,
|
|
|
|
updateChat,
|
|
|
|
deleteChat
|
|
|
|
} from './requests';
|
2025-03-11 23:38:49 +08:00
|
|
|
import type { IBusterChat } from '@/api/asset_interfaces/chat';
|
2025-02-18 11:50:21 +08:00
|
|
|
import { queryKeys } from '@/api/query_keys';
|
2025-02-22 03:07:30 +08:00
|
|
|
import { updateChatToIChat } from '@/lib/chat';
|
2025-03-06 03:14:24 +08:00
|
|
|
import { RustApiError } from '@/api/buster_rest/errors';
|
2025-03-12 00:11:34 +08:00
|
|
|
import { useMemo } from 'react';
|
2025-02-08 10:57:42 +08:00
|
|
|
|
2025-03-12 00:11:34 +08:00
|
|
|
export const useGetListChats = (
|
|
|
|
filters?: Omit<Parameters<typeof getListChats>[0], 'page_token' | 'page_size'>
|
|
|
|
) => {
|
|
|
|
const filtersCompiled: Parameters<typeof getListChats>[0] = useMemo(
|
|
|
|
() => ({ admin_view: false, page_token: 0, page_size: 3000, ...filters }),
|
|
|
|
[filters]
|
|
|
|
);
|
|
|
|
|
|
|
|
const queryFn = useMemoizedFn(() => getListChats(filtersCompiled));
|
2025-02-08 10:57:42 +08:00
|
|
|
|
2025-03-11 23:38:49 +08:00
|
|
|
return useQuery({
|
2025-03-12 00:11:34 +08:00
|
|
|
...queryKeys.chatsGetList(filtersCompiled),
|
2025-02-08 10:57:42 +08:00
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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({
|
2025-02-18 11:50:21 +08:00
|
|
|
...queryKeys.chatsGetList(params),
|
2025-02-08 11:52:25 +08:00
|
|
|
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 () => {
|
2025-03-06 01:13:24 +08:00
|
|
|
return await getChat(params).then((chat) => {
|
2025-03-07 00:35:15 +08:00
|
|
|
console.log('TODO move this to put message in a better spot');
|
2025-03-06 01:13:24 +08:00
|
|
|
return updateChatToIChat(chat, true).iChat;
|
|
|
|
});
|
2025-02-08 11:52:25 +08:00
|
|
|
});
|
|
|
|
|
2025-03-06 03:14:24 +08:00
|
|
|
useQuery({
|
2025-02-18 11:50:21 +08:00
|
|
|
...queryKeys.chatsGetChat(params.id),
|
|
|
|
queryFn,
|
2025-03-06 01:13:24 +08:00
|
|
|
enabled: !!params.id
|
2025-02-08 11:52:25 +08:00
|
|
|
});
|
2025-03-06 03:14:24 +08:00
|
|
|
|
|
|
|
return useQuery<IBusterChat, RustApiError>({
|
|
|
|
...queryKeys.chatsGetChat(params.id),
|
|
|
|
queryKey: queryKeys.chatsGetChat(params.id).queryKey,
|
|
|
|
enabled: !!params.id
|
|
|
|
});
|
2025-02-08 11:52:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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 () => {
|
2025-03-06 01:13:24 +08:00
|
|
|
return await getChat_server(params).then((chat) => {
|
2025-03-07 00:35:15 +08:00
|
|
|
console.log('TODO move this to put message in a better spot');
|
2025-03-06 01:13:24 +08:00
|
|
|
return updateChatToIChat(chat, true).iChat;
|
|
|
|
});
|
2025-02-18 11:50:21 +08:00
|
|
|
}
|
2025-02-08 10:57:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return queryClient;
|
|
|
|
};
|
2025-03-11 23:33:57 +08:00
|
|
|
|
|
|
|
export const useUpdateChat = () => {
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: updateChat,
|
|
|
|
onMutate: () => {
|
|
|
|
//this is actually handled in @useChatUpdate file
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDeleteChat = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: deleteChat,
|
|
|
|
onSuccess(data, variables, context) {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: queryKeys.chatsGetList().queryKey
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|