import { useEffect, useCallback } from "react"; import { useNavigate } from "react-router-dom"; import { useChats } from "../hooks/useChats"; import { useAuthContext } from "../context/AuthContext"; import { useUnread } from "../context/UnreadContext"; import { getPostForChat } from "../stores/chatPostStore"; import { getImageUrl } from "../config/authConstants"; import { useDeleteChat } from "../hooks/useDeleteChat"; import TrashIcon from "../components/icons/TrashIcon"; import { useTranslation } from 'react-i18next'; export default function ChatListPage() { const { t } = useTranslation(); const navigate = useNavigate(); const { chats, loading, fetchChats } = useChats(); const { isAuthenticated } = useAuthContext(); const { updateFromChats, unreadMap } = useUnread(); const { deleteChat } = useDeleteChat(); useEffect(() => { if (isAuthenticated) { fetchChats(); } }, [isAuthenticated, fetchChats]); useEffect(() => { if (chats.length > 0) { updateFromChats(chats); } }, [chats, updateFromChats]); const handleDeleteChat = useCallback(async (chatId) => { if (!window.confirm(t('chat.confirmDeleteChat'))) return; const ok = await deleteChat(chatId); if (ok) fetchChats(); }, [t, deleteChat, fetchChats]); if (!isAuthenticated) { return (
{t('chat.chats')}
{t('chat.loginToSee')}
); } return (
{t('chat.chats')}
{loading && (
{t('chat.loading')}
)} {!loading && chats.length === 0 && (
{t('chat.noChats')}
)} {!loading && chats.map((chat) => { const post = getPostForChat(chat.id); const title = post?.title || chat.otherUser?.name || t('chat.user'); const imgSrc = getImageUrl(post?.images?.[0]); const emoji = post?.emoji; const unreadCount = unreadMap[chat.id] ?? chat.unreadCount ?? chat.unreadMessages ?? 0; return (
0 ? " unread" : ""}`} onClick={() => navigate(`/chats/${chat.id}`)} >
{imgSrc ? ( ) : ( emoji || chat.otherUser?.name?.[0] || "?" )}
{title}
{chat.lastMessage?.content || t('chat.noMessages')}
{unreadCount > 0 && (
{unreadCount > 99 ? "99+" : unreadCount}
)}
); })}
); }