mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +00:00
working images in the database
This commit is contained in:
1 parent
997f550aab
commit
9e40fb8010
15 files changed
+157
-128
No files matched your search
@@ -6,12 +6,12 @@ import CloseIcon from "@material-ui/icons/Close";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import Nui from "../../../os/nui-events/utils/Nui";
|
||||
import types from "../types";
|
||||
import { withValidMedia } from "../utils/media";
|
||||
import { IMAGE_DELIMITER } from "../utils/images";
|
||||
import { withValidImage } from "../utils/images";
|
||||
import "./emoji.css";
|
||||
import EmojiSelect from "./EmojiSelect";
|
||||
import MediaDisplay from "./MediaDisplay";
|
||||
import MediaPrompt from "./MediaPrompt";
|
||||
import ImageDisplay from "./ImageDisplay";
|
||||
import ImagePrompt from "./ImagePrompt";
|
||||
import TweetText from "./TweetText";
|
||||
import ControlButtons from "./ControlButtons";
|
||||
import IconButtons from "./IconButtons";
|
||||
@@ -61,21 +61,27 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
const IMAGES = [
|
||||
"https://assets.simpleviewinc.com/simpleview/image/upload/c_limit,h_1200,q_75,w_1200/v1/clients/boston/c07044da_9483_4fd5_8e37_5b75513dcc80_6b058b6f-7bdc-4f11-b217-4561c07901f3.jpg",
|
||||
"https://www.hiusa.org/wp-content/uploads/2020/04/Aerial-View-Boston-Osman-Rana-Unsplash-1000x550-compressor-778x446.jpg",
|
||||
"https://www.mccarter.com/wp-content/uploads/2019/10/Location-Boston.png",
|
||||
];
|
||||
|
||||
export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
const [showEmoji, setShowEmoji] = useState(false);
|
||||
const [showImagePrompt, setShowImagePrompt] = useState(false);
|
||||
const [image, setimage] = useState("");
|
||||
|
||||
const [text, setText] = useState("");
|
||||
const [media, setMedia] = useState([]);
|
||||
const [mediaType, setMediaType] = useState(null);
|
||||
const [mediaLink, setMediaLink] = useState(null);
|
||||
const [images, setImages] = useState(IMAGES);
|
||||
const classes = useStyles();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
setShowImagePrompt(null);
|
||||
setShowEmoji(false);
|
||||
setMedia([]);
|
||||
|
||||
setimage("");
|
||||
setImages([]);
|
||||
setText("");
|
||||
}, []);
|
||||
|
||||
@@ -103,41 +109,37 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
: classes.displayNone;
|
||||
|
||||
const submitTweet = () => {
|
||||
const data = { message: text, media, realUser: "testUser" };
|
||||
const data = {
|
||||
message: text,
|
||||
images: images.join(IMAGE_DELIMITER),
|
||||
realUser: "testUser",
|
||||
};
|
||||
Nui.send("phone:createTweet", data);
|
||||
};
|
||||
|
||||
const handleTextChange = (e) => setText(e.target.value);
|
||||
|
||||
const addMedia = () => {
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
const addImage = () => {
|
||||
withValidImage(image, () => setImages([...images, image]));
|
||||
|
||||
withValidMedia(mediaLink, () =>
|
||||
setMedia(media.concat([{ id: uuidv4(), mediaType, mediaLink }]))
|
||||
);
|
||||
setShowImagePrompt(false);
|
||||
setimage("");
|
||||
};
|
||||
const removeMedia = (id) => setMedia(media.filter((m) => m.id !== id));
|
||||
const handleMediaChange = (e) => setMediaLink(e.target.value);
|
||||
const setMediaTypeImage = () => {
|
||||
setShowEmoji(false); // clear the emoji so we can switch between emoji/media
|
||||
setMediaType(types.IMAGE);
|
||||
const removeImage = (image) => setImages(images.filter((i) => i !== image));
|
||||
const handleimageChange = (e) => setimage(e.target.value);
|
||||
const toggleShowImagePrompt = () => {
|
||||
setShowEmoji(false); // clear the emoji so we can switch between emoji/images
|
||||
setShowImagePrompt(!showImagePrompt);
|
||||
};
|
||||
const closeMediaPrompt = () => {
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
};
|
||||
|
||||
const toggleShowEmoji = () => {
|
||||
// clear the media items so we can seemlessly toggle between emoji/media
|
||||
setMediaType(null);
|
||||
setMediaLink(null);
|
||||
// clear the images so we can seemlessly toggle between emoji/images
|
||||
setShowImagePrompt(false);
|
||||
setImages("");
|
||||
setShowEmoji(!showEmoji);
|
||||
};
|
||||
const handleEmojiClick = (emojiObject, e) => {
|
||||
setText(text.concat(emojiObject.native));
|
||||
};
|
||||
const mediaPromptVisible = mediaType && !showEmoji;
|
||||
|
||||
return (
|
||||
<div className={showHideClassName}>
|
||||
@@ -147,27 +149,27 @@ export const AddTweetModal = ({ visible, handleClose }) => {
|
||||
</Button>
|
||||
<TweetText text={text} handleChange={handleTextChange} />
|
||||
<AddTweetStatus />
|
||||
<MediaPrompt
|
||||
visible={mediaPromptVisible}
|
||||
value={mediaLink}
|
||||
handleChange={handleMediaChange}
|
||||
<ImagePrompt
|
||||
visible={showImagePrompt}
|
||||
value={image}
|
||||
handleChange={handleimageChange}
|
||||
/>
|
||||
<EmojiSelect visible={showEmoji} onEmojiClick={handleEmojiClick} />
|
||||
<MediaDisplay
|
||||
visible={!showEmoji}
|
||||
media={media}
|
||||
removeMedia={removeMedia}
|
||||
<ImageDisplay
|
||||
visible={!showEmoji && images.length > 0}
|
||||
images={images}
|
||||
removeImage={removeImage}
|
||||
/>
|
||||
<div className={classes.buttonsContainer}>
|
||||
<IconButtons
|
||||
onMediaClick={mediaType ? closeMediaPrompt : setMediaTypeImage}
|
||||
onImageClick={toggleShowImagePrompt}
|
||||
onEmojiClick={toggleShowEmoji}
|
||||
/>
|
||||
<ControlButtons
|
||||
mediaType={mediaType}
|
||||
showImagePrompt={showImagePrompt}
|
||||
showEmoji={showEmoji}
|
||||
onPrimaryClick={mediaType ? addMedia : submitTweet}
|
||||
onCloseClick={showEmoji ? toggleShowEmoji : closeMediaPrompt}
|
||||
onPrimaryClick={showImagePrompt ? addImage : submitTweet}
|
||||
onCloseClick={showEmoji ? toggleShowEmoji : toggleShowImagePrompt}
|
||||
/>
|
||||
</div>
|
||||
</Paper>
|
||||
|
||||
@@ -13,13 +13,13 @@ const useStyles = makeStyles((theme) => ({
|
||||
alignItems: "justify-content",
|
||||
marginRight: "15px",
|
||||
},
|
||||
closeMedia: {
|
||||
close: {
|
||||
marginLeft: "8px",
|
||||
},
|
||||
}));
|
||||
|
||||
export const ControlButtons = ({
|
||||
mediaType,
|
||||
showImagePrompt,
|
||||
showEmoji,
|
||||
onCloseClick,
|
||||
onPrimaryClick,
|
||||
@@ -29,11 +29,11 @@ export const ControlButtons = ({
|
||||
const classes = useStyles();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const mediaPromptVisible = mediaType && !showEmoji;
|
||||
const primaryButtonText = mediaPromptVisible
|
||||
? t("APPS_TWITTER_SUBMIT_MEDIA")
|
||||
const imagePromptVisible = showImagePrompt && !showEmoji;
|
||||
const primaryButtonText = imagePromptVisible
|
||||
? t("APPS_TWITTER_SUBMIT_IMAGE")
|
||||
: t("APPS_TWITTER_TWEET");
|
||||
const showCloseButton = mediaType || showEmoji;
|
||||
const showCloseButton = showImagePrompt || showEmoji;
|
||||
|
||||
if (isLoading) return <h1>!!!</h1>;
|
||||
|
||||
@@ -44,12 +44,12 @@ export const ControlButtons = ({
|
||||
</Button>
|
||||
{showCloseButton && (
|
||||
<Button
|
||||
className={classes.closeMedia}
|
||||
className={classes.close}
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
onClick={onCloseClick}
|
||||
>
|
||||
{t("APPS_TWITTER_CLOSE_MEDIA")}
|
||||
{t("APPS_TWITTER_CLOSE_IMAGE")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -17,11 +17,11 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
export const IconButtons = ({ onMediaClick, onEmojiClick }) => {
|
||||
export const IconButtons = ({ onImageClick, onEmojiClick }) => {
|
||||
const classes = useStyles();
|
||||
return (
|
||||
<div className={classes.buttons}>
|
||||
<Button className={classes.button} onClick={onMediaClick}>
|
||||
<Button className={classes.button} onClick={onImageClick}>
|
||||
<InsertPhotoIcon color="action" />
|
||||
</Button>
|
||||
<Button className={classes.button} onClick={onEmojiClick}>
|
||||
|
||||
+19
-12
@@ -7,7 +7,7 @@ const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
margin: "2px 15px",
|
||||
},
|
||||
mediaImage: {
|
||||
imageContainer: {
|
||||
// catch the close button here so it doesn't go all the way up to the modal
|
||||
position: "relative",
|
||||
display: "inline-block",
|
||||
@@ -29,22 +29,29 @@ const useStyles = makeStyles((theme) => ({
|
||||
maxWidth: "300px",
|
||||
padding: "0px 3px 3px 0px",
|
||||
},
|
||||
imgSmall: {
|
||||
maxHeight: "75px", // constrain images so we don't overwhelm the UI
|
||||
maxWidth: "125px",
|
||||
padding: "0px 1px 1px 0px",
|
||||
},
|
||||
}));
|
||||
|
||||
export const MediaImage = ({ mediaLink, handleClick }) => {
|
||||
export const Image = ({ link, handleClick, small }) => {
|
||||
const classes = useStyles();
|
||||
return (
|
||||
<div className={classes.mediaImage}>
|
||||
<IconButton
|
||||
className={classes.imgButton}
|
||||
onClick={handleClick}
|
||||
size="small"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
<img className={classes.img} src={mediaLink} />
|
||||
<div className={classes.imageContainer}>
|
||||
{handleClick && (
|
||||
<IconButton
|
||||
className={classes.imgButton}
|
||||
onClick={handleClick}
|
||||
size="small"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
)}
|
||||
<img className={small ? classes.imgSmall : classes.img} src={link} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MediaImage;
|
||||
export default Image;
|
||||
@@ -0,0 +1,35 @@
|
||||
import React from "react";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
|
||||
import Image from "./Image";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
margin: "2px 15px",
|
||||
},
|
||||
}));
|
||||
|
||||
export const ImageDisplay = ({ visible, images, removeImage, small }) => {
|
||||
const classes = useStyles();
|
||||
if (!visible || !images || images.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className={classes.root}>
|
||||
{images.map((link) => (
|
||||
<Image
|
||||
key={link}
|
||||
link={link}
|
||||
handleClick={removeImage ? () => removeImage(link) : null}
|
||||
small={small}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
ImageDisplay.defaultProps = {
|
||||
removeImage: null,
|
||||
small: false,
|
||||
};
|
||||
|
||||
export default ImageDisplay;
|
||||
+3
-3
@@ -14,7 +14,7 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
export const MediaPrompt = ({ visible, value, handleChange }) => {
|
||||
export const ImagePrompt = ({ visible, value, handleChange }) => {
|
||||
const { t } = useTranslation();
|
||||
const classes = useStyles();
|
||||
|
||||
@@ -27,9 +27,9 @@ export const MediaPrompt = ({ visible, value, handleChange }) => {
|
||||
onChange={handleChange}
|
||||
multiline
|
||||
size="small"
|
||||
placeholder={t("APPS_TWITTER_MEDIA_PLACEHOLDER")}
|
||||
placeholder={t("APPS_TWITTER_IMAGE_PLACEHOLDER")}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default MediaPrompt;
|
||||
export default ImagePrompt;
|
||||
@@ -1,41 +0,0 @@
|
||||
import React from "react";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
|
||||
import types from "../types";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
margin: "2px 15px",
|
||||
},
|
||||
}));
|
||||
|
||||
import MediaImage from "./MediaImage";
|
||||
|
||||
export const Media = ({ id, mediaType, mediaLink, handleClose }) => {
|
||||
switch (mediaType) {
|
||||
case types.IMAGE:
|
||||
case types.GIF:
|
||||
return <MediaImage mediaLink={mediaLink} handleClick={handleClose} />;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const MediaDisplay = ({ visible, media, removeMedia }) => {
|
||||
const classes = useStyles();
|
||||
if (!visible || media.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div className={classes.root}>
|
||||
{media.map((mediaItem) => (
|
||||
<Media
|
||||
key={mediaItem.id}
|
||||
handleClose={() => removeMedia(mediaItem.id)}
|
||||
{...mediaItem}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MediaDisplay;
|
||||
@@ -7,6 +7,7 @@ import ReplyIcon from "@material-ui/icons/Reply";
|
||||
import QuoteIcon from "@material-ui/icons/FormatQuote";
|
||||
|
||||
import { ListItem } from "../../../ui/components/ListItem";
|
||||
import ImageDisplay from "./ImageDisplay";
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
root: {
|
||||
@@ -26,11 +27,14 @@ const useStyles = makeStyles((theme) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
export const Tweet = ({ message, media }) => {
|
||||
export const Tweet = (tweet) => {
|
||||
const { message, images } = tweet;
|
||||
const classes = useStyles();
|
||||
|
||||
return (
|
||||
<ListItem className={classes.root} divider>
|
||||
<ListItemText className={classes.text} primary={message} />
|
||||
<ImageDisplay visible images={images} small />
|
||||
<div className={classes.buttonContainer}>
|
||||
<Button>
|
||||
<FavoriteBorderIcon />
|
||||
|
||||
@@ -5,7 +5,7 @@ import { List } from "../../../ui/components/List";
|
||||
import { useTweets } from "../hooks/useTweets";
|
||||
import Tweet from "./Tweet";
|
||||
|
||||
const TWEETS_REFRESH_RATE = 5000;
|
||||
const TWEETS_REFRESH_RATE = 5000; // TODO move this to twitter config
|
||||
|
||||
export function TweetList() {
|
||||
const { tweets } = useTweets();
|
||||
@@ -17,6 +17,7 @@ export function TweetList() {
|
||||
}, TWEETS_REFRESH_RATE);
|
||||
return () => window.clearTimeout(timeout);
|
||||
}, []);
|
||||
console.log(tweets);
|
||||
|
||||
return (
|
||||
<List>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useNuiEvent } from "../../../os/nui-events/hooks/useNuiEvent";
|
||||
import { useSetRecoilState } from "recoil";
|
||||
|
||||
import { IMAGE_DELIMITER } from "../utils/images";
|
||||
import types from "../types";
|
||||
import { twitterState } from "./state";
|
||||
import { useTweets } from "./useTweets";
|
||||
@@ -15,7 +16,20 @@ export const useTwitterService = () => {
|
||||
setCreateLoading(false); // on any result we should set loading to false
|
||||
};
|
||||
|
||||
useNuiEvent(types.APP_TWITTER, "fetchTweets", setTweets);
|
||||
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) : [],
|
||||
};
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
useNuiEvent(types.APP_TWITTER, "fetchTweets", _setTweets);
|
||||
useNuiEvent(types.APP_TWITTER, "createTweetLoading", setCreateLoading);
|
||||
useNuiEvent(types.APP_TWITTER, "createTweetResult", _setCreateSuccess);
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// this is a hack. We are saving images in the database as
|
||||
// delimiter separated URLS, but most common delimiters (comma, pipe
|
||||
// hash, etc.) are often found in URLS (even though it's not technically
|
||||
// within specification). To get around this we have our own delimiter
|
||||
// here we'll use to join and split these strings
|
||||
export const IMAGE_DELIMITER = "||!||";
|
||||
|
||||
export function withValidImage(link, onSuccess) {
|
||||
// only add the media if it's a valid URL
|
||||
const image = new Image();
|
||||
image.onload = onSuccess;
|
||||
image.onerror = () => {
|
||||
console.warn(`Invalid image submitted: ${link}`);
|
||||
};
|
||||
image.src = link;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
export function withValidMedia(link, onSuccess) {
|
||||
// only add the media if it's a valid URL
|
||||
const image = new Image();
|
||||
image.onload = onSuccess;
|
||||
image.onerror = () => {
|
||||
console.warn(`Invalid image submitted: ${link}`);
|
||||
};
|
||||
image.src = link;
|
||||
}
|
||||
@@ -14,9 +14,9 @@
|
||||
"APPS_SETTINGS_OPTIONS_FRAME": "Frame",
|
||||
"APPS_TWITTER": "Twitter",
|
||||
"APPS_TWITTER_PLACEHOLDER": "What's happening?",
|
||||
"APPS_TWITTER_MEDIA_PLACEHOLDER": "A link to your image or gif",
|
||||
"APPS_TWITTER_SUBMIT_MEDIA": "Add Media",
|
||||
"APPS_TWITTER_CLOSE_MEDIA": "Close",
|
||||
"APPS_TWITTER_IMAGE_PLACEHOLDER": "A link to your image or gif",
|
||||
"APPS_TWITTER_SUBMIT_IMAGE": "Add Media",
|
||||
"APPS_TWITTER_CLOSE_IMAGE": "Close",
|
||||
"APPS_TWITTER_TWEET": "Tweet",
|
||||
"APPS_TWITTER_CLOSE": "Close",
|
||||
"APPS_TWITTER_CREATE_FAILED": "Failed to send your tweet"
|
||||
|
||||
@@ -30,7 +30,6 @@ end)
|
||||
|
||||
|
||||
RegisterNUICallback('phone:fetchTweets', function()
|
||||
print('RegisterNUICallback: phone:fetchTweets')
|
||||
TriggerEvent('phone:fetchTweets')
|
||||
end)
|
||||
|
||||
|
||||
@@ -21,11 +21,12 @@ ESX.RegisterServerCallback('phone:createTweet', function(source, cb, data)
|
||||
local xPlayer = ESX.GetPlayerFromId(_source)
|
||||
local _identifier = xPlayer.getIdentifier()
|
||||
|
||||
MySQL.Async.execute('INSERT INTO npwd_twitter_tweets (`identifier`, `realUser`, `message`) VALUES (@identifier, @realUser, @message)',
|
||||
MySQL.Async.execute('INSERT INTO npwd_twitter_tweets (`identifier`, `realUser`, `message`, `images`) VALUES (@identifier, @realUser, @message, @images)',
|
||||
{
|
||||
['identifier'] = _identifier,
|
||||
['realUser'] = data.realUser,
|
||||
['message'] = data.message
|
||||
['message'] = data.message,
|
||||
['images'] = data.images
|
||||
}, function(result)
|
||||
cb(result)
|
||||
end)
|
||||
|
||||
Reference in new issue
Block a user