diff --git a/src/components/AIChat/AIChat.tsx b/src/components/AIChat/AIChat.tsx index a45f2d540..119f2d977 100644 --- a/src/components/AIChat/AIChat.tsx +++ b/src/components/AIChat/AIChat.tsx @@ -268,6 +268,11 @@ export function AIChat(props: AIChatProps) { }); queryClient.invalidateQueries(getAiCourseLimitOptions()); + queryClient.invalidateQueries({ + predicate: (query) => { + return query.queryKey[0] === 'list-chat-history'; + }, + }); }, onDetails: (details) => { const detailsJson = JSON.parse(details); @@ -378,7 +383,7 @@ export function AIChat(props: AIChatProps) { }, []); return ( -
+
)} - {aiChatHistory.length > 0 && ( + {aiChatHistory.length > 0 && !isPaidUser && ( { const today = DateTime.now().startOf('day'); + const allHistories = data?.pages?.flatMap((page) => page.data); - return data?.data?.reduce( + return allHistories?.reduce( (acc, chatHistory) => { const updatedAt = DateTime.fromJSDate( new Date(chatHistory.updatedAt), @@ -60,7 +62,7 @@ export function ListChatHistory(props: ListChatHistoryProps) { { title: string; histories: ChatHistoryWithoutMessages[] } >, ); - }, [data?.data]); + }, [data?.pages]); return (
@@ -74,6 +76,8 @@ export function ListChatHistory(props: ListChatHistoryProps) { New Chat + +
{Object.entries(groupedChatHistory ?? {}).map(([key, value]) => { if (value.histories.length === 0) { @@ -103,6 +107,71 @@ export function ListChatHistory(props: ListChatHistoryProps) { ); })}
+ + {hasNextPage && ( +
+ +
+ )}
); } + +type SearchInputProps = { + onSearch: (search: string) => void; + isLoading?: boolean; +}; + +function SearchInput(props: SearchInputProps) { + const { onSearch, isLoading } = props; + + const [search, setSearch] = useState(''); + const debouncedSearch = useDebounceValue(search, 300); + + useEffect(() => { + onSearch(debouncedSearch); + }, [debouncedSearch, onSearch]); + + return ( +
{ + e.preventDefault(); + onSearch(search); + }} + > + setSearch(e.target.value)} + /> + +
+ {isLoading ? ( + + ) : ( + + )} +
+
+ ); +} diff --git a/src/queries/chat-history.ts b/src/queries/chat-history.ts index 6336ab4ea..cc4b420fe 100644 --- a/src/queries/chat-history.ts +++ b/src/queries/chat-history.ts @@ -1,4 +1,4 @@ -import { queryOptions } from '@tanstack/react-query'; +import { infiniteQueryOptions, queryOptions } from '@tanstack/react-query'; import { httpGet } from '../lib/query-http'; import { isLoggedIn } from '../lib/jwt'; import type { RoadmapAIChatHistoryType } from '../components/RoadmapAIChat/RoadmapAIChat'; @@ -78,20 +78,24 @@ type ListChatHistoryResponse = { export function listChatHistoryOptions( query: ListChatHistoryQuery = { - perPage: '20', - currPage: '1', query: '', }, ) { - return queryOptions({ + return infiniteQueryOptions({ queryKey: ['list-chat-history', query], - queryFn: () => { + queryFn: ({ pageParam }) => { return httpGet('/v1-list-chat-history', { ...(query?.query ? { query: query.query } : {}), - ...(query?.perPage ? { perPage: query.perPage } : {}), - ...(query?.currPage ? { currPage: query.currPage } : {}), + ...(pageParam ? { currPage: pageParam } : {}), + perPage: '21', }); }, enabled: !!isLoggedIn(), + getNextPageParam: (lastPage, pages) => { + return lastPage.currPage < lastPage.totalPages + ? lastPage.currPage + 1 + : undefined; + }, + initialPageParam: 1, }); }