@@ -22,6 +22,7 @@ import {
2222 CloseSquareOutlined ,
2323 ExpandAltOutlined ,
2424 WechatWorkOutlined ,
25+ DeleteOutlined ,
2526} from "@ant-design/icons" ;
2627import "./chatbot.css" ;
2728import chatbotIcon from "../../assets/chatbot.svg" ;
@@ -59,6 +60,7 @@ interface ChatBotComponentProps {
5960
6061const ChatBotComponent : React . FC < ChatBotComponentProps > = ( props ) => {
6162 const [ expanded , setExpanded ] = useState < boolean > ( false ) ;
63+ const [ chatResetKey , setChatResetKey ] = useState < number > ( 0 ) ;
6264 const helpOptions = [ "Initialize" , "Clear" , "Help" ] ;
6365
6466
@@ -112,7 +114,7 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
112114 // Clear chat history
113115 const clearChatHistory = async ( ) => {
114116 try {
115- const clearUrl = APIService . CHATBOT_SERVICE + "genai/clear " ;
117+ const clearUrl = APIService . CHATBOT_SERVICE + "genai/reset " ;
116118 await superagent
117119 . post ( clearUrl )
118120 . set ( "Accept" , "application/json" )
@@ -211,8 +213,6 @@ const ChatBotComponent: React.FC<ChatBotComponentProps> = (props) => {
211213 await params . injectMessage ( message . content , message . role === "user" ? "user" : "bot" ) ;
212214 }
213215 await params . injectMessage ( `Loaded ${ chatHistory . length } previous messages. You can now start chatting!` ) ;
214- } else {
215- await params . injectMessage ( "No previous chat history found. You can start chatting now!" ) ;
216216 }
217217
218218 return "chat" ;
@@ -289,7 +289,7 @@ What would you like to do next?`);
289289 const success = await handleInitialization ( apiKey ) ;
290290
291291 if ( success ) {
292- await params . injectMessage ( "✅ Chatbot initialized successfully! Loading chat history... " ) ;
292+ await params . injectMessage ( "✅ Chatbot initialized successfully!" ) ;
293293
294294 // Fetch chat history after successful initialization
295295 const chatHistory = await fetchChatHistory ( ) ;
@@ -365,20 +365,62 @@ What would you like to do next?`);
365365 < span style = { { fontSize : '18px' , fontWeight : '600' , color : '#1f2937' } } >
366366 crAPI ChatBot
367367 </ span >
368- < button
369- className = "expand-chatbot-btn"
370- onClick = { ( ) => setExpanded ( ( prev ) => ! prev ) }
371- aria-label = { expanded ? "Collapse Chatbot" : "Expand Chatbot" }
372- title = { expanded ? "Collapse Chatbot" : "Expand Chatbot" }
373- onMouseEnter = { ( e ) => {
374- e . currentTarget . style . transform = 'scale(1.05)' ;
375- } }
376- onMouseLeave = { ( e ) => {
377- e . currentTarget . style . transform = 'scale(1)' ;
378- } }
379- >
380- < ExpandAltOutlined />
381- </ button >
368+ < div style = { { display : 'flex' , alignItems : 'center' , gap : '8px' } } >
369+ < button
370+ className = "delete-chat-btn"
371+ onClick = { async ( ) => {
372+ // throw a beautiful popup to confirm the action
373+ const { confirm } = await import ( 'antd' ) . then ( ( { Modal } ) => ( {
374+ confirm : Modal . confirm ,
375+ } ) ) ;
376+
377+ confirm ( {
378+ title : 'Are you sure you want to clear the chat history from crAPI servers?' ,
379+ content : 'This action cannot be undone.' ,
380+ onOk : async ( ) => {
381+ // Clear UI immediately by forcing re-render
382+ setChatResetKey ( prev => prev + 1 ) ;
383+
384+ // Clear local storage for chat history
385+ const storageKey = `react_chatbot_history_${ chatbotState . initializationRequired ? 'pending' : 'active' } ` ;
386+ localStorage . removeItem ( storageKey ) ;
387+
388+ // Also clear backend history
389+ const success = await clearChatHistory ( ) ;
390+ if ( ! success ) {
391+ // If backend clear failed, show error but keep UI cleared
392+ console . error ( 'Failed to clear backend chat history' ) ;
393+ }
394+ } ,
395+ onCancel : ( ) => { } ,
396+ } ) ;
397+ } }
398+ aria-label = "Clear Chat History"
399+ title = "Clear Chat History"
400+ onMouseEnter = { ( e ) => {
401+ e . currentTarget . style . transform = 'scale(1.05)' ;
402+ } }
403+ onMouseLeave = { ( e ) => {
404+ e . currentTarget . style . transform = 'scale(1)' ;
405+ } }
406+ >
407+ < DeleteOutlined />
408+ </ button >
409+ < button
410+ className = "expand-chatbot-btn"
411+ onClick = { ( ) => setExpanded ( ( prev ) => ! prev ) }
412+ aria-label = { expanded ? "Collapse Chatbot" : "Expand Chatbot" }
413+ title = { expanded ? "Collapse Chatbot" : "Expand Chatbot" }
414+ onMouseEnter = { ( e ) => {
415+ e . currentTarget . style . transform = 'scale(1.05)' ;
416+ } }
417+ onMouseLeave = { ( e ) => {
418+ e . currentTarget . style . transform = 'scale(1)' ;
419+ } }
420+ >
421+ < ExpandAltOutlined />
422+ </ button >
423+ </ div >
382424 </ div >
383425 ) ,
384426 } ,
@@ -408,6 +450,7 @@ What would you like to do next?`);
408450 < Col xs = { 10 } >
409451 < div className = { `app-chatbot-container${ expanded ? " expanded" : "" } ` } >
410452 < ChatBot
453+ key = { chatResetKey }
411454 flow = { flow }
412455 plugins = { plugins }
413456 settings = { settings }
0 commit comments