This commit is contained in:
Timothy J. Baek 2024-03-06 00:33:47 -08:00
parent 03907f9a8a
commit a9d74b669e

View file

@ -224,42 +224,24 @@
}; };
const messageDeleteHandler = async (messageId) => { const messageDeleteHandler = async (messageId) => {
const messageToDelete = history.messages[messageId]; const message = history.messages[messageId];
const messageParentId = messageToDelete.parentId; const parentId = message.parentId;
const messageChildrenIds = messageToDelete.childrenIds ?? []; const childrenIds = message.childrenIds ?? [];
const grandchildrenIds = [];
const hasSibling = messageChildrenIds.some(childId => history.messages[childId]?.childrenIds?.length > 0); // Iterate through childrenIds to find grandchildrenIds
for (const childId of childrenIds) {
const childMessage = history.messages[childId];
const grandChildrenIds = childMessage.childrenIds ?? [];
grandchildrenIds.push(...grandChildrenIds);
}
messageChildrenIds.forEach((childId) => { history.messages[parentId].childrenIds.push(...grandchildrenIds);
const child = history.messages[childId]; history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter(
if (child && child.childrenIds) { (id) => id !== messageId
if (child.childrenIds.length === 0 && !hasSibling) { // if last prompt/response pair );
history.messages[messageParentId].childrenIds = []
history.currentId = messageParentId;
}
else {
child.childrenIds.forEach((grandChildId) => {
if (history.messages[grandChildId]) {
history.messages[grandChildId].parentId = messageParentId;
history.messages[messageParentId].childrenIds.push(grandChildId);
}
});
}
}
// remove response await updateChatById(localStorage.token, chatId, { messages, history });
history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds
.filter((id) => id !== childId);
});
// remove prompt
history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds
.filter((id) => id !== messageId);
await updateChatById(localStorage.token, chatId, {
messages: messages,
history: history
});
}; };
</script> </script>