CompositionInput: Use sendingRef to ensure we don't double-submit

This commit is contained in:
Scott Nonnenberg 2022-06-29 19:05:41 -07:00 committed by GitHub
parent 6a509bab72
commit 8b30d24c18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -127,6 +127,7 @@ export function CompositionInput(props: Props): React.ReactElement {
const quillRef = React.useRef<Quill>();
const scrollerRef = React.useRef<HTMLDivElement>(null);
const propsRef = React.useRef<Props>(props);
const canSendRef = React.useRef<boolean>(false);
const memberRepositoryRef = React.useRef<MemberRepository>(
new MemberRepository()
);
@ -206,6 +207,7 @@ export function CompositionInput(props: Props): React.ReactElement {
return;
}
canSendRef.current = true;
quill.setText('');
const historyModule = quill.getModule('history');
@ -235,11 +237,19 @@ export function CompositionInput(props: Props): React.ReactElement {
return;
}
if (!canSendRef.current) {
log.warn(
'CompositionInput: Not submitting message - cannot send right now'
);
return;
}
const [text, mentions] = getTextAndMentions();
log.info(
`CompositionInput: Submitting message ${timestamp} with ${mentions.length} mentions`
);
canSendRef.current = false;
onSubmit(text, mentions, timestamp);
};
@ -257,6 +267,10 @@ export function CompositionInput(props: Props): React.ReactElement {
propsRef.current = props;
}, [props]);
React.useEffect(() => {
canSendRef.current = !disabled;
}, [disabled]);
const onShortKeyEnter = (): boolean => {
submit();
return false;