Files
2026-04-25 20:12:23 +08:00

165 lines
5.9 KiB
TypeScript

"use client"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardFooter, CardHeader } from "@/components/ui/card"
import { EmptyState } from "@/components/ui/empty-state"
import { listGames, listPosts } from "@/lib/api"
import { roleLabels } from "@/lib/constants"
import type { Game, Post } from "@/lib/types"
import { Heart, MessageCircle, PenSquare, Pin } from "lucide-react"
import Link from "next/link"
import { useEffect, useState } from "react"
export default function CommunityPage() {
const [games, setGames] = useState<Game[]>([])
const [posts, setPosts] = useState<Post[]>([])
const [postsLoading, setPostsLoading] = useState(true)
const [sortMode, setSortMode] = useState<"latest" | "hot">("latest")
const [selectedGame, setSelectedGame] = useState<string | null>(null)
useEffect(() => {
let cancelled = false
Promise.all([listGames(), listPosts()])
.then(([gamesItems, postsItems]) => {
if (cancelled) return
setGames(gamesItems)
setPosts(postsItems)
setPostsLoading(false)
})
.catch(() => {
if (cancelled) return
setGames([])
setPosts([])
setPostsLoading(false)
})
return () => {
cancelled = true
}
}, [])
const filteredPosts = posts
.filter((post) => {
if (!selectedGame) return true
return post.tags.includes(selectedGame)
})
.sort((a, b) => {
if (sortMode === "latest") {
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
}
const scoreA = a.likeCount + a.commentCount
const scoreB = b.likeCount + b.commentCount
return scoreB - scoreA
})
return (
<div className="container mx-auto py-8 px-4 max-w-2xl">
<div className="flex items-center justify-between mb-6">
<h1 className="text-2xl font-bold"></h1>
<Button asChild>
<Link href="/post/new">
<PenSquare className="mr-1 h-4 w-4" />
</Link>
</Button>
</div>
<div className="flex flex-col gap-4 mb-6">
<div className="flex items-center gap-2">
<Button
variant={sortMode === "latest" ? "default" : "outline"}
size="sm"
onClick={() => setSortMode("latest")}
>
</Button>
<Button
variant={sortMode === "hot" ? "default" : "outline"}
size="sm"
onClick={() => setSortMode("hot")}
>
</Button>
</div>
<div className="flex flex-wrap gap-2">
{games.map((game) => (
<Button
key={game.id}
variant={selectedGame === game.name ? "default" : "outline"}
size="sm"
className="h-7 rounded-full px-3"
onClick={() => setSelectedGame(selectedGame === game.name ? null : game.name)}
>
{game.name}
</Button>
))}
</div>
</div>
<div className="flex flex-col gap-4">
{postsLoading ? (
<div className="text-center py-12 text-muted-foreground">...</div>
) : filteredPosts.length === 0 ? (
<EmptyState title="暂无帖子" description="此分类下暂未找到相关的讨论内容" />
) : (
filteredPosts.map((post) => (
<Link key={post.id} href={`/post/${post.id}`} className="block">
<Card className="shadow-sm border-border/80 gap-4">
<CardHeader>
<div className="flex items-center gap-3">
<Avatar className="h-9 w-9">
<AvatarImage src={post.author.avatar} />
<AvatarFallback>{post.author.nickname[0]}</AvatarFallback>
</Avatar>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="text-sm font-medium">{post.author.nickname}</span>
<Badge variant="outline" className="text-[10px] px-1.5 py-0">
{roleLabels[post.author.role]}
</Badge>
{post.pinned && <Pin className="h-3 w-3 text-muted-foreground" />}
</div>
<span className="text-xs text-muted-foreground">
{new Date(post.createdAt).toLocaleDateString("zh-CN")}
</span>
</div>
</div>
</CardHeader>
<CardContent>
<h3 className="font-semibold mb-1">{post.title}</h3>
<p className="text-sm text-muted-foreground line-clamp-2">{post.content}</p>
{post.tags.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{post.tags.map((tag) => (
<Badge key={tag} variant="secondary" className="text-xs">
{tag}
</Badge>
))}
</div>
)}
</CardContent>
<CardFooter className="text-sm text-muted-foreground gap-4">
<span className="flex items-center gap-1">
<Heart
className={`h-4 w-4 ${post.liked ? "fill-destructive text-destructive" : ""}`}
/>
{post.likeCount}
</span>
<span className="flex items-center gap-1">
<MessageCircle className="h-4 w-4" />
{post.commentCount}
</span>
</CardFooter>
</Card>
</Link>
))
)}
</div>
</div>
)
}