mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
memoize a bunch of things
This commit is contained in:
1 parent
71b9c9d380
commit
04c5a84ca9
3 files changed
+43
-38
No files matched your search
@@ -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)}
|
||||
</span>
|
||||
{isNIP4Message && (
|
||||
<TooltipProvider>
|
||||
@@ -84,7 +85,9 @@ const MessageBubble = ({
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
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<HTMLTextAreaElement>) => {
|
||||
const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
};
|
||||
}, [handleSend]);
|
||||
|
||||
if (!pubkey) {
|
||||
return (
|
||||
|
||||
@@ -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 = ({
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground whitespace-nowrap flex-shrink-0">
|
||||
{formatTime(lastActivity)}
|
||||
{formatConversationTime(lastActivity)}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -105,7 +105,9 @@ const ConversationItem = ({
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
ConversationItem.displayName = 'ConversationItem';
|
||||
|
||||
const ConversationListSkeleton = () => {
|
||||
return (
|
||||
|
||||
@@ -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 (
|
||||
<>
|
||||
|
||||
Reference in new issue
Block a user