Files
juwan-frontend/app/(order)/review/[id]/page.tsx
T

167 lines
6.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
import { ArrowLeft, Lock, Star } from "lucide-react"
import Link from "next/link"
import { use, useMemo, useState } from "react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { submitReview } from "@/lib/api/reviews"
import { notifyInfo, notifySuccess } from "@/lib/toast"
import { useAuthStore } from "@/store/auth"
import { useOrderStore } from "@/store/orders"
import { useReviewStore } from "@/store/reviews"
export default function ReviewPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = use(params)
const order = useOrderStore((state) => state.orders.find((item) => item.id === id))
const userId = useAuthStore((state) => state.user?.id)
const allReviews = useReviewStore((state) => state.reviews)
// The selector returns the raw store array and useMemo derives the subset.
// This keeps useSyncExternalStore snapshots stable across render checks.
// Inline filter inside the selector creates a new array reference each call
// and can cause infinite re-render loops in Zustand v5 (pmndrs/zustand#3155).
const reviews = useMemo(() => allReviews.filter((item) => item.orderId === id), [allReviews, id])
const [rating, setRating] = useState(0)
const [hoverRating, setHoverRating] = useState(0)
const [content, setContent] = useState("")
if (!order) {
return (
<div className="container mx-auto py-8 px-4 text-center text-muted-foreground">
</div>
)
}
const hasSubmitted = Boolean(userId && reviews.some((review) => review.fromUserId === userId))
const isRevealed = reviews.length >= 2 && reviews.every((review) => !review.sealed)
if (order.status !== "pending_review") {
return (
<div className="container mx-auto py-8 px-4 max-w-lg text-center space-y-4">
<h2 className="text-xl font-bold"></h2>
<p className="text-sm text-muted-foreground"></p>
<Link href={`/order/${id}`} className="text-sm text-primary hover:underline">
</Link>
</div>
)
}
if (hasSubmitted && !isRevealed) {
return (
<div className="container mx-auto py-8 px-4 max-w-lg text-center space-y-4">
<Lock className="h-12 w-12 mx-auto text-muted-foreground" />
<h2 className="text-xl font-bold"></h2>
<p className="text-sm text-muted-foreground"></p>
<p className="text-sm text-muted-foreground"></p>
<Link href={`/order/${id}`} className="text-sm text-primary hover:underline">
</Link>
</div>
)
}
if (hasSubmitted && isRevealed) {
return (
<div className="container mx-auto py-8 px-4 max-w-lg text-center space-y-4">
<h2 className="text-xl font-bold"></h2>
<p className="text-sm text-muted-foreground"></p>
<Link href={`/order/${id}`} className="text-sm text-primary hover:underline">
</Link>
</div>
)
}
return (
<div className="container mx-auto py-8 px-4 max-w-lg">
<Link
href={`/order/${id}`}
className="inline-flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground mb-4"
>
<ArrowLeft className="h-4 w-4" />
</Link>
<Card>
<CardHeader>
<CardTitle></CardTitle>
<p className="text-sm text-muted-foreground">
{order.service.title} · {order.playerName}
</p>
</CardHeader>
<CardContent className="space-y-6">
<div className="space-y-2">
<Label></Label>
<div className="flex gap-1">
{[1, 2, 3, 4, 5].map((star) => (
<button
key={star}
type="button"
onClick={() => setRating(star)}
onMouseEnter={() => setHoverRating(star)}
onMouseLeave={() => setHoverRating(0)}
className="p-0.5"
>
<Star
className={`h-8 w-8 transition-colors ${
star <= (hoverRating || rating)
? "fill-yellow-400 text-yellow-400"
: "text-muted"
}`}
/>
</button>
))}
</div>
</div>
<div className="space-y-2">
<Label htmlFor="review-content"></Label>
<Textarea
id="review-content"
placeholder="分享你的体验..."
value={content}
onChange={(e) => setContent(e.target.value)}
rows={4}
/>
</div>
<div className="rounded-md bg-muted/50 p-3 text-xs text-muted-foreground flex items-start gap-2">
<Lock className="h-4 w-4 shrink-0 mt-0.5" />
<span></span>
</div>
<Button
className="w-full"
disabled={rating === 0 || !userId}
onClick={() => {
if (!userId) {
notifyInfo("请先登录")
return
}
const decision = submitReview({
orderId: id,
rating,
content,
})
if (decision.ok) {
notifySuccess("评价已提交")
return
}
notifyInfo(decision.message ?? "评价提交失败")
}}
>
</Button>
</CardContent>
</Card>
</div>
)
}