From 51da69bf40eb1df69f4da5c5b55e83f67bb3cdb2 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Mon, 17 Feb 2025 20:50:21 -0700 Subject: [PATCH] move chat querykeys --- .../api/buster_rest/chats/queryRequests.ts | 33 ++++++++++++------- web/src/api/createReactQuery.ts | 28 ++++------------ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/web/src/api/buster_rest/chats/queryRequests.ts b/web/src/api/buster_rest/chats/queryRequests.ts index 6ccdaa621..5c53f82f7 100644 --- a/web/src/api/buster_rest/chats/queryRequests.ts +++ b/web/src/api/buster_rest/chats/queryRequests.ts @@ -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[0]) => { - const queryFn = useMemoizedFn(() => { + const queryFn = useMemoizedFn((): Promise => { return getListChats(params); }); - const res = useCreateReactQuery({ - 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[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({ - 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; diff --git a/web/src/api/createReactQuery.ts b/web/src/api/createReactQuery.ts index 99e565eb4..473a1f8e7 100644 --- a/web/src/api/createReactQuery.ts +++ b/web/src/api/createReactQuery.ts @@ -15,46 +15,30 @@ import { useBusterNotifications } from '@/context/BusterNotifications'; import { RustApiError } from './buster_rest/errors'; import { useMemoizedFn } from 'ahooks'; -interface CreateQueryProps { - queryKey: QueryKey; - queryFn: () => Promise; - isUseSession?: boolean; +type CreateQueryProps = UseQueryOptions & { useErrorNotification?: boolean; - enabled?: boolean; - initialData?: TData; - refetchOnWindowFocus?: boolean; - refetchOnMount?: boolean; - staleTime?: number; - options?: Omit, 'queryKey' | 'queryFn'>; -} + isUseSession?: boolean; +}; export const PREFETCH_STALE_TIME = 1000 * 10; -export const useCreateReactQuery = ({ - queryKey, - queryFn, +export const useCreateReactQuery = ({ isUseSession = true, enabled = true, - initialData, refetchOnWindowFocus = false, refetchOnMount = true, useErrorNotification = true, - staleTime, - options = {} + ...options }: CreateQueryProps) => { const { openErrorNotification } = useBusterNotifications(); const accessToken = useSupabaseContext((state) => state.accessToken); const baseEnabled = isUseSession ? !!accessToken : true; - const q = useQuery({ - queryKey: queryKey, - queryFn, + const q = useQuery({ enabled: baseEnabled && !!enabled, - initialData, retry: 0, refetchOnWindowFocus, refetchOnMount, - staleTime, ...options });