"use client" import { Button } from "@/components/ui/button" import { acceptOrder, cancelPreAccept, confirmClose, payOrder, requestClose, } from "@/lib/api/orders" import type { ApiDecision } from "@/lib/errors" import { notifyInfo, notifySuccess } from "@/lib/toast" import type { Order, OrderStatus } from "@/lib/types" import { useAuthStore } from "@/store/auth" import { useChatStore } from "@/store/chat" import { useOrderStore } from "@/store/orders" import { useShopStore } from "@/store/shops" import { AlertTriangle, CheckCircle2, Clock, MessageSquare, RefreshCw, Star, XCircle, } from "lucide-react" import Link from "next/link" import { useCallback } from "react" interface OrderActionsProps { orderId: string order?: Order onOrderChange?: (order: Order) => void initialStatus: OrderStatus chatSessionId?: string serviceId: string } function showFeedback(message: string) { notifySuccess(message) } export default function OrderActions({ orderId, order: orderProp, onOrderChange, initialStatus, chatSessionId, serviceId, }: OrderActionsProps) { const currentUserId = useAuthStore((state) => state.user?.id) const storeOrder = useOrderStore((state) => state.orders.find((item) => item.id === orderId)) const order = orderProp ?? storeOrder const sessions = useChatStore((state) => state.sessions) const dispatchMode = useShopStore((state) => { if (!order?.shopId) return "manual" const shop = state.shops.find((item) => item.id === order.shopId) return shop?.dispatchMode ?? "manual" }) const resolvedChatSessionId = chatSessionId ?? sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id const status = order?.status ?? initialStatus const isConsumer = order?.consumerId === currentUserId const isPlayer = order?.playerId === currentUserId type ActionResult = { decision: ApiDecision; order?: Order } const handleDecision = useCallback((okMessage: string, result: ActionResult) => { if (result.decision.ok) { showFeedback(okMessage) return } notifyInfo(result.decision.error.msg) }, []) const runAction = useCallback( (okMessage: string, actionCall: ActionResult | Promise) => { Promise.resolve(actionCall) .then((result) => { handleDecision(okMessage, result) if (result.order) onOrderChange?.(result.order) }) .catch(() => { notifyInfo("操作失败") }) }, [handleDecision, onOrderChange], ) return (
{status === "pending_payment" && isConsumer && ( <> )} {status === "pending_accept" && ( <> {isConsumer && ( )} {isPlayer && (order?.shopId && dispatchMode === "auto" ? ( ) : ( ))} )} {(status === "in_progress" || status === "pending_close") && resolvedChatSessionId && ( )} {status === "in_progress" && ( <> )} {status === "pending_close" && ( <> {isConsumer && ( )} )} {status === "pending_review" && ( )} {status === "completed" && ( )} {status === "cancelled" && ( )} {status === "disputed" && ( )}
) }