mirror of
https://github.com/lorsanstand/Aether.git
synced 2026-06-19 12:05:16 +03:00
front fix
This commit is contained in:
@@ -18,9 +18,10 @@ export default function ChatPage() {
|
|||||||
const cachedChats = useChatStore((state) => state.chats);
|
const cachedChats = useChatStore((state) => state.chats);
|
||||||
const setChatsCache = useChatStore((state) => state.setChats);
|
const setChatsCache = useChatStore((state) => state.setChats);
|
||||||
const updateChatCache = useChatStore((state) => state.updateChat);
|
const updateChatCache = useChatStore((state) => state.updateChat);
|
||||||
|
const safeCachedChats = Array.isArray(cachedChats) ? cachedChats : [];
|
||||||
|
|
||||||
const [chats, setChats] = useState<Chat[]>(cachedChats); // Initialize with cached data
|
const [chats, setChats] = useState<Chat[]>(safeCachedChats); // Initialize with cached data
|
||||||
const [loading, setLoading] = useState(cachedChats.length === 0); // Don't show loading if we have cache
|
const [loading, setLoading] = useState(safeCachedChats.length === 0); // Don't show loading if we have cache
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
// Selected chat state
|
// Selected chat state
|
||||||
@@ -68,7 +69,7 @@ export default function ChatPage() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadChats = async () => {
|
const loadChats = async () => {
|
||||||
try {
|
try {
|
||||||
if (cachedChats.length === 0) {
|
if (safeCachedChats.length === 0) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
}
|
}
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -93,7 +94,7 @@ export default function ChatPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
loadChats();
|
loadChats();
|
||||||
}, [chatId, setChatsCache]);
|
}, [chatId, safeCachedChats.length, setChatsCache]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedChat) {
|
if (selectedChat) {
|
||||||
|
|||||||
@@ -1,5 +1,22 @@
|
|||||||
import apiClient from './api';
|
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 = {
|
export type Chat = {
|
||||||
chat_id: string;
|
chat_id: string;
|
||||||
user_id: number;
|
user_id: number;
|
||||||
@@ -34,14 +51,14 @@ const chatService = {
|
|||||||
const response = await apiClient.get('/chats/', {
|
const response = await apiClient.get('/chats/', {
|
||||||
params: { offset, limit }
|
params: { offset, limit }
|
||||||
});
|
});
|
||||||
return response.data;
|
return extractArray<Chat>(response.data);
|
||||||
},
|
},
|
||||||
|
|
||||||
async getChatMessages(chatId: string, offset: number = 0, limit: number = 50): Promise<Message[]> {
|
async getChatMessages(chatId: string, offset: number = 0, limit: number = 50): Promise<Message[]> {
|
||||||
const response = await apiClient.get(`/chats/${chatId}`, {
|
const response = await apiClient.get(`/chats/${chatId}`, {
|
||||||
params: { offset, limit }
|
params: { offset, limit }
|
||||||
});
|
});
|
||||||
return response.data;
|
return extractArray<Message>(response.data);
|
||||||
},
|
},
|
||||||
|
|
||||||
async sendMessage(data: MessageCreate): Promise<Message> {
|
async sendMessage(data: MessageCreate): Promise<Message> {
|
||||||
|
|||||||
@@ -1,5 +1,22 @@
|
|||||||
import apiClient from './api';
|
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 {
|
export interface User {
|
||||||
id: number;
|
id: number;
|
||||||
display_name: string;
|
display_name: string;
|
||||||
@@ -60,7 +77,7 @@ export const userService = {
|
|||||||
const response = await apiClient.get('/users/search', {
|
const response = await apiClient.get('/users/search', {
|
||||||
params: { username, offset, limit }
|
params: { username, offset, limit }
|
||||||
});
|
});
|
||||||
return response.data;
|
return extractUsersArray(response.data);
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteAccount: async (): Promise<void> => {
|
deleteAccount: async (): Promise<void> => {
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ export const useChatStore = create<ChatStore>()(
|
|||||||
(set) => ({
|
(set) => ({
|
||||||
chats: [],
|
chats: [],
|
||||||
|
|
||||||
setChats: (chats) => set({ chats }),
|
setChats: (chats) => set({ chats: Array.isArray(chats) ? chats : [] }),
|
||||||
|
|
||||||
updateChat: (chatId, updates) =>
|
updateChat: (chatId, updates) =>
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
chats: state.chats.map(chat =>
|
chats: (Array.isArray(state.chats) ? state.chats : []).map(chat =>
|
||||||
chat.chat_id === chatId ? { ...chat, ...updates } : chat
|
chat.chat_id === chatId ? { ...chat, ...updates } : chat
|
||||||
)
|
)
|
||||||
})),
|
})),
|
||||||
|
|||||||
Reference in New Issue
Block a user