From 889c2658ba9b4c88b4e0680db2be2fc8181e31d1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 27 Nov 2023 14:20:36 -0600 Subject: [PATCH 1/4] ComposeForm: refactor canSubmit so it works correctly in more scenarios --- .../compose/components/compose-form.tsx | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/features/compose/components/compose-form.tsx b/src/features/compose/components/compose-form.tsx index 182707b683..72acd166ed 100644 --- a/src/features/compose/components/compose-form.tsx +++ b/src/features/compose/components/compose-form.tsx @@ -101,18 +101,20 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab const formRef = useRef(null); const spoilerTextRef = useRef(null); const editorRef = useRef(null); + const { isDraggedOver } = useDraggedFiles(formRef); const text = editorRef.current?.getEditorState().read(() => $getRoot().getTextContent()) ?? ''; - const { isDraggedOver } = useDraggedFiles(formRef); + const fulltext = [spoilerText, countableText(text)].join(''); + + const isEmpty = !(fulltext.trim() || anyMedia); + const condensed = shouldCondense && !isDraggedOver && !composeFocused && isEmpty && !isUploading; + const shouldAutoFocus = autoFocus && !showSearch && !isMobile(window.innerWidth); + const canSubmit = !isSubmitting && !isUploading && !isChangingUpload && !isEmpty && length(fulltext) <= maxTootChars; const getClickableArea = () => { return clickableAreaRef ? clickableAreaRef.current : formRef.current; }; - const isEmpty = () => { - return !(text || spoilerText || anyMedia); - }; - const isClickOutside = (e: MouseEvent | React.MouseEvent) => { return ![ // List of elements that shouldn't collapse the composer when clicked @@ -125,10 +127,10 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab }; const handleClick = useCallback((e: MouseEvent | React.MouseEvent) => { - if (isEmpty() && isClickOutside(e)) { + if (isEmpty && isClickOutside(e)) { handleClickOutside(); } - }, []); + }, [isEmpty]); const handleClickOutside = () => { setComposeFocused(false); @@ -139,20 +141,12 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab }; const handleSubmit = (e?: React.FormEvent) => { + if (!canSubmit) return; + e?.preventDefault(); + dispatch(changeCompose(id, text)); - - // Submit disabled: - const fulltext = [spoilerText, countableText(text)].join(''); - - if (e) { - e.preventDefault(); - } - - if (isSubmitting || isUploading || isChangingUpload || length(fulltext) > maxTootChars || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) { - return; - } - dispatch(submitCompose(id, { history })); + editorRef.current?.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined); }; @@ -215,12 +209,6 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab ), [features, id]); - const condensed = shouldCondense && !isDraggedOver && !composeFocused && isEmpty() && !isUploading; - const disabled = isSubmitting; - const countedText = [spoilerText, countableText(text)].join(''); - const disabledButton = disabled || isUploading || isChangingUpload || length(countedText) > maxTootChars || (countedText.length !== 0 && countedText.trim().length === 0 && !anyMedia); - const shouldAutoFocus = autoFocus && !showSearch && !isMobile(window.innerWidth); - const composeModifiers = !condensed && ( @@ -323,7 +311,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab )} -