Fix twitter focusing bug (#74)

* fixes focusing bug and does some render optimizations

* fix(phone/twitter): Fixed conditional hook calls

Co-authored-by: Taso <[email protected]>
This commit is contained in:
ErikandTaso authored and GitHub committed 2021-02-08 15:24:42 -05:00
1 parent d793d1b635
commit ddb93b4de8
4 files changed
+50 -28

No files matched your search

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