mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
working emoji integration and workflow
This commit is contained in:
1 parent
2fc78df59b
commit
e577b37d6b
10 files changed
+209
-85
No files matched your search
@@ -10,6 +10,8 @@
|
||||
"@testing-library/react": "^9.5.0",
|
||||
"@testing-library/user-event": "^7.2.1",
|
||||
"core-js": "^3.6.5",
|
||||
"emoji-mart": "^3.0.0",
|
||||
"emoji-picker-react": "^3.2.3",
|
||||
"husky": "^4.2.5",
|
||||
"i18next": "^19.7.0",
|
||||
"prettier": "^2.0.5",
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import React, { useState, useCallback } from "react";
|
||||
import React, { useState, useCallback, useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import Picker from "emoji-picker-react";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import { Button, Paper } from "@material-ui/core";
|
||||
import InsertPhotoIcon from "@material-ui/icons/InsertPhoto";
|
||||
import GifIcon from "@material-ui/icons/Gif";
|
||||
import CloseIcon from "@material-ui/icons/Close";
|
||||
import EmojiIcon from "@material-ui/icons/SentimentSatisfied";
|
||||
|
||||
import types from "../types";
|
||||
import "./emoji.css";
|
||||
import EmojiSelect from "./EmojiSelect";
|
||||
import MediaDisplay from "./MediaDisplay";
|
||||
import MediaPrompt from "./MediaPrompt";
|
||||
import TweetText from "./TweetText";
|
||||
@@ -73,6 +75,11 @@ const useStyles = makeStyles((theme) => ({
|
||||
displayNone: {
|
||||
display: "none",
|
||||
},
|
||||
emojiPicker: {
|
||||
background: "none",
|
||||
border: "none",
|
||||
boxShadow: "none",
|
||||
},
|
||||
}));
|
||||
|
||||
const TEMP_MEDIA = [
|
||||
@@ -108,13 +115,46 @@ const TEMP_MEDIA = [
|
||||
},
|
||||
];
|
||||
|
||||
const URL_REGEX = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
|
||||
|
||||
export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
const [showEmoji, setShowEmoji] = useState(false);
|
||||
|
||||
const [text, setText] = useState("");
|
||||
const [media, setMedia] = useState(TEMP_MEDIA);
|
||||
const [mediaType, setMediaType] = useState(null);
|
||||
const [mediaLink, setMediaLink] = useState(null);
|
||||
const classes = useStyles();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
setShowMedia(false);
|
||||
setShowEmoji(false);
|
||||
setMedia([]);
|
||||
setText("");
|
||||
}, []);
|
||||
|
||||
// when the user presses escape we should close the modal
|
||||
const _handleEscape = (e) => {
|
||||
const isEscapeKey = e.key == "Escape" || e.key == "Esc";
|
||||
if (isEscapeKey) {
|
||||
e.preventDefault();
|
||||
reset();
|
||||
handleClose();
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
const listener = window.addEventListener("keydown", _handleEscape, true);
|
||||
return () => window.removeEventListener("keydown", listener);
|
||||
}, []);
|
||||
|
||||
const _handleClose = useCallback(() => {
|
||||
reset();
|
||||
handleClose();
|
||||
});
|
||||
|
||||
const showHideClassName = visible
|
||||
? classes.displayBlock
|
||||
: classes.displayNone;
|
||||
@@ -126,47 +166,77 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
console.log(data);
|
||||
};
|
||||
|
||||
const handleTextChange = useCallback((e) => setText(e.target.value), []);
|
||||
|
||||
const addMedia = useCallback(() => {
|
||||
// we add an ID here so that we can have a distinct identifier on the
|
||||
// client before it is added to the database. This ID is NOT what used
|
||||
// or contained in the database.
|
||||
setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }]));
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
// only add the media if it's a valid URL
|
||||
// TODO - can we test that it's a valid image in JS?
|
||||
if (mediaLink.match(URL_REGEX)) {
|
||||
// we add an ID here so that we can have a distinct identifier on the
|
||||
// client before it is added to the database. This ID is NOT what used
|
||||
// or contained in the database.
|
||||
setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }]));
|
||||
}
|
||||
}, [mediaType, mediaLink]);
|
||||
|
||||
const _removeMedia = (id) => setMedia(media.filter((m) => m.id !== id));
|
||||
const removeMedia = useCallback(_removeMedia, [media]);
|
||||
|
||||
const _handleMediaChange = (e) => setMediaLink(e.target.value);
|
||||
const handleMediaChange = useCallback(_handleMediaChange, []);
|
||||
const setMediaTypeImage = useCallback(() => {
|
||||
setShowEmoji(false); // clear the emoji so we can switch between emoji/media
|
||||
setMediaType(types.IMAGE);
|
||||
});
|
||||
const closeMediaPrompt = useCallback(() => {
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
}, []);
|
||||
|
||||
console.log(media);
|
||||
const toggleShowEmoji = useCallback(() => {
|
||||
// clear the media items so we can seemlessly toggle between emoji/media
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
setShowEmoji(!showEmoji);
|
||||
});
|
||||
const _handleEmojiClick = (emojiObject, e) => {
|
||||
setText(text.concat(emojiObject.native));
|
||||
};
|
||||
const handleEmojiClick = useCallback(_handleEmojiClick, [text]);
|
||||
|
||||
const mediaPromptVisible = mediaType && !showEmoji;
|
||||
const primaryButtonText = mediaPromptVisible
|
||||
? t("APPS_TWITTER_SUBMIT_MEDIA")
|
||||
: t("APPS_TWITTER_TWEET");
|
||||
const showCloseButton = mediaType || showEmoji;
|
||||
|
||||
return (
|
||||
<div className={showHideClassName}>
|
||||
<Paper className={classes.root}>
|
||||
<Button className={classes.close} onClick={handleClose}>
|
||||
<Button className={classes.close} onClick={_handleClose}>
|
||||
<CloseIcon color="action" />
|
||||
</Button>
|
||||
<TweetText text={text} handleChange={(e) => setText(e.target.value)} />
|
||||
{mediaType && (
|
||||
<MediaPrompt
|
||||
value={mediaLink}
|
||||
handleChange={(e) => setMediaLink(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
<MediaDisplay media={media} removeMedia={removeMedia} />
|
||||
<TweetText text={text} handleChange={handleTextChange} />
|
||||
<MediaPrompt
|
||||
visible={mediaPromptVisible}
|
||||
value={mediaLink}
|
||||
handleChange={handleMediaChange}
|
||||
/>
|
||||
<EmojiSelect visible={showEmoji} onEmojiClick={handleEmojiClick} />
|
||||
<MediaDisplay
|
||||
visible={!showEmoji}
|
||||
media={media}
|
||||
removeMedia={removeMedia}
|
||||
/>
|
||||
<div className={classes.buttonsContainer}>
|
||||
<div className={classes.leftButtons}>
|
||||
<Button
|
||||
className={classes.button}
|
||||
onClick={() => setMediaType(types.IMAGE)}
|
||||
onClick={mediaType ? closeMediaPrompt : setMediaTypeImage}
|
||||
>
|
||||
<InsertPhotoIcon color="action" />
|
||||
</Button>
|
||||
<Button className={classes.button}>
|
||||
<Button className={classes.button} onClick={toggleShowEmoji}>
|
||||
<EmojiIcon color="action" />
|
||||
</Button>
|
||||
</div>
|
||||
@@ -176,16 +246,14 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
color="primary"
|
||||
onClick={mediaType ? addMedia : submitTweet}
|
||||
>
|
||||
{mediaType
|
||||
? t("APPS_TWITTER_SUBMIT_MEDIA")
|
||||
: t("APPS_TWITTER_TWEET")}
|
||||
{primaryButtonText}
|
||||
</Button>
|
||||
{mediaType && (
|
||||
{showCloseButton && (
|
||||
<Button
|
||||
className={classes.closeMedia}
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
onClick={closeMediaPrompt}
|
||||
onClick={showEmoji ? toggleShowEmoji : closeMediaPrompt}
|
||||
>
|
||||
{t("APPS_TWITTER_CLOSE_MEDIA")}
|
||||
</Button>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import React, { memo } from "react";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import { Picker } from "emoji-mart";
|
||||
|
||||
import "./emoji.css";
|
||||
import "emoji-mart/css/emoji-mart.css";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
emojiPicker: {
|
||||
background: "none",
|
||||
border: "none",
|
||||
boxShadow: "none",
|
||||
},
|
||||
}));
|
||||
|
||||
export const EmojiSelect = ({ visible, onEmojiClick }) => {
|
||||
const classes = useStyles();
|
||||
|
||||
if (!visible) return null;
|
||||
|
||||
return (
|
||||
<Picker
|
||||
className={classes.emojiPicker}
|
||||
onClick={onEmojiClick}
|
||||
set="google"
|
||||
includes={["smileys"]}
|
||||
theme="dark"
|
||||
showPreview={false}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default memo(EmojiSelect); // The picker is an expensive render
|
||||
@@ -1,52 +1,15 @@
|
||||
import React, { useCallback } from "react";
|
||||
import { IconButton } from "@material-ui/core";
|
||||
import React from "react";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import CloseIcon from "@material-ui/icons/Close";
|
||||
|
||||
import types from "../types";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
margin: "2px 15px",
|
||||
},
|
||||
mediaImage: {
|
||||
// catch the close button here so it doesn't go all the way up to the modal
|
||||
position: "relative",
|
||||
display: "inline-block",
|
||||
},
|
||||
imgButton: {
|
||||
position: "absolute", // show the close button in the top right of the image
|
||||
top: "2px",
|
||||
right: "5px",
|
||||
backgroundColor: "#000",
|
||||
opacity: 0.7,
|
||||
"&:hover": {
|
||||
// fixes a bug where when the user closes one image material-ui puts
|
||||
// the hover status on the previous image
|
||||
backgroundColor: "#000",
|
||||
},
|
||||
},
|
||||
img: {
|
||||
maxHeight: "125px", // constrain images so we don't overwhelm the UI
|
||||
maxWidth: "500px",
|
||||
padding: "0px 3px 3px 0px",
|
||||
},
|
||||
}));
|
||||
|
||||
export const MediaImage = ({ mediaLink, handleClick }) => {
|
||||
const classes = useStyles();
|
||||
return (
|
||||
<div className={classes.mediaImage}>
|
||||
<IconButton
|
||||
className={classes.imgButton}
|
||||
onClick={handleClick}
|
||||
size="small"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
<img className={classes.img} src={mediaLink} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
import MediaImage from "./MediaImage";
|
||||
|
||||
export const Media = ({ id, mediaType, mediaLink, handleClose }) => {
|
||||
switch (mediaType) {
|
||||
@@ -58,9 +21,9 @@ export const Media = ({ id, mediaType, mediaLink, handleClose }) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const MediaDisplay = ({ media, removeMedia }) => {
|
||||
export const MediaDisplay = ({ visible, media, removeMedia }) => {
|
||||
const classes = useStyles();
|
||||
if (media.length === 0) return null;
|
||||
if (!visible || media.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className={classes.root}>
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import React from "react";
|
||||
import { IconButton } from "@material-ui/core";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import CloseIcon from "@material-ui/icons/Close";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
margin: "2px 15px",
|
||||
},
|
||||
mediaImage: {
|
||||
// catch the close button here so it doesn't go all the way up to the modal
|
||||
position: "relative",
|
||||
display: "inline-block",
|
||||
},
|
||||
imgButton: {
|
||||
position: "absolute", // show the close button in the top right of the image
|
||||
top: "2px",
|
||||
right: "5px",
|
||||
backgroundColor: "#000",
|
||||
opacity: 0.7,
|
||||
"&:hover": {
|
||||
// fixes a bug where when the user closes one image material-ui puts
|
||||
// the hover status on the previous image
|
||||
backgroundColor: "#000",
|
||||
},
|
||||
},
|
||||
img: {
|
||||
maxHeight: "125px", // constrain images so we don't overwhelm the UI
|
||||
maxWidth: "500px",
|
||||
padding: "0px 3px 3px 0px",
|
||||
},
|
||||
}));
|
||||
|
||||
export const MediaImage = ({ mediaLink, handleClick }) => {
|
||||
const classes = useStyles();
|
||||
return (
|
||||
<div className={classes.mediaImage}>
|
||||
<IconButton
|
||||
className={classes.imgButton}
|
||||
onClick={handleClick}
|
||||
size="small"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
<img className={classes.img} src={mediaLink} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MediaImage;
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { useState } from "react";
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import { TextField } from "@material-ui/core";
|
||||
@@ -14,10 +14,11 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
export const MediaPrompt = ({ value, handleChange }) => {
|
||||
export const MediaPrompt = ({ visible, value, handleChange }) => {
|
||||
const { t } = useTranslation();
|
||||
const classes = useStyles();
|
||||
|
||||
if (!visible) return null;
|
||||
return (
|
||||
<TextField
|
||||
value={value}
|
||||
|
||||
@@ -34,21 +34,6 @@ export const TwitterApp = () => {
|
||||
const openModal = useCallback(() => setModalVisible(true), []);
|
||||
const hideModal = useCallback(() => setModalVisible(false), []);
|
||||
|
||||
useEffect(() => {
|
||||
const listener = window.addEventListener(
|
||||
"keydown",
|
||||
(e) => {
|
||||
const isEscapeKey = e.key == "Escape" || e.key == "Esc";
|
||||
if (isEscapeKey) {
|
||||
e.preventDefault();
|
||||
hideModal();
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
return () => window.removeEventListener("keydown", listener);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<AppWrapper>
|
||||
<AppTitle app={twitter} className={classes.root} />
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
.emoji-mart {
|
||||
margin: 0px 15px 8px 15px !important;
|
||||
width: calc(100% - 30px) !important;
|
||||
}
|
||||
|
||||
.emoji-mart-bar {
|
||||
display: none;
|
||||
}
|
||||
+14
-1
@@ -4362,6 +4362,19 @@ elliptic@^6.5.3:
|
||||
minimalistic-assert "^1.0.0"
|
||||
minimalistic-crypto-utils "^1.0.0"
|
||||
|
||||
emoji-mart@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/emoji-mart/-/emoji-mart-3.0.0.tgz#eca24a04881e27752a6921e09f65a86ce8539a50"
|
||||
integrity sha512-r5DXyzOLJttdwRYfJmPq/XL3W5tiAE/VsRnS0Hqyn27SqPA/GOYwVUSx50px/dXdJyDSnvmoPbuJ/zzhwSaU4A==
|
||||
dependencies:
|
||||
"@babel/runtime" "^7.0.0"
|
||||
prop-types "^15.6.0"
|
||||
|
||||
emoji-picker-react@^3.2.3:
|
||||
version "3.2.3"
|
||||
resolved "https://registry.yarnpkg.com/emoji-picker-react/-/emoji-picker-react-3.2.3.tgz#c8f51d0130a1171475f94847a5dca851412aad43"
|
||||
integrity sha512-Rw29mb3wPVRwgW+kUz5kVjXqvTMRax7ye9onF5qi5pdBroQzjPWdfV//wcF+fxKO3ppoaomy0OhPS5Scq3VtFA==
|
||||
|
||||
emoji-regex@^7.0.1, emoji-regex@^7.0.2:
|
||||
version "7.0.3"
|
||||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"
|
||||
@@ -9304,7 +9317,7 @@ prompts@^2.0.1:
|
||||
kleur "^3.0.3"
|
||||
sisteransi "^1.0.4"
|
||||
|
||||
prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
|
||||
prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
|
||||
version "15.7.2"
|
||||
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
|
||||
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
|
||||
|
||||
@@ -8,5 +8,6 @@ Config.PhoneAsItem = false
|
||||
|
||||
Config.twitter = {}
|
||||
Config.twitter.characterLimit = 240
|
||||
Config.twitter.enableEmojis = true
|
||||
Config.twitter.renderImages = true -- whether or not links to images are automatically rendered on the UI
|
||||
Config.twitter.maxImages = 3 -- max amount of images that can be attached to a single tweet
|
||||
Reference in new issue
Block a user