mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
handle case where user spams new lines
This commit is contained in:
1 parent
1fca1ac200
commit
37504feec7
7 files changed
+51
-13
No files matched your search
@@ -37,7 +37,8 @@ Below is the list of configuration options:
|
|||||||
| twitter.allowEdtiableProfileName | true | Whether or not players can edit their profile name |
|
| twitter.allowEdtiableProfileName | true | Whether or not players can edit their profile name |
|
||||||
| twitter.allowDeleteTweets | true | Whether or not players can delete their own tweets |
|
| twitter.allowDeleteTweets | true | Whether or not players can delete their own tweets |
|
||||||
| twitter.allowReportTweets | true | Whether or not players can report tweets. Optional discord integration. |
|
| twitter.allowReportTweets | true | Whether or not players can report tweets. Optional discord integration. |
|
||||||
| twitter.characterLimit | 240 | Limit on how many characters tweets can contain |
|
| twitter.characterLimit | 160 | Limit on how many characters tweets can contain |
|
||||||
|
| twitter.newLineLimit | 10 | Limit on how many new lines a message can contain |
|
||||||
| twitter.enableAvatars | true | Whether or not player's can select avatars for their twitter profile |
|
| twitter.enableAvatars | true | Whether or not player's can select avatars for their twitter profile |
|
||||||
| twitter.enableEmojis | true | Whether or not player's can use emojis in tweets |
|
| twitter.enableEmojis | true | Whether or not player's can use emojis in tweets |
|
||||||
| twitter.enableImages | true | Whether or not player's can add images to tweets (external URLs) |
|
| twitter.enableImages | true | Whether or not player's can add images to tweets (external URLs) |
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
"allowDeleteTweets": true,
|
"allowDeleteTweets": true,
|
||||||
"allowReportTweets": true,
|
"allowReportTweets": true,
|
||||||
"characterLimit": 160,
|
"characterLimit": 160,
|
||||||
|
"newLineLimit": 10,
|
||||||
"enableAvatars": true,
|
"enableAvatars": true,
|
||||||
"enableEmojis": true,
|
"enableEmojis": true,
|
||||||
"enableImages": true,
|
"enableImages": true,
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import TweetMessage from './tweet/TweetMessage';
|
|||||||
import ControlButtons from './buttons/ControlButtons';
|
import ControlButtons from './buttons/ControlButtons';
|
||||||
import IconButtons from './buttons/IconButtons';
|
import IconButtons from './buttons/IconButtons';
|
||||||
import { usePhone } from '../../../os/phone/hooks/usePhone';
|
import { usePhone } from '../../../os/phone/hooks/usePhone';
|
||||||
|
import { getNewLineCount } from '../utils/message';
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
root: {
|
root: {
|
||||||
@@ -67,6 +68,7 @@ export const AddTweetModal = () => {
|
|||||||
const [link, setLink] = useState('');
|
const [link, setLink] = useState('');
|
||||||
|
|
||||||
const [images, setImages] = useState<Image[]>([]);
|
const [images, setImages] = useState<Image[]>([]);
|
||||||
|
const { characterLimit, newLineLimit } = config.twitter;
|
||||||
|
|
||||||
const reset = () => {
|
const reset = () => {
|
||||||
setShowImagePrompt(false);
|
setShowImagePrompt(false);
|
||||||
@@ -97,10 +99,16 @@ export const AddTweetModal = () => {
|
|||||||
|
|
||||||
if (!config) return null;
|
if (!config) return null;
|
||||||
|
|
||||||
|
const isValidMessage = message => {
|
||||||
|
if (message.length > characterLimit) return false;
|
||||||
|
if (getNewLineCount(message) > newLineLimit) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const submitTweet = () => {
|
const submitTweet = () => {
|
||||||
const cleanedMessage = message.trim();
|
const cleanedMessage = message.trim();
|
||||||
if (cleanedMessage.length === 0) return;
|
if (cleanedMessage.length === 0) return;
|
||||||
if (cleanedMessage.length > config.twitter.characterLimit) return;
|
if (!isValidMessage(cleanedMessage)) return;
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
message,
|
message,
|
||||||
@@ -114,11 +122,7 @@ export const AddTweetModal = () => {
|
|||||||
_handleClose();
|
_handleClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMessageChange = (e: StandardInputProps['onChange']) => {
|
const handleMessageChange = (message) => setMessage(message);
|
||||||
const message = e.target.value;
|
|
||||||
if (message.length > config.twitter.characterLimit) return;
|
|
||||||
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
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import React from 'react';
|
import React, { 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';
|
||||||
import { usePhone } from '../../../../os/phone/hooks/usePhone';
|
import { usePhone } from '../../../../os/phone/hooks/usePhone';
|
||||||
|
import { getNewLineCount } from '../../utils/message';
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
textField: {
|
textField: {
|
||||||
@@ -21,29 +22,54 @@ const useStyles = makeStyles((theme) => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
export const TweetMessage = ({ message, handleChange }) => {
|
export const TweetMessage = ({ message, handleChange }) => {
|
||||||
|
const textFieldRef = useRef(null);
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const { config } = usePhone();
|
const { config } = usePhone();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
if (!config) return null;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
let errorMessage = null;
|
||||||
|
|
||||||
const overCharacterLimit =
|
const overCharacterLimit =
|
||||||
message.trim().length > config.twitter.characterLimit;
|
message.trim().length > characterLimit;
|
||||||
const characterWarningPrompt = `${t(
|
const characterWarningPrompt = `${t(
|
||||||
'APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT'
|
'APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT'
|
||||||
)} (${config.twitter.characterLimit})`;
|
)} (${characterLimit})`;
|
||||||
|
|
||||||
|
const overNewLineLimit = getNewLineCount(message) > newLineLimit;
|
||||||
|
const newLineWarningPrompt = `${t(
|
||||||
|
'APPS_TWITTER_TWEET_MESSAGE_NEW_LINE_LIMIT'
|
||||||
|
)} (${newLineLimit})`;
|
||||||
|
|
||||||
|
if (overCharacterLimit) {
|
||||||
|
errorMessage = characterWarningPrompt;
|
||||||
|
} else if (overNewLineLimit) {
|
||||||
|
errorMessage = newLineWarningPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TextField
|
<TextField
|
||||||
value={message}
|
value={message}
|
||||||
inputProps={{ className: classes.textFieldInput }}
|
inputProps={{ className: classes.textFieldInput }}
|
||||||
className={classes.textField}
|
className={classes.textField}
|
||||||
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={(input) => input && input.focus()}
|
||||||
error={overCharacterLimit}
|
error={errorMessage !== null}
|
||||||
helperText={overCharacterLimit ? characterWarningPrompt : null}
|
helperText={errorMessage || null}
|
||||||
|
ref={textFieldRef}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
export function getNewLineCount(message: string): number {
|
||||||
|
return message.split(/\r\n|\r|\n/).length;
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ interface TwitterConfig {
|
|||||||
allowDeleteTweets: boolean;
|
allowDeleteTweets: boolean;
|
||||||
allowReportTweets: boolean;
|
allowReportTweets: boolean;
|
||||||
characterLimit: number;
|
characterLimit: number;
|
||||||
|
newLineLimit: number;
|
||||||
enableAvatars: boolean;
|
enableAvatars: boolean;
|
||||||
enableEmojis: boolean;
|
enableEmojis: boolean;
|
||||||
enableImages: boolean;
|
enableImages: boolean;
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
"APPS_TWITTER_SEARCH_TWEETS": "Search for tweets",
|
"APPS_TWITTER_SEARCH_TWEETS": "Search for tweets",
|
||||||
"APPS_TWITTER_SEARCH_TWEETS_PLACEHOLDER": "Search by profile name or message",
|
"APPS_TWITTER_SEARCH_TWEETS_PLACEHOLDER": "Search by profile name or message",
|
||||||
"APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT": "Message is over the character limit",
|
"APPS_TWITTER_TWEET_MESSAGE_CHAR_LIMIT": "Message is over the character limit",
|
||||||
|
"APPS_TWITTER_TWEET_MESSAGE_NEW_LINE_LIMIT": "Message is over the new line limit",
|
||||||
"APPS_TWITTER_CREATE_SUCCESS": "Successfully sent your tweet",
|
"APPS_TWITTER_CREATE_SUCCESS": "Successfully sent your tweet",
|
||||||
"APPS_TWITTER_CREATE_FAILED": "Failed to send your tweet",
|
"APPS_TWITTER_CREATE_FAILED": "Failed to send your tweet",
|
||||||
"APPS_TWITTER_EDIT_PROFILE_AVATAR": "Avatar URL",
|
"APPS_TWITTER_EDIT_PROFILE_AVATAR": "Avatar URL",
|
||||||
|
|||||||
Reference in new issue
Block a user