tiefen_space_h5/app/search/page.js

182 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-06-25 20:18:37 +08:00
"use client";
import React, { useEffect, useRef, useState } from "react";
2024-07-06 11:05:19 +08:00
import { Input, List, DotLoading, Toast } from "antd-mobile";
2024-06-25 20:18:37 +08:00
import { useRouter } from "next/navigation";
2024-07-06 11:05:19 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
import require from "@/utils/require";
import { debounce } from "@/utils/tools";
import Empty from "@/components/Empty";
const newDebounce = debounce(function (fn) {
fn && fn();
}, 500);
2024-06-25 20:18:37 +08:00
export default function Search() {
const router = useRouter();
// 获取屏幕高度
// const scrollHeight = 600;
const [searchValue, setSearchValue] = useState("");
2024-07-06 11:05:19 +08:00
const [loading, setLoading] = useState(false);
const [streamers, setStreamers] = useState([]);
2024-07-08 20:07:36 +08:00
// const [zones, setZones] = useState([]);
2024-06-25 20:18:37 +08:00
useEffect(() => {}, []);
2024-07-06 11:05:19 +08:00
const isNumeric = (str) => {
return /^\d+$/.test(str);
};
const getResult = async (value) => {
console.log("searchValue", value);
const isSearchInt = isNumeric(value);
let api = "";
let querryParams = "";
if (isSearchInt) {
api = "/api/streamer/list_ext_fuzzily_by_user_id";
querryParams = { user_id: parseInt(value, 10) };
} else {
api = "/api/streamer/list_ext_fuzzily_by_name";
querryParams = { name: value };
}
try {
setLoading(true);
const data = await require("POST", api, {
body: {
...querryParams,
offset: 0,
limit: 20,
},
});
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
// if (!ignore) {
2024-07-08 20:07:36 +08:00
// const zonesData = data.data.list.filter((item) => item.zones.length > 0);
2024-07-06 11:05:19 +08:00
setStreamers(data.data.list);
2024-07-08 20:07:36 +08:00
// setZones(zonesData);
2024-07-06 11:05:19 +08:00
// }
setLoading(false);
} catch (error) {
console.error(error);
}
};
2024-06-25 20:18:37 +08:00
return (
<div className="">
2024-07-08 20:07:36 +08:00
<div className="flex items-center p-4 sticky top-0 bg-black z-10">
2024-07-06 11:05:19 +08:00
<FontAwesomeIcon
icon={faAngleLeft}
size="xl"
className="mr-3"
onClick={() => {
2024-06-25 20:18:37 +08:00
router.back();
2024-07-06 11:05:19 +08:00
}}
/>
2024-06-25 20:18:37 +08:00
<div className="flex items-center w-full">
2024-07-06 11:05:19 +08:00
<div className="relative bg-[#FFFFFF1A] rounded-lg px-4 py-1 w-full">
2024-06-25 20:18:37 +08:00
<Input
placeholder="搜索Ta的昵称或id"
value={searchValue}
onChange={(val) => {
2024-07-06 11:05:19 +08:00
setSearchValue((old) => {
let test = (e) => {
if (val == "") {
setStreamers([]);
2024-07-08 20:07:36 +08:00
// setZones([]);
2024-07-06 11:05:19 +08:00
return;
}
getResult(val);
};
newDebounce(test);
return val;
});
2024-06-25 20:18:37 +08:00
}}
/>
2024-07-06 11:05:19 +08:00
<div className="absolute top-1/2 -translate-y-1/2 right-2">
{loading && <DotLoading />}
</div>
2024-06-25 20:18:37 +08:00
</div>
{searchValue && (
<p
className="text-base whitespace-no-wrap ml-2 text-primary font-bold"
2024-06-25 20:18:37 +08:00
onClick={() => {
setSearchValue("");
}}
>
清空
</p>
)}
</div>
</div>
<List className="px-4 overflow-y-auto scrollbarBox_hidden">
2024-07-06 11:05:19 +08:00
{!streamers.length && (
<div
className={`h-screen -mt-[57px] flex flex-col items-center justify-center`}
>
<Empty type="nodata" />
2024-06-25 20:18:37 +08:00
</div>
2024-07-06 11:05:19 +08:00
)}
2024-07-08 20:07:36 +08:00
<div>
{streamers?.filter(
(streamer) => streamer.zones.length > 0
).length > 0 && (
<div>
<p className="mt-2 mb-1 text-white text-xl font-medium">
空间
</p>
{streamers?.filter(
(streamer) => streamer.zones.length > 0
)?.map((item) => (
<ZoneItem data={item} key={item.id}/>
))}
</div>
)}
{streamers.length > 0 && (
<div>
<p className="mt-2 mb-1 text-white text-xl font-medium">
个人
</p>
{streamers.map((item) => (
<ZoneItem data={item} key={item.id}/>
))}
2024-06-25 20:18:37 +08:00
</div>
2024-07-08 20:07:36 +08:00
)}
</div>
2024-06-25 20:18:37 +08:00
</List>
</div>
);
}
2024-07-08 20:07:36 +08:00
const ZoneItem = ({data}) => {
const router = useRouter();
return (
<List.Item
className="!p-0"
onClick={() => router.push(`/space/${data.mid}`)}
key={data.id}
arrow={false}
>
<div className="grid grid-cols-[2.5rem_100%] gap-2 items-center" style={{maxWidth:"calc(100vw - 2rem)"}}>
<img
className="flex-none w-10 h-10 rounded-full mr-2"
src={data?.avatar?.images[0]?.urls[0]}
alt=""
/>
<div className="flex-1">
<div className="flex items-center">
<span className="text-md mr-2 text-base text-white font-medium">
{data?.name}
</span>
<span className="py-0.5 px-2 ml-1 bg-[#FFFFFF1A] rounded-full text-white text-xs font-medium">
ID {data.user_id}
</span>
</div>
<p className="text-sm text-[#FFFFFF80] pr-16 overflow-hidden whitespace-nowrap text-ellipsis">{data?.bio}</p>
</div>
</div>
</List.Item>
);
};