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 React, { useState, useEffect } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { makeStyles } from "@material-ui/core/styles";
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
import { Button, Paper } from "@material-ui/core";
|
import { Button, Paper } from "@material-ui/core";
|
||||||
import CloseIcon from "@material-ui/icons/Close";
|
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 Nui from "../../../os/nui-events/utils/Nui";
|
||||||
import { IMAGE_DELIMITER } from "../utils/images";
|
import { IMAGE_DELIMITER } from "../utils/images";
|
||||||
import { withValidImage } from "../utils/images";
|
import { withValidImage } from "../utils/images";
|
||||||
import "./emoji.css";
|
|
||||||
import EmojiSelect from "./EmojiSelect";
|
import EmojiSelect from "./EmojiSelect";
|
||||||
import ImageDisplay from "./ImageDisplay";
|
import ImageDisplay from "./ImageDisplay";
|
||||||
import ImagePrompt from "./ImagePrompt";
|
import ImagePrompt from "./ImagePrompt";
|
||||||
@@ -65,7 +63,7 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
|||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const [showEmoji, setShowEmoji] = useState(false);
|
const [showEmoji, setShowEmoji] = useState(false);
|
||||||
const [showImagePrompt, setShowImagePrompt] = useState(false);
|
const [showImagePrompt, setShowImagePrompt] = useState(false);
|
||||||
const [image, setimage] = useState("");
|
const [link, setLink] = useState("");
|
||||||
|
|
||||||
const [message, setMessage] = useState("");
|
const [message, setMessage] = useState("");
|
||||||
const [images, setImages] = useState([]);
|
const [images, setImages] = useState([]);
|
||||||
@@ -74,7 +72,7 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
|||||||
setShowImagePrompt(null);
|
setShowImagePrompt(null);
|
||||||
setShowEmoji(false);
|
setShowEmoji(false);
|
||||||
|
|
||||||
setimage("");
|
setLink("");
|
||||||
setImages([]);
|
setImages([]);
|
||||||
setMessage("");
|
setMessage("");
|
||||||
};
|
};
|
||||||
@@ -104,7 +102,10 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
|||||||
const submitTweet = () => {
|
const submitTweet = () => {
|
||||||
const data = {
|
const data = {
|
||||||
message,
|
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",
|
realUser: "testUser",
|
||||||
};
|
};
|
||||||
Nui.send("phone:createTweet", data);
|
Nui.send("phone:createTweet", data);
|
||||||
@@ -114,18 +115,29 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
|||||||
const handleMessageChange = (e) => setMessage(e.target.value);
|
const handleMessageChange = (e) => setMessage(e.target.value);
|
||||||
|
|
||||||
const addImage = () => {
|
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
|
// it's worth noting that we only perform this validation on
|
||||||
// the client of the user who is submitting the image. When
|
// the client of the user who is submitting the image. When
|
||||||
// other users see this image on their TweetList it will be
|
// other users see this image on their TweetList it will be
|
||||||
// from the database and should already have passed through
|
// from the database and should already have passed through
|
||||||
// this logic
|
// this logic
|
||||||
withValidImage(image, () => setImages([...images, image]));
|
withValidImage(cleanedLink, () => setImages([...images, image]));
|
||||||
|
|
||||||
setShowImagePrompt(false);
|
setShowImagePrompt(false);
|
||||||
setimage("");
|
setLink("");
|
||||||
};
|
};
|
||||||
const removeImage = (image) => setImages(images.filter((i) => i !== image));
|
const removeImage = (id) =>
|
||||||
const handleimageChange = (e) => setimage(e.target.value);
|
setImages(images.filter((image) => id !== image.id));
|
||||||
|
const handleimageChange = (e) => setLink(e.target.value);
|
||||||
|
|
||||||
const toggleShowImagePrompt = () => {
|
const toggleShowImagePrompt = () => {
|
||||||
setShowEmoji(false); // clear the emoji so we can switch between emoji/images
|
setShowEmoji(false); // clear the emoji so we can switch between emoji/images
|
||||||
setShowImagePrompt(!showImagePrompt);
|
setShowImagePrompt(!showImagePrompt);
|
||||||
@@ -153,7 +165,7 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
|||||||
<AddTweetStatus />
|
<AddTweetStatus />
|
||||||
<ImagePrompt
|
<ImagePrompt
|
||||||
visible={showImagePrompt}
|
visible={showImagePrompt}
|
||||||
value={image}
|
value={link}
|
||||||
handleChange={handleimageChange}
|
handleChange={handleimageChange}
|
||||||
/>
|
/>
|
||||||
<EmojiSelect visible={showEmoji} onEmojiClick={handleSelectEmoji} />
|
<EmojiSelect visible={showEmoji} onEmojiClick={handleSelectEmoji} />
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ export const ImageDisplay = ({ visible, images, removeImage, small }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
{images.map((link) => (
|
{images.map((image) => (
|
||||||
<Image
|
<Image
|
||||||
key={link}
|
key={image.id}
|
||||||
link={link}
|
link={image.link}
|
||||||
handleClick={removeImage ? () => removeImage(link) : null}
|
handleClick={removeImage ? () => removeImage(image.id) : null}
|
||||||
small={small}
|
small={small}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import QuoteIcon from "@material-ui/icons/FormatQuote";
|
|||||||
import { ListItem } from "../../../ui/components/ListItem";
|
import { ListItem } from "../../../ui/components/ListItem";
|
||||||
import ImageDisplay from "./ImageDisplay";
|
import ImageDisplay from "./ImageDisplay";
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles(() => ({
|
||||||
root: {
|
root: {
|
||||||
overflowX: "hidden",
|
overflowX: "hidden",
|
||||||
display: "flex",
|
display: "flex",
|
||||||
|
|||||||
@@ -9,15 +9,23 @@ const TWEETS_REFRESH_RATE = 5000; // TODO move this to twitter config
|
|||||||
|
|
||||||
export function TweetList() {
|
export function TweetList() {
|
||||||
const { tweets } = useTweets();
|
const { tweets } = useTweets();
|
||||||
|
console.log(tweets);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Nui.send("phone:fetchTweets", {});
|
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(() => {
|
const timeout = window.setTimeout(() => {
|
||||||
Nui.send("phone:fetchTweets", {});
|
Nui.send("phone:fetchTweets", {});
|
||||||
}, TWEETS_REFRESH_RATE);
|
}, TWEETS_REFRESH_RATE);
|
||||||
return () => window.clearTimeout(timeout);
|
return () => window.clearTimeout(timeout);
|
||||||
}, []);
|
}, []);
|
||||||
console.log(tweets);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<List>
|
<List>
|
||||||
|
|||||||
@@ -1,11 +1,28 @@
|
|||||||
import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent";
|
|
||||||
import { useSetRecoilState } from "recoil";
|
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 { IMAGE_DELIMITER } from "../utils/images";
|
||||||
import types from "../types";
|
import types from "../types";
|
||||||
import { twitterState } from "./state";
|
import { twitterState } from "./state";
|
||||||
import { useTweets } from "./useTweets";
|
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 = () => {
|
export const useTwitterService = () => {
|
||||||
const setTweets = useSetRecoilState(twitterState.tweets);
|
const setTweets = useSetRecoilState(twitterState.tweets);
|
||||||
const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading);
|
const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading);
|
||||||
@@ -17,16 +34,7 @@ export const useTwitterService = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const _setTweets = (tweets) => {
|
const _setTweets = (tweets) => {
|
||||||
setTweets(
|
setTweets(tweets.map(processTweet));
|
||||||
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) : [],
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useNuiEvent(types.APP_TWITTER, "fetchTweets", _setTweets);
|
useNuiEvent(types.APP_TWITTER, "fetchTweets", _setTweets);
|
||||||
|
|||||||
Reference in new issue
Block a user