mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +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
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user