"use client" import { Button } from "@/components/ui/button" import { getShopById } from "@/lib/api" 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 { AlertTriangle, CheckCircle2, Clock, MessageSquare, RefreshCw, Star, XCircle, } from "lucide-react" import Link from "next/link" import { useCallback, useEffect, useState } from "react" interface OrderActionsProps { orderId: string order?: Order onOrderChange?: (order: Order) => void initialStatus: OrderStatus chatTargetId?: string isPlayerParticipant?: boolean serviceId: string } function showFeedback(message: string) { notifySuccess(message) } export default function OrderActions({ orderId, order: orderProp, onOrderChange, initialStatus, chatTargetId, isPlayerParticipant, serviceId, }: OrderActionsProps) { const currentUserId = useAuthStore((state) => state.user?.id) const order = orderProp const [dispatchMode, setDispatchMode] = useState<"manual" | "auto" | null>(null) const resolvedChatTargetId = chatTargetId useEffect(() => { if (!order?.shopId) return let cancelled = false Promise.resolve() .then(() => { if (cancelled) return undefined setDispatchMode(null) return getShopById(order.shopId ?? "") }) .then((shop) => { if (cancelled || !shop) return setDispatchMode(shop.dispatchMode) }) .catch(() => { if (cancelled) return setDispatchMode(null) }) return () => { cancelled = true } }, [order?.shopId]) const status = order?.status ?? initialStatus const isConsumer = String(order?.consumerId) === currentUserId const isPlayer = isPlayerParticipant === true const isParticipant = isConsumer || isPlayer 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 !== "manual" ? ( ) : ( ))} )} {(status === "in_progress" || status === "pending_close") && resolvedChatTargetId && ( )} {status === "in_progress" && ( <> {isPlayer && ( )} {isParticipant && ( )} )} {status === "pending_close" && ( <> {isConsumer && ( )} {isParticipant && ( )} )} {status === "pending_review" && isParticipant && ( )} {status === "completed" && ( )} {status === "cancelled" && ( )} {status === "disputed" && isParticipant && ( )}
) }