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 { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { Badge } from "@/components/ui/badge"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
import { IconInput } from "@/components/ui/icon-input"
|
import { IconInput } from "@/components/ui/icon-input"
|
||||||
import { listGames, listPlayers, listShops } from "@/lib/api"
|
import { listGames, listPlayers, listShops } from "@/lib/api"
|
||||||
|
import { toApiError } from "@/lib/errors"
|
||||||
import { GameIcon } from "@/lib/game-icons"
|
import { GameIcon } from "@/lib/game-icons"
|
||||||
import { ArrowRight, Search, ShoppingBag, Star } from "lucide-react"
|
import { ArrowRight, Search, ShoppingBag, Star } from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
|
import { useEffect, useState } from "react"
|
||||||
|
|
||||||
export default async function HomePage() {
|
export default function HomePage() {
|
||||||
const [games, players, shops] = await Promise.all([listGames(), listPlayers(), listShops()])
|
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 (
|
return (
|
||||||
<div className="container mx-auto py-8 px-4 space-y-12">
|
<div className="container mx-auto py-8 px-4 space-y-12">
|
||||||
@@ -42,6 +81,11 @@ export default async function HomePage() {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</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>
|
<section>
|
||||||
<div className="flex items-center justify-between mb-4">
|
<div className="flex items-center justify-between mb-4">
|
||||||
<h2 className="text-xl font-semibold">游戏分类</h2>
|
<h2 className="text-xl font-semibold">游戏分类</h2>
|
||||||
@@ -57,14 +101,16 @@ export default async function HomePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-wrap gap-3">
|
<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>
|
<Button key={game.id} variant="secondary" className="rounded-full" asChild>
|
||||||
<Link href={`/search?game=${encodeURIComponent(game.name)}`}>
|
<Link href={`/search?game=${encodeURIComponent(game.name)}`}>
|
||||||
<GameIcon name={game.icon} className="h-5 w-5" />
|
<GameIcon name={game.icon} className="h-5 w-5" />
|
||||||
{game.name}
|
{game.name}
|
||||||
</Link>
|
</Link>
|
||||||
</Button>
|
</Button>
|
||||||
))}
|
))
|
||||||
|
: null}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -92,7 +138,8 @@ export default async function HomePage() {
|
|||||||
|
|
||||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||||
{/* Players */}
|
{/* Players */}
|
||||||
{players.map((player) => (
|
{!loading && !loadingError
|
||||||
|
? players.map((player) => (
|
||||||
<Card
|
<Card
|
||||||
key={player.id}
|
key={player.id}
|
||||||
className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60"
|
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" />
|
<Star className="h-3.5 w-3.5 fill-primary text-primary mr-0.5" />
|
||||||
{player.rating}
|
{player.rating}
|
||||||
</div>
|
</div>
|
||||||
<span className="text-xs text-muted-foreground">{player.totalOrders} 单</span>
|
<span className="text-xs text-muted-foreground">
|
||||||
|
{player.totalOrders} 单
|
||||||
|
</span>
|
||||||
<Badge
|
<Badge
|
||||||
variant={player.status === "available" ? "default" : "secondary"}
|
variant={player.status === "available" ? "default" : "secondary"}
|
||||||
className="text-[10px] px-1.5 py-0 font-normal"
|
className="text-[10px] px-1.5 py-0 font-normal"
|
||||||
@@ -136,7 +185,9 @@ export default async function HomePage() {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
{player.shopName && (
|
{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>
|
</CardContent>
|
||||||
<CardFooter className="pt-0 flex items-end justify-between">
|
<CardFooter className="pt-0 flex items-end justify-between">
|
||||||
@@ -153,7 +204,8 @@ export default async function HomePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
))}
|
))
|
||||||
|
: null}
|
||||||
|
|
||||||
{/* Community Teaser */}
|
{/* Community Teaser */}
|
||||||
<Card className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60">
|
<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>
|
</Card>
|
||||||
|
|
||||||
{/* Shops */}
|
{/* Shops */}
|
||||||
{shops.map((shop) => (
|
{!loading && !loadingError
|
||||||
|
? shops.map((shop) => (
|
||||||
<Card
|
<Card
|
||||||
key={shop.id}
|
key={shop.id}
|
||||||
className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60"
|
className="hover:shadow-md transition-shadow flex flex-col h-full border-muted/60"
|
||||||
@@ -202,7 +255,8 @@ export default async function HomePage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
))}
|
))
|
||||||
|
: null}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user