mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
re-added ids to images to handle edge case
This commit is contained in:
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}
|
||||
/>
|
||||
))}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user