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
+57 -35

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 { 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 (
<Modal visible={modalVisible} handleClose={_handleClose}>
<TweetMessage message={message} handleChange={handleMessageChange} />
<TweetMessage
modalVisible={modalVisible}
message={message}
handleChange={handleMessageChange}
/>
<ImagePrompt
visible={showImagePrompt}
value={link}
@@ -1,11 +1,11 @@
import React from 'react';
import React, { memo } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Button } from '@material-ui/core';
import InsertPhotoIcon from '@material-ui/icons/InsertPhoto';
import EmojiIcon from '@material-ui/icons/SentimentSatisfied';
import { usePhone } from '../../../../os/phone/hooks/usePhone';
const useStyles = makeStyles((theme) => ({
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);
@@ -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 (
<TextField
value={value}
inputProps={{ className: classes.textFieldInput }}
className={classes.textField}
onChange={handleChange}
onChange={handleImageChange}
multiline
size='small'
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 { 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);