create chats

This commit is contained in:
Nate Kelley 2025-02-07 19:57:42 -07:00
parent 07c750b801
commit c6ac866d72
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 74 additions and 0 deletions

View File

@ -1,3 +1,8 @@
---
description: Rules for the context directory
globs: src/**/*.{ts,tsx}
---
# Project: React TypeScript Application # 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. 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.

View File

@ -0,0 +1,2 @@
export * from './requests';
export * from './queryRequests';

View File

@ -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;
}

View File

@ -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;
};

View File

@ -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 }
});
};