diff --git a/src/components/dm/DMChatArea.tsx b/src/components/dm/DMChatArea.tsx index f510aeb..707895c 100644 --- a/src/components/dm/DMChatArea.tsx +++ b/src/components/dm/DMChatArea.tsx @@ -1,4 +1,4 @@ -import { useState, useRef, useEffect } from 'react'; +import { useState, useRef, useEffect, useCallback, memo } from 'react'; import { useConversationMessages, useDMContext } from '@/contexts/DMContext'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAuthor } from '@/hooks/useAuthor'; @@ -20,7 +20,13 @@ interface DMChatAreaProps { className?: string; } -const MessageBubble = ({ +// Helper function outside component to avoid recreation +const formatMessageTime = (timestamp: number) => { + const date = new Date(timestamp * 1000); + return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); +}; + +const MessageBubble = memo(({ message, isFromCurrentUser }: { @@ -34,11 +40,6 @@ const MessageBubble = ({ }; isFromCurrentUser: boolean; }) => { - const formatTime = (timestamp: number) => { - const date = new Date(timestamp * 1000); - return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); - }; - const isNIP4Message = message.kind === 4; return ( @@ -61,7 +62,7 @@ const MessageBubble = ({ "text-xs opacity-70", isFromCurrentUser ? "text-primary-foreground" : "text-muted-foreground" )}> - {formatTime(message.created_at)} + {formatMessageTime(message.created_at)} {isNIP4Message && ( @@ -84,7 +85,9 @@ const MessageBubble = ({ ); -}; +}); + +MessageBubble.displayName = 'MessageBubble'; const ChatHeader = ({ pubkey, onBack }: { pubkey: string; onBack?: () => void }) => { const author = useAuthor(pubkey); @@ -180,7 +183,7 @@ export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => { } }, [messages.length]); - const handleSend = async () => { + const handleSend = useCallback(async () => { if (!messageText.trim() || !pubkey || !user) return; setIsSending(true); @@ -196,14 +199,14 @@ export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => { } finally { setIsSending(false); } - }; + }, [messageText, pubkey, user, sendMessage, selectedProtocol]); - const handleKeyDown = (e: React.KeyboardEvent) => { + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } - }; + }, [handleSend]); if (!pubkey) { return ( diff --git a/src/components/dm/DMConversationList.tsx b/src/components/dm/DMConversationList.tsx index aaed78f..c31118e 100644 --- a/src/components/dm/DMConversationList.tsx +++ b/src/components/dm/DMConversationList.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { useMemo, useState, memo } from 'react'; import { AlertTriangle, Info } from 'lucide-react'; import { useDMContext } from '@/contexts/DMContext'; import { useAuthor } from '@/hooks/useAuthor'; @@ -18,7 +18,23 @@ interface DMConversationListProps { onStatusClick?: () => void; } -const ConversationItem = ({ +// Helper function outside component to avoid recreation +const formatConversationTime = (timestamp: number) => { + const now = Date.now() / 1000; + const diff = now - timestamp; + + if (diff < 60) return 'now'; + if (diff < 3600) return `${Math.floor(diff / 60)}m`; + if (diff < 86400) return `${Math.floor(diff / 3600)}h`; + if (diff < 604800) return `${Math.floor(diff / 86400)}d`; + + const date = new Date(timestamp * 1000); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + return `${month}/${day}`; +}; + +const ConversationItem = memo(({ pubkey, isSelected, onClick, @@ -40,22 +56,6 @@ const ConversationItem = ({ const avatarUrl = metadata?.picture; const initials = displayName.slice(0, 2).toUpperCase(); - // Format timestamp - const formatTime = (timestamp: number) => { - const now = Date.now() / 1000; - const diff = now - timestamp; - - if (diff < 60) return 'now'; - if (diff < 3600) return `${Math.floor(diff / 60)}m`; - if (diff < 86400) return `${Math.floor(diff / 3600)}h`; - if (diff < 604800) return `${Math.floor(diff / 86400)}d`; - - const date = new Date(timestamp * 1000); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - return `${month}/${day}`; - }; - const lastMessagePreview = lastMessage?.error ? '🔒 Encrypted message' : lastMessage?.decryptedContent || 'No messages yet'; @@ -94,7 +94,7 @@ const ConversationItem = ({ )} - {formatTime(lastActivity)} + {formatConversationTime(lastActivity)} @@ -105,7 +105,9 @@ const ConversationItem = ({ ); -}; +}); + +ConversationItem.displayName = 'ConversationItem'; const ConversationListSkeleton = () => { return ( diff --git a/src/components/dm/DMMessagingInterface.tsx b/src/components/dm/DMMessagingInterface.tsx index 8b6fea1..ecf169f 100644 --- a/src/components/dm/DMMessagingInterface.tsx +++ b/src/components/dm/DMMessagingInterface.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useState, useCallback } from 'react'; import { DMConversationList } from '@/components/dm/DMConversationList'; import { DMChatArea } from '@/components/dm/DMChatArea'; import { DMStatusInfo } from '@/components/dm/DMStatusInfo'; @@ -27,13 +27,13 @@ export const DMMessagingInterface = ({ className }: DMMessagingInterfaceProps) = const showConversationList = !isMobile || !selectedPubkey; const showChatArea = !isMobile || selectedPubkey; - const handleSelectConversation = (pubkey: string) => { + const handleSelectConversation = useCallback((pubkey: string) => { setSelectedPubkey(pubkey); - }; + }, []); - const handleBack = () => { + const handleBack = useCallback(() => { setSelectedPubkey(null); - }; + }, []); return ( <>