From d95152efe2d2d19bbae7f46fb8f7b66e065103a8 Mon Sep 17 00:00:00 2001 From: Emil Elgaard Date: Sun, 26 Mar 2023 16:59:30 -0400 Subject: [PATCH] guard against deleting the last chat --- src/lib/Sidebar.svelte | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/Sidebar.svelte b/src/lib/Sidebar.svelte index 7d02432..b8f4f6a 100644 --- a/src/lib/Sidebar.svelte +++ b/src/lib/Sidebar.svelte @@ -8,10 +8,11 @@ $: activeChatId = $params && $params.chatId ? parseInt($params.chatId) : undefined - function delChat(chatId) { + function delChat (chatId) { if (activeChatId === chatId) { // switch to another chat if deleting the active one - const newChatId = _.maxBy($chatsStorage.filter(chat => chat.id !== chatId), 'id').id + const newChatId = _.maxBy($chatsStorage.filter(chat => chat.id !== chatId), 'id')?.id + if (!newChatId) return replace(`/chat/:${newChatId}`).then(() => deleteChat(chatId)) } else { deleteChat(chatId)