Skip to content

Commit be3178f

Browse files
committed
Add delete button
1 parent f749092 commit be3178f

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

services/web/src/components/bot/Bot.tsx

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
CloseSquareOutlined,
2323
ExpandAltOutlined,
2424
WechatWorkOutlined,
25+
DeleteOutlined,
2526
} from "@ant-design/icons";
2627
import "./chatbot.css";
2728
import chatbotIcon from "../../assets/chatbot.svg";
@@ -59,6 +60,7 @@ interface ChatBotComponentProps {
5960

6061
const 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}

services/web/src/components/bot/chatbot.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,45 @@
4242
border: none;
4343
}
4444

45+
/* Delete chat button */
46+
.delete-chat-btn {
47+
border: none;
48+
outline: none;
49+
position: absolute;
50+
right: 80px;
51+
top: 10px;
52+
margin-right: 5px;
53+
border-radius: 8px;
54+
padding: 8px 12px;
55+
color: rgb(232, 234, 237);
56+
font-weight: 1000;
57+
width: 32px;
58+
height: 32px;
59+
font-size: 30px;
60+
display: flex;
61+
align-items: center;
62+
justify-content: center;
63+
transition: all 0.2s ease;
64+
background: transparent;
65+
}
66+
67+
.delete-chat-btn:hover {
68+
color: #ffffff;
69+
transform: scale(1.05);
70+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
71+
opacity: 1;
72+
background-color: rgba(0, 0, 0, 0.1);
73+
}
74+
75+
.delete-chat-btn:focus {
76+
outline: none;
77+
border: none;
78+
}
79+
80+
.delete-chat-btn:active {
81+
transform: scale(0.95);
82+
}
83+
4584

4685
.expand-chatbot-btn:after {
4786
content: "";

0 commit comments

Comments
 (0)