diff --git a/phone/src/apps/twitter/components/AddTweetModal.js b/phone/src/apps/twitter/components/AddTweetModal.js index a40204c..373050c 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.js +++ b/phone/src/apps/twitter/components/AddTweetModal.js @@ -1,5 +1,4 @@ -import React, { useState, useCallback, useEffect } from "react"; -import { useTranslation } from "react-i18next"; +import React, { useState, useEffect } from "react"; import { makeStyles } from "@material-ui/core/styles"; import { Button, Paper } from "@material-ui/core"; import CloseIcon from "@material-ui/icons/Close"; @@ -8,7 +7,6 @@ import { v4 as uuidv4 } from "uuid"; import Nui from "../../../os/nui-events/utils/Nui"; import { IMAGE_DELIMITER } from "../utils/images"; import { withValidImage } from "../utils/images"; -import "./emoji.css"; import EmojiSelect from "./EmojiSelect"; import ImageDisplay from "./ImageDisplay"; import ImagePrompt from "./ImagePrompt"; @@ -65,7 +63,7 @@ export const AddTweetModal = ({ visible, handleClose }) => { const classes = useStyles(); const [showEmoji, setShowEmoji] = useState(false); const [showImagePrompt, setShowImagePrompt] = useState(false); - const [image, setimage] = useState(""); + const [link, setLink] = useState(""); const [message, setMessage] = useState(""); const [images, setImages] = useState([]); @@ -74,7 +72,7 @@ export const AddTweetModal = ({ visible, handleClose }) => { setShowImagePrompt(null); setShowEmoji(false); - setimage(""); + setLink(""); setImages([]); setMessage(""); }; @@ -104,7 +102,10 @@ export const AddTweetModal = ({ visible, handleClose }) => { const submitTweet = () => { const data = { message, - images: images && images.length > 0 ? images.join(IMAGE_DELIMITER) : "", + images: + images && images.length > 0 + ? images.map((image) => image.link).join(IMAGE_DELIMITER) + : "", realUser: "testUser", }; Nui.send("phone:createTweet", data); @@ -114,18 +115,29 @@ export const AddTweetModal = ({ visible, handleClose }) => { const handleMessageChange = (e) => setMessage(e.target.value); 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(image, () => setImages([...images, image])); + withValidImage(cleanedLink, () => setImages([...images, image])); setShowImagePrompt(false); - setimage(""); + setLink(""); }; - const removeImage = (image) => setImages(images.filter((i) => i !== image)); - const handleimageChange = (e) => setimage(e.target.value); + 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); @@ -153,7 +165,7 @@ export const AddTweetModal = ({ visible, handleClose }) => { diff --git a/phone/src/apps/twitter/components/ImageDisplay.js b/phone/src/apps/twitter/components/ImageDisplay.js index d8ff9a5..966548c 100644 --- a/phone/src/apps/twitter/components/ImageDisplay.js +++ b/phone/src/apps/twitter/components/ImageDisplay.js @@ -15,11 +15,11 @@ export const ImageDisplay = ({ visible, images, removeImage, small }) => { return (
- {images.map((link) => ( + {images.map((image) => ( removeImage(link) : null} + key={image.id} + link={image.link} + handleClick={removeImage ? () => removeImage(image.id) : null} small={small} /> ))} diff --git a/phone/src/apps/twitter/components/Tweet.js b/phone/src/apps/twitter/components/Tweet.js index 32c8548..550414f 100644 --- a/phone/src/apps/twitter/components/Tweet.js +++ b/phone/src/apps/twitter/components/Tweet.js @@ -9,7 +9,7 @@ import QuoteIcon from "@material-ui/icons/FormatQuote"; import { ListItem } from "../../../ui/components/ListItem"; import ImageDisplay from "./ImageDisplay"; -const useStyles = makeStyles((theme) => ({ +const useStyles = makeStyles(() => ({ root: { overflowX: "hidden", display: "flex", diff --git a/phone/src/apps/twitter/components/TweetList.js b/phone/src/apps/twitter/components/TweetList.js index 87b025e..4e290a9 100644 --- a/phone/src/apps/twitter/components/TweetList.js +++ b/phone/src/apps/twitter/components/TweetList.js @@ -9,15 +9,23 @@ const TWEETS_REFRESH_RATE = 5000; // TODO move this to twitter config export function TweetList() { const { tweets } = useTweets(); + console.log(tweets); useEffect(() => { Nui.send("phone:fetchTweets", {}); + + // this is a polling implementation. It is possible that + // there is some interaction where, on a new tweet, all + // clients are sent the updated query data. Until that can + // be accomplished this is naive but robust. + // + // TODO don't call fetchTweets - implement a function that only + // returns tweets that we don't already have const timeout = window.setTimeout(() => { Nui.send("phone:fetchTweets", {}); }, TWEETS_REFRESH_RATE); return () => window.clearTimeout(timeout); }, []); - console.log(tweets); return ( diff --git a/phone/src/apps/twitter/hooks/useTwitterService.js b/phone/src/apps/twitter/hooks/useTwitterService.js index f1137b9..46c94e2 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.js +++ b/phone/src/apps/twitter/hooks/useTwitterService.js @@ -1,11 +1,28 @@ -import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent"; import { useSetRecoilState } from "recoil"; +import { v4 as uuidv4 } from "uuid"; +import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent"; import { IMAGE_DELIMITER } from "../utils/images"; import types from "../types"; import { twitterState } from "./state"; import { useTweets } from "./useTweets"; +/** + * Perform all necessary processing/transforms from the raw database + * data to what the frontend expects + * @param {object} tweet - full JSON returned from the server/database + */ +function processTweet(tweet) { + // we store images in the database as a varchar field with + // comma separated links to images, so here we split them + // back into their distinct members + console.log(tweet); + const imageLinks = tweet.images ? tweet.images.split(IMAGE_DELIMITER) : []; + console.log(imageLinks); + const images = imageLinks.map((link) => ({ id: uuidv4(), link })); + return { ...tweet, images }; +} + export const useTwitterService = () => { const setTweets = useSetRecoilState(twitterState.tweets); const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading); @@ -17,16 +34,7 @@ export const useTwitterService = () => { }; const _setTweets = (tweets) => { - setTweets( - tweets.map((tweet) => { - return { - ...tweet, - // we store images in the database as a varchar field with - // comma separated links to images - images: tweet.images ? tweet.images.split(IMAGE_DELIMITER) : [], - }; - }) - ); + setTweets(tweets.map(processTweet)); }; useNuiEvent(types.APP_TWITTER, "fetchTweets", _setTweets);