From 1fc06f2fd58747af182c7538f1cdd3748ace1af0 Mon Sep 17 00:00:00 2001 From: WofWca Date: Mon, 7 Oct 2024 22:05:06 +0400 Subject: [PATCH] fix: not scrolling to bottom on sending message Closes https://github.com/deltachat/deltachat-desktop/issues/4186. The bug was introduced in fecdbbe3676521dea0f7aee58b9beaef0c7fff4f, https://github.com/deltachat/deltachat-desktop/pull/4145. --- CHANGELOG.md | 2 +- packages/frontend/src/stores/messagelist.ts | 35 ++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd412db70..0764324646 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ - windows 64bit and 32bit protable and setup now have different filenames #4131 - scroll the selected account into view in the accounts sidebar #4137 - dev: clarify scrolling-related code #4121 -- improved performance a bit #4145 +- improved performance a bit #4145, #4188 - show contact / group name when pasting invite link in the search field #4151 - Update local help (2024-10-02) #4165 - trim whitepaces when reading from clipboard in qr code reader #4169 diff --git a/packages/frontend/src/stores/messagelist.ts b/packages/frontend/src/stores/messagelist.ts index 08b0b9a8a5..3321b58c66 100644 --- a/packages/frontend/src/stores/messagelist.ts +++ b/packages/frontend/src/stores/messagelist.ts @@ -572,10 +572,37 @@ class MessageListStore extends Store { if (messagesAlreadyLoaded) { newMessageCache = this.state.messageCache - oldestFetchedMessageListItemIndex = - this.state.oldestFetchedMessageListItemIndex - newestFetchedMessageListItemIndex = - this.state.newestFetchedMessageListItemIndex + + // Why do we need `Math.min` / `Math.max` here, instead of simply + // keeping `this.state.oldestFetchedMessageListItemIndex` + // and `this.state.newestFetchedMessageListItemIndex` as they are? + // Because some other code might update the state in such a way + // that `messageCache` and these + // `(oldest|newest)FetchedMessageListItemIndex` are out of sync: + // `messageCache` actually has a message, but + // these integers say that the message is not yet fetched. + // Namely, this can happen inside of `messageChanged` when + // it gets invoked for a not yet fetched message, and it gets + // added `messageCache` instead of getting updated. + // This, in turn, can happen when you send a message. + // + // The result would be that we'd fail to jump to message inside of + // `MessageList.tsx`, because the message wouldn't be rendered, + // because we only render messages that are between + // `oldestFetchedMessageListItemIndex` and + // `newestFetchedMessageListItemIndex` (see `activeView`). + // + // TODO it would be ideal to get ensure that we don't corrupt + // the state in the first place, but let's make + // this workaround for now. + oldestFetchedMessageListItemIndex = Math.min( + this.state.oldestFetchedMessageListItemIndex, + oldestFetchedMessageListItemIndex + ) + newestFetchedMessageListItemIndex = Math.max( + this.state.newestFetchedMessageListItemIndex, + newestFetchedMessageListItemIndex + ) } else { newMessageCache = (await loadMessages(