import React, { useState, useEffect } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { v4 as uuidv4 } from 'uuid'; import Nui from '../../../os/nui-events/utils/Nui'; import Modal from '../../../ui/components/Modal'; import { IMAGE_DELIMITER } from '../utils/images'; import { withValidImage } from '../utils/images'; import { useModal } from '../hooks/useModal'; import EmojiSelect from './EmojiSelect'; import ImageDisplay from './images/ImageDisplay'; import ImagePrompt from './images/ImagePrompt'; import TweetMessage from './tweet/TweetMessage'; import ControlButtons from './buttons/ControlButtons'; import IconButtons from './buttons/IconButtons'; import { usePhone } from '../../../os/phone/hooks/usePhone'; import { getNewLineCount } from '../utils/message'; const useStyles = makeStyles((theme) => ({ root: { zIndex: 10, background: '#424242', marginTop: '15px', width: '90%', display: 'flex', flexFlow: 'column nowrap', position: 'absolute', top: '80px', }, button: { margin: 5, marginTop: 20, }, buttonsContainer: { paddingBottom: '8px', display: 'flex', flexFlow: 'row nowrap', justifyContent: 'space-between', flex: '1 0 45px', }, emojiPicker: { background: 'none', border: 'none', boxShadow: 'none', }, })); interface Image { id: string; link: string; } export const AddTweetModal = () => { const classes = useStyles(); const { message, setMessage, modalVisible, setModalVisible } = useModal(); const { config } = usePhone(); const [showEmoji, setShowEmoji] = useState(false); const [showImagePrompt, setShowImagePrompt] = useState(false); const [link, setLink] = useState(''); const [images, setImages] = useState([]); const { characterLimit, newLineLimit } = config.twitter; const reset = () => { setShowImagePrompt(false); setShowEmoji(false); setLink(''); setImages([]); setMessage(''); }; const _handleClose = () => { reset(); setModalVisible(false); }; // 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(); _handleClose(); } }; useEffect(() => { window.addEventListener('keydown', _handleEscape, true); return () => window.removeEventListener('keydown', _handleEscape); }); if (!config) return null; const isValidMessage = message => { if (message.length > characterLimit) return false; if (getNewLineCount(message) > newLineLimit) return false; return true; } const submitTweet = () => { const cleanedMessage = message.trim(); if (cleanedMessage.length === 0) return; if (!isValidMessage(cleanedMessage)) return; const data = { message, images: images && images.length > 0 ? images.map((image) => image.link).join(IMAGE_DELIMITER) : '', realUser: 'testUser', }; Nui.send('phone:createTweet', data); _handleClose(); }; const handleMessageChange = (message) => setMessage(message); const addImage = () => { // strip any whitespace from the link in case the user // added some spaces/returns accidentally const cleanedLink = link.replace('/ /g', ''); // because we're only storing strings in the database we need // to give this an arbirtrary (but unique) id so that we can // correctly filter an array of images when the user wants to // delete them. This handles an edge case where the user adds // two of the same image. const image = { id: uuidv4(), link: cleanedLink }; // it's worth noting that we only perform this validation on // the client of the user who is submitting the image. When // other users see this image on their TweetList it will be // from the database and should already have passed through // this logic withValidImage(cleanedLink, () => setImages([...images, image])); setShowImagePrompt(false); setLink(''); }; 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 setShowImagePrompt(!showImagePrompt); }; const toggleShowEmoji = () => { // clear the images so we can seemlessly toggle between emoji/images setShowImagePrompt(false); setShowEmoji(!showEmoji); }; // this is when a user clicks on an emoji icon itself (i.e. a smiley face) // not the emoji icon on the UI const handleSelectEmoji = (emojiObject, e) => { setMessage(message.concat(emojiObject.native)); }; if (!config) return null; return ( 0} images={images} removeImage={removeImage} />
); }; export default AddTweetModal;