move chat querykeys

This commit is contained in:
Nate Kelley 2025-02-17 20:50:21 -07:00
parent b6e9268cf0
commit 51da69bf40
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 27 additions and 34 deletions

View File

@ -2,15 +2,17 @@ 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';
import type { BusterChatListItem } from '@/api/asset_interfaces';
import { queryKeys } from '@/api/query_keys';
import { updateChatToIChat } from '@/utils/chat';
export const useGetListChats = (params?: Parameters<typeof getListChats>[0]) => {
const queryFn = useMemoizedFn(() => {
const queryFn = useMemoizedFn((): Promise<BusterChatListItem[]> => {
return getListChats(params);
});
const res = useCreateReactQuery<BusterChatListItem[]>({
queryKey: ['chats', 'list', params || {}],
const res = useCreateReactQuery({
...queryKeys.chatsGetList(params),
queryFn
});
@ -27,7 +29,7 @@ export const prefetchGetListChats = async (
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
queryKey: ['chats', 'list', params || {}],
...queryKeys.chatsGetList(params),
queryFn: () => getListChats_server(params)
});
@ -35,13 +37,16 @@ export const prefetchGetListChats = async (
};
export const useGetChat = (params: Parameters<typeof getChat>[0]) => {
const queryFn = useMemoizedFn(() => {
return getChat(params);
const queryFn = useMemoizedFn(async () => {
const chat = await getChat(params);
const iChat = updateChatToIChat(chat, true).iChat;
return iChat;
});
return useCreateReactQuery<BusterChat>({
queryKey: ['chats', 'get', params.id],
queryFn
return useCreateReactQuery({
...queryKeys.chatsGetChat(params.id),
queryFn,
enabled: true
});
};
@ -52,8 +57,12 @@ export const prefetchGetChat = async (
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
queryKey: ['chats', 'get', params.id],
queryFn: () => getChat_server(params)
...queryKeys.chatsGetChat(params.id),
queryFn: async () => {
const chat = await getChat_server(params);
const iChat = updateChatToIChat(chat, true).iChat;
return iChat;
}
});
return queryClient;

View File

@ -15,46 +15,30 @@ import { useBusterNotifications } from '@/context/BusterNotifications';
import { RustApiError } from './buster_rest/errors';
import { useMemoizedFn } from 'ahooks';
interface CreateQueryProps<TData, TError = unknown> {
queryKey: QueryKey;
queryFn: () => Promise<TData>;
isUseSession?: boolean;
type CreateQueryProps<TData = unknown, TError = unknown> = UseQueryOptions<TData, TError, TData> & {
useErrorNotification?: boolean;
enabled?: boolean;
initialData?: TData;
refetchOnWindowFocus?: boolean;
refetchOnMount?: boolean;
staleTime?: number;
options?: Omit<UseQueryOptions<TData, TError, TData>, 'queryKey' | 'queryFn'>;
}
isUseSession?: boolean;
};
export const PREFETCH_STALE_TIME = 1000 * 10;
export const useCreateReactQuery = <TData, TError = unknown>({
queryKey,
queryFn,
export const useCreateReactQuery = <TData = unknown, TError = unknown>({
isUseSession = true,
enabled = true,
initialData,
refetchOnWindowFocus = false,
refetchOnMount = true,
useErrorNotification = true,
staleTime,
options = {}
...options
}: CreateQueryProps<TData, TError>) => {
const { openErrorNotification } = useBusterNotifications();
const accessToken = useSupabaseContext((state) => state.accessToken);
const baseEnabled = isUseSession ? !!accessToken : true;
const q = useQuery<TData, TError>({
queryKey: queryKey,
queryFn,
const q = useQuery({
enabled: baseEnabled && !!enabled,
initialData,
retry: 0,
refetchOnWindowFocus,
refetchOnMount,
staleTime,
...options
});