diff --git a/frontend/src/pages/ChatPage.tsx b/frontend/src/pages/ChatPage.tsx index 5693a2f..41e1df3 100644 --- a/frontend/src/pages/ChatPage.tsx +++ b/frontend/src/pages/ChatPage.tsx @@ -18,9 +18,10 @@ export default function ChatPage() { const cachedChats = useChatStore((state) => state.chats); const setChatsCache = useChatStore((state) => state.setChats); const updateChatCache = useChatStore((state) => state.updateChat); + const safeCachedChats = Array.isArray(cachedChats) ? cachedChats : []; - const [chats, setChats] = useState(cachedChats); // Initialize with cached data - const [loading, setLoading] = useState(cachedChats.length === 0); // Don't show loading if we have cache + const [chats, setChats] = useState(safeCachedChats); // Initialize with cached data + const [loading, setLoading] = useState(safeCachedChats.length === 0); // Don't show loading if we have cache const [error, setError] = useState(null); // Selected chat state @@ -68,7 +69,7 @@ export default function ChatPage() { useEffect(() => { const loadChats = async () => { try { - if (cachedChats.length === 0) { + if (safeCachedChats.length === 0) { setLoading(true); } setError(null); @@ -93,7 +94,7 @@ export default function ChatPage() { }; loadChats(); - }, [chatId, setChatsCache]); + }, [chatId, safeCachedChats.length, setChatsCache]); useEffect(() => { if (selectedChat) { diff --git a/frontend/src/services/chatService.ts b/frontend/src/services/chatService.ts index e62a039..e4afad4 100644 --- a/frontend/src/services/chatService.ts +++ b/frontend/src/services/chatService.ts @@ -1,5 +1,22 @@ import apiClient from './api'; +const extractArray = (payload: unknown): T[] => { + if (Array.isArray(payload)) { + return payload as T[]; + } + + if (payload && typeof payload === 'object') { + const record = payload as Record; + const candidates = [record.items, record.results, record.data, record.chats, record.messages]; + const firstArray = candidates.find(Array.isArray); + if (firstArray) { + return firstArray as T[]; + } + } + + return []; +}; + export type Chat = { chat_id: string; user_id: number; @@ -34,14 +51,14 @@ const chatService = { const response = await apiClient.get('/chats/', { params: { offset, limit } }); - return response.data; + return extractArray(response.data); }, async getChatMessages(chatId: string, offset: number = 0, limit: number = 50): Promise { const response = await apiClient.get(`/chats/${chatId}`, { params: { offset, limit } }); - return response.data; + return extractArray(response.data); }, async sendMessage(data: MessageCreate): Promise { diff --git a/frontend/src/services/userService.ts b/frontend/src/services/userService.ts index 384dfa9..3dc6abc 100644 --- a/frontend/src/services/userService.ts +++ b/frontend/src/services/userService.ts @@ -1,5 +1,22 @@ import apiClient from './api'; +const extractUsersArray = (payload: unknown): User[] => { + if (Array.isArray(payload)) { + return payload as User[]; + } + + if (payload && typeof payload === 'object') { + const record = payload as Record; + const candidates = [record.items, record.results, record.data, record.users]; + const firstArray = candidates.find(Array.isArray); + if (firstArray) { + return firstArray as User[]; + } + } + + return []; +}; + export interface User { id: number; display_name: string; @@ -60,7 +77,7 @@ export const userService = { const response = await apiClient.get('/users/search', { params: { username, offset, limit } }); - return response.data; + return extractUsersArray(response.data); }, deleteAccount: async (): Promise => { diff --git a/frontend/src/store/chatStore.ts b/frontend/src/store/chatStore.ts index 7b00f42..91b9638 100644 --- a/frontend/src/store/chatStore.ts +++ b/frontend/src/store/chatStore.ts @@ -14,11 +14,11 @@ export const useChatStore = create()( (set) => ({ chats: [], - setChats: (chats) => set({ chats }), + setChats: (chats) => set({ chats: Array.isArray(chats) ? chats : [] }), updateChat: (chatId, updates) => set((state) => ({ - chats: state.chats.map(chat => + chats: (Array.isArray(state.chats) ? state.chats : []).map(chat => chat.chat_id === chatId ? { ...chat, ...updates } : chat ) })),