Commit a9d74b66 authored by Timothy J. Baek's avatar Timothy J. Baek
Browse files

refac

parent 03907f9a
...@@ -224,42 +224,24 @@ ...@@ -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
messageChildrenIds.forEach((childId) => { for (const childId of childrenIds) {
const child = history.messages[childId]; const childMessage = history.messages[childId];
if (child && child.childrenIds) { const grandChildrenIds = childMessage.childrenIds ?? [];
if (child.childrenIds.length === 0 && !hasSibling) { // if last prompt/response pair grandchildrenIds.push(...grandChildrenIds);
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
history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds
.filter((id) => id !== childId);
});
// remove prompt history.messages[parentId].childrenIds.push(...grandchildrenIds);
history.messages[messageParentId].childrenIds = history.messages[messageParentId].childrenIds history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter(
.filter((id) => id !== messageId); (id) => id !== messageId
);
await updateChatById(localStorage.token, chatId, { await updateChatById(localStorage.token, chatId, { messages, history });
messages: messages,
history: history
});
}; };
</script> </script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment