mirror of https://github.com/buster-so/buster.git
create chats
This commit is contained in:
parent
07c750b801
commit
c6ac866d72
|
@ -1,3 +1,8 @@
|
|||
---
|
||||
description: Rules for the context directory
|
||||
globs: src/**/*.{ts,tsx}
|
||||
---
|
||||
|
||||
# Project: React TypeScript Application
|
||||
You are a TypeScript expert with deep knowledge of the language's features and best practices. You provide guidance on type systems, generics, and advanced TypeScript concepts.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export * from './requests';
|
||||
export * from './queryRequests';
|
|
@ -0,0 +1,8 @@
|
|||
export interface ChatListParams {
|
||||
/** Pagination token indicating the page number */
|
||||
page_token: number;
|
||||
/** Number of chat items to return per page */
|
||||
page_size: number;
|
||||
/** When true, shows all organization chats (admin only). When false, shows only user's chats */
|
||||
admin_view: boolean;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
import { useCreateReactQuery } from '@/api/createReactQuery';
|
||||
import { useMemoizedFn } from 'ahooks';
|
||||
import { QueryClient } from '@tanstack/react-query';
|
||||
import { getChats, getChats_server } from './requests';
|
||||
import type { BusterChatListItem } from '@/api/asset_interfaces';
|
||||
|
||||
export const useGetChats = (params?: Parameters<typeof getChats>[0]) => {
|
||||
const queryFn = useMemoizedFn(() => {
|
||||
return getChats(params);
|
||||
});
|
||||
|
||||
const res = useCreateReactQuery<BusterChatListItem[]>({
|
||||
queryKey: ['chats', 'list', params || {}],
|
||||
queryFn
|
||||
});
|
||||
|
||||
return {
|
||||
...res,
|
||||
data: res.data || []
|
||||
};
|
||||
};
|
||||
|
||||
export const prefetchGetChats = async (
|
||||
params?: Parameters<typeof getChats>[0],
|
||||
queryClientProp?: QueryClient
|
||||
) => {
|
||||
const queryClient = queryClientProp || new QueryClient();
|
||||
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: ['chats', 'list', params || {}],
|
||||
queryFn: () => getChats_server(params)
|
||||
});
|
||||
|
||||
return queryClient;
|
||||
};
|
|
@ -0,0 +1,24 @@
|
|||
import { mainApi } from '../instances';
|
||||
import { serverFetch } from '../../createServerInstance';
|
||||
import type { BusterChatListItem } from '@/api/asset_interfaces';
|
||||
import type { ChatListParams } from './interfaces';
|
||||
|
||||
const CHATS_BASE = '/chats';
|
||||
|
||||
// Client-side fetch version
|
||||
export const getChats = async (params?: ChatListParams): Promise<BusterChatListItem[]> => {
|
||||
const { page_token = 0, page_size = 1000, admin_view = false } = params || {};
|
||||
return mainApi
|
||||
.get<BusterChatListItem[]>(`${CHATS_BASE}/list`, {
|
||||
params: { page_token, page_size, admin_view }
|
||||
})
|
||||
.then((res) => res.data);
|
||||
};
|
||||
|
||||
// Server-side fetch version
|
||||
export const getChats_server = async (params?: ChatListParams): Promise<BusterChatListItem[]> => {
|
||||
const { page_token = 0, page_size = 1000, admin_view = false } = params || {};
|
||||
return await serverFetch<BusterChatListItem[]>(`${CHATS_BASE}/list`, {
|
||||
params: { page_token, page_size, admin_view }
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue