import {
View,
Text,
TouchableOpacity,
Modal,
TouchableWithoutFeedback,
Platform,
Image as NativeImage,
} from "react-native";
import React, { useEffect, useState, useCallback } from "react";
import { useTailwind } from "tailwind-rn";
import { Image } from "expo-image";
import VideoModal from "../VideoModal";
import { useNavigation } from "@react-navigation/native";
import { follow, unfollow, block } from "../../utils/relation";
import baseRequest from "../../utils/baseRequest";
import { generateSignature } from "../../utils/crypto";
import Toast from "react-native-toast-message";
import { get } from "../../utils/storeInfo";
import { useImageViewer } from "../../context/ImageViewProvider";
export default function Post({ data }) {
const tailwind = useTailwind();
const navigation = useNavigation();
const [like, setLike] = useState(data?.is_thumbed_up === 1 ? true : false);
const [isFollowed, setIsFollowed] = useState(
data?.is_followed === 1 ? true : false
);
const [showVideo, setShowVideo] = useState(false);
//点赞和取消点赞功能
const thumbsUp = async (id, times = 1) => {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
moment_id: id,
times: times,
...base,
});
const _response = await fetch(
`${apiUrl}/api/moment/thumbs_up?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
moment_id: id,
times: times,
...base,
}),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
} catch (error) {
console.error(error);
}
};
const handleLike = () => {
setLike(!like);
if (like) {
thumbsUp(data.id, -1);
return;
}
thumbsUp(data.id, 1);
};
//关注和取关功能
const handleFollow = async () => {
setIsFollowed(!isFollowed);
const account = await get("account");
if (isFollowed) {
unfollow(account.mid, data.mid);
return;
}
follow(account.mid, data.mid);
};
//帖子操作功能
const [operationModalVisible, setOperationModalVisible] = useState(false);
const formatZoneUpdateTime = useCallback((time) => {
if (time === 0) return "今日";
if (time === 1) return "昨日";
return `${time}日前`;
}, []);
return (
navigation.navigate("StreamerProfile", { mid: data?.mid })
}
>
{data?.streamer_ext?.name}
{isFollowed ? "已关注" : "关注"}
{data?.text}
{/* 媒体展示 */}
{data.media_component.video_ids?.length === 0 ||
data.media_component.video_ids === null ? (
) : (
{
setShowVideo(true);
}}
>
)}
{data?.streamer_ext?.is_active_within_a_week === 1 ? (
navigation.navigate("SpaceIntroduce", { mid: data?.mid })
}
style={tailwind("flex flex-row items-center")}
>
空间
{formatZoneUpdateTime(
data?.streamer_ext
?.days_elapsed_since_the_last_zones_update
)}
有更新
) : (
data?.streamer_ext?.zones?.length !== 0 && (
navigation.navigate("SpaceIntroduce", { mid: data?.mid })
}
style={tailwind("flex flex-row items-center")}
>
查看TA的空间
)
)}
{like ? (
) : (
)}
{like ? "已赞" : "点赞"}
setOperationModalVisible(!operationModalVisible)
}
>
);
}
//媒体为图片时展示内容的组件
function ImageDisplay({ media }) {
const navigation = useNavigation();
const tailwind = useTailwind();
const images = media.map((item) => ({ url: item.urls[0] }));
const { showImageViewer } = useImageViewer();
if (images.length === 0) return null;
return (
{media.length > 1 ? (
media.map((item, index) => (
{
showImageViewer({
imageUrls: images,
index: index,
});
}}
style={{ aspectRatio: 1, ...tailwind("basis-1/3 p-0.5") }}
>
))
) : (
{
showImageViewer({
imageUrls: images,
index: 0,
});
}}
style={{
width:
media[0].w < media[0].h ? (media[0].w / media[0].h) * 200 : 250,
height:
media[0].w < media[0].h ? 200 : (media[0].h / media[0].w) * 250,
}}
>
)}
);
}
//媒体为视频时展示封面的组件
function PosterDisplay({ media }) {
const tailwind = useTailwind();
return (
);
}
//帖子操作组件
function OperationModal({ visible, setVisible, data }) {
const tailwind = useTailwind();
const navigation = useNavigation();
//拉黑
const handleBlock = async () => {
const account = await get("account");
const subMid = account.mid;
const objMid = data.mid;
await block(subMid, objMid);
setVisible(false);
};
//删除
const handleDelete = async () => {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const signature = await generateSignature({
id: data.id,
...base,
});
const _response = await fetch(
`${apiUrl}/api/moment/delete?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: data.id,
...base,
}),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
Toast.show({
type: "success",
text1: "删除成功,请刷新页面",
topOffset: 60,
});
return;
} catch (error) {
console.error(error);
} finally {
setVisible(false);
}
};
//判断是否展示删除、拉黑、举报功能
const [display, setDisPlay] = useState({
delete: false,
block: true,
feedback: true,
});
useEffect(() => {
const checkAuth = async () => {
const account = await get("account");
if (account.mid === data.mid || account.role === 1 || account.role === 2)
setDisPlay((prev) => {
return { ...prev, delete: true };
});
if (account.mid === data.mid)
setDisPlay((prev) => {
return { ...prev, block: false, feedback: false };
});
};
checkAuth();
}, []);
return (
setVisible(false)}
style={{
backgroundColor: "#00000080",
...tailwind("flex flex-1 items-center justify-center"),
}}
>
{display.feedback && (
{
navigation.navigate("MessageDetail", { mid: 1 });
setVisible(false);
}}
style={tailwind("flex flex-col w-full py-2 items-center")}
>
举报
)}
{display.block && (
拉黑
)}
{display.delete && (
删除
)}
setVisible(false)}
style={tailwind("flex flex-col w-full py-2 items-center")}
>
关闭
);
}