From e7b6ba45c9f4595d0c4fad1bfbc541903649604e Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 11 Jun 2025 20:39:13 +0600 Subject: [PATCH] wip --- .../FrameRenderer/RoadmapFloatingChat.tsx | 91 +++++++++++++++---- .../RoadmapAIChatHistory.tsx | 4 +- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/components/FrameRenderer/RoadmapFloatingChat.tsx b/src/components/FrameRenderer/RoadmapFloatingChat.tsx index 25a77e64b..7ba1bff75 100644 --- a/src/components/FrameRenderer/RoadmapFloatingChat.tsx +++ b/src/components/FrameRenderer/RoadmapFloatingChat.tsx @@ -16,6 +16,7 @@ import { Fragment, useEffect, useMemo, useRef, useState } from 'react'; import { flushSync } from 'react-dom'; import { useKeydown } from '../../hooks/use-keydown'; import { + roadmapAIChatRenderer, useRoadmapAIChat, type RoadmapAIChatHistoryType, } from '../../hooks/use-roadmap-ai-chat'; @@ -33,6 +34,8 @@ import { CLOSE_TOPIC_DETAIL_EVENT } from '../TopicDetail/TopicDetail'; import { UpdatePersonaModal } from '../UserPersona/UpdatePersonaModal'; import { isLoggedIn } from '../../lib/jwt'; import { showLoginPopup } from '../../lib/popup'; +import { chatHistoryOptions } from '../../queries/chat-history'; +import { RoadmapAIChatHistory } from '../RoadmapAIChatHistory/RoadmapAIChatHistory'; type ChatHeaderButtonProps = { onClick?: () => void; @@ -47,7 +50,7 @@ function ChatHeaderButton(props: ChatHeaderButtonProps) { const { onClick, href, icon, children, className, target } = props; const classNames = cn( - 'flex items-center gap-1.5 text-xs text-gray-600 transition-colors hover:text-gray-900', + 'flex shrink-0 items-center gap-1.5 text-xs text-gray-600 transition-colors hover:text-gray-900 min-w-8', className, ); @@ -227,6 +230,22 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { }); }; + const [isChatHistoryLoading, setIsChatHistoryLoading] = useState(true); + const [activeChatHistoryId, setActiveChatHistoryId] = useState< + string | undefined + >(); + const { data: chatHistory } = useQuery( + chatHistoryOptions( + activeChatHistoryId, + roadmapAIChatRenderer({ + roadmapId, + totalTopicCount, + onSelectTopic, + }), + ), + queryClient, + ); + const { aiChatHistory, isStreamingMessage, @@ -237,13 +256,39 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { handleAbort, scrollToBottom, clearChat, + setAiChatHistory, } = useRoadmapAIChat({ + activeChatHistoryId, roadmapId, totalTopicCount, scrollareaRef, onSelectTopic, + onChatHistoryIdChange: (chatHistoryId) => { + setActiveChatHistoryId(chatHistoryId); + }, }); + useEffect(() => { + if (!chatHistory) { + return; + } + + setAiChatHistory(chatHistory?.messages ?? []); + setIsChatHistoryLoading(false); + setTimeout(() => { + scrollToBottom('instant'); + }, 0); + }, [chatHistory]); + + useEffect(() => { + if (activeChatHistoryId) { + return; + } + + setAiChatHistory([]); + setIsChatHistoryLoading(false); + }, [activeChatHistoryId, setAiChatHistory, setIsChatHistoryLoading]); + useEffect(() => { lockBodyScroll(isOpen); }, [isOpen]); @@ -293,6 +338,7 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { }; const hasMessages = aiChatHistory.length > 0; + const newTabUrl = `/${roadmapId}/ai${activeChatHistoryId ? `?chatId=${activeChatHistoryId}` : ''}`; return ( <> @@ -331,7 +377,6 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { {isOpen && ( <>
- {/* Messages area */}
} className="hidden rounded-md py-1 pr-2 pl-1.5 text-gray-500 hover:bg-gray-300 sm:flex" @@ -352,10 +397,27 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { Open in new tab + { + setIsChatHistoryLoading(true); + setActiveChatHistoryId(chatHistoryId); + }} + onDelete={(chatHistoryId) => { + if (activeChatHistoryId === chatHistoryId) { + setActiveChatHistoryId(undefined); + } + }} + onNewChat={() => { + setActiveChatHistoryId(undefined); + }} + /> + setIsOpen(false)} icon={} - className="rounded-md bg-red-100 px-1 py-1 text-red-500 hover:bg-red-200" + className="flex items-center justify-center rounded-md bg-red-100 px-1 py-1 text-red-500 hover:bg-red-200" />
@@ -412,13 +474,11 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) {
)} - {aiChatHistory.map( - (chat: RoadmapAIChatHistoryType, index: number) => ( - - - - ), - )} + {aiChatHistory.map((chat, index) => ( + + + + ))} {isStreamingMessage && !streamedMessage && ( @@ -444,7 +504,6 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { )} - {/* Input area */} {isLimitExceeded && ( { @@ -482,7 +541,7 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { /> )} - {hasMessages && ( + {hasMessages && !isPaidUser && ( { setInputValue(''); @@ -550,7 +609,7 @@ export function RoadmapFloatingChat(props: RoadmapChatProps) { {!isOpen && (