mirror of
https://github.com/lorsanstand/Aether.git
synced 2026-06-19 12:05:16 +03:00
@@ -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<Chat[]>(cachedChats); // Initialize with cached data
|
||||
const [loading, setLoading] = useState(cachedChats.length === 0); // Don't show loading if we have cache
|
||||
const [chats, setChats] = useState<Chat[]>(safeCachedChats); // Initialize with cached data
|
||||
const [loading, setLoading] = useState(safeCachedChats.length === 0); // Don't show loading if we have cache
|
||||
const [error, setError] = useState<string | null>(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) {
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
import apiClient from './api';
|
||||
|
||||
const extractArray = <T>(payload: unknown): T[] => {
|
||||
if (Array.isArray(payload)) {
|
||||
return payload as T[];
|
||||
}
|
||||
|
||||
if (payload && typeof payload === 'object') {
|
||||
const record = payload as Record<string, unknown>;
|
||||
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<Chat>(response.data);
|
||||
},
|
||||
|
||||
async getChatMessages(chatId: string, offset: number = 0, limit: number = 50): Promise<Message[]> {
|
||||
const response = await apiClient.get(`/chats/${chatId}`, {
|
||||
params: { offset, limit }
|
||||
});
|
||||
return response.data;
|
||||
return extractArray<Message>(response.data);
|
||||
},
|
||||
|
||||
async sendMessage(data: MessageCreate): Promise<Message> {
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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<void> => {
|
||||
|
||||
@@ -14,11 +14,11 @@ export const useChatStore = create<ChatStore>()(
|
||||
(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
|
||||
)
|
||||
})),
|
||||
|
||||
Reference in New Issue
Block a user