Skip to content

Commit 1866bf0

Browse files
committed
Function: Add folder data struct
1 parent b75e0ad commit 1866bf0

File tree

8 files changed

+87
-66
lines changed

8 files changed

+87
-66
lines changed

src/components/chat/ChatHistoryList.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import ConfirmDialog from '../ui/ConfirmDialog';
77
interface ChatHistoryListProps {
88
conversations: Conversation[];
99
activeConversationId: string | null;
10-
onSelectConversation: (id: string) => void;
10+
onSelectConversation: (conversationId: string) => void;
1111
onCreateNewChat: () => void;
12-
onRenameConversation: (id: string, newTitle: string) => void;
13-
onDeleteConversation: (id: string) => void;
12+
onRenameConversation: (conversationId: string, newTitle: string) => void;
13+
onDeleteConversation: (conversationId: string) => void;
1414
}
1515

1616
export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
@@ -107,26 +107,26 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
107107
};
108108
}, [isButtonHovered, isButtonVisible]);
109109

110-
const handleMenuClick = (e: React.MouseEvent, id: string) => {
110+
const handleMenuClick = (e: React.MouseEvent, conversationId: string) => {
111111
e.stopPropagation();
112-
setOpenMenuId(openMenuId === id ? null : id);
112+
setOpenMenuId(openMenuId === conversationId ? null : conversationId);
113113
};
114114

115115
const handleRenameClick = (e: React.MouseEvent, conversation: Conversation) => {
116116
e.stopPropagation();
117-
setEditingId(conversation.id);
117+
setEditingId(conversation.conversationId);
118118
setEditTitle(conversation.title);
119119
};
120120

121-
const handleDeleteClick = (e: React.MouseEvent, id: string) => {
121+
const handleDeleteClick = (e: React.MouseEvent, conversationId: string) => {
122122
e.stopPropagation();
123123

124124
// Show confirmation dialog
125125
setConfirmDialog({
126126
isOpen: true,
127127
title: 'Delete Conversation',
128128
message: 'Are you sure you want to delete this conversation? This action cannot be undone.',
129-
itemId: id,
129+
itemId: conversationId,
130130
confirmText: 'Delete',
131131
cancelText: 'Cancel',
132132
confirmColor: 'red'
@@ -212,7 +212,7 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
212212
id: 'delete',
213213
icon: Trash2,
214214
label: 'Delete',
215-
onClick: (e) => handleDeleteClick(e, conversation.id),
215+
onClick: (e) => handleDeleteClick(e, conversation.conversationId),
216216
color: 'text-red-600'
217217
}
218218
];
@@ -278,8 +278,8 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
278278
) : (
279279
<ul className="py-2">
280280
{conversations.map((conversation) => (
281-
<li key={conversation.id} className="relative">
282-
{editingId === conversation.id && !isCollapsed ? (
281+
<li key={conversation.conversationId} className="relative">
282+
{editingId === conversation.conversationId && !isCollapsed ? (
283283
<form onSubmit={handleRenameSubmit} className="px-4 py-2">
284284
<input
285285
ref={inputRef}
@@ -293,9 +293,9 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
293293
</form>
294294
) : (
295295
<button
296-
onClick={() => onSelectConversation(conversation.id)}
296+
onClick={() => onSelectConversation(conversation.conversationId)}
297297
className={`flex items-center justify-between w-full ${isCollapsed ? 'px-0 py-3 flex-col' : 'px-4 py-2'} text-left ${
298-
activeConversationId === conversation.id
298+
activeConversationId === conversation.conversationId
299299
? 'bg-blue-50 text-blue-600'
300300
: 'text-gray-700 hover:bg-gray-100'
301301
}`}
@@ -309,7 +309,7 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
309309
<MessageSquare size={16} className="flex-shrink-0 mr-2" />
310310
<span className="truncate">{conversation.title}</span>
311311
</div>
312-
<div onClick={(e) => handleMenuClick(e, conversation.id)}>
312+
<div onClick={(e) => handleMenuClick(e, conversation.conversationId)}>
313313
<MoreVertical size={16} className="flex-shrink-0 text-gray-400 hover:text-gray-600" />
314314
</div>
315315
</>
@@ -320,7 +320,7 @@ export const ChatHistoryList: React.FC<ChatHistoryListProps> = ({
320320
{!isCollapsed && (
321321
<ContextMenu
322322
items={getContextMenuItems(conversation)}
323-
isOpen={openMenuId === conversation.id}
323+
isOpen={openMenuId === conversation.conversationId}
324324
onClose={() => setOpenMenuId(null)}
325325
position={{ top: '100%', right: '12px' }}
326326
width="10rem"

src/pages/ChatPage.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const ChatPage: React.FC<ChatPageProps> = ({
106106

107107
// Get the active conversation
108108
const activeConversation = activeConversationId
109-
? conversations.find(c => c.id === activeConversationId) || null
109+
? conversations.find(c => c.conversationId === activeConversationId) || null
110110
: null;
111111

112112
// Create a new conversation
@@ -122,7 +122,7 @@ export const ChatPage: React.FC<ChatPageProps> = ({
122122

123123
// Update the state with the new list of conversations
124124
setConversations(chatService.getConversations());
125-
setActiveConversationId(newConversation.id);
125+
setActiveConversationId(newConversation.conversationId);
126126
} catch (error) {
127127
console.error('Failed to create new conversation:', error);
128128
}
@@ -161,24 +161,24 @@ export const ChatPage: React.FC<ChatPageProps> = ({
161161
};
162162

163163
// Select a conversation
164-
const handleSelectConversation = (id: string) => {
165-
setActiveConversationId(id);
164+
const handleSelectConversation = (conversationId: string) => {
165+
setActiveConversationId(conversationId);
166166

167167
// Also update in the service
168168
if (chatServiceRef.current) {
169-
chatServiceRef.current.setActiveConversation(id);
169+
chatServiceRef.current.setActiveConversation(conversationId);
170170
}
171171
};
172172

173173
// Rename a conversation
174-
const handleRenameConversation = async (id: string, newTitle: string) => {
174+
const handleRenameConversation = async (conversationId: string, newTitle: string) => {
175175
if (!isServiceInitialized || !chatServiceRef.current) return;
176176

177177
try {
178178
const chatService = chatServiceRef.current;
179179

180180
// Rename the conversation
181-
await chatService.renameConversation(id, newTitle);
181+
await chatService.renameConversation(conversationId, newTitle);
182182

183183
// Update conversations state
184184
setConversations(chatService.getConversations());
@@ -188,14 +188,14 @@ export const ChatPage: React.FC<ChatPageProps> = ({
188188
};
189189

190190
// Delete a conversation
191-
const handleDeleteConversation = async (id: string) => {
191+
const handleDeleteConversation = async (conversationId: string) => {
192192
if (!isServiceInitialized || !chatServiceRef.current) return;
193193

194194
try {
195195
const chatService = chatServiceRef.current;
196196

197197
// Delete the conversation
198-
await chatService.deleteConversation(id);
198+
await chatService.deleteConversation(conversationId);
199199

200200
// Update conversations state
201201
setConversations(chatService.getConversations());

src/services/chat-service.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class ChatService {
6666

6767
// Set first conversation as active if none is selected and there are conversations
6868
if (!this.activeConversationId && this.conversations.length > 0) {
69-
this.activeConversationId = this.conversations[0].id;
69+
this.activeConversationId = this.conversations[0].conversationId;
7070
}
7171

7272
return [...this.conversations];
@@ -90,7 +90,7 @@ export class ChatService {
9090
if (conversation) {
9191
// Update the conversation in the local list
9292
this.conversations = this.conversations.map(c =>
93-
c.id === conversationId ? conversation : c
93+
c.conversationId === conversationId ? conversation : c
9494
);
9595
return conversation;
9696
}
@@ -117,7 +117,7 @@ export class ChatService {
117117
this.conversations = [newConversation, ...this.conversations];
118118

119119
// Set as active conversation
120-
this.activeConversationId = newConversation.id;
120+
this.activeConversationId = newConversation.conversationId;
121121

122122
return newConversation;
123123
} catch (error) {
@@ -139,7 +139,7 @@ export class ChatService {
139139
throw new Error('Database service not initialized');
140140
}
141141

142-
const currentConversation = this.conversations.find(c => c.id === conversationId);
142+
const currentConversation = this.conversations.find(c => c.conversationId === conversationId);
143143
if (currentConversation === undefined) {
144144
throw new Error('Active conversation not found');
145145
}
@@ -155,7 +155,7 @@ export class ChatService {
155155

156156
// Update in memory
157157
this.conversations = this.conversations.map(c =>
158-
c.id === conversationId ? updatedConversation : c
158+
c.conversationId === conversationId ? updatedConversation : c
159159
);
160160

161161
conversationUpdate(this.conversations);
@@ -185,7 +185,7 @@ export class ChatService {
185185
};
186186

187187
this.conversations = this.conversations.map(c =>
188-
c.id === conversationId ? updatedConversation : c
188+
c.conversationId === conversationId ? updatedConversation : c
189189
);
190190

191191
conversationUpdate(this.conversations);
@@ -200,7 +200,7 @@ export class ChatService {
200200
// ---- On chunk callback ----
201201
(updated: Conversation) => {
202202
this.conversations = this.conversations.map(c =>
203-
c.id === conversationId ? updated : c
203+
c.conversationId === conversationId ? updated : c
204204
);
205205
conversationUpdate(this.conversations);
206206
},
@@ -215,7 +215,7 @@ export class ChatService {
215215

216216
// Update in memory
217217
this.conversations = this.conversations.map(c =>
218-
c.id === conversationId ? finalConversation : c
218+
c.conversationId === conversationId ? finalConversation : c
219219
);
220220

221221
conversationUpdate(this.conversations);
@@ -270,7 +270,7 @@ export class ChatService {
270270

271271
try {
272272
//#region Get message and father message in conversation
273-
const currentConversation = this.conversations.find(c => c.id === conversationId);
273+
const currentConversation = this.conversations.find(c => c.conversationId === conversationId);
274274

275275
if (!currentConversation) {
276276
throw new Error('Active conversation not found');
@@ -310,7 +310,7 @@ export class ChatService {
310310

311311
// Update in memory
312312
this.conversations = this.conversations.map(c =>
313-
c.id === conversationId ? updatedConversation : c
313+
c.conversationId === conversationId ? updatedConversation : c
314314
);
315315

316316
conversationUpdate(this.conversations);
@@ -339,7 +339,7 @@ export class ChatService {
339339
};
340340

341341
this.conversations = this.conversations.map(c =>
342-
c.id === conversationId ? updatedConversation : c
342+
c.conversationId === conversationId ? updatedConversation : c
343343
);
344344

345345
conversationUpdate(this.conversations);
@@ -354,7 +354,7 @@ export class ChatService {
354354
// ---- On chunk callback ----
355355
(updated: Conversation) => {
356356
this.conversations = this.conversations.map(c =>
357-
c.id === conversationId ? updated : c
357+
c.conversationId === conversationId ? updated : c
358358
);
359359
conversationUpdate(this.conversations);
360360
},
@@ -369,7 +369,7 @@ export class ChatService {
369369

370370
// Update in memory
371371
this.conversations = this.conversations.map(c =>
372-
c.id === conversationId ? finalConversation : c
372+
c.conversationId === conversationId ? finalConversation : c
373373
);
374374

375375
conversationUpdate(this.conversations);
@@ -423,7 +423,7 @@ export class ChatService {
423423

424424
try {
425425
//#region Get message and father message in conversation
426-
const currentConversation = this.conversations.find(c => c.id === conversationId);
426+
const currentConversation = this.conversations.find(c => c.conversationId === conversationId);
427427

428428
if (!currentConversation) {
429429
throw new Error('Active conversation not found');
@@ -480,7 +480,7 @@ export class ChatService {
480480
};
481481

482482
this.conversations = this.conversations.map(c =>
483-
c.id === conversationId ? updatedConversation : c
483+
c.conversationId === conversationId ? updatedConversation : c
484484
);
485485

486486
conversationUpdate(this.conversations);
@@ -494,7 +494,7 @@ export class ChatService {
494494
// ---- On chunk callback ----
495495
(updated: Conversation) => {
496496
this.conversations = this.conversations.map(c =>
497-
c.id === conversationId ? updated : c
497+
c.conversationId === conversationId ? updated : c
498498
);
499499
conversationUpdate(this.conversations);
500500
},
@@ -509,7 +509,7 @@ export class ChatService {
509509

510510
// Update in memory
511511
this.conversations = this.conversations.map(c =>
512-
c.id === conversationId ? finalConversation : c
512+
c.conversationId === conversationId ? finalConversation : c
513513
);
514514

515515
conversationUpdate(this.conversations);
@@ -574,7 +574,7 @@ export class ChatService {
574574
*/
575575
public getActiveConversation(): Conversation | null {
576576
if (!this.activeConversationId) return null;
577-
return this.conversations.find(c => c.id === this.activeConversationId) || null;
577+
return this.conversations.find(c => c.conversationId === this.activeConversationId) || null;
578578
}
579579

580580
/**
@@ -594,18 +594,18 @@ export class ChatService {
594594
/**
595595
* Rename a conversation
596596
*/
597-
public async renameConversation(id: string, newTitle: string): Promise<void> {
597+
public async renameConversation(conversationId: string, newTitle: string): Promise<void> {
598598
if (!this.dbService) {
599599
throw new Error('Database service not initialized');
600600
}
601601

602602
try {
603603
// Update in database
604-
await this.dbService.renameConversation(id, newTitle);
604+
await this.dbService.renameConversation(conversationId, newTitle);
605605

606606
// Update in memory
607607
this.conversations = this.conversations.map(conv =>
608-
conv.id === id ? { ...conv, title: newTitle } : conv
608+
conv.conversationId === conversationId ? { ...conv, title: newTitle } : conv
609609
);
610610
} catch (error) {
611611
console.error('Error renaming conversation:', error);
@@ -616,21 +616,21 @@ export class ChatService {
616616
/**
617617
* Delete a conversation
618618
*/
619-
public async deleteConversation(id: string): Promise<void> {
619+
public async deleteConversation(conversationId: string): Promise<void> {
620620
if (!this.dbService) {
621621
throw new Error('Database service not initialized');
622622
}
623623

624624
try {
625625
// Delete from database
626-
await this.dbService.deleteConversation(id);
626+
await this.dbService.deleteConversation(conversationId);
627627

628628
// Remove from memory
629-
this.conversations = this.conversations.filter(conv => conv.id !== id);
629+
this.conversations = this.conversations.filter(conv => conv.conversationId !== conversationId);
630630

631631
// If the active conversation was deleted, set active to null or the first available
632-
if (this.activeConversationId === id) {
633-
this.activeConversationId = this.conversations.length > 0 ? this.conversations[0].id : null;
632+
if (this.activeConversationId === conversationId) {
633+
this.activeConversationId = this.conversations.length > 0 ? this.conversations[0].conversationId : null;
634634
}
635635
} catch (error) {
636636
console.error('Error deleting conversation:', error);

0 commit comments

Comments
 (0)