re-added ids to images to handle edge case

This commit is contained in:
erik-sn committed 2020-09-25 03:30:52 -04:00
1 parent 0789f69e2f
commit a01993e178
5 files changed
+56 -28

No files matched your search

@@ -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 }) => {
<AddTweetStatus />
<ImagePrompt
visible={showImagePrompt}
value={image}
value={link}
handleChange={handleimageChange}
/>
<EmojiSelect visible={showEmoji} onEmojiClick={handleSelectEmoji} />
@@ -15,11 +15,11 @@ export const ImageDisplay = ({ visible, images, removeImage, small }) => {
return (
<div className={classes.root}>
{images.map((link) => (
{images.map((image) => (
<Image
key={link}
link={link}
handleClick={removeImage ? () => removeImage(link) : null}
key={image.id}
link={image.link}
handleClick={removeImage ? () => removeImage(image.id) : null}
small={small}
/>
))}
+1 -1
View File
@@ -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",
@@ -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 (
<List>