tiefen_space_h5/app/space/page.js

358 lines
12 KiB
JavaScript
Raw Normal View History

2024-06-25 20:18:37 +08:00
"use client";
2024-07-22 16:07:41 +08:00
import React, { useEffect, useRef, useState } from "react";
import {
Tabs,
Swiper,
Toast,
Image,
List,
InfiniteScroll,
SpinLoading,
} from "antd-mobile";
2024-06-25 20:18:37 +08:00
import PostItem from "@/components/PostItem";
import "./index.css";
import Link from "next/link";
import Empty from "@/components/Empty";
2024-06-29 12:07:37 +08:00
import { useRouter } from "next/navigation";
import PostItemSkeleton from "@/components/skeletons/PostItemSkeleton";
2024-07-22 16:07:41 +08:00
import requireAPI from "@/utils/requireAPI";
2024-07-06 16:03:15 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faRefresh } from "@fortawesome/free-solid-svg-icons";
2024-06-25 20:18:37 +08:00
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([]);
2024-07-06 16:03:15 +08:00
// const [spacePost, setSpacePost] = useState([]);
2024-07-22 19:43:20 +08:00
const [hasMore, setHasMore] = useState(1);
2024-06-25 20:18:37 +08:00
const [scrollHeight, setScrollHeight] = useState(0);
2024-07-06 16:03:15 +08:00
const [offset, setOffset] = useState(0);
const [loading, setLoading] = useState(false);
const scrollRef = useRef(null);
2024-07-10 16:50:53 +08:00
const router = useRouter();
2024-06-25 20:18:37 +08:00
// 获取屏幕高度
// const scrollHeight = 600;
useEffect(() => {
2024-07-22 19:43:20 +08:00
setScrollHeight(window.innerHeight - 57 - 44);
2024-06-25 20:18:37 +08:00
// const handleResize = () => {
// setScrollHeight(window.innerHeight - 126);
// };
// window.addEventListener("resize", handleResize);
// return () => {
// // window.removeEventListener("resize", handleResize);
// };
}, []);
2024-07-06 16:03:15 +08:00
useEffect(() => {
2024-07-10 16:50:53 +08:00
firstRequest();
2024-07-06 16:03:15 +08:00
}, [activeIndex]);
const firstRequest = () => {
resetOffset();
if (activeIndex === 0) {
getSpaceList();
}
if (activeIndex === 1) {
getSpacePosts(0).then((res) => {
setDataList(res);
});
2024-06-25 20:18:37 +08:00
}
2024-07-10 16:50:53 +08:00
};
2024-07-06 16:03:15 +08:00
const resetOffset = () => {
setOffset(0);
// setDataList([]);
};
const getSpaceList = async () => {
2024-07-22 19:43:20 +08:00
// setLoading(true);
2024-07-06 16:03:15 +08:00
try {
2024-07-22 16:07:41 +08:00
const data = await requireAPI("POST", "/api/zone/list_by_visitor_mid");
2024-07-22 19:43:20 +08:00
// setLoading(false);
2024-07-06 16:03:15 +08:00
if (data.ret === -1) {
Toast.show({
2024-07-08 20:07:36 +08:00
icon: "fail",
content: data.msg,
position: "top",
2024-07-06 16:03:15 +08:00
});
return;
}
//在末尾添加元素以展示查看更多卡片
if (data.data.list.length !== 0) {
2024-07-10 16:50:53 +08:00
const finalData = [...data.data.list];
2024-07-06 16:03:15 +08:00
setDataList(finalData);
return;
}
} catch (error) {
console.error(error);
}
};
const getSpacePosts = async (offset) => {
2024-07-22 19:43:20 +08:00
// setLoading(true);
2024-07-06 16:03:15 +08:00
try {
2024-07-22 19:43:20 +08:00
const data = await requireAPI(
"POST",
"/api/zone_moment/list_by_visitor_mid",
{
2024-07-06 16:03:15 +08:00
body: { offset, limit: 4 },
2024-07-22 19:43:20 +08:00
}
);
// setLoading(false);
2024-07-06 16:03:15 +08:00
if (data.ret === -1) {
Toast.show({
2024-07-08 20:07:36 +08:00
icon: "fail",
content: data.msg,
position: "top",
2024-07-06 16:03:15 +08:00
});
return;
}
//在末尾添加元素以展示查看更多卡片
2024-07-22 19:43:20 +08:00
setHasMore(data.data.more);
setOffset(data.data.offset);
2024-07-06 16:03:15 +08:00
if (data.data.list.length !== 0) {
const finalData = [...data.data.list];
return finalData;
}
} catch (error) {
console.error(error);
}
};
2024-06-25 20:18:37 +08:00
async function loadMore() {
2024-07-06 16:03:15 +08:00
if (!offset) return;
const append = await getSpacePosts(offset);
2024-07-10 16:50:53 +08:00
if (append) {
2024-07-08 20:07:36 +08:00
setDataList((val) => [...val, ...append]);
2024-07-22 19:43:20 +08:00
// setHasMore(append.length > 0);
2024-07-08 20:07:36 +08:00
}
2024-06-25 20:18:37 +08:00
}
return (
2024-07-10 16:50:53 +08:00
<div
className="h-screen overflow-x-hidden"
2024-07-22 19:43:20 +08:00
// style={{ maxHeight: `${scrollHeight}px` }}
2024-07-10 16:50:53 +08:00
>
2024-07-08 20:07:36 +08:00
<div className="flex justify-between items-center px-2 custom-tabs text-gray-400 sticky top-0 z-10 bg-deepBg">
2024-06-25 20:18:37 +08:00
<Tabs
activeKey={tabItems[activeIndex].key}
onChange={(key) => {
const index = tabItems.findIndex((item) => item.key === key);
setActiveIndex(index);
swiperRef.current?.swipeTo(index);
}}
>
{tabItems.map((item) => (
<Tabs.Tab
forceRender={false}
title={item.title}
key={item.key}
className="text-left"
/>
))}
</Tabs>
2024-06-29 12:07:37 +08:00
<Link
href="search"
className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full"
>
2024-07-25 19:52:30 +08:00
<Image
src={process.env.NEXT_PUBLIC_WEB_ASSETS_URL + "/icons/search.png"}
/>
2024-06-29 12:07:37 +08:00
</Link>
2024-06-25 20:18:37 +08:00
</div>
<Swiper
direction="horizontal"
indicator={() => null}
ref={swiperRef}
defaultIndex={activeIndex}
onIndexChange={(index) => {
setActiveIndex(index);
}}
>
<Swiper.Item>
2024-07-06 16:03:15 +08:00
{!activeIndex && (
<div>
2024-07-15 20:00:44 +08:00
{!loading ? (
2024-07-22 19:43:20 +08:00
// <div className="px-4 pb-8">
// </div>
<>
2024-07-17 16:58:27 +08:00
{dataList?.length > 0 ? (
2024-07-25 19:52:30 +08:00
<div className="px-4 pb-20">
2024-07-22 19:43:20 +08:00
<ul className="grid grid-cols-2 gap-2 overflow-y-auto">
2024-07-23 20:56:25 +08:00
{dataList?.map((item) => (
<li key={item.id}>
<VisitingCard data={item} />
</li>
))}
2024-07-24 14:26:46 +08:00
<li onClick={() => router.replace("/search")}>
2024-07-23 20:56:25 +08:00
<div
// onPress={() => navigation.navigate("Stream")}
// onClick={}
className="w-full h-52"
>
2024-07-25 19:52:30 +08:00
<div
className="h-full flex flex-col rounded-lg overflow-hidden bg-[#FFFFFF1A] bg-no-repeat bg-right-bottom bg-40%"
style={{
backgroundImage: `url(${
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/icons/magnifier.png"
})`,
}}
>
2024-07-23 20:56:25 +08:00
{/* <div className="w-full z-0"></div>
<div
className="w-full z-0 h-[42px]"
></div> */}
2024-07-23 20:56:25 +08:00
<div className="flex flex-col w-full h-full px-[22px] py-[30px]">
<p className="text-white font-medium text-lg">
发现更多
</p>
<p className="text-[#FFFFFF40] font-sm">
缘分就在不经意间
</p>
<Image
width={32}
height={32}
className="mt-4"
2024-07-25 19:52:30 +08:00
src={
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
"/icons/rightarrow_border.png"
}
2024-07-23 20:56:25 +08:00
/>
</div>
2024-07-15 20:00:44 +08:00
</div>
</div>
2024-07-23 20:56:25 +08:00
</li>
</ul>
2024-07-22 19:43:20 +08:00
</div>
2024-07-15 20:00:44 +08:00
) : (
<div
className={`flex flex-col items-center justify-center`}
style={{ height: `${scrollHeight}px` }}
>
<Empty type="nospace" />
<div className="flex flex-col mt-6">
<Link
2024-07-24 14:26:46 +08:00
href="search"
2024-07-15 20:00:44 +08:00
className="bg-[#FFFFFF40] px-12 py-2 rounded-full text-base text-white"
>
搜索空间
</Link>
2024-07-16 20:20:12 +08:00
{/* <Link
2024-07-24 14:26:46 +08:00
href="search"
2024-07-15 20:00:44 +08:00
className="bg-[#FFFFFF40] px-12 py-2 rounded-full text-base text-white mt-2"
>
查看推荐
2024-07-16 20:20:12 +08:00
</Link> */}
2024-07-10 16:50:53 +08:00
</div>
</div>
2024-07-15 20:00:44 +08:00
)}
2024-07-22 19:43:20 +08:00
</>
2024-07-15 20:00:44 +08:00
) : (
2024-07-06 16:03:15 +08:00
<div
className="w-full text-center flex items-center justify-center"
style={{ height: scrollHeight - 60 + "px" }}
2024-07-06 16:03:15 +08:00
>
<SpinLoading />
2024-07-06 16:03:15 +08:00
</div>
)}
</div>
2024-07-06 16:03:15 +08:00
)}
2024-06-25 20:18:37 +08:00
</Swiper.Item>
<Swiper.Item>
2024-07-10 16:50:53 +08:00
{!!activeIndex && (
2024-07-25 19:52:30 +08:00
<div className="px-4 pb-20" ref={scrollRef}>
2024-07-10 16:50:53 +08:00
<List className="scrollbarBox_hidden">
2024-07-06 16:03:15 +08:00
{loading && (
<>
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
</>
)}
{dataList?.map((item, index) => (
2024-07-10 16:50:53 +08:00
<List.Item className="!p-0" key={item.id + "_" + index}>
<PostItem
type="space"
data={item}
date={new Date(item.ct * 1000)}
/>
2024-07-10 16:50:53 +08:00
</List.Item>
))}
2024-07-06 16:03:15 +08:00
<InfiniteScroll loadMore={loadMore} hasMore={hasMore} />
</List>
2024-07-22 19:43:20 +08:00
{/* {!dataList?.length && (
2024-07-06 16:03:15 +08:00
<div
className={`flex flex-col items-center justify-center`}
style={{ height: `${scrollHeight}px` }}
>
<Empty type="nodata" />
</div>
2024-07-22 19:43:20 +08:00
)} */}
2024-06-29 12:07:37 +08:00
</div>
2024-07-10 16:50:53 +08:00
)}
2024-06-25 20:18:37 +08:00
</Swiper.Item>
</Swiper>
2024-07-06 16:03:15 +08:00
<div
className={`fixed bottom-[96px] right-4 z-[999] w-10 h-10 flex items-center justify-center bg-[#1d1d1d71] rounded-full text-white ${
2024-07-10 16:50:53 +08:00
loading && !offset ? "animate-spin" : ""
}`}
>
<FontAwesomeIcon
icon={faRefresh}
size="xl"
// onClick={firstRequest}
onClick={() => {
scrollRef.current?.scrollTo(0, 0);
firstRequest();
}}
/>
</div>
2024-07-06 11:05:19 +08:00
</div>
2024-06-25 20:18:37 +08:00
);
}
2024-06-29 12:07:37 +08:00
2024-07-06 16:03:15 +08:00
const VisitingCard = ({ data }) => {
2024-06-29 12:07:37 +08:00
const router = useRouter();
return (
<div
2024-07-10 16:50:53 +08:00
className="relative h-52"
2024-07-06 16:03:15 +08:00
onClick={() => router.push("/space/" + data?.streamer_ext?.mid)}
2024-06-29 12:07:37 +08:00
>
2024-07-06 16:03:15 +08:00
{data?.is_unread_zone_moment_exist === 1 && (
<Image
2024-07-25 19:52:30 +08:00
src={process.env.NEXT_PUBLIC_WEB_ASSETS_URL + "/images/space_new.png"}
2024-07-06 16:03:15 +08:00
width={78}
fit="contain"
className="absolute top-2 right-2"
/>
)}
2024-06-29 12:07:37 +08:00
<Image
width={"100%"}
height={"100%"}
2024-07-06 16:03:15 +08:00
src={data?.streamer_ext?.cover.images[0].urls[0]}
fit="cover"
2024-06-29 12:07:37 +08:00
className="rounded-lg"
/>
<div className="absolute bottom-0 left-0 w-full px-2 py-3 bg-[#1b1b1b] flex items-center rounded-b-lg">
<span className="font-bold overflow-hidden whitespace-nowrap text-ellipsis">
{data?.streamer_ext?.name}
</span>
2024-07-15 20:00:44 +08:00
<ul className="flex ml-2">
2024-07-23 20:56:25 +08:00
{data?.admission_price !== 0 && (
<li className="text-[10px] bg-primary rounded px-1 mr-1 whitespace-nowrap">
2024-07-23 20:56:25 +08:00
付费
</li>
2024-07-06 16:03:15 +08:00
)}
2024-07-23 20:56:25 +08:00
{data.visitor_role === 3 && (
<li className="text-[10px] bg-primary rounded px-1 mr-1 whitespace-nowrap">
2024-07-23 20:56:25 +08:00
创建者
</li>
2024-07-06 16:03:15 +08:00
)}
2024-06-29 12:07:37 +08:00
</ul>
</div>
</div>
);
};