Skip to content

Commit 95e7217

Browse files
committed
fix: draft not clearing after sending message
Closes #4430. The bug was introduced in #4144. There we made `throttle` accept the `text` argument instead of reading `this.state.text`, and `this.state.text` would get set to `''` after the message has been sent, but then we'd still invoke the throttled function, with the text from the last `onChange` event.
1 parent 21d9acb commit 95e7217

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- update `@deltachat/message_parser_wasm` from `0.11.0` to `0.12.0` #4477
2626

2727
## Fixed
28+
- fix draft not getting cleared after sending the message #
2829
- fix chat "scrolls up" right after switching (rev 2) #4431
2930
- when deleting a message from gallery, update gallery items to remove the respective item #4457
3031
- accessibility: fix arrow-key navigation stopping working after ~10 key presses #4441

packages/frontend/src/components/composer/ComposerMessageInput.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export default class ComposerMessageInput extends React.Component<
4949
this.insertStringAtCursorPosition =
5050
this.insertStringAtCursorPosition.bind(this)
5151

52+
// Remember that the draft might be updated from the outside
53+
// of this component (with the `setText` method,
54+
// e.g. when the draft gets cleared after sending a message).
55+
// This can happen _after_ an `onChange` event but _before_
56+
// the unrelying throttled function invokation.
5257
this.throttledSaveDraft = throttle((text, chatId) => {
5358
if (this.state.chatId === chatId) {
5459
this.props.updateDraftText(text.trim() === '' ? '' : text, chatId)
@@ -77,8 +82,16 @@ export default class ComposerMessageInput extends React.Component<
7782
return null
7883
}
7984

85+
/**
86+
* Sets the text area value, and ensures that `updateDraftText`
87+
* does not get invoked until the next change to the draft text.
88+
*
89+
* Useful for setting / clearing draft text afer loading it from core,
90+
* e.g. after sending the message or opening a chat with a.
91+
*/
8092
setText(text: string | null) {
8193
this.setState({ text: text || '', loadingDraft: false })
94+
this.throttledSaveDraft.cancel()
8295
}
8396

8497
setComposerSize(size: number) {

packages/shared/util.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function throttle<R, A extends any[]>(
2222
let inThrottle: boolean,
2323
timeout: ReturnType<typeof setTimeout>,
2424
lastTime: number
25-
return (...args: A) => {
25+
const ret = (...args: A) => {
2626
if (!inThrottle) {
2727
fn(...args)
2828
lastTime = performance.now()
@@ -38,4 +38,8 @@ export function throttle<R, A extends any[]>(
3838
)
3939
}
4040
}
41+
ret.cancel = () => {
42+
clearTimeout(timeout)
43+
}
44+
return ret
4145
}

0 commit comments

Comments
 (0)