"use client" import { ArrowLeft, Lock, Send } from "lucide-react" import Link from "next/link" import { use, useState } from "react" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" import { cn } from "@/lib/utils" import { useAuthStore } from "@/store/auth" import { useChatStore } from "@/store/chat" export default function ChatDetailPage({ params }: { params: Promise<{ id: string }> }) { const { id } = use(params) const session = useChatStore((state) => state.sessions.find((item) => item.id === id)) const messages = useChatStore((state) => state.messages.filter((item) => item.sessionId === id)) const sendTextMessage = useChatStore((state) => state.sendTextMessage) const [input, setInput] = useState("") const { user } = useAuthStore() if (!session) { return (
会话不存在
) } const currentUserId = user?.id ?? session.participants[0].id const other = session.participants.find((p) => p.id !== currentUserId) ?? session.participants[1] return (
{other.name[0]}
{other.name}
{session.type === "order" ? "订单会话" : "咨询会话"} {session.readonly && ( 只读 )}
{messages.map((msg) => { if (msg.type === "system") { return (
{msg.content}
) } const isMine = msg.senderId === currentUserId return (
{msg.senderName[0]}

{msg.content}

{new Date(msg.createdAt).toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit", })}

) })}
{!session.readonly ? (
{ e.preventDefault() const text = input.trim() if (!text) return const sender = session.participants.find( (participant) => participant.id === currentUserId, ) sendTextMessage( session.id, { id: currentUserId, name: sender?.name ?? user?.nickname ?? "", avatar: sender?.avatar ?? user?.avatar ?? "", }, text, ) setInput("") }} > setInput(e.target.value)} placeholder="输入消息..." className="flex-1" />
) : (
订单已关闭,会话为只读状态
)}
) }