San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 1 | import React ,{useCallback, useState}from "react"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 2 | import style from "./postList.module.css"; |
| 3 | import SelfStatus from "@/components/selfStatus/selfStatus"; |
阳菜,放晴! | 77743f4 | 2025-06-06 23:04:08 +0800 | [diff] [blame] | 4 | import Corner from "@/components/corner/corner" |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 5 | import Navbar from "@/components/navbar/navbar"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 6 | import { getPosts, unknownAPI } from "@/api/post"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 7 | import { useApi } from "@/hooks/request"; |
| 8 | import request from "@/utils/request"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 9 | import { useEffect } from "react"; |
| 10 | import { useNavigate, useSearchParams } from "react-router"; |
San3yuan | 2728ba0 | 2025-06-06 22:02:57 +0800 | [diff] [blame] | 11 | import { MainPostTag } from "@/types/common"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 12 | |
| 13 | |
| 14 | const PostList:React.FC = () => { |
| 15 | const [searchParams] = useSearchParams(); |
| 16 | const type = searchParams.get("type") || ""; |
| 17 | const nav = useNavigate(); |
| 18 | |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 19 | if(type in ['video', 'music', 'Game', 'software']) { |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 20 | nav('/') |
| 21 | } |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 22 | 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>>([]); |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 26 | |
| 27 | const handlePostClick = (postId:number) => { |
| 28 | nav(`/postsDetail?postId=${postId}`); |
| 29 | } |
| 30 | |
| 31 | useEffect(() => { |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 32 | getPostList([MainPostTag[type as keyof typeof MainPostTag]]); |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 33 | },[currentPage, pageSize]); |
| 34 | |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 35 | useEffect(()=>{ |
| 36 | console.log(tagIds) |
| 37 | getPostList([...tagIds, MainPostTag[type as keyof typeof MainPostTag]]) |
| 38 | },[tagIds]) |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 39 | 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> |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 51 | <p className={style.createDate}>{new Date(post.createdAt).toLocaleString()}</p> |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 52 | </div> |
| 53 | )) |
| 54 | ) : ( |
| 55 | <div className={style.noData}>未查询到相关帖子</div> |
| 56 | )} |
| 57 | |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 58 | </div> |
| 59 | </div> |
| 60 | <div className={style.right}> |
| 61 | <div className={style.selfStatus}> |
| 62 | <SelfStatus/> |
| 63 | </div> |
| 64 | <div className={style.filter}> |
San3yuan | 292794c | 2025-06-08 20:02:46 +0800 | [diff] [blame] | 65 | <Corner setTagIds={setTagIds}/> |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 66 | </div> |
| 67 | </div> |
| 68 | </div> |
| 69 | ) |
| 70 | } |
| 71 | |
| 72 | export default PostList; |