From 6082abadf46f6f2b281fc4273bf7717405a8bb6d Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Thu, 23 Jan 2025 09:46:34 -0500 Subject: [PATCH] Optimize conversation list --- .../src/components/ConversationCard.tsx | 91 +++++++------------ .../src/components/ConversationsNavbar.tsx | 33 +++---- 2 files changed, 45 insertions(+), 79 deletions(-) diff --git a/apps/xmtp.chat/src/components/ConversationCard.tsx b/apps/xmtp.chat/src/components/ConversationCard.tsx index 1fd3ea5a..e8dcfb92 100644 --- a/apps/xmtp.chat/src/components/ConversationCard.tsx +++ b/apps/xmtp.chat/src/components/ConversationCard.tsx @@ -1,14 +1,7 @@ -import { Card, Flex, Stack, Text } from "@mantine/core"; -import { - SortDirection, - type Conversation, - type DecodedMessage, -} from "@xmtp/browser-sdk"; -import { formatDistanceToNow } from "date-fns"; +import { Box, Card, Flex, Stack, Text } from "@mantine/core"; +import { type Conversation } from "@xmtp/browser-sdk"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router"; -import { nsToDate } from "../helpers/date"; -import { useConversation } from "../hooks/useConversation"; import styles from "./ConversationCard.module.css"; export type ConversationCardProps = { @@ -20,21 +13,6 @@ export const ConversationCard: React.FC = ({ }) => { const [memberCount, setMemberCount] = useState(0); const navigate = useNavigate(); - const [messageCount, setMessageCount] = useState(0); - const [lastMessage, setLastMessage] = useState(null); - const { getMessages } = useConversation(conversation); - - useEffect(() => { - const loadMessages = async () => { - const messages = await getMessages({ - direction: SortDirection.Descending, - limit: 1n, - }); - setLastMessage(messages[0] ?? null); - setMessageCount(messages.length); - }; - void loadMessages(); - }, [conversation]); useEffect(() => { void conversation.members().then((members) => { @@ -43,42 +21,35 @@ export const ConversationCard: React.FC = ({ }, [conversation.id]); return ( - { - if (e.key === "Enter") { - void navigate(`/conversations/${conversation.id}`); - } - }} - onClick={() => void navigate(`/conversations/${conversation.id}`)} - style={{ cursor: "pointer" }} - classNames={{ root: styles.root }}> - - - - {conversation.name || "Untitled"} - - - - {memberCount} member{memberCount !== 1 ? "s" : ""}, {messageCount}{" "} - message{messageCount !== 1 ? "s" : ""} - - {lastMessage && ( - - Last message sent{" "} - {formatDistanceToNow(nsToDate(lastMessage.sentAtNs), { - addSuffix: true, - })} + + { + if (e.key === "Enter") { + void navigate(`/conversations/${conversation.id}`); + } + }} + onClick={() => void navigate(`/conversations/${conversation.id}`)} + style={{ cursor: "pointer" }} + classNames={{ root: styles.root }}> + + + + {conversation.name || "Untitled"} + + + + {memberCount} member{memberCount !== 1 ? "s" : ""} - )} - - + + + ); }; diff --git a/apps/xmtp.chat/src/components/ConversationsNavbar.tsx b/apps/xmtp.chat/src/components/ConversationsNavbar.tsx index fd57826c..e1c74eba 100644 --- a/apps/xmtp.chat/src/components/ConversationsNavbar.tsx +++ b/apps/xmtp.chat/src/components/ConversationsNavbar.tsx @@ -4,12 +4,12 @@ import { Button, Group, LoadingOverlay, - ScrollArea, Stack, Text, } from "@mantine/core"; import type { Conversation } from "@xmtp/browser-sdk"; -import { useEffect, useState } from "react"; +import { useState } from "react"; +import { Virtuoso } from "react-virtuoso"; import { useConversations } from "../hooks/useConversations"; import { ConversationCard } from "./ConversationCard"; @@ -17,14 +17,6 @@ export const ConversationsNavbar: React.FC = () => { const { list, loading, syncing } = useConversations(); const [conversations, setConversations] = useState([]); - useEffect(() => { - const loadConversations = async () => { - const conversations = await list(); - setConversations(conversations); - }; - void loadConversations(); - }, []); - const handleSync = async () => { const conversations = await list(undefined, true); setConversations(conversations); @@ -50,18 +42,21 @@ export const ConversationsNavbar: React.FC = () => { - + {conversations.length === 0 ? ( No conversations found ) : ( - - {conversations.map((conversation) => ( - - ))} - + ( + + )} + /> )}