Refactor deleteMessage function and update deleteMessagePair usage

This commit is contained in:
Danny Liu 2024-02-20 15:12:06 -08:00
parent cd3cecd2f0
commit 941bcdc2e5
2 changed files with 13 additions and 8 deletions

View file

@ -222,13 +222,12 @@
}, 100); }, 100);
}; };
const deleteMessage = async (messageId) => { const deleteMessagePair = async (messageId) => {
history.messages[messageId].deleted = true; history.messages[messageId].deleted = true;
history.messages[history.messages[messageId].childrenIds[0]].deleted = true; history.messages[history.messages[messageId].childrenIds[0]].deleted = true;
updateChatById(localStorage.token, chatId, {
messages: messages, await updateChatById(localStorage.token, chatId, { history });
history: history await chats.set(await getChatList(localStorage.token));
});
}; };
</script> </script>
@ -247,7 +246,7 @@
> >
{#if message.role === 'user'} {#if message.role === 'user'}
<UserMessage <UserMessage
on:delete={() => deleteMessage(message.id)} on:delete={() => deleteMessagePair(message.id)}
user={$user} user={$user}
{message} {message}
isFirstMessage={messageIdx === 0} isFirstMessage={messageIdx === 0}

View file

@ -45,6 +45,10 @@
edit = false; edit = false;
editedContent = ''; editedContent = '';
}; };
const deleteMessageHandler = async () => {
dispatch('delete', message.id);
};
</script> </script>
<div class=" flex w-full"> <div class=" flex w-full">
@ -288,7 +292,9 @@
{#if !isFirstMessage} {#if !isFirstMessage}
<button <button
class="invisible group-hover:visible p-1 rounded dark:hover:text-white transition" class="invisible group-hover:visible p-1 rounded dark:hover:text-white transition"
on:click={() => dispatch('delete', { messageId: message.id })} on:click={() => {
deleteMessageHandler();
}}
> >
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"