From c6ac866d72c5db9e1890861bbc6b2ad0a474b84e Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Fri, 7 Feb 2025 19:57:42 -0700 Subject: [PATCH] create chats --- .../rules/typescript_rules.mdc} | 5 +++ web/src/api/buster_rest/chats/index.ts | 2 ++ web/src/api/buster_rest/chats/interfaces.ts | 8 +++++ .../api/buster_rest/chats/queryRequests.ts | 35 +++++++++++++++++++ web/src/api/buster_rest/chats/requests.ts | 24 +++++++++++++ 5 files changed, 74 insertions(+) rename web/{.cursorrules => .cursor/rules/typescript_rules.mdc} (93%) create mode 100644 web/src/api/buster_rest/chats/index.ts create mode 100644 web/src/api/buster_rest/chats/interfaces.ts create mode 100644 web/src/api/buster_rest/chats/queryRequests.ts create mode 100644 web/src/api/buster_rest/chats/requests.ts diff --git a/web/.cursorrules b/web/.cursor/rules/typescript_rules.mdc similarity index 93% rename from web/.cursorrules rename to web/.cursor/rules/typescript_rules.mdc index fc4c1f27c..71c52df53 100644 --- a/web/.cursorrules +++ b/web/.cursor/rules/typescript_rules.mdc @@ -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. diff --git a/web/src/api/buster_rest/chats/index.ts b/web/src/api/buster_rest/chats/index.ts new file mode 100644 index 000000000..cf25f89f5 --- /dev/null +++ b/web/src/api/buster_rest/chats/index.ts @@ -0,0 +1,2 @@ +export * from './requests'; +export * from './queryRequests'; diff --git a/web/src/api/buster_rest/chats/interfaces.ts b/web/src/api/buster_rest/chats/interfaces.ts new file mode 100644 index 000000000..307f044dc --- /dev/null +++ b/web/src/api/buster_rest/chats/interfaces.ts @@ -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; +} diff --git a/web/src/api/buster_rest/chats/queryRequests.ts b/web/src/api/buster_rest/chats/queryRequests.ts new file mode 100644 index 000000000..27da25afc --- /dev/null +++ b/web/src/api/buster_rest/chats/queryRequests.ts @@ -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[0]) => { + const queryFn = useMemoizedFn(() => { + return getChats(params); + }); + + const res = useCreateReactQuery({ + queryKey: ['chats', 'list', params || {}], + queryFn + }); + + return { + ...res, + data: res.data || [] + }; +}; + +export const prefetchGetChats = async ( + params?: Parameters[0], + queryClientProp?: QueryClient +) => { + const queryClient = queryClientProp || new QueryClient(); + + await queryClient.prefetchQuery({ + queryKey: ['chats', 'list', params || {}], + queryFn: () => getChats_server(params) + }); + + return queryClient; +}; diff --git a/web/src/api/buster_rest/chats/requests.ts b/web/src/api/buster_rest/chats/requests.ts new file mode 100644 index 000000000..383bd2949 --- /dev/null +++ b/web/src/api/buster_rest/chats/requests.ts @@ -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 => { + const { page_token = 0, page_size = 1000, admin_view = false } = params || {}; + return mainApi + .get(`${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 => { + const { page_token = 0, page_size = 1000, admin_view = false } = params || {}; + return await serverFetch(`${CHATS_BASE}/list`, { + params: { page_token, page_size, admin_view } + }); +};