diff --git a/phone/src/apps/twitter/components/AddTweetModal.tsx b/phone/src/apps/twitter/components/AddTweetModal.tsx index f617c26..a51b074 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.tsx +++ b/phone/src/apps/twitter/components/AddTweetModal.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useCallback, useState, useEffect } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { v4 as uuidv4 } from 'uuid'; @@ -83,19 +83,23 @@ export const AddTweetModal = () => { _handleClose(); } }; + + const handleimageChange = useCallback((link) => setLink(link), []); + + const handleMessageChange = useCallback((message) => setMessage(message), [ + setMessage, + ]); + useEffect(() => { window.addEventListener('keydown', _handleEscape, true); return () => window.removeEventListener('keydown', _handleEscape); }); - if (!config) return null; - const isValidMessage = message => { + const isValidMessage = (message) => { if (message.length > characterLimit) return false; - if (getNewLineCount(message) > newLineLimit) return false; - return true; - } - + return getNewLineCount(message) > newLineLimit; + }; const submitTweet = () => { const cleanedMessage = message.trim(); if (cleanedMessage.length === 0) return; @@ -113,8 +117,6 @@ export const AddTweetModal = () => { _handleClose(); }; - const handleMessageChange = (message) => setMessage(message); - const addImage = () => { // strip any whitespace from the link in case the user // added some spaces/returns accidentally @@ -137,7 +139,6 @@ export const AddTweetModal = () => { }; const removeImage = (id) => setImages(images.filter((image) => id !== image.id)); - const handleimageChange = (e) => setLink(e.target.value); const toggleShowImagePrompt = () => { setShowEmoji(false); // clear the emoji so we can switch between emoji/images @@ -159,7 +160,11 @@ export const AddTweetModal = () => { return ( - + ({ +const useStyles = makeStyles({ buttons: { display: 'flex', flexFlow: 'row nowrap', @@ -16,7 +16,7 @@ const useStyles = makeStyles((theme) => ({ background: 'transparent', minWidth: '45px', // override default material-ui spacing between buttons }, -})); +}); export const IconButtons = ({ onImageClick, onEmojiClick }) => { const classes = useStyles(); @@ -41,4 +41,4 @@ export const IconButtons = ({ onImageClick, onEmojiClick }) => { ); }; -export default IconButtons; +export default memo(IconButtons); diff --git a/phone/src/apps/twitter/components/images/ImagePrompt.tsx b/phone/src/apps/twitter/components/images/ImagePrompt.tsx index 3552983..8ad2db9 100644 --- a/phone/src/apps/twitter/components/images/ImagePrompt.tsx +++ b/phone/src/apps/twitter/components/images/ImagePrompt.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { memo, useCallback, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { makeStyles } from '@material-ui/core/styles'; import { TextField } from '@material-ui/core'; @@ -15,22 +15,29 @@ const useStyles = makeStyles((theme) => ({ })); export const ImagePrompt = ({ visible, value, handleChange }) => { + const textFieldRef = useRef(null); const { t } = useTranslation(); const classes = useStyles(); + useEffect(() => { + textFieldRef.current && textFieldRef.current.focus(); + }, [visible]); + + const handleImageChange = useCallback(e => handleChange(e.target.value), []); + if (!visible) return null; return ( input && input.focus()} + inputRef={textFieldRef} /> ); }; -export default ImagePrompt; +export default memo(ImagePrompt); diff --git a/phone/src/apps/twitter/components/tweet/TweetMessage.tsx b/phone/src/apps/twitter/components/tweet/TweetMessage.tsx index fdae702..0643613 100644 --- a/phone/src/apps/twitter/components/tweet/TweetMessage.tsx +++ b/phone/src/apps/twitter/components/tweet/TweetMessage.tsx @@ -1,4 +1,4 @@ -import React, { useRef } from 'react'; +import React, { memo, useEffect, useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { makeStyles } from '@material-ui/core/styles'; import { TextField } from '@material-ui/core'; @@ -19,28 +19,38 @@ const useStyles = makeStyles({ }, }); -export const TweetMessage = ({ message, handleChange }) => { +export const TweetMessage = ({ modalVisible, message, handleChange }) => { const textFieldRef = useRef(null); + const textFieldInputRef = useRef(null); const classes = useStyles(); const { config } = usePhone(); const { t } = useTranslation(); - if (!config) return null; const { characterLimit, newLineLimit } = config.twitter; - const _handleChange = (e) => { - // when the user types scroll the text field to the bottom - // so that we always have the latest line and error message - // in view - e.preventDefault(); - textFieldRef.current.scrollTop = textFieldRef.current.scrollHeight; - handleChange(e.target.value); - } - + useEffect(() => { + textFieldInputRef.current && textFieldInputRef.current.focus(); + // we pass in modalVisible to this component so that we can + // intelligently decide when to focus the input field. + }, [modalVisible]); + + const _handleChange = useCallback( + (e) => { + // when the user types scroll the text field to the bottom + // so that we always have the latest line and error message + // in view + e.preventDefault(); + textFieldRef.current.scrollTop = textFieldRef.current.scrollHeight; + handleChange(e.target.value); + }, + [handleChange] + ); + + if (!config) return null; + let errorMessage = null; - const overCharacterLimit = - message.trim().length > characterLimit; + const overCharacterLimit = message.trim().length > characterLimit; const characterWarningPrompt = `${t( 'APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT' )} (${characterLimit})`; @@ -64,7 +74,7 @@ export const TweetMessage = ({ message, handleChange }) => { onChange={_handleChange} multiline placeholder={t('APPS_TWITTER_TWEET_MESSAGE_PLACEHOLDER')} - inputRef={(input) => input && input.focus()} + inputRef={textFieldInputRef} error={errorMessage !== null} helperText={errorMessage || null} ref={textFieldRef} @@ -72,4 +82,4 @@ export const TweetMessage = ({ message, handleChange }) => { ); }; -export default TweetMessage; +export default memo(TweetMessage);