From 799f6eebbb442043093db0a7101f5623619e1aab Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Mon, 9 Jun 2025 10:57:01 +0600 Subject: [PATCH] wip --- src/components/AIChat/AIChat.tsx | 26 +++++------- .../AIChatHistory/AIChatHistory.tsx | 18 +++++++-- src/pages/ai/chat/index.astro | 6 +-- src/queries/chat-history.ts | 40 ++++++++++++++++++- 4 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/components/AIChat/AIChat.tsx b/src/components/AIChat/AIChat.tsx index 61ed86243..ed3540b2f 100644 --- a/src/components/AIChat/AIChat.tsx +++ b/src/components/AIChat/AIChat.tsx @@ -43,7 +43,6 @@ import { AIChatCourse } from './AIChatCouse'; import { showLoginPopup } from '../../lib/popup'; import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal'; import { readChatStream } from '../../lib/chat'; -import type { ChatHistoryDocument } from '../../queries/chat-history'; export const aiChatRenderer: Record = { 'roadmap-recommendations': (options) => { @@ -55,27 +54,20 @@ export const aiChatRenderer: Record = { }; type AIChatProps = { - chatHistory?: Pick; messages?: RoadmapAIChatHistoryType[]; + chatHistoryId?: string; + setChatHistoryId?: (chatHistoryId: string) => void; }; export function AIChat(props: AIChatProps) { - const { chatHistory: defaultDetails, messages: defaultMessages } = props; + const { + messages: defaultMessages, + chatHistoryId: defaultChatHistoryId, + setChatHistoryId: setDefaultChatHistoryId, + } = props; const toast = useToast(); - const [chatDetails, setChatDetails] = useState<{ - chatHistoryId: string; - title: string; - } | null>( - defaultDetails - ? { - chatHistoryId: defaultDetails._id, - title: defaultDetails.title, - } - : null, - ); - const [message, setMessage] = useState(''); const [isStreamingMessage, setIsStreamingMessage] = useState(false); const [streamedMessage, setStreamedMessage] = @@ -183,7 +175,7 @@ export function AIChat(props: AIChatProps) { }, credentials: 'include', body: JSON.stringify({ - chatHistoryId: chatDetails?.chatHistoryId, + chatHistoryId: defaultChatHistoryId, messages: messages.slice(-10), force, }), @@ -252,7 +244,7 @@ export function AIChat(props: AIChatProps) { return; } - setChatDetails(detailsJson); + setDefaultChatHistoryId?.(chatHistoryId); window.history.replaceState({}, '', `/ai/chat/${chatHistoryId}`); }, }); diff --git a/src/components/AIChatHistory/AIChatHistory.tsx b/src/components/AIChatHistory/AIChatHistory.tsx index d06c30b29..3b38174d0 100644 --- a/src/components/AIChatHistory/AIChatHistory.tsx +++ b/src/components/AIChatHistory/AIChatHistory.tsx @@ -7,13 +7,17 @@ import { useEffect, useState } from 'react'; import { AIChatLayout } from './AIChatLayout'; type AIChatHistoryProps = { - chatHistoryId: string; + chatHistoryId?: string; }; export function AIChatHistory(props: AIChatHistoryProps) { - const { chatHistoryId } = props; + const { chatHistoryId: defaultChatHistoryId } = props; + + const [isLoading, setIsLoading] = useState(!!defaultChatHistoryId); + const [chatHistoryId, setChatHistoryId] = useState( + defaultChatHistoryId || undefined, + ); - const [isLoading, setIsLoading] = useState(true); const { data } = useQuery(chatHistoryOptions(chatHistoryId), queryClient); useEffect(() => { @@ -31,7 +35,13 @@ export function AIChatHistory(props: AIChatHistoryProps) { )} - {!isLoading && } + {!isLoading && ( + + )} ); } diff --git a/src/pages/ai/chat/index.astro b/src/pages/ai/chat/index.astro index 8651f5b76..c1d571ab7 100644 --- a/src/pages/ai/chat/index.astro +++ b/src/pages/ai/chat/index.astro @@ -1,7 +1,7 @@ --- import SkeletonLayout from '../../../layouts/SkeletonLayout.astro'; -import { AIChat } from '../../../components/AIChat/AIChat'; import { AIChatLayout } from '../../../components/AIChatHistory/AIChatLayout'; +import { AIChatHistory } from '../../../components/AIChatHistory/AIChatHistory'; --- - - - + diff --git a/src/queries/chat-history.ts b/src/queries/chat-history.ts index 6ab2feaf4..5c64c327d 100644 --- a/src/queries/chat-history.ts +++ b/src/queries/chat-history.ts @@ -23,7 +23,7 @@ export interface ChatHistoryDocument { updatedAt: Date; } -export function chatHistoryOptions(chatHistoryId: string) { +export function chatHistoryOptions(chatHistoryId?: string) { return queryOptions({ queryKey: ['chat-history', chatHistoryId], queryFn: async () => { @@ -31,6 +31,10 @@ export function chatHistoryOptions(chatHistoryId: string) { `/v1-chat-history/${chatHistoryId}`, ); + if (data.title) { + document.title = data.title; + } + const messages: RoadmapAIChatHistoryType[] = []; for (const message of data.messages) { messages.push({ @@ -52,6 +56,40 @@ export function chatHistoryOptions(chatHistoryId: string) { messages, }; }, + enabled: !!isLoggedIn() && !!chatHistoryId, + }); +} + +type ListChatHistoryQuery = { + perPage?: string; + currPage?: string; + query?: string; +}; + +type ListChatHistoryResponse = { + data: Omit[]; + totalCount: number; + totalPages: number; + currPage: number; + perPage: number; +}; + +export function listChatHistoryOptions( + query: ListChatHistoryQuery = { + perPage: '20', + currPage: '1', + query: '', + }, +) { + return queryOptions({ + queryKey: ['chat-history', query], + queryFn: () => { + return httpGet('/v1-list-chat-history', { + ...(query?.query ? { query: query.query } : {}), + ...(query?.perPage ? { perPage: query.perPage } : {}), + ...(query?.currPage ? { currPage: query.currPage } : {}), + }); + }, enabled: !!isLoggedIn(), }); }