From f9f3fe5ec02ef63259e1ad3ac2fa1de7335f9e54 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 2 Mar 2024 23:23:49 -0800 Subject: [PATCH] refac delete logic --- src/lib/components/chat/Messages.svelte | 132 +++++++++++------------- src/routes/(app)/+page.svelte | 4 +- src/routes/(app)/c/[id]/+page.svelte | 4 +- 3 files changed, 64 insertions(+), 76 deletions(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index fd0a6e83..77e6cad0 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -223,32 +223,22 @@ }, 100); }; - // TODO: change delete behaviour - // const deleteMessageAndDescendants = async (messageId: string) => { - // if (history.messages[messageId]) { - // history.messages[messageId].deleted = true; - - // for (const childId of history.messages[messageId].childrenIds) { - // await deleteMessageAndDescendants(childId); - // } - // } - // }; - - // const triggerDeleteMessageRecursive = async (messageId: string) => { - // await deleteMessageAndDescendants(messageId); - // await updateChatById(localStorage.token, chatId, { history }); - // await chats.set(await getChatList(localStorage.token)); - // }; - const messageDeleteHandler = async (messageId) => { - if (history.messages[messageId]) { - history.messages[messageId].deleted = true; + const messageParentId = history.messages[messageId]?.parentId; - for (const childId of history.messages[messageId].childrenIds) { - history.messages[childId].deleted = true; - } + if (messageParentId !== null) { + history.messages[messageParentId].childrenIds = [] } - await updateChatById(localStorage.token, chatId, { history }); + + delete history.messages[messageId]; + history.currentId = messageParentId; + + await tick(); + + await updateChatById(localStorage.token, chatId, { + messages: messages, + history: history + }); }; @@ -258,57 +248,55 @@
{#key chatId} {#each messages as message, messageIdx} - {#if !message.deleted} -
-
- {#if message.role === 'user'} - messageDeleteHandler(message.id)} - user={$user} - {message} - isFirstMessage={messageIdx === 0} - siblings={message.parentId !== null - ? history.messages[message.parentId]?.childrenIds ?? [] - : Object.values(history.messages) - .filter((message) => message.parentId === null) - .map((message) => message.id) ?? []} - {confirmEditMessage} - {showPreviousMessage} - {showNextMessage} - {copyToClipboard} - /> - {:else} - { - console.log('save', e); +
+
+ {#if message.role === 'user'} + messageDeleteHandler(message.id)} + user={$user} + {message} + isFirstMessage={messageIdx === 0} + siblings={message.parentId !== null + ? history.messages[message.parentId]?.childrenIds ?? [] + : Object.values(history.messages) + .filter((message) => message.parentId === null) + .map((message) => message.id) ?? []} + {confirmEditMessage} + {showPreviousMessage} + {showNextMessage} + {copyToClipboard} + /> + {:else} + { + console.log('save', e); - const message = e.detail; - history.messages[message.id] = message; - await updateChatById(localStorage.token, chatId, { - messages: messages, - history: history - }); - }} - /> - {/if} -
+ const message = e.detail; + history.messages[message.id] = message; + await updateChatById(localStorage.token, chatId, { + messages: messages, + history: history + }); + }} + /> + {/if}
- {/if} +
{/each} {#if bottomPadding} diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 04dc922c..da3aff23 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -342,7 +342,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ @@ -550,7 +550,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 69ebfde5..85638c40 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -355,7 +355,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({ @@ -563,7 +563,7 @@ content: $settings.system } : undefined, - ...messages.filter((message) => !message.deleted) + ...messages ] .filter((message) => message) .map((message, idx, arr) => ({