"use client"; import React, { useEffect, useRef, useState,useRef } from "react"; import { Tabs, Swiper, Toast, Image, List, InfiniteScroll } from "antd-mobile"; import PostItem from "@/components/PostItem"; import "./index.css"; import Link from "next/link"; import Empty from "@/components/Empty"; import { useRouter } from "next/navigation"; import PostItemSkeleton from "@/components/skeletons/PostItemSkeleton"; import require from "@/utils/require"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faRefresh } from "@fortawesome/free-solid-svg-icons"; const tabItems = [ { key: "space", title: "空间" }, { key: "post", title: "动态" }, ]; // const scrollHeight = 700; // const scrollHeight = window.innerHeight-126 export default function Space() { const swiperRef = useRef(null); const [activeIndex, setActiveIndex] = useState(0); const [dataList, setDataList] = useState([]); // const [spacePost, setSpacePost] = useState([]); const [hasMore, setHasMore] = useState(true); const [scrollHeight, setScrollHeight] = useState(0); const [offset, setOffset] = useState(0); const [loading, setLoading] = useState(false); const scrollRef = useRef(null); // 获取屏幕高度 // const scrollHeight = 600; useEffect(() => { setScrollHeight(window.innerHeight - 57); // const handleResize = () => { // setScrollHeight(window.innerHeight - 126); // }; // window.addEventListener("resize", handleResize); // return () => { // // window.removeEventListener("resize", handleResize); // }; }, []); useEffect(() => { firstRequest() }, [activeIndex]); const firstRequest = () => { resetOffset(); if (activeIndex === 0) { getSpaceList(); } if (activeIndex === 1) { getSpacePosts(0).then((res) => { setDataList(res); }); } } const resetOffset = () => { setOffset(0); // setDataList([]); }; const getSpaceList = async () => { setLoading(true); try { const data = await require("POST", "/api/zone/list_by_visitor_mid"); setLoading(false); if (data.ret === -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); return; } //在末尾添加元素以展示查看更多卡片 if (data.data.list.length !== 0) { const finalData = [...data.data.list, { id: 999999, last: true }]; setDataList(finalData); return; } } catch (error) { console.error(error); } }; const getSpacePosts = async (offset) => { setLoading(true); try { const data = await require("POST", "/api/zone_moment/list_by_visitor_mid", { body: { offset, limit: 4 }, }); setLoading(false); if (data.ret === -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); return; } //在末尾添加元素以展示查看更多卡片 if (data.data.list.length !== 0) { const finalData = [...data.data.list]; setOffset(offset + 1); return finalData; } } catch (error) { console.error(error); } }; async function loadMore() { if (!offset) return; const append = await getSpacePosts(offset); if(append){ setDataList((val) => [...val, ...append]); setHasMore(append.length > 0); } } return (