import { create } from 'zustand'; import { io, Socket } from 'socket.io-client'; import { useAuthStore } from './auth'; interface Message { id: string; conversationId: string; direction: 'inbound' | 'outbound'; content: string; contentType: 'text' | 'image' | 'product' | 'order'; senderType: 'customer' | 'agent' | 'system' | 'ai'; senderId: string | null; senderName: string | null; aiGenerated: boolean; createdAt: string; } interface Conversation { id: string; customerId: string; customerName: string | null; customerAvatar: string | null; status: 'active' | 'waiting' | 'closed' | 'transferred'; lastMessage: string | null; lastMessageAt: string | null; unreadCount: number; platform: { id: string; name: string; displayName: string; }; } interface SocketState { socket: Socket | null; connected: boolean; currentConversationId: string | null; messages: Message[]; conversations: Conversation[]; typing: { [conversationId: string]: { userId: string; username: string } }; connect: () => void; disconnect: () => void; joinConversation: (conversationId: string) => void; leaveConversation: (conversationId: string) => void; sendMessage: (conversationId: string, content: string) => void; addMessage: (message: Message) => void; setConversations: (conversations: Conversation[]) => void; addConversation: (conversation: Conversation) => void; updateConversation: (conversationId: string, updates: Partial) => void; clearMessages: () => void; } export const useSocketStore = create((set, get) => ({ socket: null, connected: false, currentConversationId: null, messages: [], conversations: [], typing: {}, connect: () => { const { token } = useAuthStore.getState(); if (!token) return; const socket = io('/', { auth: { token }, transports: ['websocket', 'polling'], }); socket.on('connect', () => { set({ connected: true }); console.log('Socket connected'); }); socket.on('disconnect', () => { set({ connected: false }); console.log('Socket disconnected'); }); socket.on('message:new', (message: Message) => { get().addMessage(message); }); socket.on('conversation:new', (conversation: Conversation) => { get().addConversation(conversation); }); socket.on('user:typing', ({ conversationId, userId, username }) => { set((state) => ({ typing: { ...state.typing, [conversationId]: { userId, username }, }, })); }); socket.on('transfer:required', ({ conversationId, transferTo }) => { // 处理转接通知 console.log('Transfer required:', conversationId, transferTo); }); set({ socket }); }, disconnect: () => { const { socket } = get(); if (socket) { socket.disconnect(); set({ socket: null, connected: false }); } }, joinConversation: (conversationId) => { const { socket } = get(); if (socket) { socket.emit('conversation:join', conversationId); set({ currentConversationId: conversationId, messages: [] }); } }, leaveConversation: (conversationId) => { const { socket } = get(); if (socket) { socket.emit('conversation:leave', conversationId); set({ currentConversationId: null, messages: [] }); } }, sendMessage: (conversationId, content) => { const { socket } = get(); if (socket) { socket.emit('message:send', { conversationId, content, contentType: 'text', }); } }, addMessage: (message) => { set((state) => ({ messages: [...state.messages, message], })); }, setConversations: (conversations) => { set({ conversations }); }, addConversation: (conversation) => { set((state) => ({ conversations: [conversation, ...state.conversations], })); }, updateConversation: (conversationId, updates) => { set((state) => ({ conversations: state.conversations.map((c) => c.id === conversationId ? { ...c, ...updates } : c ), })); }, clearMessages: () => { set({ messages: [] }); }, }));