"use client";
import React, {
useEffect,
useRef,
useState,
useImperativeHandle,
forwardRef,
} from "react";
import { Tabs, Swiper, Toast, InfiniteScroll, List, Image } from "antd-mobile";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faRefresh } from "@fortawesome/free-solid-svg-icons";
import PostItem from "../components/PostItem";
// import { sleep } from "antd-mobile/es/utils/sleep";
import "./index.css";
import PostItemSkeleton from "@/components/skeletons/PostItemSkeleton";
import Link from "next/link";
import requireAPI from "@/utils/requireAPI";
import Empty from "@/components/Empty";
import { get } from "@/utils/storeInfo";
import StreamerNavigator from "@/components/StreamerNavigator";
import AddToHome from "@/components/AddToHome";
import InfiniteScrollContent from "@/components/InfiniteScrollContent";
const variables = {
"@active-line-color": "#f00", // 将主题色改为红色
};
const tabItems = [
{ key: "commend", title: "推荐" },
{ key: "follow", title: "关注" },
];
export default function Home() {
const recommPostRef = useRef();
const followPostRef = useRef();
const swiperRef = useRef(null);
const [activeIndex, setActiveIndex] = useState(0);
const [scrollHeight, setScrollHeight] = useState(0);
const childrenFunc = () => {
if (!activeIndex) {
recommPostRef.current?.doRefresh();
} else {
followPostRef.current?.doRefresh();
}
};
return (
{
const index = tabItems.findIndex((item) => item.key === key);
setActiveIndex(index);
swiperRef.current?.swipeTo(index);
}}
>
{tabItems.map((item) => (
))}
null}
ref={swiperRef}
defaultIndex={activeIndex}
onIndexChange={(index) => {
setActiveIndex(index);
}}
>
{!activeIndex && (
)}
{!!activeIndex && (
)}
);
}
const RecommPostList = forwardRef(({ scrollHeight }, ref) => {
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const [currentSearchKey, setCurrentSearchKey] = useState(2);
const [commenPostList, setCommenPostList] = useState([]);
useEffect(() => {
// getRecommPostList(2).then((res) => {
// setCommenPostList(res);
// setHasMore(true)
// });
}, []);
useImperativeHandle(
ref,
() => {
return { doRefresh };
},
[]
);
async function doRefresh() {
// await sleep(1000);
// Toast.show({
// icon: "fail",
// content: "刷新失败",
// });
// throw new Error("刷新失败");
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth", // 可选,平滑滚动效果
});
const list = await getRecommPostList(1);
setCommenPostList(list);
setHasMore(true);
}
async function loadMore() {
// debugger
const list = await getRecommPostList(!commenPostList.length ? 2 : 0);
if (list.length == 0) {
setHasMore(false);
}
setCommenPostList([...commenPostList, ...list]);
}
const getRecommPostList = async (type = 2) => {
setLoading(true);
const data = await requireAPI("POST", "/api/moment/recomm_list", {
body: { op_type: type },
});
setLoading(false);
if (data.ret == -1) {
// Toast.show({
// icon: "fail",
// content: data.msg,
// position: "top",
// });
} else {
return data.data.recomm_list;
}
};
return (
//
{loading && !commenPostList.length && (
)}
{commenPostList?.map((item) => (
))}
{/* {commenPostList?.length == 0 && !loading && (
)} */}
{/* {!!commenPostList?.length && (
)} */}
);
});
const FollowPostList = forwardRef(({ scrollHeight }, ref) => {
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(false);
const [followPostList, setFollowPostList] = useState([]);
const [currentTime, setCurrentTime] = useState();
const [offset, setOffset] = useState(0);
const ids = useRef(null);
useEffect(() => {
getFollowIds().then((res) => {
ids.current = res;
getFollowPostList(res, 0);
});
}, []);
useImperativeHandle(
ref,
() => {
return { doRefresh };
},
[]
);
async function doRefresh() {
// await sleep(1000);
// Toast.show({
// icon: "fail",
// content: "刷新失败",
// });
// throw new Error("刷新失败");
// getRecommPostList(1);
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth", // 可选,平滑滚动效果
});
// setFollowPostList([]);
await getFollowPostList(ids.current, 0);
}
async function loadMore() {
await getFollowPostList(ids.current, offset);
// const newList = [...followPostList, ...list];
// setFollowPostList(newList);
}
const getFollowIds = async () => {
setLoading(true);
setCurrentTime(Math.floor(new Date().getTime() / 1000));
const data = await requireAPI(
"POST",
"/api/account_relation/list_follow",
{
body: { offset, limit: 4 },
},
true
);
return data;
};
const getFollowPostList = async (data, offset) => {
if (data.data.list.length > 0) {
//查关注主播展示资料
const followsResponse = await requireAPI(
"POST",
"/api/moment/list_by_mids",
{
body: {
offset,
limit: 4,
ct_upper_bound: currentTime,
mids: data.data.list?.map((item) => item.obj_mid),
},
}
);
// debugger;
// console.log("offset", followsResponse.data.offset);
setOffset(followsResponse.data.offset);
setHasMore(followsResponse.data.more);
setLoading(false);
if (data.ret == -1) {
// Toast.show({
// icon: "fail",
// content: data.msg,
// position: "top",
// });
} else {
setFollowPostList((old) => {
if (!offset) {
return followsResponse.data.list;
} else {
return [...old, ...followsResponse.data.list];
}
});
}
} else {
setLoading(false);
}
};
return (
{/*
*/}
{loading && !followPostList.length && (
)}
{followPostList?.map((item, index) => (
))}
{!followPostList?.length && (
)}
{!!followPostList?.length && (
)}
{/* */}
);
});