🐛 fix(message): prevent history loss when editing messages with duplicate IDs
- Store editing message object reference using useRef to avoid ID conflicts - Use object reference comparison first before falling back to ID matching - Apply fix to both save and cancel operations in message editing - Clear reference after edit completion to prevent stale object issues Previously, when editing imported messages that contained duplicate IDs, the findIndex operation would match the first occurrence rather than the intended message, causing conversation history truncation when saving edits. This change stores and uses object references for accurate message identification during the editing process.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useCallback, useState, useRef } from 'react';
|
||||
import { Toast, Modal } from '@douyinfe/semi-ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { getTextContent, buildApiPayload, createLoadingAssistantMessage } from '../utils/messageUtils';
|
||||
@@ -14,10 +14,12 @@ export const useMessageEdit = (
|
||||
const { t } = useTranslation();
|
||||
const [editingMessageId, setEditingMessageId] = useState(null);
|
||||
const [editValue, setEditValue] = useState('');
|
||||
const editingMessageRef = useRef(null);
|
||||
|
||||
const handleMessageEdit = useCallback((targetMessage) => {
|
||||
const editableContent = getTextContent(targetMessage);
|
||||
setEditingMessageId(targetMessage.id);
|
||||
editingMessageRef.current = targetMessage;
|
||||
setEditValue(editableContent);
|
||||
}, []);
|
||||
|
||||
@@ -25,8 +27,11 @@ export const useMessageEdit = (
|
||||
if (!editingMessageId || !editValue.trim()) return;
|
||||
|
||||
setMessage(prevMessages => {
|
||||
const messageIndex = prevMessages.findIndex(msg => msg.id === editingMessageId);
|
||||
if (messageIndex === -1) return prevMessages;
|
||||
let messageIndex = prevMessages.findIndex(msg => msg === editingMessageRef.current);
|
||||
|
||||
if (messageIndex === -1) {
|
||||
messageIndex = prevMessages.findIndex(msg => msg.id === editingMessageId);
|
||||
}
|
||||
|
||||
const targetMessage = prevMessages[messageIndex];
|
||||
let newContent;
|
||||
@@ -82,12 +87,14 @@ export const useMessageEdit = (
|
||||
});
|
||||
|
||||
setEditingMessageId(null);
|
||||
editingMessageRef.current = null;
|
||||
setEditValue('');
|
||||
Toast.success({ content: t('消息已更新'), duration: 2 });
|
||||
}, [editingMessageId, editValue, t, inputs, parameterEnabled, sendRequest, setMessage, saveMessages]);
|
||||
|
||||
const handleEditCancel = useCallback(() => {
|
||||
setEditingMessageId(null);
|
||||
editingMessageRef.current = null;
|
||||
setEditValue('');
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user