import React, { useEffect, useMemo, useState } from "react"; import { FormattedMessage, useIntl } from "react-intl"; import { useNavigate, useParams } from "react-router-dom"; import UnreadCount from "@/Pages/Messages/UnreadCount"; import ProfileImage from "@/Element/User/ProfileImage"; import { parseId } from "@/SnortUtils"; import NoteToSelf from "@/Element/User/NoteToSelf"; import useLogin from "@/Hooks/useLogin"; import usePageWidth from "@/Hooks/usePageWidth"; import NoteTime from "@/Element/Event/NoteTime"; import DmWindow from "@/Pages/Messages/DmWindow"; import { Chat, ChatType, useChatSystem } from "@/chat"; import { ChatParticipantProfile } from "@/Pages/Messages/ChatParticipant"; import classNames from "classnames"; import NewChatWindow from "@/Pages/Messages/NewChatWindow"; const TwoCol = 768; export default function MessagesPage() { const login = useLogin(); const { formatMessage } = useIntl(); const navigate = useNavigate(); const { id } = useParams(); const [chat, setChat] = useState(); const pageWidth = usePageWidth(); useEffect(() => { const parsedId = parseId(id ?? ""); setChat(id ? parsedId : undefined); }, [id]); const chats = useChatSystem(); const unreadCount = useMemo(() => chats.reduce((p, c) => p + c.unread, 0), [chats]); function openChat(e: React.MouseEvent, type: ChatType, id: string) { e.stopPropagation(); e.preventDefault(); navigate(`/messages/${encodeURIComponent(id)}`); } function noteToSelf(chat: Chat) { return (
openChat(e, chat.type, chat.id)}>
); } function conversationIdent(cx: Chat) { if (cx.participants.length === 1) { return ; } else { return (
{cx.participants.map(v => ( ))} {cx.title ?? }
); } } function conversation(cx: Chat) { if (!login.publicKey) return null; const participants = cx.participants.map(a => a.id); if (participants.length === 1 && participants[0] === login.publicKey) return noteToSelf(cx); const isActive = cx.id === chat; return (
openChat(e, cx.type, cx.id)}> {conversationIdent(cx)}
{cx.unread > 0 && }
); } return (
{(pageWidth >= TwoCol || !chat) && (
{chats .sort((a, b) => { const aSelf = a.participants.length === 1 && a.participants[0].id === login.publicKey; const bSelf = b.participants.length === 1 && b.participants[0].id === login.publicKey; if (aSelf || bSelf) { return aSelf ? -1 : 1; } return b.lastMessage > a.lastMessage ? 1 : -1; }) .map(conversation)}
)} {chat ? : pageWidth >= TwoCol &&
}
); }