/* eslint-disable @typescript-eslint/no-explicit-any */ import { FileType } from "../components/common/FilePreview"; import { create } from "zustand"; import { ChatParticipant } from "../types/type"; /** Minimal shape needed to update the sidebar after a send */ export interface SentMessageSignal { _id: string; conversationId: string; message: string; senderId: string; media?: any[]; status: string; createdAt: string; updatedAt: string; } interface ChatUIState { isChatOpen: boolean; unreadCount: number; selectedConversation: { /** The API returns an array; older payloads sent a single object. */ participantDetails: ChatParticipant[] | ChatParticipant; unreadMessageIds?: string[]; lastMessage?: any; _id: string; type?: 'personal' | 'service'; bookingId?: string; title?: string; serviceId?: string; serviceTitle?: string; } | null; setSelectedConversation: ( selectedConversation: ChatUIState["selectedConversation"] ) => void; messages: { _id: string; text: string; conversationId?: string; sender: { senderId: string; role: string; }; status: string; isOptimistic: boolean; message: string; createdAt: string; media: { type: FileType; url: string; name: string; size: number; uploadProgress: number; uploadError: string }[]; isUploading: boolean; }[]; setMessages: (messages: ChatUIState["messages"] | ((prev: ChatUIState["messages"]) => ChatUIState["messages"])) => void; updateMessageStatus: (messageId: string, status: string) => void; toggleChat: () => void; onlineUsers: string[]; setOnlineUsers: (users: string[]) => void; searchTerm: string; setSearchTerm: (searchTerm: string) => void; /** Signal published by MessageInput after a successful send so the sidebar can update */ lastSentMessage: SentMessageSignal | null; setLastSentMessage: (msg: SentMessageSignal | null) => void; } const useChatUIStore = create((set) => ({ isChatOpen: false, unreadCount: 0, selectedConversation: null, messages: [], setMessages: (updater: any) => set((state) => ({ messages: typeof updater === "function" ? updater(state.messages) : updater, })), updateMessageStatus: (messageId, status) => set((state) => ({ messages: state.messages.map((msg) => msg._id === messageId ? { ...msg, status } : msg ), })), setSelectedConversation: (selectedConversation) => set({ selectedConversation }), toggleChat: () => set((state) => ({ isChatOpen: !state.isChatOpen })), onlineUsers: [], setOnlineUsers: (users) => set({ onlineUsers: users }), searchTerm: "", setSearchTerm: (searchTerm) => set({ searchTerm }), lastSentMessage: null, setLastSentMessage: (msg) => set({ lastSentMessage: msg }), })); export default useChatUIStore;