fix(home): load catalog client-side
This commit is contained in:
+64
-10
@@ -1,15 +1,54 @@
|
||||
"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, CardTitle } from "@/components/ui/card"
|
||||
import { IconInput } from "@/components/ui/icon-input"
|
||||
import { listGames, listPlayers, listShops } from "@/lib/api"
|
||||
import { toApiError } from "@/lib/errors"
|
||||
import { GameIcon } from "@/lib/game-icons"
|
||||
import { ArrowRight, Search, ShoppingBag, Star } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export default async function HomePage() {
|
||||
const [games, players, shops] = await Promise.all([listGames(), listPlayers(), listShops()])
|
||||
export default function HomePage() {
|
||||
const [games, setGames] = useState<Awaited<ReturnType<typeof listGames>>>([])
|
||||
const [players, setPlayers] = useState<Awaited<ReturnType<typeof listPlayers>>>([])
|
||||
const [shops, setShops] = useState<Awaited<ReturnType<typeof listShops>>>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loadingError, setLoadingError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const [nextGames, nextPlayers, nextShops] = await Promise.all([
|
||||
listGames(),
|
||||
listPlayers(),
|
||||
listShops(),
|
||||
])
|
||||
|
||||
if (cancelled) return
|
||||
|
||||
setGames(nextGames)
|
||||
setPlayers(nextPlayers)
|
||||
setShops(nextShops)
|
||||
setLoading(false)
|
||||
} catch (err: unknown) {
|
||||
if (cancelled) return
|
||||
setLoading(false)
|
||||
setLoadingError(toApiError(err).msg)
|
||||
}
|
||||
}
|
||||
|
||||
void load()
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="container mx-auto py-8 px-4 space-y-12">
|
||||
@@ -42,6 +81,11 @@ export default async function HomePage() {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{loading && <p className="text-sm text-muted-foreground text-center">加载中...</p>}
|
||||
{!loading && loadingError && (
|
||||
<p className="text-sm text-muted-foreground text-center">{loadingError}</p>
|
||||
)}
|
||||
|
||||
<section>
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-xl font-semibold">游戏分类</h2>
|
||||
@@ -57,14 +101,16 @@ export default async function HomePage() {
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{games.map((game) => (
|
||||
{!loading && !loadingError
|
||||
? games.map((game) => (
|
||||
<Button key={game.id} variant="secondary" className="rounded-full" asChild>
|
||||
<Link href={`/search?game=${encodeURIComponent(game.name)}`}>
|
||||
<GameIcon name={game.icon} className="h-5 w-5" />
|
||||
{game.name}
|
||||
</Link>
|
||||
</Button>
|
||||
))}
|
||||
))
|
||||
: null}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -92,7 +138,8 @@ export default async function HomePage() {
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
{/* Players */}
|
||||
{players.map((player) => (
|
||||
{!loading && !loadingError
|
||||
? players.map((player) => (
|
||||
<Card
|
||||
key={player.id}
|
||||
className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60"
|
||||
@@ -109,7 +156,9 @@ export default async function HomePage() {
|
||||
<Star className="h-3.5 w-3.5 fill-primary text-primary mr-0.5" />
|
||||
{player.rating}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground">{player.totalOrders} 单</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{player.totalOrders} 单
|
||||
</span>
|
||||
<Badge
|
||||
variant={player.status === "available" ? "default" : "secondary"}
|
||||
className="text-[10px] px-1.5 py-0 font-normal"
|
||||
@@ -136,7 +185,9 @@ export default async function HomePage() {
|
||||
))}
|
||||
</div>
|
||||
{player.shopName && (
|
||||
<p className="text-xs text-muted-foreground mt-3">所属店铺: {player.shopName}</p>
|
||||
<p className="text-xs text-muted-foreground mt-3">
|
||||
所属店铺: {player.shopName}
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
<CardFooter className="pt-0 flex items-end justify-between">
|
||||
@@ -153,7 +204,8 @@ export default async function HomePage() {
|
||||
</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
))}
|
||||
))
|
||||
: null}
|
||||
|
||||
{/* Community Teaser */}
|
||||
<Card className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60">
|
||||
@@ -174,7 +226,8 @@ export default async function HomePage() {
|
||||
</Card>
|
||||
|
||||
{/* Shops */}
|
||||
{shops.map((shop) => (
|
||||
{!loading && !loadingError
|
||||
? shops.map((shop) => (
|
||||
<Card
|
||||
key={shop.id}
|
||||
className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60"
|
||||
@@ -202,7 +255,8 @@ export default async function HomePage() {
|
||||
</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
))}
|
||||
))
|
||||
: null}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user