"use client"; import React, { useEffect, useState, useMemo, useCallback } from "react"; import Photos from "../Photos"; import { useRouter } from "next/navigation"; import PaySpacePost from "../PaySpacePost"; import { Image, Popover, Divider, Toast } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleRight } from "@fortawesome/free-solid-svg-icons"; import { handleFollow, thumbsUp, handleBlock } from "@/api/public"; import { get } from "@/utils/storeInfo"; import { Inter } from "next/font/google"; import requireAPI from "@/utils/requireAPI"; import { getcountLines } from "@/utils/tools"; const inter = Inter({ subsets: ["latin"] }); export default function PostItem({ type, follow, date = new Date(), data = {}, showFollow = true, }) { const router = useRouter(); const [isOpenText, setIsOpenText] = useState(false); const [popoverVisible, setPopoverVisible] = useState(false); const [isFollow, setIsFollow] = useState(data.is_followed); const [isThumbsUp, setIsThumbsUp] = useState( data?.is_thumbed_up || data?.is_zone_moment_thumbed_up ); const [account, setAccount] = useState( data?.is_thumbed_up || data?.is_zone_moment_thumbed_up ); //判断是否是发帖人 const [isCreator, setIsCreator] = useState(false); useEffect(() => { const currentAccount = get("account"); setAccount(currentAccount); if (currentAccount?.mid === data.mid) setIsCreator(true); return () => { router.prefetch("/profile/" + data.mid); }; }, []); const getDays = useMemo(() => { const today = new Date(); const days = Math.floor((today - data.ct) / (1000 * 60 * 60 * 24)); return days; }, []); function findLinksInText(text) { // 定义一个正则表达式来匹配URL // 注意:这个正则表达式可能无法匹配所有可能的URL,但可以匹配大部分常见的格式 var urlPattern = /https?:\/\/[^\s\/$.?#].[^\s]*/g; // 使用正则表达式的exec或match方法查找匹配项 // exec在全局搜索中需要循环调用,而match在全局模式下直接返回所有匹配项 var matches = text?.match(urlPattern); // // 如果没有找到匹配项,返回空数组 // if (!matches) { // return text; // } if (matches) { matches?.forEach((el) => { // console.log("index", text.indexOf(el)); text = text.replace( el, `#网页链接` ); }); // console.log("matches", matches); // console.log("text", text); } // 返回找到的链接数组 return text; } const formatZoneUpdateTime = useCallback((time) => { if (time === 0) return "今日"; if (time === 1) return "昨日"; return `${time}日前`; }, []); const handleDelete = async (type) => { try { const _data = await requireAPI( "POST", `/api/${type == "space" ? "zone_moment" : "moment"}/delete`, { body: { id: data?.id, }, } ); if (_data.ret === -1) { Toast.show({ icon: "fail", content: _data.msg, position: "top", }); return; } Toast.show({ icon: "success", content: "删除成功,请刷新页面", position: "top", }); return; } catch (error) { console.error(error); } finally { setPopoverVisible(false); } }; //置顶 const handleTop = async () => { try { const body = { zone_moment_ids: [data.id], op_type: data.is_headed ? 0 : 1, }; const _data = await requireAPI("POST", "/api/zone_moment/head", { body, }); if (_data.ret === -1) { Toast.show({ icon: "fail", content: _data.msg, position: "top", }); return; } Toast.show({ icon: "success", content: "操作成功,请刷新页面查看", position: "top", }); return; } catch (error) { console.error(error); } finally { setPopoverVisible(false); } }; return (
{type == "space" && data?.is_headed === 1 && ( )}
{ router.push("/profile/" + data.mid); }} >
{data.streamer_ext?.name} {type == "post" && showFollow && ( handleFollow(isFollow, data?.mid, setIsFollow)} className="rounded-full bg-[#FFFFFF1A] px-2 py-1 text-xs text-white font-medium" > {isFollow ? "已关注" : "关注"} )}
{(type == "space" ? data?.status === 0 || data?.status === 1 : data?.status === 3 || data?.status === 4) && (
审核中
)} {(type == "space" ? data?.status === 3 : data?.status === 5 || data?.status === 6) && (

{ router.push( `${ type == "space" ? "/space/editSpacePost" : "/streamerPosts/editPost" }/${data.id}` ); }} > 审核未通过,请重新编辑

)}
{!data?.is_zone_moment_unlocked ? ( ) : (
)} {!( !data?.is_zone_moment_unlocked && data?.text_visible_range < 999 ) && (data.text?.length > 140 || getcountLines(data.paid_text || "") > 1) && (
setIsOpenText(!isOpenText)} > {isOpenText ? "收起" : "全文"}
)}
{data.media_component && ( )} {type == "space" && !!data.c_type && ( )}
{type == "post" ? (
router.push("/space/person_space_introduce/" + data.mid) } > {data.streamer_ext.is_active_within_a_week ? ( <> {/* {data.streamer_ext.days_elapsed_since_the_last_zones_update < 7 && `空间${ data.streamer_ext.days_elapsed_since_the_last_zones_update === 0 ? "今日" : "new" === 1 ? "昨日" : "new" === 2 ? "前天" : data.streamer_ext.days_elapsed_since_the_last_zones_update + "天前" }有更新`} 空间 */} 空间 {formatZoneUpdateTime( data?.streamer_ext ?.days_elapsed_since_the_last_zones_update )} 有更新 ) : ( data?.streamer_ext?.zones?.length !== 0 && (
查看TA的空间
) )}
) : (
{getDays < 3 ? `${ getDays === 0 ? "今日" : "new" === 1 ? "昨日" : "前天" }` : date.getMonth() + 1 + "-" + date.getDate()} {(date.getHours() > 9 ? date.getHours() : "0" + date.getHours()) + ":" + (date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes())}
)}
thumbsUp(data.id, isThumbsUp, setIsThumbsUp, type == "space") } > {isThumbsUp == 1 ? "已赞" : "点赞"}
{type == "space" && account?.mid == data?.mid ? ( <>
  • {data.is_headed ? "取消置顶" : "置顶"}

  • { router.push(`/space/editSpacePost/${data.id}`); }} > 编辑

  • handleDelete("space")} > 删除
  • ) : type == "post" && account?.mid == data?.mid ? ( <>
  • handleDelete("post")} > 删除
  • ) : ( <>
  • { router.push("/messageDetail"); }} > 举报

  • { const subMid = account.mid; const objMid = data.mid; await handleBlock(subMid, objMid); setPopoverVisible(false); }} > 拉黑
  • )} } trigger="click" placement="left" > setPopoverVisible(!popoverVisible)} > ···
    {/*
    */}
    ); }