192 lines
8.7 KiB
JavaScript
192 lines
8.7 KiB
JavaScript
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import { useLocation, useNavigate, useParams } from "react-router-dom";
|
||
import { useTranslation } from "react-i18next";
|
||
import Sidebar from "../Sidebar";
|
||
import ChatListItem from "../chat/ChatListItem";
|
||
import Toast from "../Toast";
|
||
import AuthModal from "../AuthModal";
|
||
import { ProfileModal } from "../profile";
|
||
import RulesModal from "../../pages/RulesModal";
|
||
import ChatPage from "../../pages/ChatPage";
|
||
import { DesktopIcons } from "./DesktopIcons";
|
||
import { useChatList } from "../../hooks/useChatList";
|
||
import { useProfileData } from "../../hooks/useProfileData";
|
||
import { useAuthContext } from "../../context/AuthContext";
|
||
import { getPostForChat } from "../../stores/chatPostStore";
|
||
import { getChatDisplayInfo } from "../../utils/chatDisplay";
|
||
|
||
/**
|
||
* DesktopChats — двухпанельный мессенджер десктопа: список чатов слева,
|
||
* открытая переписка справа. Один компонент обслуживает /chats, /chats/new
|
||
* и /chats/:chatId, поэтому список не перезагружается при переключении чатов.
|
||
*/
|
||
export default function DesktopChats() {
|
||
const { t } = useTranslation();
|
||
const navigate = useNavigate();
|
||
const location = useLocation();
|
||
const { chatId } = useParams();
|
||
const isDraft = location.pathname === "/chats/new";
|
||
const { user } = useAuthContext();
|
||
const profileData = useProfileData(user);
|
||
const {
|
||
chats, loading, fetchChats, unreadMap, isAuthenticated,
|
||
handleDeleteChat, deleteError, clearDeleteError,
|
||
} = useChatList();
|
||
const [query, setQuery] = useState("");
|
||
const [modal, setModal] = useState(null);
|
||
|
||
// Новое сообщение в открытом чате → обновляем превью в списке
|
||
// (с небольшой задержкой, чтобы сервер успел сохранить сообщение).
|
||
const refreshTimerRef = useRef(null);
|
||
const refreshList = useCallback(() => {
|
||
clearTimeout(refreshTimerRef.current);
|
||
refreshTimerRef.current = setTimeout(() => fetchChats({ silent: true }), 700);
|
||
}, [fetchChats]);
|
||
useEffect(() => () => clearTimeout(refreshTimerRef.current), []);
|
||
|
||
const items = useMemo(() => {
|
||
const q = query.trim().toLowerCase();
|
||
return chats
|
||
.map((chat) => {
|
||
const post = getPostForChat(chat.id);
|
||
const info = getChatDisplayInfo(chat, post, unreadMap, t);
|
||
return { chat, post, info };
|
||
})
|
||
.filter(({ info }) => !q
|
||
|| String(info.title || "").toLowerCase().includes(q)
|
||
|| String(info.preview || "").toLowerCase().includes(q));
|
||
}, [chats, unreadMap, query, t]);
|
||
|
||
const onDelete = async (id) => {
|
||
const deleted = await handleDeleteChat(id);
|
||
if (deleted && String(id) === String(chatId)) navigate("/chats", { replace: true });
|
||
};
|
||
|
||
const goFeed = (state) => navigate("/feed", state ? { state } : undefined);
|
||
|
||
return (
|
||
<div className="f-app fd-chats-app">
|
||
<Sidebar
|
||
open={false}
|
||
activeNav="chat"
|
||
onClose={() => {}}
|
||
onNavClick={(key) => goFeed({ desktopNav: key })}
|
||
isAuthenticated={isAuthenticated}
|
||
onLoginClick={() => setModal("auth")}
|
||
onRulesClick={() => setModal("rules")}
|
||
onReviewsClick={() => navigate("/reviews")}
|
||
onCreatePost={() => (isAuthenticated ? goFeed({ desktopAction: "create" }) : setModal("auth"))}
|
||
onProfileClick={() => setModal("profile")}
|
||
onMapClick={() => navigate("/feed?view=map")}
|
||
profileData={profileData}
|
||
/>
|
||
|
||
<div className="fd-chats">
|
||
<section className="fd-chats-list" aria-label={t("chat.chats")}>
|
||
<div className="fd-chats-list-head">
|
||
<h1 className="fd-chats-title">
|
||
{t("chat.chats")}
|
||
{chats.length > 0 && <span className="fd-chats-count">{chats.length}</span>}
|
||
</h1>
|
||
{isAuthenticated && chats.length > 0 && (
|
||
<label className="fd-chats-search">
|
||
<DesktopIcons.Search size={17} />
|
||
<input
|
||
type="search"
|
||
value={query}
|
||
onChange={(e) => setQuery(e.target.value)}
|
||
placeholder={t("desktop.chats.search")}
|
||
aria-label={t("desktop.chats.search")}
|
||
/>
|
||
</label>
|
||
)}
|
||
</div>
|
||
|
||
<div className="fd-chats-scroll">
|
||
{!isAuthenticated ? (
|
||
<div className="fd-empty fd-empty-sm">
|
||
<div className="fd-empty-sub">{t("chat.loginToSee")}</div>
|
||
</div>
|
||
) : loading ? (
|
||
Array.from({ length: 7 }, (_, i) => (
|
||
<div key={i} className="fd-chat-skeleton" aria-hidden="true">
|
||
<span className="fd-skeleton fd-chat-skeleton-avatar" />
|
||
<span className="fd-chat-skeleton-lines">
|
||
<span className="fd-skeleton fd-skeleton-line" />
|
||
<span className="fd-skeleton fd-skeleton-line fd-skeleton-line-short" />
|
||
</span>
|
||
</div>
|
||
))
|
||
) : chats.length === 0 && !isDraft ? (
|
||
<div className="fd-empty fd-empty-sm">
|
||
<div className="fd-empty-icon"><DesktopIcons.Message size={24} /></div>
|
||
<div className="fd-empty-title">{t("chat.noChats")}</div>
|
||
</div>
|
||
) : items.length === 0 && !isDraft ? (
|
||
<div className="fd-empty fd-empty-sm">
|
||
<div className="fd-empty-sub">{t("desktop.chats.nothingFound")}</div>
|
||
</div>
|
||
) : (
|
||
<>
|
||
{isDraft && (
|
||
<div className="fd-chat-row is-active">
|
||
<div className="chat-list-item fd-chat-draft">
|
||
<div className="chat-list-avatar"><DesktopIcons.Plus size={18} /></div>
|
||
<div className="chat-list-info">
|
||
<div className="chat-list-name">{location.state?.post?.title || t("desktop.chats.newChat")}</div>
|
||
<div className="chat-list-preview">{t("desktop.chats.newChat")}</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
{items.map(({ chat, post }) => (
|
||
<div
|
||
key={chat.id}
|
||
className={`fd-chat-row${String(chat.id) === String(chatId) ? " is-active" : ""}`}
|
||
>
|
||
<ChatListItem
|
||
chat={chat}
|
||
post={post}
|
||
unreadMap={unreadMap}
|
||
t={t}
|
||
onOpen={(id) => navigate(`/chats/${id}`)}
|
||
onDelete={onDelete}
|
||
/>
|
||
</div>
|
||
))}
|
||
</>
|
||
)}
|
||
</div>
|
||
</section>
|
||
|
||
<section className="fd-chats-main">
|
||
{!isAuthenticated ? (
|
||
<div className="fd-chats-placeholder">
|
||
<div className="fd-chats-placeholder-icon"><DesktopIcons.Message size={30} /></div>
|
||
<div className="fd-empty-title">{t("chat.loginToSee")}</div>
|
||
<button type="button" className="fd-btn fd-btn-primary" onClick={() => setModal("auth")}>
|
||
{t("feed.login")}
|
||
</button>
|
||
</div>
|
||
) : chatId || isDraft ? (
|
||
<ChatPage key={chatId || "new"} embedded onActivity={refreshList} />
|
||
) : (
|
||
<div className="fd-chats-placeholder">
|
||
<div className="fd-chats-placeholder-icon"><DesktopIcons.Message size={30} /></div>
|
||
<div className="fd-empty-title">{t("desktop.chats.selectTitle")}</div>
|
||
<div className="fd-empty-sub">{t("desktop.chats.selectSub")}</div>
|
||
</div>
|
||
)}
|
||
</section>
|
||
</div>
|
||
|
||
{deleteError && <Toast message={deleteError} type="error" onDismiss={clearDeleteError} />}
|
||
{modal === "auth" && <AuthModal onClose={() => setModal(null)} />}
|
||
{modal === "rules" && <RulesModal onClose={() => setModal(null)} />}
|
||
{modal === "profile" && isAuthenticated && (
|
||
<ProfileModal onClose={() => setModal(null)} initialData={profileData} />
|
||
)}
|
||
</div>
|
||
);
|
||
}
|