import React, { useState } from 'react';
import { Text, ScrollView, FlatList, StyleSheet, View } from 'react-native';
import Hyperlink from 'react-native-hyperlink'
import { Button, Card, Chip } from 'react-native-paper';
import API from './../API.js';
import UserName from './UserName.js';
import Media from './Media.js';
import Comment from "./Comment";
import NewComment from './NewComment.js';
import Moment from 'moment';
import { useSnapshot } from 'valtio';
import GlobalState from '../contexts/GlobalState.js';
import i18n from "../i18nMessages.js";
let Post = (props) => {
const gState = useSnapshot(GlobalState);
const viewer = gState.me;
let [showCommentsB, changeshowCommentsB] = useState(false);
let [post, changePost] = useState(props.post);
let [likes, changeLikes] = useState(Object.keys(post.reactions).length);
let [bookmarked, changeBookmarked] = useState(post.bookmarks && post.bookmarks.includes(viewer._id));
let toProfileText = post.toProfile && post.toProfile !== post.profileid ?
{">"} : undefined;
let cleanContent = post.content.replace(/@[A-z]+:.+\w/g, '');
//cleanContent = convertLinks(cleanContent);
const newComentAdded = (commentData) => {
let newPostObj = { ...post };
newPostObj.comments.push(commentData);
changePost(newPostObj);
};
const newPostReaction = () => {
let newPostObj = { ...post };
if (!newPostObj.reactions[viewer._id]) {
changeLikes(likes + 1);
newPostObj.reactions[viewer._id] = { type: "like" };
API.newPostReaction(post._id);
} else {
changeLikes(likes - 1);
delete newPostObj.reactions[viewer._id];
API.removePostReaction(viewer._id);
}
changePost(newPostObj);
}
const newPostBookmark = () => {
if (!post.bookmarks || !post.bookmarks.includes(viewer._id)) {
changeBookmarked(true);
if (!post.bookmarks) post.bookmarks = [];
post.bookmarks.push(viewer._id);
API.newPostBookmark(post._id);
} else {
changeBookmarked(false);
post.bookmarks = post.bookmarks.filter(id => id != viewer._id);
API.removePostReaction(post._id)
}
}
const renderComment = ({ item }) => (
);
return (
{!post.nonOrganicType ?
{toProfileText}
{" " + Moment(post.createdAt).fromNow()}
{cleanContent}
:
{i18n.t("message.news")}
{cleanContent}
}
{showCommentsB && }
{
showCommentsB &&
item.createdAt}
/>
}
);
}
export default React.memo(Post);
const styles = StyleSheet.create({
userName: {
fontSize: 14,
fontWeight: 'bold',
marginBottom: 5,
fontSize: 18,
},
card: {
margin: 8,
backgroundColor: "#FFFFFF"
},
comment: {
margin: 8,
marginTop: 0,
padding: 8
}
});