fix(dashboard): scope owner and service views by resolved shop

This commit is contained in:
zetaloop
2026-02-22 17:14:52 +08:00
parent 1f2dc1434b
commit 1dfcd3927d
11 changed files with 269 additions and 70 deletions
+40 -29
View File
@@ -2,17 +2,25 @@
import { Clock, MessageSquare, RefreshCw } from "lucide-react"
import Link from "next/link"
import { useEffect, useState } from "react"
import { useState } from "react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { statusLabels } from "@/lib/constants"
import {
isActiveOrder,
isCompletedOrder,
isDisputedOrder,
isPendingDispatch,
} from "@/lib/domain/order-filters"
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
import type { OrderStatus, UserRole } from "@/lib/types"
import { cn } from "@/lib/utils"
import { useAuthStore } from "@/store/auth"
import { useChatStore } from "@/store/chat"
import { useOrderStore } from "@/store/orders"
import { useShopStore } from "@/store/shops"
const statusColors: Record<OrderStatus, string> = {
pending_payment: "bg-yellow-100 text-yellow-800",
@@ -51,47 +59,46 @@ const ownerTabs = [
export default function OrderListPage() {
const { currentRole, user } = useAuthStore()
const userId = user?.id ?? "u1"
const shops = useShopStore((state) => state.shops)
const ownerShop = resolveOwnerShop(user?.id, shops)
return <OrderListContent key={currentRole} currentRole={currentRole} userId={userId} />
return (
<OrderListContent
key={currentRole}
currentRole={currentRole}
userId={user?.id}
ownerShopId={ownerShop?.id}
/>
)
}
function OrderListContent({ currentRole, userId }: { currentRole: UserRole; userId: string }) {
function OrderListContent({
currentRole,
userId,
ownerShopId,
}: {
currentRole: UserRole
userId?: string
ownerShopId?: string
}) {
const [tab, setTab] = useState<TabFilter | "pending">("all")
const orders = useOrderStore((state) => state.orders)
const sessions = useChatStore((state) => state.sessions)
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
const ownerShopId = "shop1"
const tabs =
currentRole === "consumer" ? consumerTabs : currentRole === "player" ? playerTabs : ownerTabs
useEffect(() => {
orders.forEach((order) => {
if (order.status === "pending_payment" || order.status === "cancelled") return
ensureOrderSession(order)
})
}, [orders, ensureOrderSession])
const roleFiltered = orders.filter((order) => {
if (currentRole === "consumer") return order.consumerId === userId
if (currentRole === "player") return order.playerId === userId
return order.shopId === ownerShopId
if (currentRole === "consumer") return userId ? order.consumerId === userId : false
if (currentRole === "player") return userId ? order.playerId === userId : false
return ownerShopId ? order.shopId === ownerShopId : false
})
const filtered = roleFiltered.filter((order) => {
if (tab === "pending") return order.status === "pending_accept"
if (tab === "active") {
return [
"pending_payment",
"pending_accept",
"in_progress",
"pending_close",
"pending_review",
].includes(order.status)
}
if (tab === "completed") return order.status === "completed" || order.status === "cancelled"
if (tab === "disputed") return order.status === "disputed"
if (tab === "pending") return isPendingDispatch(order.status)
if (tab === "active") return isActiveOrder(order.status)
if (tab === "completed") return isCompletedOrder(order.status, { includeCancelled: true })
if (tab === "disputed") return isDisputedOrder(order.status)
return true
})
@@ -119,7 +126,11 @@ function OrderListContent({ currentRole, userId }: { currentRole: UserRole; user
<TabsContent value={tab} className="mt-4 space-y-3">
{filtered.length === 0 ? (
<div className="text-center py-12 text-muted-foreground"></div>
<Card>
<CardContent className="py-8 text-center text-sm text-muted-foreground">
</CardContent>
</Card>
) : (
filtered.map((order) => (
<Card key={order.id}>