|
|
@@ -291,96 +291,35 @@ export const useChatStore = create<ChatStoreState>((set, get) => ({
|
|
291
|
291
|
* @param sessionId - Session ID to delete
|
|
292
|
292
|
*/
|
|
293
|
293
|
deleteSession: async (sessionId: string) => {
|
|
294
|
|
- try {
|
|
295
|
|
- // ⭐ 在删除前先获取会话信息,用于后续判断是否需要关闭编辑器
|
|
296
|
|
- const sessionToDelete = get().sessions.find((s) => s.id === sessionId);
|
|
297
|
|
-
|
|
298
|
|
- // 调用后端 API 删除会话的所有文档
|
|
299
|
|
- const { deleteSessionHistory } = await import('../services/sessionService');
|
|
300
|
|
- await deleteSessionHistory(sessionId);
|
|
301
|
|
-
|
|
302
|
|
- // 删除成功后,从本地状态中移除
|
|
303
|
|
- set((state) => {
|
|
304
|
|
- const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
305
|
|
- saveSessionsToStorage(updatedSessions);
|
|
306
|
|
-
|
|
307
|
|
- // If deleting current session, clear current session
|
|
308
|
|
- const newCurrentSessionId =
|
|
309
|
|
- state.currentSessionId === sessionId ? null : state.currentSessionId;
|
|
310
|
|
-
|
|
311
|
|
- return {
|
|
312
|
|
- currentSessionId: newCurrentSessionId,
|
|
313
|
|
- sessions: updatedSessions,
|
|
314
|
|
- messages: newCurrentSessionId === null ? [] : state.messages,
|
|
315
|
|
- };
|
|
316
|
|
- });
|
|
317
|
|
-
|
|
318
|
|
- // ⭐ 新增逻辑: 检查编辑器中是否打开了该会话的文档
|
|
319
|
|
- // 如果是,关闭编辑器并返回导出记录面板
|
|
320
|
|
- if (sessionToDelete) {
|
|
321
|
|
- const { useEditorStore } = await import('./editorStore');
|
|
322
|
|
- const { useUIStore } = await import('./uiStore');
|
|
323
|
|
- const editorState = useEditorStore.getState();
|
|
324
|
|
- const uiState = useUIStore.getState();
|
|
325
|
|
-
|
|
326
|
|
- // 检查当前打开的文档是否属于被删除的会话
|
|
327
|
|
- // 通过检查 documentId 是否在被删除会话的 exportRecords 中
|
|
328
|
|
- if (editorState.documentId) {
|
|
329
|
|
- const isDocumentFromSession = sessionToDelete.exportRecords.some(
|
|
330
|
|
- (record) => record.documentId === editorState.documentId
|
|
331
|
|
- );
|
|
332
|
|
-
|
|
333
|
|
- if (isDocumentFromSession) {
|
|
334
|
|
- // 1. 关闭编辑器状态
|
|
335
|
|
- editorState.reset();
|
|
336
|
|
- // 2. 关闭文档预览,返回导出记录面板
|
|
337
|
|
- uiState.closeDocumentPreview();
|
|
338
|
|
- message.info('已关闭编辑器中的文档');
|
|
339
|
|
- }
|
|
340
|
|
- }
|
|
341
|
|
- }
|
|
342
|
|
- } catch (error) {
|
|
343
|
|
- // ⭐ 在删除前先获取会话信息
|
|
344
|
|
- const sessionToDelete = get().sessions.find((s) => s.id === sessionId);
|
|
|
294
|
+ const sessionToDelete = get().sessions.find((s) => s.id === sessionId);
|
|
|
295
|
+ if (!sessionToDelete) return;
|
|
345
|
296
|
|
|
346
|
|
- // 即使后端删除失败,也从本地状态中移除(因为可能是网络问题)
|
|
347
|
|
- set((state) => {
|
|
348
|
|
- const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
349
|
|
- saveSessionsToStorage(updatedSessions);
|
|
|
297
|
+ const { deleteSessionHistory } = await import('../services/sessionService');
|
|
|
298
|
+ await deleteSessionHistory(sessionId);
|
|
350
|
299
|
|
|
351
|
|
- const newCurrentSessionId =
|
|
352
|
|
- state.currentSessionId === sessionId ? null : state.currentSessionId;
|
|
353
|
|
-
|
|
354
|
|
- return {
|
|
355
|
|
- currentSessionId: newCurrentSessionId,
|
|
356
|
|
- sessions: updatedSessions,
|
|
357
|
|
- messages: newCurrentSessionId === null ? [] : state.messages,
|
|
358
|
|
- };
|
|
359
|
|
- });
|
|
360
|
|
-
|
|
361
|
|
- // ⭐ 错误情况下也需要关闭编辑器并返回导出记录面板
|
|
362
|
|
- if (sessionToDelete) {
|
|
363
|
|
- const { useEditorStore } = await import('./editorStore');
|
|
364
|
|
- const { useUIStore } = await import('./uiStore');
|
|
365
|
|
- const editorState = useEditorStore.getState();
|
|
366
|
|
- const uiState = useUIStore.getState();
|
|
367
|
|
-
|
|
368
|
|
- if (editorState.documentId) {
|
|
369
|
|
- const isDocumentFromSession = sessionToDelete.exportRecords.some(
|
|
370
|
|
- (record) => record.documentId === editorState.documentId
|
|
371
|
|
- );
|
|
|
300
|
+ set((state) => {
|
|
|
301
|
+ const updatedSessions = state.sessions.filter((s) => s.id !== sessionId);
|
|
|
302
|
+ saveSessionsToStorage(updatedSessions);
|
|
|
303
|
+ const newCurrentSessionId =
|
|
|
304
|
+ state.currentSessionId === sessionId ? null : state.currentSessionId;
|
|
372
|
305
|
|
|
373
|
|
- if (isDocumentFromSession) {
|
|
374
|
|
- // 1. 关闭编辑器状态
|
|
375
|
|
- editorState.reset();
|
|
376
|
|
- // 2. 关闭文档预览,返回导出记录面板
|
|
377
|
|
- uiState.closeDocumentPreview();
|
|
378
|
|
- message.info('已关闭编辑器中的文档');
|
|
379
|
|
- }
|
|
380
|
|
- }
|
|
381
|
|
- }
|
|
|
306
|
+ return {
|
|
|
307
|
+ currentSessionId: newCurrentSessionId,
|
|
|
308
|
+ sessions: updatedSessions,
|
|
|
309
|
+ messages: newCurrentSessionId === null ? [] : state.messages,
|
|
|
310
|
+ };
|
|
|
311
|
+ });
|
|
382
|
312
|
|
|
383
|
|
- throw error; // 重新抛出错误供调用者处理
|
|
|
313
|
+ const { useEditorStore } = await import('./editorStore');
|
|
|
314
|
+ const { useUIStore } = await import('./uiStore');
|
|
|
315
|
+ const editorState = useEditorStore.getState();
|
|
|
316
|
+ if (
|
|
|
317
|
+ editorState.documentId &&
|
|
|
318
|
+ sessionToDelete.exportRecords.some((record) => record.documentId === editorState.documentId)
|
|
|
319
|
+ ) {
|
|
|
320
|
+ editorState.reset();
|
|
|
321
|
+ useUIStore.getState().closeDocumentPreview();
|
|
|
322
|
+ message.info('已关闭编辑器中的文档');
|
|
384
|
323
|
}
|
|
385
|
324
|
},
|
|
386
|
325
|
|