mirror of https://github.com/buster-so/buster.git
move chat querykeys
This commit is contained in:
parent
b6e9268cf0
commit
51da69bf40
|
@ -2,15 +2,17 @@ import { useCreateReactQuery } from '@/api/createReactQuery';
|
||||||
import { useMemoizedFn } from 'ahooks';
|
import { useMemoizedFn } from 'ahooks';
|
||||||
import { QueryClient } from '@tanstack/react-query';
|
import { QueryClient } from '@tanstack/react-query';
|
||||||
import { getListChats, getListChats_server, getChat, getChat_server } from './requests';
|
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]) => {
|
export const useGetListChats = (params?: Parameters<typeof getListChats>[0]) => {
|
||||||
const queryFn = useMemoizedFn(() => {
|
const queryFn = useMemoizedFn((): Promise<BusterChatListItem[]> => {
|
||||||
return getListChats(params);
|
return getListChats(params);
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = useCreateReactQuery<BusterChatListItem[]>({
|
const res = useCreateReactQuery({
|
||||||
queryKey: ['chats', 'list', params || {}],
|
...queryKeys.chatsGetList(params),
|
||||||
queryFn
|
queryFn
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ export const prefetchGetListChats = async (
|
||||||
const queryClient = queryClientProp || new QueryClient();
|
const queryClient = queryClientProp || new QueryClient();
|
||||||
|
|
||||||
await queryClient.prefetchQuery({
|
await queryClient.prefetchQuery({
|
||||||
queryKey: ['chats', 'list', params || {}],
|
...queryKeys.chatsGetList(params),
|
||||||
queryFn: () => getListChats_server(params)
|
queryFn: () => getListChats_server(params)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -35,13 +37,16 @@ export const prefetchGetListChats = async (
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useGetChat = (params: Parameters<typeof getChat>[0]) => {
|
export const useGetChat = (params: Parameters<typeof getChat>[0]) => {
|
||||||
const queryFn = useMemoizedFn(() => {
|
const queryFn = useMemoizedFn(async () => {
|
||||||
return getChat(params);
|
const chat = await getChat(params);
|
||||||
|
const iChat = updateChatToIChat(chat, true).iChat;
|
||||||
|
return iChat;
|
||||||
});
|
});
|
||||||
|
|
||||||
return useCreateReactQuery<BusterChat>({
|
return useCreateReactQuery({
|
||||||
queryKey: ['chats', 'get', params.id],
|
...queryKeys.chatsGetChat(params.id),
|
||||||
queryFn
|
queryFn,
|
||||||
|
enabled: true
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -52,8 +57,12 @@ export const prefetchGetChat = async (
|
||||||
const queryClient = queryClientProp || new QueryClient();
|
const queryClient = queryClientProp || new QueryClient();
|
||||||
|
|
||||||
await queryClient.prefetchQuery({
|
await queryClient.prefetchQuery({
|
||||||
queryKey: ['chats', 'get', params.id],
|
...queryKeys.chatsGetChat(params.id),
|
||||||
queryFn: () => getChat_server(params)
|
queryFn: async () => {
|
||||||
|
const chat = await getChat_server(params);
|
||||||
|
const iChat = updateChatToIChat(chat, true).iChat;
|
||||||
|
return iChat;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return queryClient;
|
return queryClient;
|
||||||
|
|
|
@ -15,46 +15,30 @@ import { useBusterNotifications } from '@/context/BusterNotifications';
|
||||||
import { RustApiError } from './buster_rest/errors';
|
import { RustApiError } from './buster_rest/errors';
|
||||||
import { useMemoizedFn } from 'ahooks';
|
import { useMemoizedFn } from 'ahooks';
|
||||||
|
|
||||||
interface CreateQueryProps<TData, TError = unknown> {
|
type CreateQueryProps<TData = unknown, TError = unknown> = UseQueryOptions<TData, TError, TData> & {
|
||||||
queryKey: QueryKey;
|
|
||||||
queryFn: () => Promise<TData>;
|
|
||||||
isUseSession?: boolean;
|
|
||||||
useErrorNotification?: boolean;
|
useErrorNotification?: boolean;
|
||||||
enabled?: boolean;
|
isUseSession?: boolean;
|
||||||
initialData?: TData;
|
};
|
||||||
refetchOnWindowFocus?: boolean;
|
|
||||||
refetchOnMount?: boolean;
|
|
||||||
staleTime?: number;
|
|
||||||
options?: Omit<UseQueryOptions<TData, TError, TData>, 'queryKey' | 'queryFn'>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PREFETCH_STALE_TIME = 1000 * 10;
|
export const PREFETCH_STALE_TIME = 1000 * 10;
|
||||||
|
|
||||||
export const useCreateReactQuery = <TData, TError = unknown>({
|
export const useCreateReactQuery = <TData = unknown, TError = unknown>({
|
||||||
queryKey,
|
|
||||||
queryFn,
|
|
||||||
isUseSession = true,
|
isUseSession = true,
|
||||||
enabled = true,
|
enabled = true,
|
||||||
initialData,
|
|
||||||
refetchOnWindowFocus = false,
|
refetchOnWindowFocus = false,
|
||||||
refetchOnMount = true,
|
refetchOnMount = true,
|
||||||
useErrorNotification = true,
|
useErrorNotification = true,
|
||||||
staleTime,
|
...options
|
||||||
options = {}
|
|
||||||
}: CreateQueryProps<TData, TError>) => {
|
}: CreateQueryProps<TData, TError>) => {
|
||||||
const { openErrorNotification } = useBusterNotifications();
|
const { openErrorNotification } = useBusterNotifications();
|
||||||
const accessToken = useSupabaseContext((state) => state.accessToken);
|
const accessToken = useSupabaseContext((state) => state.accessToken);
|
||||||
const baseEnabled = isUseSession ? !!accessToken : true;
|
const baseEnabled = isUseSession ? !!accessToken : true;
|
||||||
|
|
||||||
const q = useQuery<TData, TError>({
|
const q = useQuery({
|
||||||
queryKey: queryKey,
|
|
||||||
queryFn,
|
|
||||||
enabled: baseEnabled && !!enabled,
|
enabled: baseEnabled && !!enabled,
|
||||||
initialData,
|
|
||||||
retry: 0,
|
retry: 0,
|
||||||
refetchOnWindowFocus,
|
refetchOnWindowFocus,
|
||||||
refetchOnMount,
|
refetchOnMount,
|
||||||
staleTime,
|
|
||||||
...options
|
...options
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue