blob: 2312361254dff053263a7a37233f44f114dea0a8 [file] [log] [blame]
San3yuan292794c2025-06-08 20:02:46 +08001import React ,{useCallback, useState}from "react";
San3yuana2ee30b2025-06-05 21:20:17 +08002import style from "./postList.module.css";
3import SelfStatus from "@/components/selfStatus/selfStatus";
阳菜,放晴!77743f42025-06-06 23:04:08 +08004import Corner from "@/components/corner/corner"
San3yuana2ee30b2025-06-05 21:20:17 +08005import Navbar from "@/components/navbar/navbar";
San3yuana2ee30b2025-06-05 21:20:17 +08006import { getPosts, unknownAPI } from "@/api/post";
San3yuana2ee30b2025-06-05 21:20:17 +08007import { useApi } from "@/hooks/request";
8import request from "@/utils/request";
San3yuana2ee30b2025-06-05 21:20:17 +08009import { useEffect } from "react";
10import { useNavigate, useSearchParams } from "react-router";
San3yuan2728ba02025-06-06 22:02:57 +080011import { MainPostTag } from "@/types/common";
San3yuana2ee30b2025-06-05 21:20:17 +080012
13
14const PostList:React.FC = () => {
15 const [searchParams] = useSearchParams();
16 const type = searchParams.get("type") || "";
17 const nav = useNavigate();
18
San3yuan292794c2025-06-08 20:02:46 +080019 if(type in ['video', 'music', 'Game', 'software']) {
San3yuana2ee30b2025-06-05 21:20:17 +080020 nav('/')
21 }
San3yuan292794c2025-06-08 20:02:46 +080022 const {data:postList, refresh:getPostList} = useApi((tags) => request.get(getPosts + `?keyword&tags=${tags.join(',')}&author`), false);
23 const [currentPage, setCurrentPage] = useState(1);
24 const [pageSize, setPageSize] = useState(10);
25 const [tagIds, setTagIds] = useState<Array<number>>([]);
San3yuana2ee30b2025-06-05 21:20:17 +080026
27 const handlePostClick = (postId:number) => {
28 nav(`/postsDetail?postId=${postId}`);
29 }
30
31 useEffect(() => {
San3yuan292794c2025-06-08 20:02:46 +080032 getPostList([MainPostTag[type as keyof typeof MainPostTag]]);
San3yuana2ee30b2025-06-05 21:20:17 +080033 },[currentPage, pageSize]);
34
San3yuan292794c2025-06-08 20:02:46 +080035 useEffect(()=>{
36 console.log(tagIds)
37 getPostList([...tagIds, MainPostTag[type as keyof typeof MainPostTag]])
38 },[tagIds])
San3yuana2ee30b2025-06-05 21:20:17 +080039 return (
40 <div className={style.container}>
41 <div className={style.left}>
42 <div className={style.navbar}>
43 <Navbar current={type}/>
44 </div>
45 <div className={style.content}>
46 {postList && postList.length > 0 ? (
47 postList.map((post: { postId: number; postTitle: string; postContent: string; createdAt:string }) => (
48 <div key={post.postId} className={style.contentItem} onClick={() => handlePostClick(post.postId)}>
49 <h3>{post.postTitle}</h3>
50 <p>{post.postContent.substring(0, 20)}</p>
San3yuan292794c2025-06-08 20:02:46 +080051 <p className={style.createDate}>{new Date(post.createdAt).toLocaleString()}</p>
San3yuana2ee30b2025-06-05 21:20:17 +080052 </div>
53 ))
54 ) : (
55 <div className={style.noData}>未查询到相关帖子</div>
56 )}
57
San3yuana2ee30b2025-06-05 21:20:17 +080058 </div>
59 </div>
60 <div className={style.right}>
61 <div className={style.selfStatus}>
62 <SelfStatus/>
63 </div>
64 <div className={style.filter}>
San3yuan292794c2025-06-08 20:02:46 +080065 <Corner setTagIds={setTagIds}/>
San3yuana2ee30b2025-06-05 21:20:17 +080066 </div>
67 </div>
68 </div>
69 )
70}
71
72export default PostList;